Connecting Tech Pros Worldwide Forums | Help | Site Map

Arrays in PHP

Newbie
 
Join Date: Nov 2008
Posts: 3
#1: Nov 1 '08
Hi there,

I currently have a script that queries 2 tables in SQL, pulls the data and then using an array, sorts the data by date and displays the content in the correct order (Latest action 1st).

This is working fine... but... I want to know if there is a better way to do this (improve performance/use less memory/increase speed) as this is called a LOT being as it's used on the home page.

Expand|Select|Wrap|Line Numbers
  1. krsort($data);
  2. foreach ($data AS $time => $action)
  3. {            
  4.     echo " {$action} <br />\n";
  5. }

Thanks a lot!

Regards,

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,758
#2: Nov 1 '08

re: Arrays in PHP


Hi.

You would probably be best of sorting the data in your SQL queries.
Could we see them?
Newbie
 
Join Date: Nov 2008
Posts: 3
#3: Nov 1 '08

re: Arrays in PHP


Quote:

Originally Posted by Atli

Hi.

You would probably be best of sorting the data in your SQL queries.
Could we see them?

Hi there,

I'd rather not paste our code here... but we are looking for help in other areas of PHP to increase the overall performance of our site... so if you fancy helping out (can pay a little) then feel free to PM your msn, aim, sykpe or gtalk username and I will add you!

Regards,
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,953
#4: Nov 1 '08

re: Arrays in PHP


Quote:

Originally Posted by youcubez

Hi there,

I'd rather not paste our code here... but we are looking for help in other areas of PHP to increase the overall performance of our site... so if you fancy helping out (can pay a little) then feel free to PM your msn, aim, sykpe or gtalk username and I will add you!

Regards,

There is a jobs forum up there ^
Newbie
 
Join Date: Nov 2008
Posts: 3
#5: Nov 2 '08

re: Arrays in PHP


Thank you.

Ok... so perhaps before we post a job offer... we'll try to elaborate here.

So we have the following:

[PHP]
$feed = mysql_query("SELECT * FROM XXXX WHERE Date = $today") OR die(mysql_error());

while ($user = mysql_fetch_assoc($feed))
{
if($user['Action'] == 'NewMember')
{
$data[$user['Time']] = .....
}

if($user['Action'] == 'NewCube')
{
$data[$user['Time']] = ...
}
More.... if statements here....
}

krsort($data);
foreach ($data AS $time => $action)
{
echo " {$action} <br />\n";
}
[/PHP]

So we check for all new actions today and then sort via Date/Time (latest on top).

We are therefore looking to find out if this is the best way to go about doing this or if there is a better way...

Thank you

Regards,
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,758
#6: Nov 2 '08

re: Arrays in PHP


You could simply add an ORDER BY clause to the query.
Like:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM XXXX WHERE Date = $today ORDER BY Time DESC
  2.  
That should return the results sorted by the "Time" field, highest value first.
If you want it sorted the other way around, change the DESC to ASC. (or simply remove it. ASC is the default order)

This should be far more efficient that the PHP array sort. (Not that I've actually tested it)
Reply