UQ13A Posted June 6, 2009 Share Posted June 6, 2009 Hi, when i run this script i dont get all the results from my mysql database. My database has 2 records but when i run this script it only shows 1 Code below <?php if(isset($_GET['Find'])) { if($_GET['model'] == "Enter Model") { echo "Sorry you have to fill in the model box"; exit; } else { // Create the variables again. $make = $_GET['make']; $model = $_GET['model']; $model1 = strtoupper($model); // mysql query $query = "SELECT * FROM `cars` WHERE make='$make' AND model='$model1'"; $result = mysql_query($query); $numResults = mysql_num_rows($result); if ($numResults == 0) { echo "<u><center>Your search results for $make $model1 returned 0 results</center></u><br /><br /><br /><br />"; exit; } else { // data from query, react accordingly echo "<u><center>Search Results for $make $model1</center></u><br /><br /><br /><br />"; $i=1; $row = mysql_fetch_array($result); while($i<=5) { $image = $row['image']; $price = $row['Price']; $town = $row['Town']; $colour = $row['colour']; $comments = $row['Comments']; $price1 = round($price, 2); print "<div id=\"carPicture\"> <tr> <td><div id=\"carPicture\"><img src=$image width=\"125\" height=\"125\" border=\"5\"/></div></td> <td><div id=\"carText\"><div align=\"center\"><strong>Make $make Model $model1 Price £ $price1 Location $town </strong></div> <div align=\"left\"> <ul class=\"wspc\"> <li><strong>$colour</strong></li> </ul> <br /> </div> </div></td> <td><div id=\"comments\"><textarea name=\"textarea\" id=\"textarea\" cols=\"45\" rows=\"5\" readonly=\"readonly\">$comments</textarea></div></td> </tr> </div><br />"; $i++; } } } } ?> thanks Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/ Share on other sites More sharing options...
Maq Posted June 6, 2009 Share Posted June 6, 2009 Change this: $row = mysql_fetch_array($result); to this: while($row = mysql_fetch_array($result)) { Don't forget to add the closing '}' at the end of your script. I also don't get the point of this line: while($i Won't that just display the same record 5 times? Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850636 Share on other sites More sharing options...
UQ13A Posted June 6, 2009 Author Share Posted June 6, 2009 thanks for that but now my results show on top of each other Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850640 Share on other sites More sharing options...
Maq Posted June 6, 2009 Share Posted June 6, 2009 Like I said, please post the relevant code. Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850643 Share on other sites More sharing options...
UQ13A Posted June 6, 2009 Author Share Posted June 6, 2009 here is the code that shows the results, it is in the same script as above print "<div id=\"carPicture\"> <tr> <td><div id=\"carPicture\"><img src=$image width=\"125\" height=\"125\" border=\"5\"/></div></td> <td><div id=\"carText\"><div align=\"center\"><strong>Make $make Model $model1 Price £ $price1 Location $town </strong></div> <div align=\"left\"> <ul class=\"wspc\"> <li><strong>$colour</strong></li> </ul> <br /> </div> </div></td> <td><div id=\"comments\"><textarea name=\"textarea\" id=\"textarea\" cols=\"45\" rows=\"5\" readonly=\"readonly\">$comments</textarea></div></td> </tr> </div><br />"; Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850647 Share on other sites More sharing options...
Maq Posted June 6, 2009 Share Posted June 6, 2009 I need the PHP part as well. Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850649 Share on other sites More sharing options...
UQ13A Posted June 6, 2009 Author Share Posted June 6, 2009 here it is <?php if(isset($_GET['Find'])) { if($_GET['model'] == "Enter Model") { echo "Sorry you have to fill in the model box"; exit; } else { // Create the variables again. $make = $_GET['make']; $model = $_GET['model']; $model1 = strtoupper($model); // mysql query $query = "SELECT * FROM `cars` WHERE make='$make' AND model='$model1'"; $result = mysql_query($query); $numResults = mysql_num_rows($result); if ($numResults == 0) { echo "<u><center>Your search results for $make $model1 returned 0 results</center></u><br /><br /><br /><br />"; exit; } else { // data from query, react accordingly echo "<u><center>Search Results for $make $model1</center></u><br /><br /><br /><br />"; $i=1; while($row = mysql_fetch_array($result)) { $image = $row['image']; $price = $row['Price']; $town = $row['Town']; $colour = $row['colour']; $comments = $row['Comments']; $price1 = round($price, 2); print "<div id=\"carPicture\"> <tr> <td><div id=\"carPicture\"><img src=$image width=\"125\" height=\"125\" border=\"5\"/></div></td> <td><div id=\"carText\"><div align=\"center\"><strong>Make $make Model $model1 Price £ $price1 Location $town </strong></div> <div align=\"left\"> <ul class=\"wspc\"> <li><strong>$colour</strong></li> </ul> <br /> </div> </div></td> <td><div id=\"comments\"><textarea name=\"textarea\" id=\"textarea\" cols=\"45\" rows=\"5\" readonly=\"readonly\">$comments</textarea></div></td> </tr> </div><br />"; $i++; } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850650 Share on other sites More sharing options...
wildteen88 Posted June 6, 2009 Share Posted June 6, 2009 Looking at that code the problem is more to do with HTML than PHP. You cannot place <div> tags between a <table> and <tr> tags. Well actually looking further you don't seem to be opening/closing your table! Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850652 Share on other sites More sharing options...
UQ13A Posted June 6, 2009 Author Share Posted June 6, 2009 Looking at that code the problem is more to do with HTML than PHP. You cannot place <div> tags between a <table> and <tr> tags. Well actually looking further you don't seem to be opening/closing your table! So how would i do this? Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850653 Share on other sites More sharing options...
Daniel0 Posted June 6, 2009 Share Posted June 6, 2009 Well, that sort of depends on what you want to do, and considering you're the only one who knows that, you're the only one who can answer that question... Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850654 Share on other sites More sharing options...
UQ13A Posted June 6, 2009 Author Share Posted June 6, 2009 Well, that sort of depends on what you want to do, and considering you're the only one who knows that, you're the only one who can answer that question... Well i want to display my results in a table, a seperate table for each result. 1st record is displayed in a table and the 2nd is displayed in another and so on Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850658 Share on other sites More sharing options...
wildteen88 Posted June 6, 2009 Share Posted June 6, 2009 Change <div id=\"carPicture\"> to <table id=\"carPicture\" border="0" cellpadding="\0\" cellspacing=\"0\"> Now change </div><br />"; $i++; to </table>"; $i++; Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850660 Share on other sites More sharing options...
UQ13A Posted June 6, 2009 Author Share Posted June 6, 2009 Change <div id=\"carPicture\"> to <table id=\"carPicture\" border="0" cellpadding="\0\" cellspacing=\"0\"> Now change </div><br />"; $i++; to </table>"; $i++; i coppied the code just like you said but the table still come appear on top of each other Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850661 Share on other sites More sharing options...
wildteen88 Posted June 6, 2009 Share Posted June 6, 2009 Whats the css you're applying to your carPicture id? Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850663 Share on other sites More sharing options...
Maq Posted June 6, 2009 Share Posted June 6, 2009 You need to put the table tags outside of the while loop. Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850664 Share on other sites More sharing options...
UQ13A Posted June 6, 2009 Author Share Posted June 6, 2009 Whats the css you're applying to your carPicture id? this is all my css, #carPicture { position:absolute; left:17px; top:25px; width:676px; height:160px; z-index:1; } #carText { position:absolute; left:117px; top:10px; width:555px; height:160px; z-index:1; } #comments { position:absolute; left:269px; top:101px; width:288px; height:86px; z-index:2; } Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850665 Share on other sites More sharing options...
UQ13A Posted June 6, 2009 Author Share Posted June 6, 2009 You need to put the table tags outside of the while loop. i have done what you said but no luck still the same, text over text Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850682 Share on other sites More sharing options...
wildteen88 Posted June 6, 2009 Share Posted June 6, 2009 If you're using tables you cannot position items within each cell. Remove all your divs and css you code will work as you intend. Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850683 Share on other sites More sharing options...
UQ13A Posted June 6, 2009 Author Share Posted June 6, 2009 If you're using tables you cannot position items within each cell. Remove all your divs and css you code will work as you intend. thank you for that it shows all results but now they are all over the place ??? Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850685 Share on other sites More sharing options...
UQ13A Posted June 6, 2009 Author Share Posted June 6, 2009 Iv fixed it Thanks for all the help guys and girls (if there were any) Quote Link to comment https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/#findComment-850694 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.