Jump to content

If else statement within while loop


ultraloveninja

Recommended Posts

I am trying to build this out in my head, but I am unsure of the correct way to do it.

 

I am running a sql query to pull info from the DB.  I already have it checking the number of rows with a if statement, but I need it to check to see if a particular row is empty.  If it is empty it needs to echo out one thing and if not another.  But I also need it to run the loop till it ends with all the info from the DB is out.  I am having it check to see if there is an image path in the row.  If not have it echo the HTML without the image path and if so, have it included.  but some results may or may not have an image so it needs to display both not one or the other.

 

Here's my code.  I am pretty sure I have this structured wrong (Well, I know it's wrong cause it doesn't run):

 


<?php

$sql = "select * from tbl_tribue where letter = '$_GET[letter]' order by letter asc";		
$result = mysql_query($sql,$conn);
$num_rows = mysql_num_rows($result);

if ($num_rows=="0") {
echo "<p>No pet tributes for the letter.</p>";

    } else {




while ($row = mysql_fetch_array($result)){
$petname = $row['petname'];
$desc = $row['description'];
$family = $row['familyname'];
$img = $row['imgpath'];


if ($img="") {

echo <<<END

<div>
<p><strong>$petname</strong><br />
$desc<br />
<em>- $family</em></p>
</div>

END;
}  
} else {

echo <<<END

<div>
<img src="$img" class="photoWithBorder2" style="float:left; margin-right:10px; margin-bottom:10px;" />
<p><strong>$petname</strong><br />
$desc<br />
<em>- $family</em></p>
</div>

END;
}
}

?>


I left out the DB info.  Anyways, I am I close or WAY off the mark?  Thanks for any help!

Link to comment
Share on other sites

If no rows are returned $num_rows should be blank, not equal to zero. Therefore change the first if to:

 

if ($num_rows == '') {
echo "<p>No pet tributes for the letter.</p>";

    } else {
//the rest of your code
}

 

Edit:: Changed to == '';

Link to comment
Share on other sites

If no rows are returned $num_rows should be blank, not equal to zero. Therefore change the first if to:

 

if ($num_rows == '') {
echo "<p>No pet tributes for the letter.</p>";

    } else {
//the rest of your code
}

 

Edit:: Changed to == '';

 

Better to just check it like:

 

if (!$num_rows) // if no rows are returned
{
   // error
}
else
{
   // code
}

 

It's a bit more clear, as mysql_num_rows returns false if unsuccessful.

Link to comment
Share on other sites

Ok, here's what I got:

 

 


<?php
$sql = "select * from tbl_tribue where letter = '$_GET[letter]' order by letter asc";		
$result = mysql_query($sql,$conn);
$num_rows = mysql_num_rows($result);

if ($num_rows =='') {
echo "<p>No pet tributes for the letter.</p>";

    } else {

while ($row = mysql_fetch_array($result)){
$petname = $row['petname'];
$desc = $row['description'];
$family = $row['familyname'];
$img = $row['imgpath'];


if (empty($img)) {

echo <<<END

<div>
<p><strong>$petname</strong><br />
$desc<br />
<em>- $family</em></p>
</div>

END;
}  
} else {

echo <<<END

<div>
<img src="$img" class="photoWithBorder2" style="float:left; margin-right:10px; margin-bottom:10px;" />
<p><strong>$petname</strong><br />
$desc<br />
<em>- $family</em></p>
</div>

END;
}
}


?>

 

I tried to run it and it looks like it broke the page  :P

 

I think I might be missing a brace maybe...somewhere?

Link to comment
Share on other sites

Set display_errors to -1.

 

I set it to this:

 

ini_set("display_errors",-1);

 

And it's still blank.

 

Hmmmm...maybe I have a loop or something that's not working or closed off?

 

D'oh, my mistake.  Put the following at the top of your script:

 

error_reporting(-1);
ini_set("display_errors", 1);

Link to comment
Share on other sites

This is where taking the extra few seconds to lay out your work nicely helps. I would have spotted the problem in a split second had you indented nicely :P

 

The problem is that the else statement, after your if(empty($img)) statement, is written after the while loop closes.

Link to comment
Share on other sites

This is where taking the extra few seconds to lay out your work nicely helps. I would have spotted the problem in a split second had you indented nicely :P

 

The problem is that the else statement, after your if(empty($img)) statement, is written after the while loop closes.

 

 

Alright, I am confused.  I know that the first else statement is to check to see if there are any rows to be returned, if not then show the error.  If so, then run the loop.  I guess my question is how to I get it to check to see if there is an imagepath and if so, either show the HTML with the imagepath and if there is not then show the HTML without it.  Only reason why is that if some entries may or may not have an image and I can set it to show the img src but then it will just show a broken image when the page is parsed.

 

But that's my biggest hangup since I am not sure where I need to put the case statment for it to check to see if the imagepath is empty or not.

Link to comment
Share on other sites

An else statement is a counterpart of an if statement. You are basically saying:

if(statement is true)
   {
       //do this code
   }
else
   {
      //do this code
   }

 

In your code, you have placed the else after the while loop. This cannot be executed because you can't be saying:

whiles(statement is true)
   {
       //do this code
   }
else
   {
       //do this code
   }

 

That would mean the else statement would be constantly running if the while loop wasn't running. It would create an infinite loop and the server would just say "No, I'm not doing that, get stuffed", which it is doing.

 

What you need to do is move the else statement to be right after the if statement whiles both being contained in the while loop. Does that make a bit more sense?

Link to comment
Share on other sites

Here's what I'd do, based on what you have:

 

$letter = $_GET['letter']; // <-- It's NOT a good idea to simply trust whatever is present in $_GET or $_POST, but that's a discussion on another topic

$query = "SELECT * FROM tbl_tribue WHERE letter = $letter ORDER BY letter ASC";
$result = mysql_query($query);

if (mysql_num_rows($result))
{
   while($row = mysql_fetch_assoc($result))
   {
         $hasImage = empty($row['imgpath']) ? "" : "<img src=\"{$row['imgpath']}\" class=\"photoWithBorder2\" style=\"float:left; margin-right:10px; margin-bottom:10px;\" />\n";

         echo "<div>\n$hasImage<p><strong>{$row['petname']}</strong><br />\n{$row['description']}<br />\n<em>- {$row['familyname']}</em></p>\n</div>";
   }
}
else
{
   echo "<p>No pet tributes for the letter.</p>";
}

Link to comment
Share on other sites

Just so I understand, this part:

 

? "" :

 

in this:

 

$hasImage = empty($row['imgpath']) ? "" : "<img src=\"{$row['imgpath']}\" class=\"photoWithBorder2\" style=\"float:left; margin-right:10px; margin-bottom:10px;\" />\n";

 

is doing a check (persay) to see if the row is empty?  I am not too familiar with the ?"" part

Link to comment
Share on other sites

Well, sh*t.  RTFM on me then.

 

Thanks for your help!  Your solution worked just fine.  It's makes sense now to have it use that ternary operator which I was unfamiliar with.  Most of what I've needed for this page as been just one thing or the other and just using a simple array to output that info.  But this really helps!  Thanks again!

Link to comment
Share on other sites

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.