Jump to content

[SOLVED] PHP Loop not working


UQ13A

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/161210-solved-php-loop-not-working/
Share on other sites

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?

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 />";

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++;
}
}
}
}
?>

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

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

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.