Arrays in PHP

Discussion in 'PHP' started by you-cubez, Jun 3, 2007.

  1. #1
    Hi - I have the following code:

    <?php
    
    // Connects to your Database 
    
    // Get data from first table
    $query1 = mysql_query("SELECT * FROM Customers WHERE Datem = CURDATE()") OR die(mysql_error());
    
    // Get data from second table
    $query2 = mysql_query("SELECT Cubez.*, Customers.Name FROM Cubez, Customers WHERE Cubez.Date1 = CURDATE() AND Customers.Email = Cubez.Email GROUP BY Customers.Name") OR die(mysql_error());
    
    // Get data from third table
    $query3 = mysql_query("SELECT Topup.*, Customers.Name FROM Topup, Customers WHERE Topup.Datem = CURDATE() AND Customers.Email = Topup.Email GROUP BY Customers.Name") OR die(mysql_error());
    $data = array();
    
    
    // Loop through first table
    
    while ($user = mysql_fetch_assoc($query1))
    {
        $data[$user['Time']] = '<img src=images/newmem.png><a class=W8NB> <u>'. $user['Name'] .'</u> Becomes a Member <a class=G7>['. $user['Time'] .']</a>';
    }
    
    // Loop through second table
    
    while ($purchase = mysql_fetch_assoc($query2))
    {
        $data[$purchase['Time']] =  '<img src=images/newcube.png><a class=W8NB> <u>'. $purchase['Name'] .'</u> Purchases a Cube (ID #'. $purchase['CubeID'] .') <a class=G7>['. $purchase['Time'] .']</a>';
    }
    
    // Loop through third table
    
    while ($top = mysql_fetch_assoc($query3))
    {
        $data[$top['Time']] = '<img src=images/newtop.png><a class=W8NB> <u>'. $top['Name'] .'</u> Tops Up Cube ID #'. $top['CubeID'] .' £'. $top['Value'] .'.00 <a class=G7>['. $top['Time'] .']</a>';
    }
    
    
    // Sort array keys in reverse order
    
    
    krsort($data);     
    foreach ($data AS $time => $action)
    {    
    	echo "{$action} <br />\n";
    }
    
    
    ?>
    PHP:
    This shows every piece of information stored in the array - How do I limit it to show just the last 5 pieces of information stored???
     
    you-cubez, Jun 3, 2007 IP
  2. Cesay

    Cesay Peon

    Messages:
    121
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think array_slice will do the trick.

    us.php.net/manual/en/function.array-slice.php
     
    Cesay, Jun 3, 2007 IP
  3. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Cesay, that would be a waste of processing - getting all the records from DB and only using 5.

    Instead, to the SQL queries, add an "ORDER BY date DESC" or "ORDER BY primary key DESC" if it is an incrementing one and then "LIMIT 5" at the end.
     
    krt, Jun 3, 2007 IP
  4. you-cubez

    you-cubez Active Member

    Messages:
    842
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    60
    #4
    All sorted! Cesay - That was the right way as the array stores information from 3 Different Tables! All sorted
     
    you-cubez, Jun 4, 2007 IP