spearchilduser Posted November 30, 2011 Share Posted November 30, 2011 Ive gotten some results user selcts check box on first page The php page will say which brackets it falls between example price is between 100-300 say I dont seem to be able to populate a table with the data in the database:S $row_number= 0; while ($row = mysql_fetch_array($result3)) { If (($row["price"] = $price_low) && ($row["price"] <= $price_high)) //price If (($row["storage"] = $storage_low) && ($row["storage"] <= $storage_high)) //storage if (($row["Processor "] = $processor_low) && ($row["Processor"] <= $processor_high)) //Processor { $row_number++; ?> <tr> <td align="center"><?php print $row["Computer_Price"]; ?> </td> <td align="center"><?php print $row["Computer_Storage"]; ?> </td> <td align="center"><?php print $row["Computer_ProcessorSpeed"]; ?> </td> </tr> </table> Thats wat im usign atm the price_low and price_high are what sets the low and high price for the search Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/ Share on other sites More sharing options...
denno020 Posted November 30, 2011 Share Posted November 30, 2011 Looking at the excerpt of code, you never open the table tag? And with the closing tag where it is, it looks like you're going to close a table everytime the loop runs.. You want the opening and closing table tags outside of the while loop.. Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292599 Share on other sites More sharing options...
spearchilduser Posted November 30, 2011 Author Share Posted November 30, 2011 sorry i missed the beginning HTML> <BODY> <table width="75%" border="1" align="center"> <th> <td align="center">SELECTED Computers</td> <tr> <td>Price</td> <td>Storage</td> <td>Processor</td> </tr> <?PHP is just above the previosu code so if i move the table outside of the while loop it shoudl be ok ? from the syntax etc nothgin else i might need to add? Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292601 Share on other sites More sharing options...
denno020 Posted November 30, 2011 Share Posted November 30, 2011 This is how you would usually set up the loops to create a table (psuedo code): <table> while loop condition { <tr><td> //add content to the table cell </td></tr> }//close while loop </table> That code will create one table, will multiple table rows, each with only one column. Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292603 Share on other sites More sharing options...
spearchilduser Posted November 30, 2011 Author Share Posted November 30, 2011 Ok cheers but im confused as to how to do this not the way i have as i cant wrap the whoel thing in PHP tags cos itll geta error so as ive doen ive ended them after the whiel loop then made the 2nd row of the table and doen the titel row before the whiel loop is there a different way to do it ? <table width="75%" border="1" align="center"> <th> <td align="center">SELECTED Computers</td> <tr> <td>Price</td> <td>Storage</td> <td>Processor</td> </tr> <?PHP while ($row = mysql_fetch_array($result)) { If (($row["price"] = $price_low) && ($row["price"] <= $price_high)) //price If (($row["storage"] = $storage_low) && ($row["storage"] <= $storage_high)) //storage if (($row["Processor "] = $processor_low) && ($row["Processor"] <= $processor_high)) //Processor { $row_number++; }}; ?> <tr> <td align="center"><?php print $row["Price"]; ?> </td> <td align="center"><?php print $row["Storage"]; ?> </td> <td align="center"><?php print $row["Processor"]; ?> </td> </tr> </table> Im relatively new so i cant think of another way to do it sorry Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292605 Share on other sites More sharing options...
denno020 Posted November 30, 2011 Share Posted November 30, 2011 do you get any output at all? Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292606 Share on other sites More sharing options...
denno020 Posted November 30, 2011 Share Posted November 30, 2011 can you post your whole php file.. Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292607 Share on other sites More sharing options...
spearchilduser Posted November 30, 2011 Author Share Posted November 30, 2011 yes i can ill brefily exaplin the point : its a very simpel recomendation system where the first page the user selects a check box. On the php page it works out the average of each attibute in the databse and stores these in functions.i also have fucntiosn set up to work out 75% of the average 125% of the average. Then a switch will say the low and high values of the selection using those average fucntions and hopefully show the results for it. if that makes any scence <?php mysql_connect("localhost", "root") or die(mysql_error()); // makes a connection mysql_select_db("computers") or die('I cannot connect to the database because: ' . mysql_error()); //conects to the database $query = "SELECT * FROM computer_details"; //selects all data from the computer details table and stores it $query1 = "SELECT * FROM computer_details"; //selects all data from the computer details table and stores it $query2 = "SELECT * FROM computer_details"; //selects all data from the computer details table and stores it $query3 = "SELECT * FROM computer_details"; //selects the data from the computer price field in the computer details table and stores it // store the records of the queries into a reuslts variable $result = mysql_query($query) or die(mysql_error()); $result1 = mysql_query($query1) or die(mysql_error()); $result2 = mysql_query($query2) or die(mysql_error()); $result3 = mysql_query($query3) or die(mysql_error()); $total_price = 0; $total_storage = 0; $total_processor = 0; $number_of_items = 0; @$price = $_POST['price']; @$processor=$_POST['processor']; @$storage=$_POST['storage']; ?> <?php while($row = mysql_fetch_array($result)) { $total_price = $row["Computer_Price"] + $total_price ; $total_items = $number_of_items = $number_of_items + 1; } $av_price = $total_price/$total_items; // finds the average of the price in the computer price field int he datase by addign all the values up and dividing by the total items echo 'Average Price: ' ; print $av_price; print "<br>"; // displays the average price while($row = mysql_fetch_array($result1)) { $total_storage = $row["Computer_Storage"] + $total_storage ; $total_items = $number_of_items = $number_of_items + 1; } $av_storage = $total_storage/$total_items; // finds the average of the storage space in the computer price field int he datase by addign all the values up and dividing by the total items echo 'Average Storage: ' ; print $av_storage; print "<br>"; while($row = mysql_fetch_array($result2)) { $total_processor = $row["Computer_ProcessorSpeed"] + $total_processor ; $total_items = $number_of_items = $number_of_items + 1; } $av_processor = $total_processor/$total_items; // finds the average of the processor speed in the computer price field int he datase by addign all the values up and dividing by the total items echo 'Average Processor Speed: ' ; print $av_processor; print "<br>";print "<br>";print "<br>"; //Price $lowav_price = 0; $medlowav_price = 0.75 * $av_price; $highlowav_price = 1.25 * $av_price; $lowav_storage = 0; $medlowav_storage = 0.75 * $av_storage; $highlowav_storage = 1.25 * $av_storage; $lowav_processor = 0; $medlowav_processor= 0.75 * $av_processor; $highlowav_processor = 1.25 * $av_processor; // works out a percentage of the average from the $av_price/storage/processor variables // for the follwing bit the user will select a check box which will have a value and these are the cases for each option. //each option is set ot be either 0 - 75% of the average or 75%- 125 % of the average or 125% and above based on the average of the Data in the table switch ($price) { case '1' : $price_low = 0; $price_high = $medlowav_price ; print " (I would like to pay less and get less specifications) < ".$price_low."-".$price_high.">"; break; case '2' : $price_low = $medlowav_price; $price_high = $highlowav_price; print " (I would like to pay for what I get so what I get is equivalent to the price) < ".$price_low."-".$price_high.">"; break; case '3' : $price_low = $medlowav_price; $price_high = $highlowav_price ; print " (I would prefer to Pay as much as possible to get the best computer) < ".$price_low."-".$price_high.">"; break; case '4' : $price_low = 0; $price_high = $highlowav_price ; print " (I have no preference) < ".$price_low."-".$price_high.">"; break; } print ' price option = '.$price; print "<br>";print "<br>"; switch ($storage) { case '1' : $storage_low= 0; $storage_high = $medlowav_storage ; print " (I dont want a lot of storage space) < ".$storage_low."-".$storage_high.">"; break; case '2' : $storage_low = $medlowav_storage; $storage_high = $highlowav_storage; print " (I would like to have a decent amount of storage space) < ".$storage_low."-".$storage_high.">"; break; case '3' : $storage_low = $medlowav_storage; $storage_high = $highlowav_storage; print " (I prefer to pay more for bigger storage) < ".$storage_low."-".$storage_high.">"; break; case '4' : $storage_low= 0; $storage_high = $highlowav_storage ; print " (I have no preference) < ".$storage_low."-".$storage_high.">"; break; } print ' storage option = '.$storage; print "<br>";print "<br>"; switch ($processor) { case '1' : $processor_low = 0; $processor_high = $medlowav_processor ; print " (I only use the computer for the internet or a word processor ) < ".$processor_low."-".$processor_high.">"; break; case '2' : $processor_low = $medlowav_processor; $processor_high = $highlowav_processor; print " (I generally only have a couple of programs open at a time ) < ".$processor_low."-".$processor_high.">"; break; case '3' : $processor_low = $medlowav_processor; $processor_high = $highlowav_processor; print " (Yes I generally like to have as many programs as I can active at a time without slowing the computer down) < ".$processor_low."-".$processor_high.">"; break; case '4' : $processor_low = 0; $processor_high = $highlowav_processor ; print " (I have no preference) < ".$processor_low."-".$processor_high.">"; break; } print ' processor option = '.$processor; print "<br>";print "<br>"; ?> <HTML> <BODY> <table width="75%" border="1" align="center"> <th> <td align="center">SELECTED Computers</td> <tr> <td>Price</td> <td>Storage</td> <td>Processor</td> </tr> <?PHP while ($row = mysql_fetch_array($result)) { If (($row["price"] = $price_low) && ($row["price"] <= $price_high)) //price If (($row["storage"] = $storage_low) && ($row["storage"] <= $storage_high)) //storage if (($row["Processor "] = $processor_low) && ($row["Processor"] <= $processor_high)) //Processor { $row_number++; }}; ?> <tr> <td align="center"><?php print $row["Price"]; ?> </td> <td align="center"><?php print $row["Storage"]; ?> </td> <td align="center"><?php print $row["Processor"]; ?> </td> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292610 Share on other sites More sharing options...
spearchilduser Posted November 30, 2011 Author Share Posted November 30, 2011 . Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292611 Share on other sites More sharing options...
spearchilduser Posted November 30, 2011 Author Share Posted November 30, 2011 is it completely wrong or are there only one or two errors if its coimpletely wrong ill just stop now but if theres only a few errors id liek to trya nd correct them thanx Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292626 Share on other sites More sharing options...
denno020 Posted November 30, 2011 Share Posted November 30, 2011 sorry mate I got distracted watching a tv show.. I'll look through it now.. I'm only looking at your query and while loop really, just wanted to see the placing of the {'s and }'s.. I'll reply in a sec Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292639 Share on other sites More sharing options...
spearchilduser Posted November 30, 2011 Author Share Posted November 30, 2011 no thats ok thanx very much any help woudl be appreicated Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292643 Share on other sites More sharing options...
denno020 Posted November 30, 2011 Share Posted November 30, 2011 do you have skype or anything so I can talk to you through your code? Might be a bit easier than constantly replying here.. Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292645 Share on other sites More sharing options...
spearchilduser Posted November 30, 2011 Author Share Posted November 30, 2011 unfortunatley not i only have facebook and this Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292646 Share on other sites More sharing options...
denno020 Posted November 30, 2011 Share Posted November 30, 2011 any chance u can download skype? If not I'll try and make do here.. Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292647 Share on other sites More sharing options...
spearchilduser Posted November 30, 2011 Author Share Posted November 30, 2011 im just downloading it onto my computer now Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292652 Share on other sites More sharing options...
denno020 Posted November 30, 2011 Share Posted November 30, 2011 let me know ur skype name when you've got it up and going.. Quote Link to comment https://forums.phpfreaks.com/topic/252124-making-a-table-from-results/#findComment-1292653 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.