引号和 ifelse 语句的 PHP 代码 - 语法正确吗?
这是一个 PHP 页面,显示使用 JOIN 的查询结果 (基于教程中的一个并针对我的数据库进行了调整):
<?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'] . '"'; echo $result_ar['product_name']; ?>
</td>
</tr>
</table>
<?php
$i+=1;
}
?>
但是,这里的问题不是连接,而是这个 PHP 编码:
<?php $result_ar['product_name'] = ($result_ar['product_name'] == '')? $result_ar['product_name'] : '"' . $result_ar['product_name'] . '"'; echo $result_ar['product_name']; ?>
我尝试了这个,它显示了以下内容(本问题开头的代码的完整结果) :
John Zanussi "1500 Washing Machine"
James Hotpoint "3000 Washing Machine"
Simon Hotpoint
我很惊讶它的工作原理,这只是一个测试语句,看看代码是否工作。
http://www.w3schools.com/php/php_if_else.asp 是我的工具这。
如果我是正确的,这意味着它将把 product_name 列中的任何数组放在引号中,但如果该列为空,那么它将不显示引号。
只是检查一下我是否正确 - 尝试在这里温习我的 PHP 技能!
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'] . '"'; echo $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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是对的。代码中的三元运算相当于:
此处使用三元运算有点令人困惑,因为第一种情况(空值)仍然会导致空值。更常见的是,您会看到相反的情况——三元运算用于为空变量创建默认值。
在回显变量的上下文中,您可以将其缩短为此表达式。在回显变量之前无需重新分配变量。您可以回显三元运算的结果。
要使用三元运算来分配类(例如,可能有粗体文本),您可以在表格单元格中使用类似的内容:
结果是:
You're correct. The ternary operation in your code is equivalent to:
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.
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:
The result is: