Page 1 of 1

Array Sorting

Posted: Mon Mar 27, 2006 3:58 pm
by kurupt4
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello all, 

This is for a webpoll application.
And my ISP is NOT running 5.0 and has no intentions of installing it in the near future.

In the database I have 2 fields, 1 that holds the choices (Duke, Memphis, UCLA and so forth separated by commas) and 
the other holds the values (0,1,45, and so forth also separated by commas.

below is my code:

Code: Select all

<?php
include ('../connection.php');
$theQuery = "SELECT * FROM pswebpoll WHERE pollid = 24";
$theResults = mysql_query($theQuery);
while ($theRow = mysql_fetch_row($theResults))
{
	$pollid = $theRow[0];
	$question = $theRow[1];
	$choices  = $theRow[2];
	$totalchoices  = $theRow[3];
	$votetally  = $theRow[4];
	$startdate = $theRow[5];
	$enddate = $theRow[6];
}

// split the choices

$newchoices = split(",", $choices, -1);
$newtally = split(",", $votetally, -1);

//put them into yet another array with the value first and then the choice

for ($count = 0; $count < sizeof($newtally); $count++)
{
	$thenewchoices[] = $newtally[$count] . "," .  $newchoices[$count];
}

// this gives me 12,Duke; 34,Memphis; 123,UCLA and so forth

krsort($thenewchoices); //I am not sure if this is the right one, what i want to do is sort them in descending order so it would give me
123,UCLA; 34,Memphis; 12,Duke and so forth

?>
My question is this: will this work and how can I loop thru the newly sorted array to display it?

I am a rather newbie to PHP so any and all advice will be much appreciated and welcomed.

Thank you

Leo Zayas
http://www.PoliceSoftball.com


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Mar 27, 2006 4:25 pm
by s.dot
my guess would be something like

Code: Select all

$new_array = array_reverse(asort($old_array,SORT_NUMERIC));

Re: Array Sorting

Posted: Tue Mar 28, 2006 12:25 am
by jmut
kurupt4 wrote: .....
And my ISP is NOT running 5.0 and has no intentions of installing it in the near future.
.......
You could have a look at this package. It provides some function in php4 that are originaly written for php5 only.
http://pear.php.net/package/PHP_Compat/docs

Posted: Tue Mar 28, 2006 12:29 am
by feyd
Piece of advice, never use split() if you, kurupt4, aren't going to use regex. Even if you are, I'll suggest preg_split() over it anyday. In the posted code's case, explode() would work perfectly.