dardime Posted October 30, 2015 Share Posted October 30, 2015 Hi! Good day! I need help in displaying all records on my table that have the same stockin_id as it shows only one record in my query.I used this code to fetch: <tr> <?php if(!empty($stocks)) { $total = 0; foreach($stocks as $stock) { $page->table = "stock_stockin_product"; $page->cond = "stockin_id='".$stock["id"]."'"; $page->selectQuery(); $stockin = $page->fetchQuery(1); ?> <th><?php echo $stockin["id"]; ?></th> <td><?php echo $stock["id"]; ?></td> <td><?php echo date ('d-m-Y H:i', $stock["stock_date"]); ?></td> <td><?php echo $stockin["price"]; ?></td> <td><?php echo $stockin["quantity"]; ?></td> <td>$<?php echo number_format($stockin['quantity'] * $stockin['price'], 2, '.', ','); ?></td> <td><?php echo $stock["branch"]; ?></td> </tr> This is my output and shows only one record for stockin_id=2515. This is the total records from database stockin_id=2515. Thanks in advance for your help and reply. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/298924-display-all-records-with-the-same-stockin_id/ Share on other sites More sharing options...
benanamen Posted October 30, 2015 Share Posted October 30, 2015 Post your Class. At first glance I would say it might have to do with $stockin = $page->fetchQuery(1) Quote Link to comment https://forums.phpfreaks.com/topic/298924-display-all-records-with-the-same-stockin_id/#findComment-1524787 Share on other sites More sharing options...
dardime Posted October 30, 2015 Author Share Posted October 30, 2015 (edited) Hi Thank you for your reply. Here is my class function: <?php session_start(); include_once("db.class.php"); class paging extends db { //for mysql paging var $page; var $page_limit; var $page_details = array(); var $page_results = array(); var $grid_columns; var $grid_results = array(); var $tab_border; var $tab_width; var $tab_padding; var $tab_spacing; var $tab_col_width; var $tab_header = array(); var $tab_header_class; var $tab_data_class; var $tab_data_odd_class; var $tab_data_even_class; function generatePaging() { if(!$this->page) $this->page = 1; $this->result = mysql_query($this->myquery); if($this->result) { $total = mysql_num_rows($this->result); $last = intval(($total/$this->page_limit)); if($this->page<=1) { $start = 0; $prev = 1; } else { $start = ($this->page-1)*$this->page_limit; $prev = $this->page-1; } $end = $this->page_limit; $next = $this->page+1; $last = intval($total/$this->page_limit); if(($total/$this->page_limit) > $last) $last += 1; if(!$last) $last = 1; if($next>$last) $next = $last; $this->myquery = $this->myquery." LIMIT ".$start.",".$end; //echo $this->myquery; $this->result = mysql_query($this->myquery); $end = mysql_num_rows($this->result); for($i=0;$row=mysql_fetch_assoc($this->result);$i++) { $this->page_results[] = $row; } if($this->page_results) { $start += 1; $to = $start + $end - 1; } else { $start = 0; $to = 0; } if($to>$total) $to = $total; for($j=0,$i=$start;$i<=$last;$i++,$j++) { $pages[$j] = $i; } $this->page_details = array( "prev" => $prev, "next" => $next, "start" => $start, "to" => $to, "total" => $total, "last" => $last, "pages" => $pages ); } return 1; } function generateGrid() { $w = 0; $x = $this->grid_columns; $z = count($this->page_results)/$x; $y = intval($z); if($z>$y) $y++; for($j=0;$j<$y;$j++) { for($i=0;$i<$x;$i++,$w++) { if($this->page_results[$w]) $this->grid_results[$j][$i] = $this->page_results[$w]; else break; } } return 1; } function tabulate() { if(count($this->tab_header)) { $table = "<table"; if($this->tab_border) $table .= " border='".$this->tab_border."'"; if($this->tab_width) $table .= " width='".$this->tab_width."'"; if($this->tab_padding) $table .= " cellpadding='".$this->tab_padding."'"; if($this->tab_spacing) $table .= " cellspacing='".$this->tab_spacing."'"; $table .= ">"; if($this->tab_header) { $table .= "<tr>"; $i = 0; foreach($this->tab_header as $tab_header) { $table .= "<td"; if($this->tab_header_class) $table .= " class='".$this->tab_header_class."'"; if($this->tab_header_align) $table .= " align='".$this->tab_header_align."'"; if($this->tab_col_width[$i]) $table .= " width='".$this->tab_col_width[$i]."'"; $table .= ">".$tab_header."</td>"; $i++; } $table .= "</tr>"; } if($j=count($this->page_results)) { for($i=0;$i<$j;$i++) { $table .= "<tr>"; foreach($this->page_results[$i] as $val) { $table .= "<td"; if($this->tab_data_class) $table .= " class='".$this->tab_data_class."'"; else if($this->tab_data_even_class && !($k%2)) $table .= " class='".$this->tab_data_even_class."'"; else if($this->tab_data_odd_class && ($k%2)) $table .= " class='".$this->tab_data_odd_class."'"; $table .= ">".$val."</td>"; } $table .= "</tr>"; } } $table .= "<table>"; return $table; } else { echo "error: header lacking."; return 0; } } } ?> function generateSelectQuery() { if(is_array($this->required)) { $j = count($this->required); for($i=0;$i<$j;$i++) { $req .= "`".$this->required[$i]."`"; if($j-1 != $i) $req .= ","; } $myquery = "SELECT ".$req." FROM ".$this->table; } else { if(!$this->required) $myquery = "SELECT * FROM ".$this->table; else $myquery = "SELECT ".$this->required." FROM ".$this->table; } if($this->cond) $myquery .= " WHERE ".$this->cond; if($this->groupby) $myquery .= " GROUP BY ".$this->groupby; if($this->order) $myquery .= " ORDER BY ".$this->order; if($this->ordertype) $myquery .= " ".$this->ordertype; if($this->limit) $myquery .= " LIMIT ".$this->limit; //echo $myquery; if($myquery) { $this->myquery = $myquery; return $myquery; } return 0; } function selectQuery() { $this->generateSelectQuery(); $this->result = mysql_query($this->myquery); if($this->result) return 1; $this->error = mysql_error(); return 0; } function fetchQuery($limit=0) { $temp = array(); if($this->result) { if(!$limit) { for($i=0;$row = mysql_fetch_assoc($this->result);$i++) { $temp[$i] = $row; } } else if($limit == 1) { $temp = mysql_fetch_assoc($this->result); } else { for($i=0;(($i<$limit)&&($row = mysql_fetch_assoc($this->result)));$i++) { $temp[$i] = $row; } } } return $temp; } Edited October 30, 2015 by dardime Quote Link to comment https://forums.phpfreaks.com/topic/298924-display-all-records-with-the-same-stockin_id/#findComment-1524789 Share on other sites More sharing options...
benanamen Posted October 30, 2015 Share Posted October 30, 2015 (edited) * Edited October 30, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298924-display-all-records-with-the-same-stockin_id/#findComment-1524791 Share on other sites More sharing options...
benanamen Posted October 30, 2015 Share Posted October 30, 2015 (edited) Try doing $stockin = $page->fetchQuery() If that doesnt work try $stockin = $page->fetchQuery(0) By the way, your mysql code is obsolete. You need to use PDO with prepared statements. Edited October 30, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298924-display-all-records-with-the-same-stockin_id/#findComment-1524793 Share on other sites More sharing options...
dardime Posted October 30, 2015 Author Share Posted October 30, 2015 hi ther. i tried both but it doesnt work. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/298924-display-all-records-with-the-same-stockin_id/#findComment-1524796 Share on other sites More sharing options...
mac_gyver Posted October 30, 2015 Share Posted October 30, 2015 (edited) the reason you are getting the wrong output is because you are trying to use the database methods in the pagination class to run your own data retrieval queries, inside of a loop, separately from the pagination query. the pagination class shouldn't be extending the db class, it should be dependent on the db class, so that you don't find yourself calling page methods that don't have anything to do with pagination. you also wouldn't be calling your own data retrieval queries, since that would bypass what the pagination is trying to accomplish. for pagination to work, you would build one query that gets the data you want in the order that you want it. the pagination methods would just query for and retrieve the correct logical page of data based on the page number that has been requested. to do what you are attempting using this code, you would build your one main query so that it gets the data you want and orders the rows by the stockin_id, so that rows having the same stockin_id value will be together in the result set. Edited October 30, 2015 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/298924-display-all-records-with-the-same-stockin_id/#findComment-1524812 Share on other sites More sharing options...
dardime Posted November 1, 2015 Author Share Posted November 1, 2015 Hi there thanks for your reply. I need help to join two tables so i can get all the result. Thanks in advance for your help. stock_stockin stock_stockin_product I need to get this result but i dont know how to join these two tables. Quote Link to comment https://forums.phpfreaks.com/topic/298924-display-all-records-with-the-same-stockin_id/#findComment-1525036 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.