Welcome Guest, Not a member yet? Register   Sign In
move to next row/previous row
#1

[eluser]handoyo[/eluser]
Hi all,i'm trying to create backend for order and order details with move to next/previous order details..I got no problems with viewing single order details.But i have no idea for viewing next/previous order details..What should i do to create it?Thanks alot guys.. Here are some of my codes...

model/MDetail.php
Code:
function getDetail($id)
    {
        $data = array();
        $options = array('no_order' => id_clean($id));
        $Q = $this->db->getwhere('order_detail',$options);
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
            //$data = $Q->row_array();
        }
        $Q->free_result();    
        return $data;
    }

model/MOrder.php
Code:
function getOrder($no)
    {
        $data = array();
        $options = array('id' => id_clean($no));
        $Q = $this->db->getwhere('order',$options,1);
        if ($Q->num_rows() > 0)
        {
            $data = $Q->row_array();
        }
        $Q->free_result();    
        return $data;
    }

controller/order.php
Code:
function detail($id)
    {
    //$this->benchmark->mark('query_start');
    
    $data['title'] = "Order detail";
    $data['main'] = 'admin/order_detail';
    $data['order'] = $this->MOrder->getOrder($id);
    $data['detail']= $this->MDetail->getDetail($id);  
    if (!count($data['detail']))
    {
    redirect('admin/order/index','refresh');
    }
    $this->load->vars($data);
    $this->load->view('dashboard');
    }
#2

[eluser]daparky[/eluser]
Can you not use the pagination class and only show next and previous link?
#3

[eluser]handoyo[/eluser]
Ok,thanks a lot..I'll try using paging...
#4

[eluser]daparky[/eluser]
Let me know how you get on.
#5

[eluser]handoyo[/eluser]
Sorry daparky..I show all orders in one page,and create link to detail page based on order number like the pictures here :

order

details

I can't figure out how to use the paging as it was based on order number..Thanks a lot...
#6

[eluser]Michael Wales[/eluser]
Are you trying to display previous/next links on the details page? Is the order number always consecutive? You would probably do something like this:

Code:
// Returns the previous order number, based off of the current order number
function get_previous($order_num = NULL) {
  // SQL: SELECT order_num FROM orders WHERE order_num < '0005' ORDER BY order_num DESC LIMIT 1
  $this->db->select('order_num');
  $this->db->order_by('order_num', DESC);
  $query = $this->db->get_where('orders', array('order_num <', $order_num), 1, 0);
  
  if ($query->num_rows() > 0) {
    return $query->row()->order_num;
  }
  return FALSE;
}

// Returns the next order number, based off of the currently viewed order
function get_next($order_num = NULL) {
  // SQL: SELECT order_num FROM orders WHERE order_num > '0005' ORDER BY order_num ASC LIMIT 1
  $this->db->select('order_num');
  $this->db->order_by('order_num', ASC);
  $query = $this->db->get_where('orders', array('order_num >', $order_num), 1, 0);
  
  if ($query->num_rows() > 0) {
    return $query->row()->order_num;
  }
  return FALSE;
}


Call both of those functions in your controller, assigning them to variables that will be passed to your view. Then just link it up within the view.
#7

[eluser]handoyo[/eluser]
Yes i do Michael,i'll try it..Thanks a lot..I will post the results here...By the way,what if the order number was combined with i.e date?
#8

[eluser]handoyo[/eluser]
Hi Michael,i'm trying the code you gave me this way..

db.sql
Code:
create table if not exists `order` (
`id` int(1) unsigned not null auto_increment,
`no_order` int(11) not null,
`custid` int(10) unsigned not null,
`tgl_pesan` date not null default '0000-00-00 00:00:00',
`tgl_kirim` date not null default '0000-00-00 00:00:00',
`total` decimal(12,0) NOT NULL default '0',
`comments` varchar(255) default NULL,
`status` int(11) NOT NULL default '0',
PRIMARY KEY  (`id`),
KEY `idx_custid` (`custid`)
)ENGINE=MyISAM;
INSERT INTO `order` (`id`, `no_order`, `custid`, `tgl_pesan`, `tgl_kirim`, `total`, `comments`, `status`) VALUES
(1, 1, 1, '2009-10-23', '0000-00-00', 100000, NULL, 0),
(2, 2, 2, '2009-10-24', '0000-00-00', 10000, NULL, 0);

