I have a table named account.
Table: account
+---------+---------------------+-----------+
| user_id | email | u_name |
+---------+---------------------+-----------+
| 63 | Vicman@gmai.com | Pross |
| 64 | Pink@gmai.com | Pross |
| 73 | Victor@gmail.com | Victorss |
| 74 | ifeco_man@yahoo.com | Ifecosss |
+---------+---------------------+-----------+
But I want to change all the display u_name into " No " from my table whose u_name = "Pross" in my page like this:
+---------+---------------------+-----------+
| user_id | email | u_name |
+---------+---------------------+-----------+
| 63 | Vicman@gmai.com | No |
| 64 | Pink@gmai.com | No |
| 73 | Victor@gmail.com | Victorss |
| 74 | ifeco_man@yahoo.com | Ifecosss |
+---------+---------------------+-----------+
But my code here replaces all the rows with "No". Seems like the placement of my condition is wrong. Here's my code in php to fetch data from database.
<!--############# TABLE CONTENT ##############-->
<tbody>
<?php
while($row = mysql_fetch_array($s1)) {
# Columns from DB
$db_id = $row['user_id'];
$db_email = $row['email'];
$db_uname = $row['u_name'];
# Condition to handle display of u_name row
if($db_uname == "Pross") {
$db_uname_edit = "No";
}
?>
<tr>
<td><?php echo $db_id ?></td>
<td><?php echo $db_email ?></td>
<td><?php echo $db_uname_edit ?></td>
</tr>
<?php } ?>
</tbody>
<!--##########################################-->
But the unwanted output of this is :
+---------+---------------------+-----------+
| user_id | email | u_name |
+---------+---------------------+-----------+
| 63 | Vicman@gmai.com | No |
| 64 | Pink@gmai.com | No |
| 73 | Victor@gmail.com | No |
| 74 | ifeco_man@yahoo.com | No |
+---------+---------------------+-----------+
Why ? Can anybody help me with my code above ? Thanks
You should set the value of $db_uname_edit to $db_uname in else part:
if($db_uname == "Pross") {
$db_uname_edit = "No";
} else {
$db_uname_edit = $db_uname;
}
# Condition to handle display of u_name row
if($db_uname == "Pross") {
$db_uname_edit = "No";
}
else {
$db_uname_edit = $db_uname;
}
This should work.
And here is the shortened edition, you won't need an if
clause:
<td><?php echo $db_uname == 'Pross' ? 'No' : $db_uname ?></td>
change your "if" statement to:
if($db_uname === "Pross") {
$db_uname_edit = "No";
}
when compareing strings you should use the "===" operator instead of "=="
you need to add else
part in if
condition if condition not matched then code goto else condition
if($db_uname == "Pross") {
$db_uname_edit = "No";
} else {
$db_uname_edit = $db_uname;
}
OR better user ternary
operator to reduce code
$db_uname_edit = $db_uname == "Pross" ? "No" : $db_uname;
And You can do this in DB
query, that would be good for performance
select user_id, email, IF(u_name = 'Pross', 'No', u_name) AS u_name FROM account
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.