PHP If Statement within While Loop

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
slegendre1
Forum Newbie
Posts: 3
Joined: Thu Jun 03, 2010 3:04 pm

PHP If Statement within While Loop

Post by slegendre1 »

Hello,

I'm still learning PHP. Wondering if anyone could lend some thoughts on a small piece of code i'm trying to get working.

Within my PHP page I spit out some information, which works fine, but within one portion, I would like to display a certain value based on the records "status" field within my database. Pretty strait forward, but I cannot figure out the syntax to get this done.

Here is my call to grab my info from the database:

Code: Select all

$rs_recent = mysql_query("select * from PHPAUCTIONXL_letus where user_name = '".$_SESSION["user_name"]."' order by appraisalid desc") or die(mysql_error());
Within my page here is the code that displays the information

Code: Select all

        <? while ($prows = mysql_fetch_array($rs_recent)) {?>
        <tr>
          <td>#<? echo $prows['appraisalid'];?></td>
          <td><? echo $prows['fname'].' '.$prows['lname'];?></td>
          <td><? echo substr($prows['t_description'],0,50).'...';?></td>
          <td><a href="completed_appraisal.php?appid=<? echo $prows['appraisalid'];?>">View</a></td>
        </tr>
        <? } ?>
Here is what i'm trying to accomplish:

Code: Select all

        <? while ($prows = mysql_fetch_array($rs_recent)) {?>
        <tr>
          <td>#<? echo $prows['appraisalid'];?></td>
          <td><? echo $prows['fname'].' '.$prows['lname'];?></td>
          <td><? echo substr($prows['t_description'],0,50).'...';?></td>
IF status='5' then display this:
          <td><a href="completed_appraisal.php?appid=<? echo $prows['appraisalid'];?>">View</a></td>
else display this
         <td>In progress</td>
        </tr>
        <? } ?>
Thanks in advance for the help!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP If Statement within While Loop

Post by AbraCadaver »

This should do it:

Code: Select all

<? if($prows['status'] == '5') { ?>
          <td><a href="completed_appraisal.php?appid=<? echo $prows['appraisalid'];?>">View</a></td>
<? } else { ?>
         <td>In progress</td>
<? } ?>
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: PHP If Statement within While Loop

Post by internet-solution »

Try this

Code: Select all


<?php 
	while ($prows  = mysql_fetch_array($rs_recent)) {
        echo"<tr>
          <td>#".$prows['appraisalid']."</td>
          <td>".$prows['fname']." ".$prows['lname']."</td>
          <td>".substr($prows['t_description'],0,50)."...</td>";
		if(status=='5')
			echo"<td><a href='completed_appraisal.php?appid=".$prows['appraisalid']."'>View</a></td>";
		else 
         	       echo"<td>In progress</td>";

		} 
?>
slegendre1
Forum Newbie
Posts: 3
Joined: Thu Jun 03, 2010 3:04 pm

Re: PHP If Statement within While Loop

Post by slegendre1 »

Thats what I was going for! I think the code will work, but I have something wrong.

I receive the following error when I try to visit the page.
Parse error: parse error, unexpected $ in /home/content/n/u/g/site/html/members/myaccount.php on line 243

Here is the full code

Code: Select all

        <? while ($prows = mysql_fetch_array($rs_recent)) {?>
        <tr>
          <td>#<? echo $prows['appraisalid'];?></td>
          <td><? echo $prows['fname'].' '.$prows['lname'];?></td>
          <td><? echo substr($prows['t_description'],0,50).'...';?></td>
           <? if($prows['status'] == '5') { ?>
          <td><a href="completed_appraisal.php?appid=<? echo $prows['appraisalid'];?>">View</a></td>
          <? } else { ?>
          <td>In Progress</td>
        </tr>
        <? } ?>
slegendre1
Forum Newbie
Posts: 3
Joined: Thu Jun 03, 2010 3:04 pm

Re: PHP If Statement within While Loop

Post by slegendre1 »

Wahoo! internet-solution's code did the trick! Thanks everyone for your help!
Post Reply