在与facebox集成的代码点火器中显示单行


Displaying a single row in codeigniter integrated with facebox

我最近一直在使用facebox,这很好。但当我把它和codeigniter一起使用时,这对我来说太难了

<a rel="facebox" href="<?php echo site_url('logincon/vieworders/');?>">View Orders</a>

我的目标是用特定的id传递它,就像下面的代码一样,它在CI:中不起作用

<a rel="facebox" href="echo site_url('logincon/vieworders'))" id='.$row['reservation_id'].' title="Click To View Orders">View Orders</a>

哪个是哪个?

这是我的代码:

在我的控制器中:

public function vieworders()
 {  
    // $this->data['date'] = $this->loginmod->getdate();
     $data['date'] = $this->loginmod->getdate();
     $data['name'] = $this->loginmod->getimage();
     $data['order'] = $this->loginmod->getorderdetail();
     $data['reservation'] = $this->loginmod->getdesign();
     $this->load->view('vieworders', $data);
 }

在我的模型中:

public function getdate(){
$q=$this->db->get('orders_date');
if($q->num_rows()>0){
return $q->result_array();
}
}
public function getimage() {
    $query = $this->db->get("image");
    if($query->num_rows()>0){
    return $query->result_array();
}
}
public function getorderdetail() {
    $query = $this->db->get("order_detail");
    if($query->num_rows()>0){
    return $query->result_array();
}
}
public function getdesign()
{
    $query = $this->db->get('reservation');
    if($query->num_rows()>0){
    return $query->result_array();
}
}

在我看来:

<table cellpadding="1" cellspacing="1" id="resultTable">
<thead>
    <tr>
        <th  style="border-left: 1px solid #C1DAD7">Order Date </th>
        <th>Photo size </th>
        <th> Quantity </th>
    </tr>
</thead>
<tbody>
<tr class="record">
        <?php foreach ($date as $da) { ?>
         <td><div align="left"><?php echo $da['date']; ?></div></td>
        <?php } ?> 
        <?php foreach ($name as $images) { ?>
        <td style="border-left: 1px solid #C1DAD7;"><?php echo $images['name']; ?> </td>
        <?php } ?> 
        <?php foreach ($order as $detail) { ?>
        <td><?php
            echo $detail['quantity'];?></td>    
        <?php } ?> 
</tr>
</tbody>

这些的输出显示了数据库中的所有日期、名称和数量。这就是我的问题,我只想要一行或者每个查询只有一个输出。

据我所知,当你深入项目时,你正在使用的方法很难维护,因为为模型文件中的每个表创建一个函数不是一个好主意,如果你写一个更好工作、更容易记忆的通用函数,那会很耗时。现在说到这里,你已经创建了四个函数,我将创建两个通用函数,而不是创建四个,这将在你的项目中很有价值,也将解决你的问题。

  1. 型号

返回单行数据的功能

function get_record($table, $where=NULL)
        {
            if(!empty($where))
            {
                $this->db->where($where);
            }
            $query=$this->db->get($table);
            if($query->num_rows>0)
            {
                return $query->row();
            }
            else
            {
                return 0;
            }
        }

返回多行数据的函数

function get_records($table, $where=NULL)
    {
        if(!empty($where))
        {
            $this->db->where($where);
        }
        $query=$this->db->get($table);
        if($query->num_rows>0)
        {
            return $query->result();
        }
        else
        {
            return array();
        }
    }
  1. 控制器

现在,这取决于你是选择结果集(完整记录)还是只选择一行,看看有多容易

对于单个结果

     $data['date'] = $this->loginmod->get_record('orders_date');
     $data['name'] = $this->loginmod->get_record('image');
     $data['order'] = $this->loginmod->get_record('order_detail');
     $data['reservation'] = $this->loginmod->get_record('reservation');

现在用于多个记录

    $data['date'] = $this->loginmod->get_records('orders_date');
     $data['name'] = $this->loginmod->get_records('image');
     $data['order'] = $this->loginmod->get_records('order_detail');
     $data['reservation'] = $this->loginmod->get_records('reservation');

查看

