问题描述
如果/ else使用以下内容,我该怎么做一个参数。我想将变量$ condition定义为表A和表B的交集,并最终在这条件为真的情况下,然后执行一些代码。
How can I make an argument if/else with the following. I would like to define the variable $condition as the intersection of Table A and Table B, and ultimately, where this condition is true, then execute some code.
这是我有PHP(我试图匹配table1.field1和table2.field1,如果是真的,那么我将添加一个CSS类到< tr>
)
Here is the PHP I have (I am trying to match up the table1.field1 and table2.field1 and where true, then I will add a CSS class to the <tr>
)
$result = $dbh->query("SELECT field1, field2, field3, field4 FROM table1 ORDER BY field1 DESC");
$result->setFetchMode(PDO::FETCH_ASSOC);
$match = $dbh->query("SELECT table1.field1 FROM table1 INNER JOIN table2 WHERE table2.field1 = table1.field1");
$condition = **???????**
此外,有没有办法简化此代码?我觉得它太久了。马克吐温有一句话......我很抱歉这封信太长了,我没有时间把它缩短。我希望我可以说这是相关的,但我已经在这一段时间了!任何帮助将不胜感激。
Also, is there a way to simplify this code? I feel like its too long. Mark Twain has a saying... "I'm sorry this letter is too long, I didn't have time to make it shorter". I wish I could say that this is relevant here, but I've been at this for a while! Any help would be greatly appreciated.
推荐答案
如何(在您的第一次和当时的查询中)
What about something like (in your first and then-only query)
$result = $dbh->query("
SELECT
t1.field1,
t1.field2,
t1.field3,
t1.field4,
IF(t2.field1 IS NULL, 0, 1) `match`
FROM
table1 t1
LEFT JOIN table2 t2 ON t2.field1 = t1.field1
");
(你应该添加反引号(`
)围绕匹配
,因为它是一个MySQL关键字)
并且在你的循环中
(you should put backticks (`
) around match
, since it's a MySQL keyword)
and in your loop
<tr class="<?=$v["match"] ? "match" : ""?>">
这篇关于PHP If / Else Argument和PDO对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!