简体   繁体   English

php&mysql 在渲染另一个表的输出时搜索 mysql 表

[英]php&mysql search mysql table while rendering output from another table

I have MYSQL table1 with columns (id(primary key), name, location, type) and table2 with columns (table1id(relation), confirmation(int(1 or 2))我有带有列(id(主键),名称,位置,类型)的 MYSQL table1 和带有列(table1id(关系),确认(int(1 或 2))的 table2

now what I would like to do is the following .现在我想做的是以下内容。 I want to get data from table1 and render it to HTML table (I can do that easly) my problem is the following .我想从 table1 获取数据并将其呈现到 HTML 表(我可以很容易地做到这一点)我的问题如下。 I want to give each HTML Table row a CSS class according to it's confirmation status from table2 .我想根据 table2 的确认状态为每个 HTML Table 行提供一个 CSS 类。

I hope I could explain what i want in a correct way .我希望我能以正确的方式解释我想要什么。

When you have two tables with related data, commonly you use a JOIN statement to get a result with the stuff from both tables.当您有两个包含相关数据的表时,通常使用JOIN语句从两个表中获取结果。 You can read about it here: JOIN Syntax .您可以在此处阅读有关它的信息: JOIN Syntax The MySQL docs can be hard to read at first, but if you're planning on using the database you should get started learning to read the docs as well. MySQL 文档一开始可能很难阅读,但如果您打算使用数据库,您也应该开始学习阅读文档。

In your case, it would be something like:在你的情况下,它会是这样的:

SELECT table1.*, table2.confirmation FROM table1 JOIN table2 ON table1.id = table2.table1id

There are different ways to join tables;连接表有多种方式; this example will not return a result unless there is a row in table2 that matches the row in table1.此示例不会返回结果,除非 table2 中的行与 table1 中的行匹配。

If you take a look at your result from that, you will see that your result has data from both tables.如果您从中查看结果,您将看到您的结果包含来自两个表的数据。

When you render it, you just need to tweak your current code to add a class to each row.渲染它时,您只需要调整当前代码以向每一行添加一个类。 Inside the loop you use for rendering, just add a bit to each row:在用于渲染的循环内,只需向每一行添加一点:

$output .= '<tr class="' . ($resultRow->confirmation == 1 ? 'confirmed' : 'unconfirmed') . '">';
$output .= [whatever you currently render into the row];
$output .= '</tr>';

Obviously since you didn't post any code I can't really guess what your rendering loop looks like, but hopefully you can adapt what I wrote to match your style.显然,由于您没有发布任何代码,我无法真正猜测您的渲染循环是什么样子,但希望您可以调整我编写的内容以匹配您的风格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM