sasori Posted June 12, 2013 Share Posted June 12, 2013 let's say i have a an object...it contains 100 objects/array..and then in it's sql query, I didn't use offset nor limit. my question now is, is there are way to manipulate the returned result using only PHP to display only like e.g 24 objects/array ? my objective is, I wanna paginate based on 24 result and not with 100 ..so how? Link to comment https://forums.phpfreaks.com/topic/279076-how-to-limit-the-result-using-php-without-using-limit-clause-when-fetching-rows-in-database-query/ Share on other sites More sharing options...
rwhite35 Posted June 12, 2013 Share Posted June 12, 2013 You'll want to run a query like: $count_result=$mysqli->query("SELECT count(*) FROM tbl_name"); which will return the total row count (ie 100) then you can divide that by the number of results per page. Once you have that you can run additional queries using: SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name LIMIT $start, $perpage where the starting record is the next record after the last query (say 20 per page). First time through $start = 0 and $perpage = 20. The next time through, $start = 21, $perpage = 20. Sorry left out a critical part, $count_result is used to define $perpage. So in the 100/20 example, you would generate 5 pages with 20 results each. Link to comment https://forums.phpfreaks.com/topic/279076-how-to-limit-the-result-using-php-without-using-limit-clause-when-fetching-rows-in-database-query/#findComment-1435566 Share on other sites More sharing options...
AbraCadaver Posted June 12, 2013 Share Posted June 12, 2013 Would need to see an example of the query and what is returned. Link to comment https://forums.phpfreaks.com/topic/279076-how-to-limit-the-result-using-php-without-using-limit-clause-when-fetching-rows-in-database-query/#findComment-1435568 Share on other sites More sharing options...
mac_gyver Posted June 12, 2013 Share Posted June 12, 2013 you can paginate an array using array_slice(). the second and 3rd parameters, offset and length, operate the same as the LIMIT operands do in a query. Link to comment https://forums.phpfreaks.com/topic/279076-how-to-limit-the-result-using-php-without-using-limit-clause-when-fetching-rows-in-database-query/#findComment-1435571 Share on other sites More sharing options...
sasori Posted June 13, 2013 Author Share Posted June 13, 2013 you can paginate an array using array_slice(). the second and 3rd parameters, offset and length, operate the same as the LIMIT operands do in a query. same suggestion as other developers to whom I have asked from... but anyway...I have solved my issue on my own by just adding a simple if statement $container = array(); foreach($hugeObject as $k => $obj){ if(isset($obj[$obj->USERID])){ continue; if(count($container) < 24) $container[$obj->USERID] = $obj; } Link to comment https://forums.phpfreaks.com/topic/279076-how-to-limit-the-result-using-php-without-using-limit-clause-when-fetching-rows-in-database-query/#findComment-1435657 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.