我的代码正在构建一个网页,我根据计算机ID(machine_id
)显示多个数据表。最终用户希望能够滚动单个页面上的所有表格,而不是每台机器都有一个页面。
附件是页面的截图(实际上在页面上显示的不仅仅是两个表)。我可以修改我的代码,以便我在machine_id
更改时不会在While循环中进行多次查询吗?
$db = new Database();
$result = mysqli_query($conn, "SELECT machine_id, machine_name, display_order FROM machines WHERE active_board = 'YES' ORDER BY display_order ASC");
echo "<table>";
while ($row = mysqli_fetch_assoc($result)) {
$mach_id = $row["machine_id"];
$machine_name = $row["machine_name"];
$presspage = $row["machine_id"];
$machine_name = $row["machine_name"];
$daycount = '7';
$rows = $db -> select("SELECT a.machine_id, a.job_id, a.component, a.production_date, a.colors, a.hours, a.quantity, a.is_completed, a.artwork_image,
j.job_id, j.job_number, j.customer_id, j.job_name, j.total_cost, j.due_date, j.rework, j.pps, j.personalization, j.shipped, j.wave_csr,
c.customer_id, c.customer_name, c.board_color, c.text_color, u.full_name, p.component_name, d.delivery_method
FROM job_assignments a
LEFT JOIN jobs j ON a.job_id = j.job_id
LEFT JOIN customers c ON j.customer_id = c.customer_id
LEFT JOIN users u ON j.wave_csr = u.user_id
LEFT JOIN job_components p ON a.component = p.component_code
LEFT JOIN delivery_methods d ON j.method_id = d.method_id
WHERE a.machine_id = $presspage
ORDER BY j.job_number ASC");
echo "<tr>";
echo "<td>";
echo "<div id='dhtmlgoodies_dragDropContainer'>";
echo "<div id='dhtmlgoodies_mainContainer' align='center' vertical-align='middle'>";
echo "<h2>" . $machine_name . "</h2>";
for($x=0;$x<$daycount;$x++) {
$thehours = 0;
$theday = date('l M d', strtotime($date . " + {$x} day"));
$theday2 = date('Y-m-d', strtotime($date . " + {$x} day"));
$daycode = date('Md', strtotime($date . " + {$x} day"));
$totalhours = 0;
$pastdue_hours = 0;
$totalvalue = 0.00;
echo "<div style='width:180px; height:1500px; '>";
echo "<p>" . $theday . "</p>";
foreach ($rows as $row) {
$prod_date = $row["production_date"];
$shipped = $row["shipped"];
$hours = $row["hours"];
$value = $row["total_cost"];
$assignment_completed = 0;
$is_completed = $row["is_completed"];
if (($prod_date < date('Y-m-d')) AND ($shipped == 'NO') ) {
$prod_date = date('Y-m-d');
};
...
答案 0 :(得分:0)
您可以将这两个查询加入
SELECT m.machine_id, m.machine_name, m.display_order, a.job_id, a.component, a.production_date, a.colors, a.hours, a.quantity, a.is_completed, a.artwork_image,
j.job_id, j.job_number, j.customer_id, j.job_name, j.total_cost, j.due_date, j.rework, j.pps, j.personalization, j.shipped, j.wave_csr,
c.customer_id, c.customer_name, c.board_color, c.text_color, u.full_name, p.component_name, d.delivery_method
FROM
machines m
LEFT JOIN job_assignments a USING (machine_id)
LEFT JOIN jobs j ON a.job_id = j.job_id
LEFT JOIN customers c ON j.customer_id = c.customer_id
LEFT JOIN users u ON j.wave_csr = u.user_id
LEFT JOIN job_components p ON a.component = p.component_code
LEFT JOIN delivery_methods d ON j.method_id = d.method_id
WHERE a.machine_id = $presspage and m.active_board = 'YES'
ORDER BY m.display_order, j.job_number;
答案 1 :(得分:0)
您可以先将所有机器ID从第一个查询中获取到一个数组中,然后一次性对该数组中的所有机器ID执行第二个查询。以下是一个示例:
$db = new Database();
$result = mysqli_query($conn, "SELECT machine_id, machine_name, display_order FROM machines WHERE active_board = 'YES' ORDER BY display_order ASC");
$active_board_machine_ids = array();
while ($row = mysqli_fetch_assoc($result)) {
$active_board_machine_ids[] = $row["machine_id"];
}
$rows = $db -> select("SELECT a.machine_id, a.job_id, a.component, a.production_date, a.colors, a.hours, a.quantity, a.is_completed, a.artwork_image,
j.job_id, j.job_number, j.customer_id, j.job_name, j.total_cost, j.due_date, j.rework, j.pps, j.personalization, j.shipped, j.wave_csr,
c.customer_id, c.customer_name, c.board_color, c.text_color, u.full_name, p.component_name, d.delivery_method
FROM job_assignments a
LEFT JOIN jobs j ON a.job_id = j.job_id
LEFT JOIN customers c ON j.customer_id = c.customer_id
LEFT JOIN users u ON j.wave_csr = u.user_id
LEFT JOIN job_components p ON a.component = p.component_code
LEFT JOIN delivery_methods d ON j.method_id = d.method_id
WHERE a.machine_id IN ('".implode("','",$active_board_machine_ids)."')
ORDER BY j.job_number ASC");