开发者

PHP code for quotation marks and ifelse statement - correct syntax?

开发者 https://www.devze.com 2023-03-20 08:37 出处:网络
This is a PHP page which displays the results from a query using JOIN (based on one from atutorial and adapted for my database):

This is a PHP page which displays the results from a query using JOIN (based on one from a tutorial and adapted for my database):

    <?php
$connection = mysql_connect("localhost", "root", "PASSWORD") or die("Error connecting to database");
mysql_select_db("products", $connection);
$result = mysql_query("SELECT * FROM buyers LEFT JOIN products USING (id);", $connection) or die("error querying database");
$i = 0;
while($result_ar = mysql_fetch_assoc($result)){
?>
<table>
<tr <?php if($i%2 == 1){ echo "class='body2'"; }else{echo "class='body1'";}?>>
<td>
<?php echo $result_ar['buyer_name']; ?></td>
<td>
<?php echo $result_ar['manufacturer']; ?>
</td>
<td>
<?php $result_ar['product_name'] = ($result_ar['product_name'] == '')? $result_ar['product_name'] : '"' . $result_ar['product_name'] . '"'; ec开发者_如何学Pythonho $result_ar['product_name']; ?>
</td>
</tr>
</table>
<?php
$i+=1;
}
?>

However, it's not the join that's the issue, here, but this PHP coding:

<?php $result_ar['product_name'] = ($result_ar['product_name'] == '')? $result_ar['product_name'] : '"' . $result_ar['product_name'] . '"'; echo $result_ar['product_name']; ?>

I tried this and it displayed the following (full result of code at beginning of this question):

John Zanussi "1500 Washing Machine"
James Hotpoint "3000 Washing Machine"
Simon Hotpoint

I was surprised it worked, it was just a test statement to see if the code worked.

http://www.w3schools.com/php/php_if_else.asp was my tool for this.

If I'm correct, it means it will put any array from the product_name column in quotation marks but if the column is blank then it will not display quotation marks.

Just checking to see if I'm correct - trying to brush up on my PHP skills here!


You're correct. The ternary operation in your code is equivalent to:

// If the product_name isn't empty, surround it in quotes.
if (!empty($result_ar['product_name']))
{
  $result_ar['product_name'] = "\"{$result_ar['product_name']}\"";
}

Using the ternary operation here is kind of confusing since the first case (empty value) still results in an empty value. More often you would see the opposite -- ternary operation used to create a default value for an empty variable.

In your context of echoing out the variable, you can shorten it to this expression. There's no need to reassign the variable before echoing it. You can echo the result of the ternary operation.

// Based on your version...
echo ($result_ar['product_name'] == '')? $result_ar['product_name'] : '"' . $result_ar['product_name'] . '"';

// Clearer version with the empty case last rather than first...
echo !empty($result_ar['product_name']) ? "\"{$result_ar['product_name']}\"" : "";

To use a ternary operation to assign a class (which could have bold text, for example) you could use something like this inside a table cell:

<td class='<?php echo $result_ar['product_name'] == "Zanussi" ? "special_bold_class" : "regular_class";?>'>

The result is:

<td class='special_bold_class'>
0

精彩评论

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