Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql queery and if stament? 1

Status
Not open for further replies.

sophielois

Technical User
Sep 8, 2005
66
GB
hey,

I have a little problem i cant work out how to do the following


Code:
<?php


$result = mysql_query("SELECT * FROM candidates ORDER BY Name ASC");

    while($row = mysql_fetch_object($result))
        {
            echo "<tr>";
            echo "<td>$row->first_name $row->last_name</td>";
            echo "<td>$row->level_2</td>";
            echo "<td>$row->reg_num</td>";
            echo "<td>$row->wrk_place</td>";
            echo "<td>$row->signup_date</td>";
            echo "<td>$row->tel</td>";
            echo "<td>$row->email_address</td>";
            echo "<td>$row->ass_name</td>";
            echo "<td>$row->iv_name</td>";
            echo "<td><a href=\"/admin/can/canedit.php?userid=$row->userid\">View</a></td>";
            echo "</tr>";
            
        }

        
?>


How do incorporate the below if statement into the mysql queery so that echo "<td>$row->level_2</td>"; will display "car 2" or "car 3" for every row

Code:
<?

if ($row->level_2 == 1) {
echo "Car 2";
} else{
echo "Car 3";
}

Sophiex
 
Maybe this is what you are looking for?
Code:
while($row = mysql_fetch_object($result)) {
	echo "<tr>";
	echo "<td>$row->first_name $row->last_name</td>";
	[COLOR=red]echo '<td>';
	if ($row->level_2 == 1) {
		echo "Car 2";
	} else{
		echo "Car 3";
	}
	echo '</td>';[/color]
	[s]echo "<td>$row->level_2</td>";[/s]
	echo "<td>$row->reg_num</td>";
	echo "<td>$row->wrk_place</td>";
	echo "<td>$row->signup_date</td>";
	echo "<td>$row->tel</td>";
	echo "<td>$row->email_address</td>";
	echo "<td>$row->ass_name</td>";
	echo "<td>$row->iv_name</td>";
	echo "<td><a href=\"/admin/can/canedit.php?userid=$row->userid\">View</a></td>";
	echo "</tr>";
}

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
When I first encountered the ternary operator it looked like completely unintelligible what was going on.
However, once I understood the concept and 'diction' of the ternary operator I started using it since it is nicely compact:
Code:
# syntax is for example:
# expression ? true value : false value
while($row = mysql_fetch_object($result)) {
    echo "<tr>";
    echo "<td>$row->first_name $row->last_name</td>";
[COLOR=green]    echo '<td>Car '.($row->level_2 == 1)?"2":"3";[/color]
[s] echo '<td>';
    if ($row->level_2 == 1) {
        echo "Car 2";
    } else{
        echo "Car 3";
    }[/s]
    echo '</td>';
    echo "<td>$row->reg_num</td>";
    echo "<td>$row->wrk_place</td>";
    echo "<td>$row->signup_date</td>";
    echo "<td>$row->tel</td>";
    echo "<td>$row->email_address</td>";
    echo "<td>$row->ass_name</td>";
    echo "<td>$row->iv_name</td>";
    echo "<td><a href=\"/admin/can/canedit.php?userid=$row->userid\">View</a></td>";
    echo "</tr>";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top