ultraloveninja Posted November 29, 2011 Share Posted November 29, 2011 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! Quote Link to comment Share on other sites More sharing options...
Spring Posted November 29, 2011 Share Posted November 29, 2011 -I'm just curious, what is this? echo <<<END and END; Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted November 29, 2011 Share Posted November 29, 2011 -I'm just curious, what is this? echo <<<END and END; Heredoc Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted November 29, 2011 Share Posted November 29, 2011 Your if is trying to assign $img, not check if it's empty. = means assignment, == means logical equals. That said, if you're checking for an empty string, use empty. Quote Link to comment Share on other sites More sharing options...
ultraloveninja Posted November 29, 2011 Author Share Posted November 29, 2011 Oh, was that it??? Was my structure correct? Quote Link to comment Share on other sites More sharing options...
joe92 Posted November 29, 2011 Share Posted November 29, 2011 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 == ''; Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted November 29, 2011 Share Posted November 29, 2011 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. Quote Link to comment Share on other sites More sharing options...
ultraloveninja Posted November 29, 2011 Author Share Posted November 29, 2011 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 I think I might be missing a brace maybe...somewhere? Quote Link to comment Share on other sites More sharing options...
Spring Posted November 29, 2011 Share Posted November 29, 2011 What's the error though? Quote Link to comment Share on other sites More sharing options...
ultraloveninja Posted November 29, 2011 Author Share Posted November 29, 2011 It's blank...odd. I have this in there too: ini_set("display_errors",1); And nothing displays. Usually that will do it. 500 server error? This is on FF, BTW. Quote Link to comment Share on other sites More sharing options...
Spring Posted November 29, 2011 Share Posted November 29, 2011 It's blank...odd. I have this in there too: ini_set("display_errors",1); And nothing displays. Usually that will do it. 500 server error? This is on FF, BTW. The entire page is blank? Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted November 29, 2011 Share Posted November 29, 2011 Set display_errors to -1. Quote Link to comment Share on other sites More sharing options...
ultraloveninja Posted November 29, 2011 Author Share Posted November 29, 2011 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? Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted November 29, 2011 Share Posted November 29, 2011 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); Quote Link to comment Share on other sites More sharing options...
ultraloveninja Posted November 29, 2011 Author Share Posted November 29, 2011 Ok did that too and the page still loads blank. Quote Link to comment Share on other sites More sharing options...
joe92 Posted November 29, 2011 Share Posted November 29, 2011 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 The problem is that the else statement, after your if(empty($img)) statement, is written after the while loop closes. Quote Link to comment Share on other sites More sharing options...
ultraloveninja Posted November 29, 2011 Author Share Posted November 29, 2011 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 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. Quote Link to comment Share on other sites More sharing options...
joe92 Posted November 29, 2011 Share Posted November 29, 2011 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? Quote Link to comment Share on other sites More sharing options...
ultraloveninja Posted November 29, 2011 Author Share Posted November 29, 2011 Ok, it's make some more sense. I guess I am not sure how I can have my other case statement check to see if the imagepath is empty before it spits out the array from the while loop since that is how it's checking. Sorry, still learning this as I go. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted November 29, 2011 Share Posted November 29, 2011 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>"; } Quote Link to comment Share on other sites More sharing options...
ultraloveninja Posted November 29, 2011 Author Share Posted November 29, 2011 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 Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted November 29, 2011 Share Posted November 29, 2011 It's the ternary operator, which is a shorthand version of if/else. This: $var = (expression1) ? expression2 : expression3 is the same as this: if (expression1) { $var = expression2; } else { $var = expression3; } http://php.net/manual/en/language.operators.comparison.php Quote Link to comment Share on other sites More sharing options...
ultraloveninja Posted November 29, 2011 Author Share Posted November 29, 2011 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.