PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old October 9th, 2002, 02:03 PM
adrian adrian is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 20 adrian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
php sorting help

For all the MySql quaries i've made using php i've been able to just order the data to be displayed using ORDER BY whatever, but now i need to put a list in order where a football team with the most points appears at the top of the list and the team with the lowest points at the bottom. I've read about rsort in the php manual but none of the examples seem to be related to what i need to do. If someone could help me out by telling me what code i need and where i need to put it that would be great. Below is my code and any help would be greatly appreciated. Sorry if this question seems a bit dumb but i'm still just a newbie at this.

$table_name = "$age";

$connection = @mysql_connect("$host", "$user", "$pass")
or die("Couldn't connect.");

$db = @mysql_select_db($db_name, $connection)
or die("Couldn't select database.");


$sql = "SELECT team, played, won, draw, lost, gf, ga, points, gd
FROM $table_name
ORDER BY points
";

$result = @mysql_query($sql,$connection)
or die("Couldn't execute query.");

$num=mysql_numrows($result);
$i=0;

while ($i < $num) {

$team=mysql_result($result,$i,"team");
$played=mysql_result($result,$i,"played");
$won=mysql_result($result,$i,"won");
$draw=mysql_result($result,$i,"draw");
$lost=mysql_result($result,$i,"lost");
$gf=mysql_result($result,$i,"gf");
$ga=mysql_result($result,$i,"ga");
$points=mysql_result($result,$i,"points");
$gd=mysql_result($result,$i,"gd");
$bgcolor = ($bgcolor == "#008000" ? "#06A806": "#008000");

?>
<tr <? echo "bgcolor=$bgcolor" ?>>
<td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo "$team"; ?></font></td>
<td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo "$played"; ?></font></td>
<td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo "$won"; ?></font></td>
<td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo "$draw"; ?></font></td>
<td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo "$lost"; ?></font></td>
<td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo "&nbsp;$gf &nbsp;"; ?></font></td>
<td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo "$ga"; ?></font></td>
<td align="center"><font color="#FFFFFF" size="1" face="verdana"><? echo "$points"; ?></font></td>
</tr>
<?


++$i;
}
MYSQL_CLOSE();

?>

Reply With Quote
  #2  
Old October 9th, 2002, 06:59 PM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
That is almost right, you just forgot DESC at the end of the order, to get the highest to lowest, right now its lowest to highest

plus ive rewriten your code as there is a more efficient way to do this

PHP Code:
 $table_name "$age"

$dbconn = @mysql_connect("$host""$user""$pass"
or die(
"Couldn't connect to database server."); 

$db = @mysql_select_db($db_name$dbconn)
or die(
"Couldn't select database."); 


$query mysql_query("SELECT team, played, won, draw, lost, gf, ga, points, gd
FROM $table_name ORDER BY points DESC"
); 

while(
$row mysql_fetch_array($query))
{

$bgcolor = ($bgcolor == "#008000" "#06A806""#008000");

?>
<tr <? echo "bgcolor=$bgcolor" ?>

<td align="center">
<font color="#FFFFFF" size="1" face="verdana">
<? echo $row["team"]; ?>
</font>
</td>

<td align="center">
<font color="#FFFFFF" size="1" face="verdana">
<? echo $row["played"]; ?>
</font>
</td> 

<td align="center">
<font color="#FFFFFF" size="1" face="verdana">
<? echo $row["won"]; ?>
</font>
</td>

<td align="center">
<font color="#FFFFFF" size="1" face="verdana">
<? echo $row["draw"]; ?>
</font>
</td>

<td align="center">
<font color="#FFFFFF" size="1" face="verdana">
<? echo $row["lost"]; ?>
</font>
</td> 

<td align="center">
<font color="#FFFFFF" size="1" face="verdana">
<? echo $row["gf"]; ?>
</font>
</td>

<td align="center">
<font color="#FFFFFF" size="1" face="verdana">
<? echo $row["ga"]; ?>
</font>
</td> 

<td align="center">
<font color="#FFFFFF" size="1" face="verdana">
<? echo $row["points"]; ?>
</font>
</td>

</tr>
<?


}
MYSQL_CLOSE();

?> 


Just to let you know what ive done, ive added DESC to your sql query.

removed

$num=mysql_numrows($result);
$i=0;

while ($i < $num) {

$team=mysql_result($result,$i,"team");
$played=mysql_result($result,$i,"played");
$won=mysql_result($result,$i,"won");
$draw=mysql_result($result,$i,"draw");
$lost=mysql_result($result,$i,"lost");
$gf=mysql_result($result,$i,"gf");
$ga=mysql_result($result,$i,"ga");
$points=mysql_result($result,$i,"points");
$gd=mysql_result($result,$i,"gd");
$bgcolor = ($bgcolor == "#008000" ? "#06A806": "#008000");


if you want it to return more then one row (multidimention array)

then simply use

while ($row = mysql_fetch_array($query))

its much more efficient,

mysql_result places a big load on the server is your calling it that many times, its much more efficient to use $row

if your echo'ing a var you dont need to place it in "$var". you can just echo it like this

echo $var;

finally,

<? echo $row["gf"]; ?>

because $row is now an array, you can call it like this

Reply With Quote
  #3  
Old October 10th, 2002, 01:04 AM
adrian adrian is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 20 adrian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks alot Ben for your help. It's great when someone takes the time to not only reply to a plea for help but also shows better ways to acheive the same results. It really does make the learning process easier.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > php sorting help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway