Forum Moderators: coopster

Message Too Old, No Replies

php if help

         

adamnichols45

2:35 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



HI,

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

hughie

3:00 pm on Feb 19, 2005 (gmt 0)

10+ Year Member



not sure i follow, you can't loop through an empty array. Can you explain a bit more

Cheers,
hughie

adamnichols45

3:06 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i want to loop through this
$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");

and display it with the IF

adamnichols45

3:44 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ok this is what i want and i have done the code

if(mysql_num_rows($sql) == 0) {
while ($row = mysql_fetch_array($sql))
$make = $row['MakeOfCar'];
echo ($row['MakeOfCar']);

} else {

But nothing is printed why is this blank?

hughie

3:46 pm on Feb 19, 2005 (gmt 0)

10+ Year Member



still dont getcha but..

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

hughie

3:49 pm on Feb 19, 2005 (gmt 0)

10+ Year Member



yeah, nothing is coming out because there is nothing in the row array, the while loop will only spit out the contents of the array, if it's empty it will just return zilch.

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]

adamnichols45

3:50 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ok here is the entire code .

$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&nbsp;$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

adamnichols45

3:52 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I know that there are no results.

Im saying if results == 0

then do another select on just make of car and
display the results for that

hughie

3:54 pm on Feb 19, 2005 (gmt 0)

10+ Year Member



if results==0 then you need to do another query to get just the make of the car without the modelofcar.

$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&nbsp;$countie<img src="images/$app" alt="" border="0"><br><br></td>
</tr>
</table>
EOD;
print $tab;
}

adamnichols45

6:55 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why is this code not displaying anything please i know it should be.

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

jatar_k

7:00 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if(mysql_num_rows($sql) == 0) {
while ($roww = mysql_fetch_array($boo))

the logic there is wrong

if there are no rows returned from mysql then loop through the returned results

I would think you mean

if(mysql_num_rows($sql) > 0) {

adamnichols45

7:46 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



no i do not mean that what is happening is that when it fins no results it then gets the array from another select method which does return results. except now it is not showing these in my choosen method.

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

jatar_k

7:55 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try it this way just to see what is going on

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

adamnichols45

7:58 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i do recieve one record then but i want to be able to retrieve all the records returned from the database which is about 5.

jatar_k

7:59 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ahh

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

adamnichols45

8:17 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That returns me no results.

here is what i am using that does works

if(mysql_num_rows($sql) == 0) {
while ($row = mysql_fetch_array($boo))

print $row['makeofcar'];

I would just like to be able to edit that and put it in a table etc so i can have thr records on seperate rows etc

jatar_k

8:24 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the one you show there should work

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
}

adamnichols45

8:32 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



why wont something like this work

print $row[makeofcar] $row[modelofcar];

?

jatar_k

8:36 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you want to make it inline then do

echo '<p>',$row[makeofcar],' ',$row[modelofcar];

or

echo "<table width='570' border='0'class=setb><tr><td valign='middle' rowspan='2' bgcolor=EDEDED width='70' class=blackb >",$row[makeofcar]," ",$row[modelofcar],"</td></tr></table>";

adamnichols45

8:55 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



this works fine except it does not output any content with regard to modelofcar! why does it just print make and not model?

jatar_k

8:56 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you will need to look to your select statement then

adamnichols45

9:05 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



this is so annoying why wont it just print this message hello once then loop through the records

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

ergophobe

10:59 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Late to the party and I haven't read all of the above as carefully as I should, but on a quick read it seems entirely possible that you would not get anything.

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.

adamnichols45

10:16 am on Feb 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

No everything i have is as i want it except i wanted hello to print as well as then run through the while loop

Regards
Adam