Page 1 of 1

PHP If Statement within While Loop

Posted: Thu Jun 03, 2010 3:12 pm
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!

Re: PHP If Statement within While Loop

Posted: Thu Jun 03, 2010 3:19 pm
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>
<? } ?>

Re: PHP If Statement within While Loop

Posted: Thu Jun 03, 2010 3:26 pm
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>";

		} 
?>

Re: PHP If Statement within While Loop

Posted: Thu Jun 03, 2010 3:32 pm
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>
        <? } ?>

Re: PHP If Statement within While Loop

Posted: Thu Jun 03, 2010 4:08 pm
by slegendre1
Wahoo! internet-solution's code did the trick! Thanks everyone for your help!