for statement in if statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ebbatten
Forum Newbie
Posts: 11
Joined: Thu May 18, 2006 2:20 pm

for statement in if statement

Post by ebbatten »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm hoping someone can help me. I have to make it so that a coded table is written when there are less than two comments posted. If there are no comments, then nothing should be posted. My problem is that i can't figure out how to fill out loop statement to just skip over the code if no comments exist. I've tried every variation of if else, do while and can't find an elegant way. Any suggestions?

Code: Select all

$query2 = "SELECT * FROM comments where recipe_id = '$rnum' ORDER BY 'date' ASC";
$result2 = mysql_query($query2, $link) or die('Could not add retrieve comments : ' . mysql_error($link));
$total_comments = mysql_num_rows($result2);
for ($x=0; $x <= $total_comments; $x++){
            $row2 = mysql_fetch_array($result2);
            $rcomment[$x] = $row2['comment'];
            $ruser_id[$x] = $row2['user_id'];
            $cdate[$x] = $row2['date'];
            $crating[$x] = $row2['rating'];
            }

for($x=0;$x<2;$x++){
  echo"<table>";
  echo"<tr> ";
  echo"<td colspan=\"3\"><em><a href=\"index.php?view=allcomments&rnum=$rnum\"><strong>View All Comments:</a></em> Total Comments:</strong> &nbsp; $total_comments</td>";
  echo"</tr>";
   echo"<tr>";
  echo"<td width=\"111\" >&nbsp;&nbsp;Member:</td>";
  echo"<td width=\"119\" >&nbsp;$ruser_id[$x]</td>";
  echo"<td width=\"133\" align=\"right\">&nbsp;&nbsp;Date:&nbsp;&nbsp;</td>";
  echo"<td width=\"133\" >&nbsp;$cdate[$x]</td>";
  echo"</tr>";
 
  echo"<tr>";
  echo"<td valign=\"top\">&nbsp;&nbsp;Comment:</td>";
  echo"<td colspan=\"3\">$rcomment[$x]</td>";
  echo"</tr>";
  echo"<tr>";
  echo"<td colspan=\"4\">&nbsp;&nbsp;<hr></td></tr>";
  echo"</table>";
  }

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

I believe you would do something like this:

Code: Select all

$query2 = "SELECT * FROM comments where recipe_id = '$rnum' ORDER BY 'date' ASC";
$result2 = mysql_query($query2, $link) or die('Could not add retrieve comments : ' . mysql_error($link));
$total_comments = mysql_num_rows($result2);

if ($total_comments >= '2'){
for ($x=0; $x <= $total_comments; $x++){
            $row2 = mysql_fetch_array($result2);
            $rcomment[$x] = $row2['comment'];
            $ruser_id[$x] = $row2['user_id'];
            $cdate[$x] = $row2['date'];
            $crating[$x] = $row2['rating'];
            }

for($x=0;$x<2;$x++){
  echo"<table>";
  echo"<tr> ";
  echo"<td colspan=\"3\"><em><a href=\"index.php?view=allcomments&rnum=$rnum\"><strong>View All Comments:</a></em> Total Comments:</strong> &nbsp; $total_comments</td>";
  echo"</tr>";
   echo"<tr>";
  echo"<td width=\"111\" >&nbsp;&nbsp;Member:</td>";
  echo"<td width=\"119\" >&nbsp;$ruser_id[$x]</td>";
  echo"<td width=\"133\" align=\"right\">&nbsp;&nbsp;Date:&nbsp;&nbsp;</td>";
  echo"<td width=\"133\" >&nbsp;$cdate[$x]</td>";
  echo"</tr>";
 
  echo"<tr>";
  echo"<td valign=\"top\">&nbsp;&nbsp;Comment:</td>";
  echo"<td colspan=\"3\">$rcomment[$x]</td>";
  echo"</tr>";
  echo"<tr>";
  echo"<td colspan=\"4\">&nbsp;&nbsp;<hr></td></tr>";
  echo"</table>";
  }
}else{
     //do something else
}
ebbatten
Forum Newbie
Posts: 11
Joined: Thu May 18, 2006 2:20 pm

Post by ebbatten »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Thanks. That almost works, it's just that when only one comment is posted, It prints it the one comment, and then an empty comment for the two key values. I made some changes, and this is what I have now.

Code: Select all

if ($total_comments > 0)  {

for ($x=0; $x <= 2; $x++){
            $row2 = mysql_fetch_array($result2);
            $rcomment[$x] = $row2['comment'];
            $ruser_id[$x] = $row2['user_id'];
            $cdate[$x] = $row2['date'];
            $crating[$x] = $row2['rating'];
            }
for($x=0;$x<2;$x++){
  echo"<table>";
  echo"<tr> ";
  echo"<td colspan=\"3\"><em><a href=\"index.php?view=allcomments&rnum=$rnum\"><strong>View All Comments:</a></em> Total Comments:</strong> &nbsp; $total_comments</td>";
  echo"</tr>";
   echo"<tr>";
  echo"<td width=\"111\" >&nbsp;&nbsp;Member:</td>";
  echo"<td width=\"119\" >&nbsp;$ruser_id[$x]</td>";
  echo"<td width=\"133\" align=\"right\">&nbsp;&nbsp;Date:&nbsp;&nbsp;</td>";
  echo"<td width=\"133\" >&nbsp;$cdate[$x]</td>";
  echo"</tr>";
 
  echo"<tr>";
  echo"<td valign=\"top\">&nbsp;&nbsp;Comment:</td>";
  echo"<td colspan=\"3\">$rcomment[$x]</td>";
  echo"</tr>";
  echo"<tr>";
  echo"<td colspan=\"4\">&nbsp;&nbsp;<hr></td></tr>";
  echo"</table>";
  }
}else{
    echo"<P>Rate this recipe!</p>";
}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

I don't know why it's doing that, but shouldn't it say

Code: Select all

if($total_comments >=2)
because you want it to loop when you have 2 or more comments?

also:
ebbatten's code wrote:<td colspan="3"><em><a href="index.php?view=allcomments&rnum=$rnum"><strong>View All Comments:</a></em> Total Comments:</strong> &nbsp; $total_comments</td>
its generally a bad idea to do this:

Code: Select all

<em><strong>Stuff Here</em></strong>
You shouldn't have tags overlapping like that. The general rule of thumb is that more general, broader-reaching tags go outside and more specific tags go inside, so your code becomes

Code: Select all

<strong><em><a href="...">View All Comments:</a></em> Total Comments</strong>
Both will output the same thing, but from a behind-the-scenes stand point, it looks better, and will not confuse a browser.

On a side note, it makes your code easier to read when you use the

Code: Select all

bbTag instead of

Code: Select all

for php
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: for statement in if statement

Post by RobertGonzalez »

ebbatten wrote:I'm hoping someone can help me. I have to make it so that a coded table is written when there are less than two comments posted. If there are no comments, then nothing should be posted.
So basically you are checking to see if there is one comment (1 < 2 && 1 > 0), yes?
Post Reply