Jump to content

Php Arrays With Basic Mysql Queries Or Mysql Advanced Queries?


kalster

Recommended Posts

i am using php for the main programming language. i would like to display a html table for the logged in users level rank. so if that user is at a rank of 20, i would like to display a table from rank 15 to 25. that would display the logged in user at the middle of the table. however, if the user has a rank of 2 then i would need to display a rank table from 1 to 10. So i am not sure if mysql can do this kind of query.

 

would it be more easer for me to create php arrays from a mysql results where each array would hold the usernames rank and then display those arrays in a html table? My idea is if i had the arrays holding the users mysql information then i could use those arrays to make all kinds of user information html tables, including some advanced php searches, instead of using a different mysql query for the results. The problem with arrays is that the memory usage would be double that of a query. so what would be the best way to go about this. php arrays using a few basic mysql queries or mysql advanced queries?

Link to comment
Share on other sites

I'd do something like this

 

$mysqli = new mysqli('host', 'user', 'pwd', 'database');

$targetId = intval($_GET['id']);	 // get the target id

/******************************
* Find row number of target id
******************************/
$sql = "SELECT row
FROM (
SELECT pid,
 @rownum := @rownum + 1 AS row
FROM bg_points
 INNER JOIN (
	 SELECT @rownum := 0
	 ) as init
ORDER BY points DESC
) as findrow
WHERE pid = $targetId";

$res = $mysqli->query($sql);
$row = $res->fetch_assoc();

/******************************
* get the start row for query
******************************/
$start = $row['row'] < 5 ? 0 : $row['row'] - 5;
$res->free();

/***********************************
* get rows either side of target
************************************/
$sql = "SELECT ranking, pid, points
FROM (
SELECT pid,
 @rownum := @rownum + 1 AS row,
 @rank := if (@prev<>points, @rownum, @rank) AS ranking,
 @prev:= points AS points
FROM bg_points
 INNER JOIN (
	 SELECT @rownum := 0, @prev := -1, @rank := 0
	 ) as init
ORDER BY points DESC
) as findranks
LIMIT $start, 10";

$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
// output row data
}

Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.