Jump to content

Print specified text, if the SQL table's column's value is 0.


username01

Recommended Posts

while($row = mysql_fetch_row($query)) {
echo "<tr>";
echo '<td>' . $row[0] . '</td>';
echo '<td>' . $row[1] . '</td>';
echo '<td>' . $row[2] . '</td>';
echo '<td>' . $row[3] . '</td>';
echo '<td>' . $row[4] . '</td>';
echo "</tr>\n";
}

Currently I have this code. I would like to make it print certain text, let's say... 'car', if the value of table's column 'automobiles' is equal to 0. Otherwise - print the real value.

I've tried if statement and $one = $two... But that apparently didn't worked.

 

Thanks.

while($row = mysql_fetch_row($query)) {

if $row == '0' {$row = $text_specified}

echo "<tr>";



echo '<td>' . $row[0] . '</td>';



echo '<td>' . $row[1] . '</td>';



echo '<td>' . $row[2] . '</td>';



echo '<td>' . $row[3] . '</td>';



echo '<td>' . $row[4] . '</td>';



echo "</tr>\n";
}

 

My equation syntax is correct, but code is wrong.

 

 

if $row == '0' {$row = $text_specified}

 

Row won't equal 0. one of the values in row could be. row[0] might = 0, or row[1]. But row will never because of the while.

You need to check on EACH column/key

if(mysql_num_rows($query)) {
  while($row = mysql_fetch_row($query)) {	  
    echo "<tr>";	
    echo '<td>' . $row[0] . '</td>';	
    echo '<td>' . $row[1] . '</td>';	
    echo '<td>' . $row[2] . '</td>';	
    echo '<td>' . $row[3] . '</td>';	
    echo '<td>' . $row[4] . '</td>';	
    echo "</tr>\n";
}
} else {
  echo 'No rows returned';
}

 

Use the above code, if mysql_num_rows() returns 1 or more rows it will equate to true, else it will equate to false.

 

Regards, PaulRyan.

if(mysql_num_rows($query)) {
  while($row = mysql_fetch_row($query)) {	  
    echo "<tr>";	
    echo '<td>' . $row[0] . '</td>';	
    echo '<td>' . $row[1] . '</td>';	
    echo '<td>' . $row[2] . '</td>';	
    echo '<td>' . $row[3] . '</td>';	
    echo '<td>' . $row[4] . '</td>';	
    echo "</tr>\n";
}
} else {
  echo 'No rows returned';
}

 

Use the above code, if mysql_num_rows() returns 1 or more rows it will equate to true, else it will equate to false.

 

Regards, PaulRyan.

 

That doesn't sound like what the OP wants to DO though.

 

"if the value of table's column 'automobiles' is equal to 0. Otherwise - print the real value."

 

That refers to one column in the row. Not the whole row.

Thanks, but it didn't worked still:

while($row = mysql_fetch_row($query)) {
if $row[3] == '0' {$row[3] = $value_text}
echo "<tr>";
echo '<td>' . $row[0] . '</td>';
echo '<td>' . $row[1] . '</td>';
echo '<td>' . $row[2] . '</td>';
echo '<td>' . $row[3] . '</td>';
echo '<td>' . $row[4] . '</td>';
echo "</tr>\n";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.