Jump to content

Putting SQL Data into Array and Sorting it


carleihar

Recommended Posts

Well, the title pretty much says it all. I'm trying to extract data from an array, put it into a 2D array and then sort it. Then I need to put it's "place" in the order into the table.

 

I've sort of got something so far but it seems like my while loop isn't working correctly.

 

$q="SELECT COUNT(*), horseID, total FROM enteredHorses";
$r = mysqli_query ($dbc, $q) or trigger_error(mysqli_error($dbc));
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

        $count = $row['COUNT(*)'];
        $horse_id = $row['horseID'];
        $total = $row['total'];
        

        

for ($counter = 1; $counter <= $count; $counter += 1) {

$i = 0;

$id[$i] = $horse_id;

$total[$i] = $total;


$i = $i+1;

}

} //end while

array_multisort($total, SORT_DESC, $id);


while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

for ($counter = 1; $counter <= $count; $counter += 1) {

$i = 0;
$place = '1';

$q = "INSERT INTO enteredHorses (place) VALUES ('$place') WHERE horse_id='$id[$i']'";
$r = mysqli_query ($dbc, $q) or trigger_error(mysqli_error($dbc));

$i = $i + 1;
$place = $place + 1;

}

}

 

I'm getting this error:

 

"array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag "

 

I'm really baffled. Any help?

Link to comment
Share on other sites

:confused:  I don't get it... if I understand correctly what you are trying to do is:

 

- Read your table EnteredHorses and depending on the value of the column "total" assign the proper "place" to the horse.... right?

 

so.. why don't do that in this way :

 

$q="SELECT horseID, total FROM enteredHorses ORDER BY total DESC";  

$r = mysqli_query ($dbc, $q) or trigger_error(mysqli_error($dbc));

$place = 1;
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

   $q = "UPDATE enteredHorses SET place = '$place' WHERE horse_id='".$row['horseID'] . "'";
   $r = mysqli_query ($dbc, $q) or trigger_error(mysqli_error($dbc));

   $place = $place + 1;

}

 

and even then, I will question why you need the column place if only with a simple select you can have the proper order, hence the horse's place.

 

Link to comment
Share on other sites

Thanks! Yes, this is what I wanted to do I just had a hard time with my loops. The reason I want to do it this way is so that each "horse" can have it's own page and show the placings he's had in recent shows.

 

But now it's giving me errors. I keep getting this error all day!

 

"line 14: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given "

<?php

require_once('includes/mysqli_connect.php');
include('includes/config.inc.php');


include ('includes/header01.inc.php');

$q="SELECT horseID, total FROM enteredHorses";  

$r = @mysqli_query ($dbc, $q); // Run the query.

$place = 1;
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

   $q = "UPDATE enteredHorses SET place = '$place' WHERE horseID = '" . $row['horseID'] . "'";
   $r = @mysqli_query ($dbc, $q) or trigger_error(mysqli_error($dbc));

   $place = $place + 1;

}


include('includes/footer01.inc.php');
?>

 

I've checked my mysqli_connect.php, it's working...I've checked spelling on database...urg.

Link to comment
Share on other sites

$q="SELECT horseID, total FROM enteredHorses";  

$r = @mysqli_query ($dbc, $q); // Run the query.

 

first: In your select (1st line) you didn't include the ORDER BY that I wrote in the code that I gave to you.

second: re-write the second line in this way:

 

$r = mysqli_query ($dbc, $q) or die(mysqli_error($dbc)); // Run the query and show the error on failure... Do not use the "@" in front...

Link to comment
Share on other sites

$r = mysqli_query ($dbc, $q) or die(mysqli_error($dbc)); // Run the query and show the error on failure... Do not use the "@" in front...

 

you have a blank space after mysql_query... delete it and try...

$r = mysqli_query($dbc, $q) or die(mysqli_error($dbc)); // Run the query and show the error on failure... Do not use the "@" in front...

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.