PHP - Help With Arrays And A Bit Of Math.
Hi, I am TheMeq. I am currently developing a Text-Based MMORPG in PHP/MySQL. I have quite alot of the code laid down, its just this bit that is starting to throw me a bit. I've been working on it since 8am this morning.. it is now 4:30pm and i think it's nearly there. It's just not working the way I would like.
So if I was to ask for help, should I just post what code I have? Similar TutorialsI have this thing that i am trying to make but i cant get it to work.. can anyone help? Code: [Select] function sql_read( $dbname,$dbusername,$dbpassword ) { $names = array(); $password = array(); $connect = @mysql_connect("mysql11.000webhost.com",$dbusername,$dbpassword) or die("Could Not Connect"); @mysql_select_db ($dbname) or die("Could not find DataBase"); $query = mysql_query("select * from users"); $numrows = mysql_num_rows($query); if ($numrows > 0){ while($row = mysql_fetch_assoc($query)){ $names[] = $row["uname"]; $password[] = $row["password"]; $id = $row["id"]; } $return = array($names,$password,$id); }else{ $return = array(); } return $return[]; } $names = array(); $names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0]; The error i get is Code: [Select] Parse error: syntax error, unexpected '[' in /home/a5480952/public_html/sql/index.php on line 28 Line 28 is "$names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0];" Please help... i REALLLLD need help with this.. ask questions if you want to know more about what i am trying to do... thanks! I'm having troubling with trying to create a function to spit out a single array with the following array. I can write it in a away that looks through the arrays manually. the results i am trying to generate is that each item in generated array. Array to convert array( "account" => array( "login", "register", "logout", "edit", ), "p" => array( "report", ), "array1.0" => array( "array2.0" => array( "array3.0", "array3.1 ), "array2.1", ), generating the array will look like this
Array ( [0] => account [1] => account/login [2] => account/register [3] => account/logout [4] => account/edit [5] => p [6] => p/report [7] => array1.0 [8] => array1.0/array2.0 [9] => array1.0/array2.0/array3.0 [10] => array1.0/array2.0/array3.1 [11] => array1.0/array2.1 ) The idea is that id generates a single array with combined labels and arrays inside, etc. I just can't figure out how to create a script that will create this array even If I add a new value or array.
is there a math function that give you an integer value when u divide 2 numbers? ie in 7/3 i just want 2 as a result not floating number. thanks in advance. Hello Everyone, I'm a newbie trying to improve my script below. The script works fine but I would like to add the 3 radio buttons computations enhancements Ergo, if a user chooses 'Matte Paper" a 10% would be added to the Price Quote result. If a user chooses ' Gloss Paper, a 10% would be added to the Price Quote result. If a user chooses 'Canvas" , a 20 % would be added to the Price Quote result. If someone could assist with the math or post me a helpful link it would be greatly appreciated. Thank you. Paul from Melbourne, Australia. <div style="border:dashed 1px; width:570px;"> <form method="post" action="<?php echo $_SERVER['php_SELF'];?>"> <p> 1. Enter the size (100w.x180h. cm) you want your picture printed: Width <input type="text" name="width" size="1" maxlength="3" value="0" onFocus="this.value=''; this.style.color='#ff0000';"> Height <input type="text" name="height" size="1" value="0" maxlength="3" value="0" onFocus="this.value=''; this.style.color='#ff0000';"> </p> <p> 2. Choose the printing material for your pictu <input type="radio" value="cms in Matte paper" name="material" size="3" maxlength="3" /> Matte Paper <input type="radio" value="cms in Gloss Paper:" name="material" size="3" maxlength="3" /> Gloss Paper <input type="radio" value="cms in Canvas:" name="material" size="3" maxlength="3" /> Canvas </p> <p> 3. Submit your info for our Price Quote below : <input type="submit" name="submit" value="Submit" /> or: <input type="submit" name="clear" value="Reset" /> </p> <p> 5. Our Price Quote to print your artwork sized: <?php $width = $_POST['width']; /* if artwork wider than 101 cm no quote, exits*/ if ($width >=101) {echo "Error!"; exit ;} $height = $_POST['height']; /* if artwork higher than 181 cm no quote, script exits */ if ($height >=181) {echo "Error!"; exit ;} $measurement = $_POST['measurement']; $material = $_POST['material']; $result = $width * $height; echo " $width x $height $material is: "; echo "$".number_format($result,2). "" ; ?> </p> </form> </div> Trying to get the percentage of rent for a property in php. Im completely lost .... Code: [Select] <?php $purchase = 44000; $rent = 9860; $percent = $purchase * $rent; //this is wrong, it returns 4.3 but should be over 18 echo "Percentage is {$percent}%"; ?> Hi guys. I'm using php to randomly generate a simple math verification question. But, I'm having a hard time validating the input. Can someone please tell me what I'm doing wrong? Thank you } else if(trim($verify) != $_SESSION['UserData']['Math'][0]+$_SESSION['UserData']['Math'][1]){ $error = '<div class="error_message">Attention! The verification number you entered is incorrect.</div>'; } <?php echo "<span class='label'><span class='required'>*</span>What is ".$_SESSION['UserData']['Math'][0]." + ".$_SESSION['UserData']['Math'][1]."?</span> "; ?> <input name="verify" type="text" id="verify" size="3" value="<?=$verify;?>" /><br /><br /> I am trying to create a math equation inside a variable but I am struggling to achieve the desired result. On one php page I have this:
$x = "date('Y')"; On my main page I have linked to the above and also $description but the result I am getting is: In this classic lecture which was delivered over date('Y') - 1989 years ago,
I want to create a variable so I don't need to go back every year and change the description. Is this possible? Robert
I'm trying to make a script to check a files date time and if its under so many seconds long the page will refresh. here is what I've got. I've been testing the output of the date() functions but when i do the math all i get is a 0. where did i go wrong? is it just i can't do math with dates? Thanks in advanced. Code: [Select] <html><head> <meta http-Equiv="Cache-Control" Content="no-cache"> <meta http-Equiv="Pragma" Content="no-cache"> <meta http-Equiv="Expires" Content="0"> <? $proFile = "profilePics/profile.jpg"; //if ( date ("m d Y H:i:s.") - date ("m d Y H:i:s.", filemtime($proFile)) <= 00 00 0000 00:00:04 ){ //this is where the magic should happen. //echo "<meta http-equiv="refresh" content='4'>"; } ?> </head> <? echo date ("m d Y H:i:s."); echo "<br>"; echo date ("m d Y H:i:s.", filemtime($proFile)); echo "<br>"; echo date ("m d Y H:i:s.") - date ("m d Y H:i:s.", filemtime($proFile)); //only outputs 0 ?> I have two arrays, both with the same key values. I'd like to combine them. So for instance... Code: [Select] <?php $array1['abcd'] = array( 'value1' => "blah", 'value2' => "blahblah"); $array1['efgh'] = array( 'value1' => "ha", 'value2' => "haha", 'valuex' => "xyz"); $array2['abcd'] = array('value3' => "three", 'value4' => "four"); $array2['efgh'] = array( 'value3' => "hohoho", 'value6' => "six6"); function combine_arrays($array1,$array2) { //*combining* return $single_array; } echo "<pre>"; print_r(combine_arrays($array1,$array2)); echo "</pre>"; /* would produce ['abcd'] = ( 'value1' => "blah", 'value2' => "blahblah", 'value3' => "three", 'value4' => "four" ) ['efgh'] = ( 'value1' => "ha", 'value2' => "haha", 'valuex' => "xyz", 'value3' => "hohoho", 'value6' => "six6" ) */ ?> What's the easiest way to do this? I am doing some simple math and it is spitting out very odd results. //First Loop to get the values $get_values = mysql_query("SELECT * FROM table"); while($values = mysql_fetch_array)){ $value_one += $values[value_one]; $value_two += $values[value_two]; } $difference = $value_one - $value_two; echo"$value_one - $value_two = $difference; The database field types are DECIMAL(10,2). The output of the individual values is echoing the correct 0.00 format but after it loops for awhile the difference seems to come out differently. and have close to 5 or 6 places after the decimal. if i echo the values as it is looping and adding them it seems to not be adding correctly either. What am I doing wrong? My code is $answer = (1/212500); output is 4.90205690308E-6 while I need I output as 0.00000470588 Please help, and tell me what is the specific problem, Hi guys! Am trying to get hold of a PHP Math Expression Parser that follows correct precedence and executes custom functions. Can somebody share his scripts? or lead me to the right directions? Hope you can help! Thanks a lot! My boss is gonna kill me soon. This is 1 week overdue already. Jon Please deal with me for a moment as I try to explain what is occurring. I have created drop down menu that you choose a name and the php/MySQL will run multiple queries to display information regarding the chosen name. The query works great. There is no problem with the query. However when you leave the page, either by navigation or by X'ing out. When you return there are two errors that pop up: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in You can then use the drop down menu and choose a name and it all works great. It is my belief that these errors occur due to how the query was written, as the two queries that have these errors both have math in them. My question is, how do I block these errors from occurring when you return to the page? Here is the query: <?php //Worst Regular Season Record include_once('../other/functions.php'); $con = mysql_connect($hostname, $username, $password) OR DIE ('Unable to connect to database! Please try again later.'); $db = mysql_select_db($dbname, $con); $query = "SELECT win, loss, year, teamname FROM standings, owners WHERE owners.owner_id = standings.owner_id AND win = (SELECT MIN(win) FROM standings) AND standings.owner_id = $thing ORDER BY year"; $result = mysql_query($query); $row = mysql_fetch_array($result); @ mysql_data_seek($result, 0); if(mysql_num_rows($result)>0) { echo "<table CELLPADDING=5 border =1>"; echo "<tr>"; echo "<th align=center colspan=4 > Worst Regular Season Record </th>"; echo "</tr>"; while ($row = mysql_fetch_array($result)) { echo "<tr>"; echo '<td align=center> In '.$row['year'].', the '.$row['teamname'].' had '.$row['win'].' wins, and '.$row['loss'].' losses.</td>'; echo "</tr>"; } } else { } echo "</table>"; mysql_close($con); ?> Hi guys, I just decided to add an addition question in my site for submitting a form, not gonna say where coz one of you will most proberly try to prove it doesnt work! I have a feeling its easy enough for someone to send my form by getting the two variables and evaluating them. But how would they do that? and how would they know the names of the variables to send...? Is there a way of doing a math captcha that cannot be hacked? Code: [Select] if ($user['amount'] >= 15.00){ $user['max_stars'] = 3; } Ok and Code: [Select] if ($user['amount'] >= 20.00){ $user['max_stars'] = 4; }How can I write a function that will loop through the 15,20,25,30,35, all the way up to 600$ or if the max_Stars get's up to 80? I really don't want to have 80 if functions in my code :\ The title may not fit but only thing i could think of. I am pulling data from MSSQL database table.... I need everything from that table but i need to do some basic math funcitons to the resulting data. So the quesiton is should i do the math for this in the SQL query if so how do i pull all fields and do the math in an effcient way. Or should i let php do the math for me after i get all the data i need. If it is better to use php how can i do this in a while loop and get the math result and not a printed equation. Just looking for a few pointers never had to do math before with php This topic has been moved to PHP Math Help. http://www.phpfreaks.com/forums/index.php?topic=350528.0 This topic has been moved to PHP Math Help. http://www.phpfreaks.com/forums/index.php?topic=357542.0 This topic has been moved to PHP Math Help. http://www.phpfreaks.com/forums/index.php?topic=310665.0 Good day, I have problem on subtracting time. Can anyone give me a hint how will I going to do that.
I already manage to get the SUM on time addition using this method.
$data = array('1:52:37','9:56:39','08:04:22','05:32:20'); echo convertTime($data); - ANSWER 25:25:58 function convertTime($data){ $seconds = null; $minutes = null; $hours = null; foreach($data as $key => $value){ $time1 = explode(':',$value); list($h[$key],$m[$key],$s[$key]) = $time1; $seconds += $s[$key]; $minutes += $m[$key]; $hours += $h[$key]; } $total_sec_min = intval($seconds / 60); $total_seconds = $seconds % 60; #$total_seconds; $total_minutes = $minutes % 60; if($total_sec_min > 0){ $total_minutes = $total_minutes + $total_sec_min; } $total_min_hr = intval($minutes / 60); $total_hours = $hours; if($total_min_hr > 0){ $total_hours = $total_hours + $total_min_hr; } return $total_hours.':'.sprintf("%02s",$total_minutes).':'.sprintf("%02s",$total_seconds); }But I really got hard time when it comes to subtraction. |