Forum Moderators: coopster
Below is my code and i want to be able to print varible depending on wether or not a car is found.
if(mysql_num_rows($sql) == 0){
print 'Sorry no matches found';
} else {
while($row = mysql_fetch_array($sql)){
blah blah blah
I just would like to know how i can also include the WHILE loop in the IF part of the script. so If rows == 0 then do the while loop as well and print results..
Thanks Guys
and display it with the IF
so
$result=mysql_query("SELECT * FROM cars");
if (mysql_num_rows($result)==0)
{
echo 'SOrry no cars';
}
else
{
while($row=mysql_fetch_Array(result)
{
// LOOP THROUGH CARS
}
}
if there are no rows returned by the query then you can't loop through it as there will be nothing to loop through.
Am i barking up the right tree? ;-)
hughie
could be that you mean
if(mysql_num_rows($sql)!= 0) // notice the!=
{
while ($row = mysql_fetch_array($sql))
$make = $row['MakeOfCar'];
echo ($row['MakeOfCar']);
}
else
{
echo 'SORRY NO CARS';
}
ta,
hughie
[edited by: hughie at 3:51 pm (utc) on Feb. 19, 2005]
$sql = mysql_query("SELECT makeofcar, ModelOfCar, CountyLocation, price, app, regletter, cardescription, orderid, display, vehiclecc FROM cars WHERE countylocation ='$countie' AND makeofcar ='$carrymake' AND modelofcar ='$carrymodel' AND display ='0' ORDER BY price LIMIT $from, $max_results");
// removed from inside of the while() loop because it will
// reset to blank on every iteration and you will only end up with last result
if(mysql_num_rows($sql) == 0) {
while ($row = mysql_fetch_array($sql))
$make = $row['MakeOfCar'];
print $make;
echo ($row);
} else {
while($row = mysql_fetch_array($sql)){
$make = $row['MakeOfCar'];
$model = $row['ModelOfCar'];
$reg = $row['regletter'];
$year2 = $row['year2'];
$size = $row['size'];
$photodesc = $row['cardescription'];
$price = $row['price'];
$id = $row['orderid'];
$app = $row['app'];
// Build your formatted results here.
$aaa = $row['makeofcar'];
$bbb = $row['modelofcar'];
$ccc = $row['regletter'];
$ddd = $row['cardescription'];
$idshow = $row['orderid'];
$tab = '';
$tab .=<<<EOD
<table width='570' border='0'class=setb>
<tr>
<td height=50 width=220 height="130" bgcolor=EDEDED class=sehead><a href="#" onClick="window.open('search2.php?id=$idshow','mywindow','width=560,height=400')">$aaa $bbb</a></td>
<td valign="middle" rowspan="2" bgcolor=EDEDED width="70" class=blackb >$price</td>
</tr>
<tr>
<td width=380 bgcolor=EDEDED class=fs>$ddd $countie<img src="images/$app" alt="" border="0"><br><br></td>
</tr>
</table>
EOD;
print $tab;
}
What i want todo is do a search on just make of car IF THERE ARE NO RESULTS
$sql = mysql_query("SELECT makeofcar, ModelOfCar, CountyLocation, price, app, regletter, cardescription, orderid, display, vehiclecc FROM cars WHERE countylocation ='$countie' AND makeofcar ='$carrymake' AND modelofcar ='$carrymodel' AND display ='0' ORDER BY price LIMIT $from, $max_results");
// removed from inside of the while() loop because it will
// reset to blank on every iteration and you will only end up with last result
if(mysql_num_rows($sql) == 0) {
// SEPERATE QUERY TO JUST PULL OUT THE MAKE OF CAR
$sql = mysql_query("SELECT makeofcar WHERE countylocation ='$countie' AND makeofcar ='$carrymake' AND display ='0' ORDER BY price LIMIT $from, $max_results");
while ($row = mysql_fetch_array($sql))
{
$make = $row['MakeOfCar'];
print $make;
}
} else {
while($row = mysql_fetch_array($sql)){
$make = $row['MakeOfCar'];
$model = $row['ModelOfCar'];
$reg = $row['regletter'];
$year2 = $row['year2'];
$size = $row['size'];
$photodesc = $row['cardescription'];
$price = $row['price'];
$id = $row['orderid'];
$app = $row['app'];
// Build your formatted results here.
$aaa = $row['makeofcar'];
$bbb = $row['modelofcar'];
$ccc = $row['regletter'];
$ddd = $row['cardescription'];
$idshow = $row['orderid'];
$tab = '';
$tab .=<<<EOD
<table width='570' border='0'class=setb>
<tr>
<td height=50 width=220 height="130" bgcolor=EDEDED class=sehead><a href="#" onClick="window.open('search2.php?id=$idshow','mywindow','width=560,height=400')">$aaa $bbb</a></td>
<td valign="middle" rowspan="2" bgcolor=EDEDED width="70" class=blackb >$price</td>
</tr>
<tr>
<td width=380 bgcolor=EDEDED class=fs>$ddd $countie<img src="images/$app" alt="" border="0"><br><br></td>
</tr>
</table>
EOD;
print $tab;
}
$boo = mysql_query("SELECT makeofcar, ModelOfCar, CountyLocation, price, app, regletter, cardescription, orderid, display, vehiclecc FROM cars WHERE countylocation ='$countie' AND display ='0' ORDER BY price LIMIT $from, $max_results");
// removed from inside of the while() loop because it will
// reset to blank on every iteration and you will only end up with last result
if(mysql_num_rows($sql) == 0) {
while ($roww = mysql_fetch_array($boo))
$tabb = '';
$tabb .=<<<EOD
<table width='570' border='0'class=setb>
<tr>
<td valign="middle" rowspan="2" bgcolor=EDEDED width="70" class=blackb >$roww[makeofcar]</td>
</tr>
</table>
EOD;
print $tabb;
if(mysql_num_rows($sql) == 0) {
while ($roww = mysql_fetch_array($boo))
$make = $roww['makeofcar'];
$tabb = '';
$tabb .=<<<EOD
<table width='570' border='0'class=setb>
<tr>
<td valign="middle" rowspan="2" bgcolor=EDEDED width="70" class=blackb > $roww['makeofcar']; </td>
</tr>
</table>
EOD;
print $tabb;
Why is this please
if(mysql_num_rows($sql) == 0) {
while ($roww = mysql_fetch_array($boo))
$make = $roww['makeofcar'];
?>
<table width='570' border='0'class=setb>
<tr>
<td valign="middle" rowspan="2" bgcolor=EDEDED width="70" class=blackb ><?php echo $make;?></td>
</tr>
</table>
<?php
}
// continued php code here
you can jump in and out of php like that and it should work just fine
maybe it is a problem with braces
if(mysql_num_rows($sql) == 0) {
while ($roww = mysql_fetch_array($boo)) {
$make = $roww['makeofcar'];
?>
<table width='570' border='0'class=setb>
<tr>
<td valign="middle" rowspan="2" bgcolor=EDEDED width="70" class=blackb ><?php echo $make;?></td>
</tr>
</table>
<?php
}
}
// continued php code here
it is doing
while ($row = mysql_fetch_array($boo)) print $row['makeofcar'];
where as if you do this
while ($row = mysql_fetch_array($boo)) $make = $row['makeofcar'];
then at the end echo $make, it will be the final value assigned to $make
your braces are messed up and that is definitely your problem, or one of them, you just need to figure out what needs to be in here
while ($row = mysql_fetch_array($boo)) {
// code in the loop
}
if(mysql_num_rows($sql) == 0) print (hello);
while ($row = mysql_fetch_array($boo))
{
echo "<table width='570' border='0'class=setb><tr><td valign='middle' rowspan='2' bgcolor=EDEDED width='170' class=blackb >",$row[makeofcar]," ",$row[ModelOfCar],"</td></tr></table>";
}
how annoying is php
If the query
$sql = mysql_query("whatever you have here");
returns one row, the condition fails and you don't print HELLO. By the way, PHP is more forgiving than most languages, but
print(hello);
should be
print("hello");
Then if the query
$boo = mysql_query("whatever you have here");
returns zero rows, you don't enter the loop.
So can we backtrack to this
$sql = mysql_query([fill in as appropriate]);
$boo = mysql_query([fill in as appropriate]);
echo "<br>Query SQL returned " . mysql_num_rows($sql) . " rows.";
echo "<br>Query BOO returned " . mysql_num_rows($boo) . " rows.";
What do you get in those cases? Are you sure you mean to have two different names for two different query results or are you getting confused? I think you mean to have
if(mysql_num_rows($sql) == 0) print (hello);
while ($row = mysql_fetch_array($sql))
(or $boo) as the case may be.