Does anyone know RedbeanPHP? – PHP – SitePoint Forums


I’m using RedbeanPHP ORM to design a personal text-based web game.

So what I’m trying to do is list 10 rows of users from the database ranked by my ranking field.

I will have a list of users. I want to put a button for the next page which will allow the user to go to the next page of 10 users.

Right now it shows my list of users but does not rank it by rank. I also need to figure out how to show only 10 users and add a next page button that shows the next 10.

"; // Display how many users are in the database

for($i = 1; $i <= $userCount; $i++) { // Print Rows using the Count
 $galaxyuser = R::getRow("SELECT * FROM `users` WHERE `id` = '$i' ORDER BY `rank` ASC"); // Get Each User Info
 //Get User Information into Variables
 $pt = $galaxyuser['planettype']; 
 $money = $galaxyuser['money'];
 $covtroops = $galaxyuser['covtroops'];
 $atktroops = $galaxyuser['atktroops'];
 $deftroops = $galaxyuser['deftroops'];
 $covlvl = $galaxyuser['covlvl'];
 $yourcovstrength = ($data['covtroops']*$data['covlvl']*3000);
 $theircovstrength = ($galaxyuser['covtroops']*$galaxyuser['covlvl']*3000);
 // Determine Whether Your Covert Strength is higher than theirs so you can see what resources are onhand.
 if($yourcovstrength >= $theircovstrength){
	$covmoney = number_format($money);
 $size = number_format($galaxyuser['atktroops']+$galaxyuser['deftroops']+$galaxyuser['untrained']+$galaxyuser['miners']+$galaxyuser['covtroops']);	
	 $covmoney = "?????????";
	 $size = "?????????";
// Get Planet Image Name Depending on Planet Type.
	 $pl = "planet1_small";
	 $pl= "planet2_small";
	  $pl = "planet3_small";
	  $pl = "planet4_small";
	   $pl = "planet5_small";
	   $pl = "planet6_small";

//Print User Information
 print "
"; print ""; print "".$galaxyuser['username']."
"; print "               Army Size:".$size."
"; print "              Onhand Crystals:".$covmoney."
"; print "Crystal Attack | Civillian Raid | Intellegence Report"; print "

"; } ?>

Thanks for any help regarding this topic.

What you are asking for is called pagination. Paging involves four tasks –

  1. A query to get the total number of matching rows. The R::count() method apparently does this.
  2. A data retrieval query that gets a logical page of data, in any order you want… This query needs a LIMIT term x,y to return the correct rows from the result set.
  3. Code to display the data logic page.
  4. Code to create and generate pagination links.

There are countless php/mysql paging examples on the web to get you started. If me/others have time they will post an example.

A few points about the existing code –

  1. Do not execute queries inside loops. This is extremely inefficient, due to the number of communications between the web server and the database server. For what you do, there is no need. If you needed to find data matching a set of IDs, for example, you would do it all in one query using a WHERE clause that matches all the IDs you want.
  2. There is no guarantee that ids are contiguous, so the loop from 1 to user count may not match all user ids.
  3. Keep variable names generic. If the purpose of this code is to get and display Galaxy user data, once you have a major comment mentioning it, a row of data retrieved should simply be named $row.
  4. Don’t copy variables into other variables for nothing. It’s just a waste of typing time. Just use the original variables. There is nothing wrong with using a variable like $row[‘planettype’]and in fact, after making the copy of those variables, you are using the originals anyway, so those lines of code are a complete waste of time.
  5. Don’t write code for every possible value. for example planet types. Instead, use a mapping array, where the array index is the input value and the array element is the output value, then directly use the input value to retrieve the output value from the mapping table. This data can actually be stored in a database table, then you have to JOIN this table in the SELECT query to get the planet type names, instead of needing any php code.
  6. You can put php variables directly in a quoted string. This will eliminate all extra quotes and focus points, which usually lead to a lot of typing errors.
  7. Do not use   to format the output. Use CSS.

Thanks for that. I was able to google “pagnation with redbeanphp” and it turned up many useful links.


Here is a typical paging code for what you showed us that should work (untested), with most of the points made above –

= 1 && $val <= $total_pages; });

// build pagination links

// get a copy of any existing get parameters
$get = $_GET;

// produce previous
$get['page'] = $page - 1;
$qs = http_build_query($get,'', '&');
$pagination_links .= $page == 1 ? 'prev ' : "prev ";

// produce numerical links
foreach($links as $link)
	$get['page'] = $link;
	$qs = http_build_query($get,'', '&');
	$pagination_links .= $link == $page ? "$link " : "$link ";

// produce next
$get['page'] = $page + 1;
$qs = http_build_query($get,'', '&');
$pagination_links .= $page == $total_pages ? 'next' : "next";

// html document starts here...

There is no data to display.

"; } else { // there is data to display echo "Player Count is Currently: $total_rows
"; // Display how many users are in the database // calculate this only once, before the start of any looping $yourcovstrength = $data['covtroops']*$data['covlvl']*3000; // loop over the logical page of data foreach($result_data as $row) { $theircovstrength = $row['covtroops']*$row['covlvl']*3000; // Determine Whether Your Covert Strength is higher than theirs so you can see what resources are onhand. if($yourcovstrength >= $theircovstrength){ $covmoney = number_format($row['money']); $size = number_format($row['atktroops']+$row['deftroops']+$row['untrained']+$row['miners']+$row['covtroops']); }else{ $covmoney = "?????????"; $size = "?????????"; } // Get Planet Image Name Depending on Planet Type. $pl = isset($planet_map[$row['planettype']]) ? $planet_map[$row['planettype']] : 'no planet image'; //Print User Information print "
"; print ""; print "{$row['username']}
"; print "               Army Size:$size
"; print "              Onhand Crystals:$covmoney
"; print "Crystal Attack | Civillian Raid | Intellegence Report"; print "

"; } } ?>

Wow you rock! It works perfectly!

How to contact you. I learned so much from this and I can’t seem to be able to message you on these forums.

Thanks again. It’s even listed by rank :slight_smile:

You must reach trust level 1 before you can send personal messages, @bmanflores11.

However, we ask that you do not use the message system to ask for help, as this is not in the spirit of the forums. Public discussion means that the thread helps not only the person who asked the question, but also anyone else with a similar problem. smile

1 like

Ok I was wondering. I have never seen a forum that restricts messaging. I was curious about this. Thanks for the help.

1 like


Comments are closed.