Arrays in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • youcubez
    New Member
    • Nov 2008
    • 3

    Arrays in PHP

    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.

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

    Thanks a lot!

    Regards,
    Last edited by Markus; Nov 1 '08, 02:42 PM. Reason: added # tags
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

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

    Comment

    • youcubez
      New Member
      • Nov 2008
      • 3

      #3
      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,

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        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 ^

        Comment

        • youcubez
          New Member
          • Nov 2008
          • 3

          #5
          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("SE LECT * FROM XXXX WHERE Date = $today") OR die(mysql_error ());

          while ($user = mysql_fetch_ass oc($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,

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            You could simply add an ORDER BY clause to the query.
            Like:
            [code=mysql]
            SELECT * FROM XXXX WHERE Date = $today ORDER BY Time DESC
            [/code]
            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)

            Comment

            Working...