I am working on a web project in php that could potentially have a lot of users. From an administrative point of view, I am attempting to display a table containing information about each user, and seeing as the users table could grow extremely large, I would like to use pagination.
On the backend, I created a service that expects a limit and an offset parameter that will be used to query the database for records within the appropriate range. The service returns the total count of records in the table, along with the records matching the query
public static function getUsersInfo($limit = 50, $offset=1)
{
$users_count = Users::count(
array(
"column" => "user_id"
)
);
$users_info = array();
$users = Users::query()
->order('created_at')
->limit($limit, $offset)
->execute()
->toArray();
foreach ($users as $index => $user) {
$users_info[$index]['user_id'] = $user['user_id'];
$users_info[$index]['name'] = $user['first_name'] . " " . $user['last_name'];
$users_info[$index] ['phone'] = $user['phone'];
$users_info[$index] ['profile_image_url'] = $user['profile_image_url'];
}
$results = array(
'users_count' => $users_count,
'users_info' => $users_info
);
return !empty($results) ? $results : false;
}
On the frontend, what I would like to achieve ideally is, have the navigation displayed at the bottom of the table, with the typical previous, next buttons, and additionally a few numbers that allow the user to quickly navigate to a desired page if the page number displayed. This is what I have so far for the UsersController, with no pagination.
class UsersController extends ControllerBase
{
public function indexAction()
{
$usersObject = new Users();
$data = $usersObject->getUsers();
if ($data['status'] == Constants::SUCCESS) {
$users = $data['data']['users_info'];
$users_count = $data['data']['users_count'];
$this->view->setVar('users', $users);
}
echo $this->view->render('admin/users');
}
public function getUsersAction()
{
echo Pagination::create_links(15, 5, 1);
}
}
I don't have any working pagination yet, but I was thinking a good way to go would be to create a Pagination library with a create_links function that takes the
total_count of records in the database, so I know how many pages are expected
limit so I know how many records to collect
cur_page so I know where to start retrieving from
So when that function is called with the correct parameters, it would generate the html code to achieve the pagination, and that in turn can then be passed to the view and displayed.
I have never done this before, but from the research I have done so far, it seems like this might be a good way to approach it. Any guidance, suggestions, or anything at all really, regarding this would be greatly appreciated.
It looks like you are you using some bespoke MVC-ish framework. While it does not answer your question exactly I have a few points:
If you are looking at a lot of users, pagination is the least of your problems. You need to consider how the database is indexed, how the results are returned and much more.
Without understanding the underlying database abstract layer / driver you are using it is difficult to determine whether or not your ->limit($limit, $offset) line will work correctly. The offset should probably default to 0, but without knowing the code it is hard to say.
The ternary operator in your first method (return !empty($results) ? $results : false;) is currently valueless, because the statement before it will mean the variable will always be an array.
Avoid echo statements in controllers. They should return to a templating engine to output a view.
You Users class would be better named User, as the MVC framework implies that the 'Model' is a singular entity.
While it is not a general rule, most pagination systems I have used have worked on a zero-index system (Page 1 is Page 0), so calculating the limit range is simple:
$total_records = 1000;
$max_records = 20;
$current_page = 0;
$offset = $max_records * $current_page;
$sql = "SELECT * FROM foo LIMIT $offset, $max_records";
Related
It's hard to explain what I want exactly but I've gotta try to...
Laravel Eloquent inspired me to write a simple php class to work with databse.
As we know We can do this in laravel:
$run = DB::table('users')->where('id', 3)->where('level', 2)->get();
Also we do that:
$run = DB::table('users')->where('id', 3)->where('level', 2)->get()->count();
Also we can do that:
$run = DB::table('users')->where('id', 3)->where('level', 2)->get()->first();
Even we can do that too:
$run = DB::table('users')->where('id', 3)->where('level', 2)->get()->pluck('id')->toArray();
And that I have not ever tried but I believe it works too:
$run = DB::table('users')->where('id', 3)->where('level', 2)->get()->pluck('id')->toArray()->first();
The question is "How does it work?"
How should I write to return suitable results in any of their ways?
// It was easy to write my code to return total results if I write like that
$run = DB::from('users')->where('id', 3)->where('level', 2)->get()->count();
// Or to return first result if I write like that
$run = DB::from('users')->where('id', 3)->where('level', 2)->get()->first();
// But what sould I do to return all the results if write like that (As eloquent works).
$run = DB::from('users')->where('id', 3)->where('level', 2)->get();
I need something like "if - else case for methods" like:
function __construct() {
if(if aint`t no calling any methods except **get()** ){
// Lets return default method
return $this->results();
}
else{
// Do whatever...
}
}
There is my whole code:
https://github.com/amirandev/PHP-OOP-DB-CLASS/blob/main/db.php
As I know, when you are trying something like that
$run = DB::from('users')->get()->count();
You get all users and php/laravel count users, meaning
$users = DB::from('users')->get(); // get all users
$usersConut = $users->count(); //count them away of database/mysql
The same thing with first()
when you are using this code DB::from('users')->count(); you are actually asking MySql for count not counting them in the backend.
I highly recommend using this package barryvdh/laravel-debugbar to help you see the database queris.
Each method returns $this. So the next method will have the class with the modifications done by the previous method. It's quite easy to achieve that.
class Example
{
private string $sentence = '';
public function make()
{
return $this->start()->end();
}
public function start()
{
$this->sentence .= 'This will be a ';
return $this;
}
public function end()
{
$this->sentence .= 'whole sentence.';
}
}
BTW, Eloquent query builder converts the method chain into an SQL query string. That's why it works really fast. If you just query one table, let's say users, via the query builder, and then filter the results in your application (like it was based on the where condition), the process will be quite slow because the hard work will be done by your app.
SQL is extremely fast, that's why we want to complete as many tasks as possible on the SQL side.
In the past I've worked with framework as Slim or CodeIgniter, both provide method such as getWhere(), this method return true or false if the array content passed to the getWhere was found on database table.
Actually I've created my own layer class that extends PDO functionality, my goal is create a method that take care to look for a specific database content based on the supplied parameters, currently I created this:
public function findRecordById($table, $where = null, $param = null)
{
$results = $this->select("SELECT * FROM $table WHERE $where",$param);
if(count($results) == 0)
{
return false;
}
return true;
}
for search a content I simply do:
if(!$this->db->findRecordById("table_name", "code = :key AND param2 = :code",
array("key" => $arr['key'], "code" => $arr['code']))){
echo "content not found";
}
now all working pretty well but I think that the call on the condition is a bit 'too long and impractical, I would like to optimize everything maybe going all the content into an array or something, but until now I have a precise idea. Some help?
I don't quite understand your question, but for the code provided I could tell that such a method should never belong to a DB wrapper, but to a CRUD class, which is a completely different story.
If you want to use such a method, it should be a method of a Model class, and used like this
$article = Article::find($id);
While for a database wrapper I would strongly suggest you to keep with raw SQL
$sql = "SELECT * FROM table_name WHERE code = :key AND param2 = :code";
$data = $this->db->query($sql, $arr)->fetchAll();
is the clean, tidy, and readable code which anyone will be able to read and understand.
As a bonus, you will be able to order the query results using ODER BY operator.
How can one retrieve the total number of items found with count() via the Zend Pagintor, NOT the page count?
Current tests come up with this approximation as the closest without manipulating the DBSelect count method:
$totalCount = $pagintor->count() * $paginator->getItemCountPerPage();
My question relates to the count() process used by the paginator to get the total number of records.
I've seen this: Zend Framework 2 - Pagination
and read these docs
http://framework.zend.com/manual/current/en/modules/zend.paginator.advanced.html
http://framework.zend.com/manual/current/en/modules/zend.paginator.usage.html
Are we to customise the count() method for the pagination object just to get the count as per last link?
class MyDbSelect extends Zend\Paginator\Adapter\DbSelect
{
public function count()
{
$select = new Zend\Db\Sql\Select();
$select->from('item_counts')->columns(array('c'=>'post_count'));
$statement = $this->sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
$row = $result->current();
$this->rowCount = $row['c'];
return $this->rowCount;
}
}
$adapter = new MyDbSelect($query, $adapter);
$paginator = new Zend\Paginator\Paginator($adapter);
Maybe I've missed something (probably true...) but since the pagination object has already gone to the trouble of compiling a 'count' why/how can we access this number without doing any other hurdles or obstacle courses...
Is there a $paginator->getTotalCount() method somewhere to access this variable...
The final result might be something like '20 records of 4536 total' where 4536 the total.
Many thanks in advance.
ENV: ZF 2.3.9 (not 2.4+)
Sounds like you want totalItemCount.
$paginator->getTotalItemCount();
I'm requesting the community's wisdom because I want to avoid bad coding practices and/or mistakes.
I'm having a php class wich is an objects manager. It does all the work with the database: inserting new data, updating it, getting it and deleting it (I've read it's called CRUD...). So it has a function that gets an element by id.
What I want to write is a function that gets a list of objects from the table.
I will then use a mysql query that goes something like
SELECT * FROM mytable WHERE column1='foo'
And then some order by and limit/offset.
However, in my application there are different cases in which I will need different lists from this table. The WHERE clause will then be different.
Should I write different functions, one per type of list?
Or should I write one generic function to which I will send arguments that then dynamically creates the query? If so, do you have any advice on how to do this properly?
EDIT:
Thanks for all your answers! I should tell that I'm not using any framework (maybe wasn't the best idea...), so I didn't know about query builders. I'll investigate that (either finding a standalone uery builder or migrating to a framework or writing my own, I don't know yet). That will be useful any time I need to execute a mysql query :-)
Although I'm still confused:
Let's say I need several lists of clients (objects), for example all clients, clients over 18, clients currently online...
What approach would be best to retrieve those lists? I can either have 3 functions in my clients manager
allClients() {//execute a specific query and return list of objects}
allClientsOver18() {//execute specific query and return list of objects}
allClientsOnline() {//execute specific query and return list of objects}
or I can have one function tht builds the query based on parameters
listClients($some, $parameters)
{
//Build the query based on the parameters (definitely need a query builder!)
//Execute the query
//return list of objects
}
Which approach would be best (I guess it depends on circumstances) and mostly, why?
Thanks in advance!
Rouli
Thanks for all the info on query builders, I didn't even know it existed! :-) However I'm still confused as to wether I should write one specific function for each case (that function can still use the query builder to write its specific query), or write one generic function that builds dynamically the query based onf parameters. Which would be better in which case? I've added an example in my question, hope it makes it clearer!
This depends on how often you use each of these isolated queries, how complex the conditions are and how often you my need to combine the conditions with other queries. For eaxample if each the "online" and "over18" are just simple conditions then you could just use the normal findBy logic from my example:
$table = new MyTable($db);
$onlineOnly = $table->findBy(array('is_online' => true), null, null);
$over18Only = $table->findBy(array('is_over_18' => true), null, null);
$onlineOver18 = $table->findBy(array('is_over_18' => true, 'is_online' => true), null, null);
If the query is more complex - for example to get over 18 clients you have to do:
select client.*, (YEAR(CURDATE()) - YEAR(client.birthdate)) as age
FROM client
WHERE age >= 18
Then its probably better to make this into a separate method or create methods to work on Query objects directly to add complex conditions for example - especially if you will need this condition in a few different queries in the app:
$table = new MyTable($db);
// creates a basic query defaulted to SELECT * FROM table_name
$query = $table->createQuery();
// adds the complex condition for over 18 resulting in
// SELECT table_name.*, (YEAR(CURDATE()) - YEAR(table_name.birthdate)) as age WHERE age >= 18
$over18 = $table->applyOver18Query($query)->execute();
This way you can apply your over 18 condition easily to any query with out manually manipulating the builder ensure that your over 18 condition is consistent. But for simplicity you could also have a convenience method like the following:
public function findOver18By(array $criteria, $limit = null, $offest = null) {
$query = $this->findBy($criteria, $limit, $offset);
$this->applyOver18Query($query);
return $query->execute();
}
Normally you would use some kind of query builder at the lower level like:
$query = $db->createQuery()
->select($fields)
->from($tableName)
->where($fieldName, $value);
$results = $query->execute();
Then you might have a class that makes use of this like:
class MyTable
{
protected $tableName = 'my_table';
protected $db;
public function __construct($db) {
$this->db = $db;
}
public function findBy(array $criteria, $limit = null, $offset = null) {
$query = $this->db->createQuery();
$query->select('*')->from($this->tableName);
foreach ($criteria as $col => $value) {
// andWhere would determine internally whether or not
// this is the initial WHERE clause or an AND clause
// something similar would happen with an orWhere method
$query->andWhere($col, $value);
}
if (null !== $limit) {
$query->limit($limit);
}
if (null !== $offset) {
$query->offset($offset);
}
return $query->execute();
}
}
Usage would look like:
$table = new MyTable($db);
$result = $table->findBy(array('column1' => 'foo'), null, null);
This is a lot to implement on your own. Most people use an ORM or a DBAL to provide these features and those are often included with a framework like Eloquent with Laravel, or Doctrine with Symfony.
I guess at start you should need some main data like
$main = [
'from' = '`from_table`',
]
Then you should add selects if had
$selects = ['fields1','field2'];
$where = ['some condition', 'other condition'];
Then you could
$query = "SELECT ".implode(',', $selects ." FROM ".$main['from']."
WHERE ".implode('AND ', $where .";";
That's some approaches for simple one table query.
If you need Joins, then $selects better would be make with aliasos, so no field will be lost if they are not different, like
select temp.id as temp_id , temp2.id temp2_id from temp
left join temp2 on temp2.temp_id = temp.id
Feel free to ask some questions, maybe i haven't told , but you should also check bound parameters with some functions to avoid sql injections
I suggest using a CLASS for your database which holds all your database accessing functions as it makes your code cleaner making it more easier to look through for errors or modifications.
class Database
{
public function connect() { }
public function disconnect() { }
public function select() { }
public function insert() { }
public function delete() { }
public function update() { }
}
sample connect function for connecting to a selected database.
private db_host = ‘’;
private db_user = ‘’;
private db_pass = ‘’;
private db_name = ‘’;
public function connect()
{
if(!$this->con)
{
$myconn = mysqli_connect($this->db_host,$this->db_user,$this->db_pass);
if($myconn)
{
$seldb = mysqli_select_db($this->db_name,$myconn);
if($seldb)
{
$this->con = true;
return true;
} else
{
return false;
}
} else
{
return false;
}
} else
{
return true;
}
}
with this approach will make creating CRUD functions easier. Heres a sample insert function.
public function insert($table,$values,$rows = null)
{
if($this->tableExists($table))
{
$insert = 'INSERT INTO '.$table;
if($rows != null)
{
$insert .= ' ('.$rows.')';
}
for($i = 0; $i < count($values); $i++)
{
if(is_string($values[$i]))
$values[$i] = '"'.$values[$i].'"';
}
$values = implode(',',$values);
$insert .= ' VALUES ('.$values.')';
$ins = #mysql_query($insert);
if($ins)
{
return true;
}
else
{
return false;
}
}
}
heres a quick view on using this.
;<?php;
$db->insert('myDataBase',array(3,"Name 4","this#wasinsert.ed")); //this takes 3 paramteres
$result = $db->getResult(); //Assuming you already have getResult() function.
print_r($result);
?>
EDIT
there are more purist approach to handling database operations. I highly suggest it because handling information is very delicate and should be fronted with many safety measures But it requires deeper php knowledge. Try PDO for php and this article by matt bango on prepared statements and its significance.
Imagine a "Games" class used to track games between opponents. Is it better OOP to have 1 method to retrieve games based on user input parameters or is it better to have multiple methods specific to the retrieval goals?
class Games {
function get_games($game_id = NULL, $stadium_id = NULL, $start_date = NULL,
$end_date = NULL, $count = 999); {}
}
VS
class Games {
function get_all_games($count = 999); {}
function get_game_by_id($game_id = 1); {}
function get_games_by_stadium($stadium_id = 1); {}
function get_games_by_dates($start_date = NULL; $end_date = NULL) {}
}
Explanation of benefits and any coding / snytax tips would be appreciated. Thanks.
The more I practice OOP the more I find myself following a rule about passing parameters to methods. Kind of like having many levels of nested if statements, I find that if I have more than two I might be doing something wrong.
Keep your code simple. You're writing a method that does something, not a block of procedural code that does everything. If you want to get a game, then get a game. If you want to get a list for a date range, then do that.
However I would point out that you don't really need get_all_games() - You can just allow for get_games_by_dates() to be passed with no parameters. If it doesn't get any then it would get the games for every date since forever (all the games)
I would always err on the side of OOP code. the reason being is that it makes you code much easier to maintain and read. The more functions you have the easier it is to follow code later on down the road
I would go for separate methods since you are using lots of paramaters with default values.
If you want to get all games you would have to do:
$games->get_games(NULL, NULL, NULL, NULL, 999);
Assuming that your get_....() functions are returning all game data, I would write a single function to return this data, based on an id passed in, and write a series of find_...() functions to return an array of found ids. This will have the added benefit of making it easier to override the data retrieval code in decendant classes.
class Games {
public function get_game($game_id) {
// Return game details (array/object) for $game_id, or FALSE if not found.
}
public function find_all_games() {
// Return array of ids for all games.
}
public function find_games_by_dates($start_date = NULL, $end_date = NULL) {
// Return array of ids between $start_date and $end_date unless NULL.
}
}
You can then call:
$oGames = new Games() ;
$aGames = $oGames->find_all_games() ;
foreach($aGames as $id) {
$aGame = $oGames->get_game($id) ;
if($aGame !== FALSE) { // This check might be skipped if you trust the array of ids from find_all_games().
// Assuming an array is returned.
echo "Game Found: ".$aGame['name']."\n" ;
}
}
The benefit of "multiple methods specific to the retrieval goals" is that you can add/remove goals. The problem with using one monolithic function with a bunch of parameters is that, should you decide to add/remove a way to get games, you'd have to change the interface. Which would break any code that uses it.
Each method should be as concise as possible, performing only one function.