Jump to content

Recommended Posts

Hi I posted a similar problem yesterday but I d[code]ont think I explained properly. What I am trying to do is get all of the usernames out of one table in a database then get statistics out of a different table for each of those usernames. I then want to sort the data so that the highest level is printed in the table first. I tried using ORDER BY in the sql statement but this did not seem to work. I think this is because it has to get the stats out one by one in the order that the [/code]usernames come out of the first table.

here is the code that I have so far:

[code]//Get data out of db
$query = "SELECT * FROM phpbb_users";
$result = mysql_query($query) or die("Could not query: " . mysql_error());

//Put required data into an array
$info = array();
$i = 0;
while ($row = mysql_fetch_assoc($result))
{
$info[$i]['username'] = $row['rsusername'];
$info[$i]['user_id'] = $row['user_id'];
$i++;
}

//Display each row
$i = 0;
foreach ($info as $r => $value)
{
$user = $info[$r]['username'];
$id = $info[$r]['user_id'];

$query = "SELECT * FROM stats WHERE userID='$user' AND cat='$skill' ORDER BY statC DESC";
$result = mysql_query($query) or die("Could not query: " . mysql_error());


$data = array();
$i = 0;
while ($row = mysql_fetch_assoc($result))
{
$data[$i]['userID'] = $row['userID'];
$data[$i]['cat'] = $row['cat'];
$data[$i]['statA'] = $row['statA'];
$data[$i]['statB'] = $row['statB'];
$data[$i]['statC'] = $row['statC'];
$i++;
}

foreach ($data as $r => $value)
{


echo '<tr>
<td class="row1"><span class="genmed">' .$rank . '</span></td>
<td class="row1"><span class="genmed"><a href="profile.php?mode=viewprofile&u=' . $id . '">' . $user . '</a></span></td>
<td class="row1"><span class="genmed">' . $data[$r]['statA'] . '</span></td>
<td class="row1"><span class="genmed">' . $data[$r]['statB'] . '</span></td>
<td class="row1"><span class="genmed">' . $data[$r]['statC'] . '</span></td>
      </tr>';
$i++;
}
$i++;
}[/code]

Is there anyway that I can sort the table by $row['statC']?
Link to comment
https://forums.phpfreaks.com/topic/21738-sorting-data-retrieved-from-db/
Share on other sites

Sorry I must not have explained properly again. I have tried to use ORDER BY but it did not work.

I am trying to sort the second query not the first. I think the problem is that mysql cannot order the data as it comes out because the first statement which gets each username determines the order in which the second statement gets the data out. I'm sorry I am really bad at describing things.
well, you could try using arsort() (or asort for ascending) on your data array, but I've never tried it on a multi-dimensional array. I would do a more sloppy (but predictable) separate array for each element, then use $my_array[key($data)] to access it.

you might also try enclosing your field names in `slanted quotes` (whatever they're called)

$query = "SELECT * FROM `stats` WHERE `userID`='$user' AND `cat`='$skill' ORDER BY `statC` DESC";
again, mysql should be able to handle it.

When I run into this kind of problem, I plug my query into phpmyadmin and see what happens. If it doesn't work right, I rebuild the query using myphpadmin's interface, and it almost always reveals my problem.
No it does not seem to work for a multidimensional array. I have used the following code in the past for multi dimensional arrays:

[code]
function desc($a, $b)
{
if($a['statC'] == $b['statC'])
return 0;
else
return ($a['statC'] > $b['statC']) ? -1 : 1;
}
usort($data, 'desc');
[/code]

The problem with using this code this time is the 2nd for each loop is nested inside the first so it tries to declare this function for every user resulting in an error. any idea on how to modify the above code so that it doesnt have to be declared as a function?
I'm getting kind of hazy on this thing, but I'd imagine you'd want to build all the arrays first -- end loop. then sort the arrays, then loop again through printing the arrays. This should bypass the foreach loop altogether, no?
okay, back to simple:

do it just like you're doing it now EXCEPT, create a simple array alongside the multidimensional one. all it does is use the same $i as the key, but the value is the field you want to sort by. do you asort (or arsort for reverse sort) to the simple array, reset it, then use it as the key to the big array.
[code=php:0]do {
  echo $data[key($simple)']['whatever'];
} while(next($simple));[/code]
Thanks for all the help so far but what you suggested outputs the data in the same order as before and asort/arsort still has no effect.

here is the code i added:

[code]$simple = array();
$simple[$i]['statC'] = $data[$i]['statC'];

arsort($simple);

do {
echo $data[key($simple)]['statC'];
}
while(next($simple));[/code]
did you put it alongside? the other arrays?

[code=php:0] while ($row = mysql_fetch_assoc($result))
{
$data[$i]['userID'] = $row['userID'];
$data[$i]['cat'] = $row['cat'];
$data[$i]['statA'] = $row['statA'];
$data[$i]['statB'] = $row['statB'];
$simple[$i] = $row['statC'];
$data[$i]['statC'] = $row['statC'];
$i++;
}
arsort($simple);[/code]
Yes I did but it still puts them in the order that the usernames come out of the first table. asort/arsort have no effect while the 2nd for each loop is inside the first. If I use the 2nd loop on its own it does work but the problem there is that it doesnt do it for every user in the first tble onstead it does it just does it for the users in the 2nd table.
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.