phpcodeigniterfacebox

Displaying a single row in codeigniter integrated with facebox


I've been using facebox recently and it's okay. But when i got to use it with codeigniter, its so hard for me. In this code that i put in codeigniter:

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

I aim to pass it with specific id, like this code below which is not working in CI:

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

Which is which?

Here's my codes:

In my controller:

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);

 }

In my model:

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();
}
}

And in my view:

<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>

The output of these, it shows all the date,name and quantity that are in the database. Thats my problem, I only want a single row or only one output each query.


Solution

  • As far as i am seeing the method you are using is difficult to maintain when you goes deeper in your project because of creating a function for every table in model file is not a good idea it's gona be time consuming what if you write generic function which better work and easily memorize able. Now coming to the point you have created four functions and i am going to create two generic function instead of making four and it will be valuable in your project and also will solve your problem as well .

    1. Model

    Function for returning single row data

    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 for returning multiple rows data

    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. Controller

    Now its depends upon you weather to take resultset(full records) or either select only one row see how easy it is.

    For single results

         $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');
    

    Now for multiple records

        $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');
    

    View

    This is for multiple records if you have use get_records function so you have to use foreach to loop through every result and display it here

    <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>
    

    But if you have used get_record function you no longer need foreach loop just verify on if(isset()) and echo out the column.

        <?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 } ?>
    

    I hope this will help you alot in your project as well as clear your concept about creating generic functions instead of creating function for every different table.