I want make a pagination than i find this problem. pagination limit query not working properly.
if($this->input->get('page'))
{
$start = abs(($this->input->get('page', TRUE)-1)*$config['per_page']);
}else{
$start = 0;
}
$condition = array();
if (preg_match('/WMV/', $whatFind)) {
$condition = array('registerId' => $whatFind);
}
$results = $this->db->
group_by('registerId')->
//($start, $config['per_page'])->
order_by('id', 'desc')->
get_where('tbl_applications', $condition)->
result();
You need to use the limit
$this->db->limit(X);
where X is the limit rows to return
$results = $this->db
->group_by('registerId')
->limit($start)
->order_by('id', 'desc')
->get_where('tbl_applications', $condition)
->result();
If you want to limit with a fixed digit (10), then you should do this in your query
->limit(10)
Hello you need to define limit and add in you query.
May below link will help you
Codeigniter Pagination From Database limit, offset
Related
Lets say I have a article table & I want to get the latest articles (10 of them) & then also be able to make another call to get the next 10 latest articles (10-20). To give you an idea to why I want to be able to do this, it's so I can add a nextUrl field to my API call.
I was hoping I'd be able to use offset & limit to achieve this. But it seems as though I can only do this from the records that were entered first. Any ideas?
For example, try this pagination in web-service:
<?php
$perpage = '10';
$total_records = '50'; // so there will be 5 pages (50/10 = 5)
$page = "1";
function test($perpage,$page) {
if($page == '') {
$page = 1;
}
if($page == 1)
{
$start = 0;
$end = $perpage ;
}
else
{
$page -= 1;
$start = $page * $perpage;
$end = $perpage;
}
$q = $this->db->query("SELECT * FROM tbl_name ORDER BY id DESC LIMIT $start, $end");
if($q->num_rows() > 0) {
return $q->result_array();
} else {
return '';
}
}
?>
This is in CodeIgniter framework.
Each time send page number.
First time it will be 1, then second time 2, and so on.. It will give 10 records per page.
Earlier this day a asked a question about an update query. But now i want to select some things ( and it is working ) but I also want to order them and put a limit on it.
This is the code to select all the food :
public function getFood($id)
{
$id = (int)$id;
$rowset = $this->tableGateway->select(array('kindOfFood_id' => $id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
But how can i do this :
Select * from KindOfFood ==> order by kindOfFood_votes DESC ?
I saw on the documentation you can do something like this, but it doesn't work with me?
$rowset = $artistTable->select(function (Select $select) {
$select->where->like('name', 'Brit%');
$select->order('name ASC')->limit(2);
});
Are you looking to return only single row or multiple rows.
Try this for multiple rows -
use Zend\Db\Sql\Select; //at the top of the page among other use statements.
public function getFood($id)
{
$id = (int) $id;
$select = new Select(TABLE_NAME); //CHANGE TABLE_NAME as per needs
$select->where('kindOfFood_id = ' . $id);
$select->order('kindOfFood_votes DESC');
$resultSet = $this->tableGateway->selectWith($select); //Will get array of rows.
//$row = $rowset->current(); THIS IS FOR RETURNING ONLY SINGLE ROW NOT ALL ROWS
if (!$resultSet) {
throw new \Exception("Could not find rows with food id - $id");
}
return $resultSet;
}
Can access the returned resultSet via loop. Eg: foreach
foreach($resultSet as $row) {
echo $row->kindOfFood_id; //or something
}
Note:
If you need only
Select * from KindOfFood order by kindOfFood_votes DESC
then remove the $select->where('kindOfFood_id = ' . $id); line from above.
As you have observed I have put an ambiguous title for this question, as simply I do not realize (by lack of deep knowledge) if this is the true problem or not.
Let's start then with a short description:
Here is where I select my products:
$select_prod = "SELECT * FROM product WHERE product_id IN ($some_array)";
After that here is where I define the pagination stuff:
$query_page = mysql_query($select_prod);
$product_total = mysql_num_rows($query_page);
$page_rows = 4;
$last = ceil($product_total/$page_rows);
if ($page < 1) {
$page = 1;
} elseif ($page > $last) {
$page = $last;
}
$limit = 'limit ' .($page - 1) * $page_rows .',' .$page_rows;
And where I prepare the render
$page_query = mysql_query($select_prod . $limit);
$results = array();
while ($array_filter = mysql_fetch_array($page_query)) {
$results[] = $array_filter;
}
Until this point everything is flowing easily, and I get my products listed as I wanted, BUT in random ORDER.
I have tried to include "ORDER BY price ASC" at the end of the first query like this:
$select_prod = "SELECT * FROM product WHERE product_id IN ($some_array) ORDER BY price ASC";
but for a strange reason fails to list the products with the error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given...
I've already had several hours in trying to find where could be the problem, and this forum seems to be the final try for me, after that I would let them order as they want to.
You really need to print out your full query directly before execution. Try this instead:
$select_prod = "SELECT * FROM product WHERE product_id IN ($some_array) ORDER BY price ASC ";
Or, change the limit line to have a space before the limit.
What I believe the problem is the the lack of space before limit. The snippet in ()limit 100 is valid SQL. The snippet in () order by price asclimit 100 is not valid SQL.
I want to run following query in symfony doctrine.
SELECT p.id AS id FROM skiChaletPrice p WHERE ski_chalet_id = ? AND month = ?
I wrote my doctrine query as following.
$q = Doctrine_Query::create()
->select('p.id AS id')
->from('skiChaletPrice p')
->andWhere('ski_chalet_id = ?', $chaletId)
->andWhere('month = ?', $from);
$result = $q->fetchOne();
if ($result->count() > 0) {
return $result->toArray();
} else {
return null;
}
But my result always include all columns in the table. What the issue? Please help me.
The issue is that fetchOne() will return a Doctrine object, which implicitly contains all the columns in the table. $result->toArray() is converting that doctrine object to an array, which is why you get all the columns.
If you only want a subset of column, don't hydrate an object, instead do something like this:
$q = Doctrine_Query::create()
->select('p.id AS id')
->from('skiChaletPrice p')
->andWhere('ski_chalet_id = ?', $chaletId)
->andWhere('month = ?', $from);
$results = $q->execute(array(), Doctrine::HYDRATE_SCALAR);
See http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/data-hydrators.html
This is how I should do it:
$result = Doctrine_Query::create()
->select('id')
->from('skiChaletPrice')
->andWhere('ski_chalet_id = ?', $chaletId)
->andWhere('month = ?', $from)
->limit(1)
->fetchOne(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
// result will be a single id or 0
return $result ?: 0;
// if you want array($id) or array() inseatd
// return (array) $result;
I want to fetch intermediate rows from by database.
Like for last 10 rows i will use limit :
return Doctrine_Query::create()
->select('v.*')
->from('Video v')
->where("v.community_id='$community_id' AND v.user_id='$user_id' AND v.published='$published'")
->orderBy('v.id DESC')
->limit(10)
->execute();
but what if i want 110-120 rows?Can anybody tell me about that? how to write this kind of query in doctrine
Use the offset() clause.
You could use a Doctrine_Pager
$page = 10;
$limit = 10;
$query = Doctrine_Query::create()
->select('t.*')
->from('SomeTable t')
$pager = new Doctrine_Pager(
$query,
$page,
$limit
);
$rows = $pager->execute();
For rows 110-120, you want
LIMIT 109, 10
use offset clause...chk dis