引号和 ifelse 语句的 PHP 代码 - 语法正确吗?

发布于 2024-11-23 20:04:10 字数 1644 浏览 1 评论 0原文

这是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮光沉寂 2024-11-30 20:04:11

你是对的。代码中的三元运算相当于:

// 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']}\"";
}

此处使用三元运算有点令人困惑,因为第一种情况(空值)仍然会导致空值。更常见的是,您会看到相反的情况——三元运算用于为空变量创建默认值。

在回显变量的上下文中,您可以将其缩短为此表达式。在回显变量之前无需重新分配变量。您可以回显三元运算的结果。

// 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']}\"" : "";

要使用三元运算来分配类(例如,可能有粗体文本),您可以在表格单元格中使用类似的内容:

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

结果是:

<td class='special_bold_class'>

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'>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文