开发者

Echo items with value of three

I need to echo all items in my column that have the value of three. If a开发者_如何学Python field has the value of three I then need to echo the 'name' and 'description' on that row.

This is what I have so far

$result = mysql_query($query1) or die(mysql_error());    
while ($row = mysql_fetch_array($result))    
{  
  echo $row['status'];
}

I need to write 'if $row[`status´ == 3 echo 'description' 'name' else echo 'no current staus'

I hope I made some sense because I am seriously confused


You can actually select the result which only has 3 value in its status

for that use this

$query = "SELECT * FROM yourtable WHERE status='3'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array(result)) {
      echo "Name: ".$row['name']."<br>Description: ".$row['description'];
}

and if you want to parse you result to display only those of value this then use this

$query = "SELECT * FROM yourtable";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array(result)) {
      if($row['status']==3) {
           echo "Name: ".$row['name']."<br>Description: ".$row['description'];
      }
}


if $row['status'] == 3 {
    echo ($row['description']." ".$row['name']);
}
 else
{ 
   echo ('no current staus');
}

The "." in the first echo means string concatenation. I'm separating description and name with a single space character, feel free to change this.


I'm expecting something more since you have the "i need to write" part that is the answer?

$result = mysql_query($query1) or die(mysql_error());    
while ($row = mysql_fetch_assoc($result))     // fetch_assoc better for your case
{  
   if( $row['status'] == 3 ) {
      echo $row['description'];
   }else {
      echo 'no current staus';
    }

}

For the sake of info for tasks with a numeric status

switch( $row['status'] ) {
    case 0:
        // do something
        break;
    case 1:
        // do something
        break;
    case 2:
        // do something
        break;
    case 3:
        // do something
        break;
    default:
        echo 'No status set';    
}


if ($row['status'] == 3) {
  echo $row['name'];
  echo $row['description'];
}


$result = mysql_query($query1) or die(mysql_error());    
while ($row = mysql_fetch_array($result))    
{
    if ($row['status'] == 3) {
        echo $row['name'] . ' - ' . $row['description'];
    }
    else {
        echo 'no curent status';
    }

}


if($row['status']==3) {
  echo $row['description'];
  echo $row['name'];
} else {
  echo 'no current status';
}

Maybe you should learn about basics of PHP and algorithms and make sure you understand this, before you move on.


This?

$result = mysql_query($query1) or die(mysql_error());    
while ($row = mysql_fetch_array($result))    
{  
  if (!in_array(3, $row['status']))
  {
    echo 'no current staus';
    break;
  }

  echo $row['description'] . <br />;
  echo $row['name'] . <br />;
}


or you could use SQL to select the values for you...

$query1 = SELECT * FROM table WHERE column = '3';

$result = mysql_query($query1) or die(mysql_error());    
while ($row = mysql_fetch_array($result))    
{  
  echo $row['status'];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