php和sql-where(更改变量)

vc9ivgsu  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(301)

早上好,我正在努力处理这个代码。。
结果“10”被设置为$fod,但需要替换为显示被调用行的id,以便sql where可以在相关行中列出正确的数据。我得到的结果见图片。
查看当前结果和期望结果
如您所见,我需要行id 10来显示10的项目,行id 11来显示11的项目。请帮助我,如何调用这些行才能正确显示?
调用函数:
控制器.php

$data['order_list'] = $this->product->data_ordershalf();
$fod = 10;
$data['order_listfull'] = $this->product->data_ordersfull($fod);

函数.php:

function data_ordershalf(){
    $this->db->select('*');
    $this->db->join('order_detail', 'order_detail.orderid=orders.id', 'left');
    $this->db->join('customers', 'customers.id=orders.customerid', 'left');
    $this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
    $this->db->from('orders');
    $this->db->group_by('orderid');
    $rs = $this->db->get();
    return $rs->result_array();
}
function data_ordersfull($fod){
    $this->db->select('*');
    $this->db->join('orders', 'orders.id=order_detail.orderid', 'left');
    $this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
    $this->db->from('order_detail');
    $this->db->where('orderid',$fod);
    $rs = $this->db->get();
    return $rs->result_array();

视图.php:

<?php if(!$order_list){ ?>
<tbody>
    <tr>
        <th colspan="7"><center>No orders placed</center></th>
    </tr>
</tbody>
<?php } else { $sr = 1; ?>
<tbody>
    <?php foreach( $order_list as $row) { ?>
    <tr>
        <th scope="row"><?php echo $row['id']; ?></th>
        <td><?php echo $row['date']; ?></td>
        <td><?php echo $row['name']; ?></td>
        <td>
            <table>
                <?php foreach( $order_listfull as $row2) { ?>
                <tr>
                    <th scope="row"><?php echo $row2['id']; ?></th>
                    <td><?php echo $row2['product_name']; ?></td>
                    <td><?php echo $row2['quantity']; ?></td>
                    <td></td>
                    <td><?php echo $row2['price']; ?></td>
                 </tr>
                <?php } ?>
            </table>
        </td>
        <td><?php echo $row['quantity']; ?></td>
        <td><?php echo $row['price']; ?></td>
    </tr>
    <?php } ?>
</tbody>
<?php } ?>
cl25kdpy

cl25kdpy1#

您可以创建helper并将以下函数放入其中。

function data_ordersfull($fod){
    $this->db->select('*');
    $this->db->join('orders', 'orders.id=order_detail.orderid', 'left');
    $this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
    $this->db->from('order_detail');
    $this->db->where('orderid',$fod);
    $rs = $this->db->get();
    return $rs->result_array();
}

=========================视图.php======================================

<?php if(!$order_list){ ?>
            <tbody>
              <tr>
                <th colspan="7"><center>No orders placed</center></th>
              </tr>
            </tbody>
            <?php } else { $sr = 1; ?>
            <tbody>
              <?php foreach( $order_list as $row) { 
$order_listfull=data_ordersfull($row['id']);
?>
              <tr>
                <th scope="row"><?php echo $row['id']; ?></th>
                <td><?php echo $row['date']; ?></td>
                <td><?php echo $row['name']; ?></td>
                <td><table>
              <?php foreach( $order_listfull as $row2) { ?>
              <tr>
                <th scope="row"><?php echo $row2['id']; ?></th>
                <td><?php echo $row2['product_name']; ?></td>
                <td><?php echo $row2['quantity']; ?></td>
                <td></td>
                <td><?php echo $row2['price']; ?></td>

              </tr>
              <?php } ?></table></td>
                <td><?php echo $row['quantity']; ?></td>
                <td><?php echo $row['price']; ?></td>

                  </tr>
                  <?php } ?>
                </tbody>
                <?php } ?>
qeeaahzv

qeeaahzv2#

function data_ordershalf(){
    $this->db->select('*');
    $this->db->from('orders');
    $this->db->join('order_detail', 'order_detail.orderid=orders.id', 'left');
    $this->db->join('customers', 'customers.id=orders.customerid', 'left');
    $this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
    $this->db->group_by('order_detail.orderid');
    $rs = $this->db->get();
    return $rs->result_array();
}
function data_ordersfull($fod){
    $this->db->select('*');
    $this->db->from('order_detail');
    $this->db->join('orders', 'orders.id=order_detail.orderid', 'left');
    $this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
    $this->db->where('order_detail.orderid',$fod);
    $rs = $this->db->get();
    return $rs->result_array();

相关问题