Getting Data from SQL, Putting it in Array and Sorting it

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
carleihar
Forum Commoner
Posts: 36
Joined: Fri Nov 13, 2009 5:59 pm

Getting Data from SQL, Putting it in Array and Sorting it

Post by carleihar »

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. Perhaps it's a syntax error? Maybe you could shed some light?

Code: Select all

$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;

}

}

Last edited by carleihar on Sun Aug 15, 2010 6:03 pm, edited 1 time in total.
mattbranch12
Forum Newbie
Posts: 18
Joined: Thu Jul 09, 2009 3:14 pm
Location: Hagerstown, MD

Re: Getting Data from SQL, Putting it in Array and Sorting i

Post by mattbranch12 »

What is while loop not doing or is there an error.
liamallan
Forum Newbie
Posts: 19
Joined: Sat Aug 14, 2010 6:25 pm

Re: Getting Data from SQL, Putting it in Array and Sorting i

Post by liamallan »

how about:

Code: Select all

$q="SELECT COUNT(*), horseID FROM enteredHorses ORDER BY horseID";
carleihar
Forum Commoner
Posts: 36
Joined: Fri Nov 13, 2009 5:59 pm

Re: Getting Data from SQL, Putting it in Array and Sorting i

Post by carleihar »

I could sort by the total from the sql database, but that still leaves me with the problem how to store that place's number in a table.
liamallan
Forum Newbie
Posts: 19
Joined: Sat Aug 14, 2010 6:25 pm

Re: Getting Data from SQL, Putting it in Array and Sorting i

Post by liamallan »

<table width="90%" border="1" align="center" cellpadding="3" cellspacing="0">
<tr>
<td ><strong>Horse ID</strong></td>
<td><strong>Horse Name</strong></td>
</tr>

<?php
while($rows=mysqli_fetch_array($r)){ // Start looping table row
?>
<tr>
<td><? echo $rows['horseID']; ?></td>
<td><? echo $rows['horsename']; ?></td>
</tr>

<?php
// Exit looping
}
?>
carleihar
Forum Commoner
Posts: 36
Joined: Fri Nov 13, 2009 5:59 pm

Re: Getting Data from SQL, Putting it in Array and Sorting i

Post by carleihar »

That's not really what I'm asking to do...

I want to put the id and its total into a two dimensional array and then sort the array. Then I want to store the "place" in the array (1st, 2nd, 3rd) and then store that into a table.
Post Reply