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
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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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  ???

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.