在与facebox集成的codeigniter中显示单行

时间:2014-10-23 12:04:02

标签: php codeigniter facebox

我最近一直在使用facebox,没关系。但是当我使用codeigniter时,它对我来说太难了。在我放入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>

这些输出显示数据库中的所有日期,名称和数量。多数民众赞成我的问题,我只想要一行或每个查询只有一个输出。

2 个答案:

答案 0 :(得分:1)

据我所知,当你深入项目时,你正在使用的方法难以维护,因为为模型文件中的每个表创建一个函数并不是一个好主意,如果你写的话,它会耗费时间。通用功能,更好地工作,容易记忆能力。 现在我们已经创建了四个函数,我将创建两个通用函数而不是四个函数,它将在您的项目中有价值,也将解决您的问题。

  1. 模型
  2. 返回单行数据的功能

    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. 控制器
    2. 现在它依赖于你的天气来获取结果集(完整记录)或者只选择一行来看它是多么容易。

      单个结果

           $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 :(得分:0)

应连接数据库表。

例如: 1)预订(rervation_id,名称,......) 2)orders_date(date_id,reservation_id,...) 3)图像(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>