如果您使用get_records函数,则这适用于多个记录,因此您必须使用foreach循环遍历每个结果并在此处显示

<tr class="record">
        <?php foreach ($date as $da) { ?>
         <td><div align="left"><?php echo $da->date; ?></div></td>
        <?php } ?> 
        <?php foreach ($name as $images) { ?>
        <td style="border-left: 1px solid #C1DAD7;"><?php echo $images->name; ?> </td>
        <?php } ?> 
        <?php foreach ($order as $detail) { ?>
        <td><?php
            echo $detail->quantity;?></td>    
        <?php } ?> 
      <?php foreach ($reservation as $row) { ?>
       <td>
<a rel="facebox" href="<?=site_url('logincon/vieworders')?>" id='<?=$row['reservation_id']?>' title="Click To View Orders">View Orders</a>
        </td>  
       <?php } ?>   
</tr>

但是,如果您使用了get_record函数,则不再需要foreach循环,只需验证if(isset())并回显该列即可。

    <?php if(isset($name)) { ?>
    <td style="border-left: 1px solid #C1DAD7;"><?php echo $name['name']; ?> </td>
    <?php } ?> 
    <?php if(isset($order)) { ?>
    <td><?php
        echo $order['quantity'];?></td>    
    <?php } ?>

    <?php if(isset($reservation)) { ?>
        <td>
<a rel="facebox" href="<?=site_url('logincon/vieworders')?>" id='<?=$row['reservation_id']?>' title="Click To View Orders">View Orders</a>
        </td>    
        <?php } ?>

我希望这将对你的项目有很大帮助,并明确你关于创建通用函数而不是为每个不同的表创建函数的概念。

应该连接数据库表。

示例:1) 预订(revation_id、名称…)2) orders_date(date_id,reservation_id,…)3) image(image_id、reservation_id…)4) order_detail(detail_id、reservation_id…)

<table cellpadding="1" cellspacing="1" id="resultTable">
<thead>
    <tr>
        <th  style="border-left: 1px solid #C1DAD7">Order Date </th>
        <th>Photo size </th>
        <th> Quantity </th>
    </tr>
</thead>
<tbody>
<tr class="record">
        <?php
        if ( @$reservations )
        {
            foreach ( $reservations as $reservation )
            {
                $reservation_date = $reservation_image = $reservation_detail = "";
                if ( @$dates )
                {
                    foreach ( $dates as $date )
                    {
                        if ( $date->reservation_id == $reservation->reservation_id )
                        {
                            $reservation_date = $date->name...(or anything)
                        }
                    }
                }   
                if ( @$images )
                {
                    foreach ( $images as $image )
                    {
                        if ( $image->reservation_id == $reservation->reservation_id )
                        {
                            $reservation_image = $images->name...(or anything)
                        }
                    }
                }           
                if ( @$details )
                {
                    foreach ( $details as $detail )
                    {
                        if ( $detail->reservation_id == $reservation->reservation_id )
                        {
                            $reservation_detail = $detail->name...(or anything)
                        }
                    }
                }

                echo '<tr class="record">'
                . '<td><div align="left">'.$reservation_date.'</div></td>'
                . '<td style="border-left: 1px solid #C1DAD7;">'.$reservation_image.'</td>'
                . '<td>'.$reservation_detail.'</td>'
                .'</tr>';
            }
        }
        ?>
</tr>
</tbody>

而不是

<a rel="facebox" href="echo site_url('logincon/vieworders'))" id='.$row['reservation_id'].' title="Click To View Orders">View Orders</a>

使用

<?= anchor(site_url('logincon/vieworders'), 'View Orders', 'id='.$row["reservation_id"].' title="Click To View Orders"') ?>

编辑:

型号

<?php
public function getdate()
{
    $q=$this->db->get('orders_date');
    $q = $this->db->query("SELECT field1, field2, date AS field3");
    if ( $q->num_rows() > 0 )
    {
        return $q->result_array();
    }
}
?>

查看

<tbody>
<tr class="record">
        <?php foreach ($date as $da) { ?>
         <td><div align="left"><?php echo $da['field3']; ?></div></td>
        <?php } ?> 
        ...
</tr>
</tbody>