create table if not exists `order_detail` (
`id` int(11) unsigned not null auto_increment,
`no_order` int(11) not null,
`tgl` date,
`produk_id` int(10) unsigned not null default '0',
`nama_produk` varchar(70) not null default '0',
`harga` decimal(10,0) not null default '0',
`qty` char(2) not null default '0',
`total` decimal(10,0) not null default '0',
PRIMARY KEY  (`id`)
)ENGINE=MyISAM;
INSERT INTO `order_detail` (`id`, `no_order`, `tgl`, `produk_id`, `nama_produk`, `harga`, `qty`, `total`) VALUES
(1, 1, '2009-10-23', 1, 'test', 100000, '1', 100000);

controller/order.php
Code:
function detail($id)
    {
    //$this->benchmark->mark('query_start');
    
    $data['title'] = "Order detail";
    $data['main'] = 'admin/order_detail';
    $data['order'] = $this->MOrder->getOrder($id);
    $data['detail']= $this->MDetail->getDetail($id);
    $data['previous']=$this->get_previous($id);
    $data['next']=$this->get_next($id);
      
    if (!count($data['detail']))
    {
    redirect('admin/order/index','refresh');
    }
    $this->load->vars($data);
    $this->load->view('dashboard');
    }    
    
  // Returns the previous order number, based off of the current order number
  function get_previous($id)
  {
  // SQL: SELECT order_num FROM orders WHERE order_num < '0005' ORDER BY order_num DESC LIMIT 1
  $this->db->select('no_order');
  $this->db->order_by('no_order', DESC);
  $query = $this->db->getwhere('order', array( 'no_order <',$id));
  if ($query->num_rows() > 0) {
    return $query->row()->no_order;
  }
  return FALSE;
  }

  // Returns the next order number, based off of the currently viewed order
  function get_next($id)
  {
  // SQL: SELECT order_num FROM orders WHERE order_num > '0005' ORDER BY order_num ASC LIMIT 1
  $this->db->select('no_order');
  $this->db->order_by('no_order', ASC);
  $query = $this->db->getwhere('order', array('no_order >', $id), 1, 0);
  if ($query->num_rows() > 0) {
    return $query->row()->no_order;
  }
  return FALSE;
  }

view/order_detail.php
Code:
<h1>&lt;?php echo $title;?&gt;</h1>
&lt;?php
if ($this->session->flashdata('message')){
    echo "<div class='message'>".$this->session->flashdata('message')."</div>";
}
        
echo "<p><label for='prev'>Prev : ".anchor("admin/order/detail/".$order['no_order'],$list['no_order'])."</label>";
echo "|";
echo "<p><label for='next'>Next : ".anchor("admin/order/detail/".$order['no_order'],$list['no_order'])."</label><br/>";

echo "<p><label for='orderid'>No Order : ".$order['no_order']."</label><br/>";
echo "<p><label for='orderid'>Tanggal Pesan : ".$order['tgl_pesan']."</label><br/>";

echo "<table border='1' cellspacing='0' cellpadding='3' width='600'>\n";
    echo "<tr valign='top'>\n";
    echo "<th>Nama Produk</th>\n<th>Qty</th>\n";
    echo "</tr>\n";


if (count($detail)){
    foreach ($detail as $key => $list){
        echo "<tr valign='top'>\n";
        echo "<td>".$list['nama_produk']."</td>";
        echo "<td>".$list['qty']."</td>\n";
        echo "</td>\n";
        echo "</tr>\n";
    }
    
}
echo "</table>";
?&gt;

When i try to run it,it gives me
Code:
Unknown column '0' in 'where clause'

SELECT `no_order` FROM (`order`) WHERE `0` = 'no_order <' AND `1` = '1' ORDER BY `no_order` DESC

Thanks...
#9

[eluser]Michael Wales[/eluser]
You are calling the getwhere() method twice in your model.
Code:
$query = $this->db->getwhere('order', array( 'no_order <',$id));

1) The method is actually called get_where. I didn't know getwhere() would work, but I imagine its a remnant from earlier CI days. Probably best to correct this now, in case it gets deprecated in future releases.

2) This is an error in my code example - I wasn't really paying attention. The second parameter is an array where the key is the column and value is the value you want to search for. The array you and I are passing (since we're only defining values) looks like this:
Code:
Array (
  [0] => 'no_order',
  [1] => 1
)

This is why you are getting that MySQL error. The actual code for the get_where() call should be:
Code:
$query = $this->db->getwhere('order', array( 'no_order <' => $id));

So our array would then be formatted as:
Code:
Array (
  ['no_order'] => 12345
)

Sorry about that! Make sure you change the code in both methods!
#10

[eluser]handoyo[/eluser]
Thank Michael,i got it working already..Suppose i combine it with string or date,then i have to explode it first,do i?Thanks Michael.. ^_^




Theme © iAndrew 2016 - Forum software by © MyBB