SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Confusion about multi-dimensional arrays

    hello,

    I'm trying to deal with a multidimensional array in a way that I never have before.

    My process is as follows:

    1. query all resources
    2. query all bookings that are relevant
    3. loop through bookings and create a multidimensional array
    4. while looping between 0 and 24 (hours of day) check if the resource_id matches any arrays in the bookings array, then change the cell bg colour to indicate that a booking spans the given hours.

    here is some code
    PHP Code:
    $query_resources mysql_query("SELECT id, title FROM resources");

    $query_bookings mysql_query("
        SELECT b.id, b.resource_id, HOUR(b.start) AS start, HOUR(b.finish) AS finish, b.status, c.username 
        FROM bookings b, clients c 
        WHERE DATE(b.start) = '
    $current_date' AND c.id = b.client_id");
        
    $bookings = array();
        
    echo 
    "<h1>Today's Bookings</h1>";

    while (
    $data mysql_fetch_array($query_bookings))
    {
        
    $id $data['id'];
        
        
    $bookings[$id] = array(
        
    'id' => $data['id'],
        
    'resource_id' => $data['resource_id'],
        
    'start' => $data['start'],
        
    'finish' => $data['finish'],
        
    'status' => $data['status'],
        
    'client_name' => $data['username']
        );

    the array (print_r) output looks like this:
    Code:
    Array ( 
    [1] => Array ( [id] => 1 [resource_id] => 1 [start] => 10 [finish] => 13 [status] => 2 [client_name] => tool )
    [2] => Array ( [id] => 2 [resource_id] => 1 [start] => 14 [finish] => 20 [status] => 1 [client_name] => rage against the machine ) 
    )
    Next, I loop through the resources (eg. different classrooms) and attempt to find out if any bookings exist. The problem is, I need to search inside an array to see if the resource_id exists, and I can't work it out....
    PHP Code:
    while ($data = mysql_fetch_array($query_resources)) 
    {
            $resource_id = $data['id'];
        $resource_name = $data['title'];

          <tr class="<?php echo $bgcolor?>">
            
            <td><?php echo $resource_name?> (<?php echo $count_bookings?>)</td>
            <?php 
            
            
    for ($i=0$i<24$i++) 
            { 
            
            if (
    array_key_exists($resource_id$bookings))
            {

                
    $start $bookings[$resource_id]['start'];
                
    $finish $bookings[$resource_id]['finish'];
                
    $client_name $bookings[$resource_id]['client_name'];
                
                if (
    $start <= $i and $finish >= $i
                {
                    
    $class 'yellow'
                    
    $cell_contents $client_name;
                } else { 
                    
    $class 'white';
                    
    $cell_contents '&nbsp;';
                }
                
            } else {
                
    $class 'white';
                
    $cell_contents '&nbsp;';
            }
            
            
            
    ?>
            <td class="<?php echo $class?>"><?php echo $cell_contents?></td>
            <?php ?>
            
          </tr>
          <?php ?>
    I think the question is, how do I select an array by a value in the array, rather than the "primary" array key?
    Last edited by wheeler; May 14, 2007 at 18:43.
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  2. #2
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just go throught the value and compare each value to the on you want to compare with.

    I don't understand why you don't use just one query:

    Code:
    SELECT b.id, b.resource_id, r.id AS resId, r.title AS resTitl, HOUR(b.start) AS start, HOUR(b.finish) AS finish, b.status, c.username
    FROM bookings b, clients c, resources r
    WHERE DATE(b.start) = '$current_date' AND c.id = b.client_id  AND r.id = b.resource_id

  3. #3
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I kept them seperate because I want to list all of the resources regardless of whether there is a booking, and I also want to list each resource only once. I know that just listing all the bookings and their associated resources would be easy, but unfortunately I have to make it this way

    I think my explanation is a bit complicated...

    how about we have this 2 dimensional array
    Code:
    Array ( 
    [1] => Array ( [id] => 1 [resource_id] => 1 [start] => 10 [finish] => 13 [status] => 2 [client_name] => tool )
    [2] => Array ( [id] => 2 [resource_id] => 1 [start] => 14 [finish] => 20 [status] => 1 [client_name] => rage against the machine ) 
    )
    I'm looping through a seperate query that gives me a check value of 1, and then 2

    what trigger can I use to find if check value 1 = any resource_id in the above array (which in this case would be both), and then gain access to the other values in the array?
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  4. #4
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    function findArray($array$key$value)
    {
        
    $result = array();
        foreach(
    $array as $subArray) {
            if(
    $subArray[$key] == $value) {
                
    $result[] = $subArray;
            }
        }
        if(
    count($result) > 0) {
            return 
    $result;
        } else {
            return 
    null;
        }
    }
    $array = array(array('id' => 1'resource_id' => 1'start' => 10'finish' => 13'status' => 2'client_name' => 'tool'),
                   array(
    'id' => 2'resource_id' => 1'start' => 10'finish' => 20'status' => 2'client_name' => 'rage against machine'));
    print_r(findArray($array'client_name''tool')); //returns the first array
    print_r(findArray($array'resource_id'1)); //will return the first array

    ?>

  5. #5
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    man, thanks! i've been trying to work this out all day! just bumped up my array skills, cheers!
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  6. #6
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is the array
    PHP Code:
    $data 
    array(
    array(
    'id' => 1'resource_id' => 1'start' => 10'finish' => 13'status' => 2'client_name' => 'tool'),
    array(
    'id' => 2'resource_id' => 1'start' => 10'finish' => 20'status' => 2'client_name' => 'rage against machine'));

    $count_bookings 2// usually a mysql query but not necessary here

    // the function
    function findArray($array$key$value)
    {
        
    $result = array();
        foreach(
    $array as $subArray) {
            if(
    $subArray[$key] == $value) {
                
    $result[] = $subArray;
            }
        }
        if(
    count($result) > 0) {
            return 
    $result;
        } else {
            return 
    null;
        }

    The current method is only showing the second array in my cells, as can be seen in the file attached "method-1.gif". They show in the right place, but the first booking does not show up at all.

    As can be seen, I need to have 25 cells per row. This is what producing the picture "method-1.gif".
    PHP Code:
          <tr <?php bgcolor(); ?>>
            
            <td class="bold"><?php echo "$resource_name ($count_bookings)"?></td>
            <?php 
            
            
    for ($i=0$i<24$i++) 
            { 
                
    // check each start time in bookings to see if it is in the current timespan
                
    for ($x=-1$x<$count_bookings$x++) 
                {
                    
    $start $data[$x]['start'];
                    
    $finish $data[$x]['finish'] - 1;
                    
    $client_name $data[$x]['client_name'];
                    
                    if (!empty(
    $client_name)) 
                    {
                        if (
    $start <= $i and $finish >= $i) {
                        echo 
    "<p class='bold'>$start <= $i and $finish >= $i (x = $x$client_name, f=$finish)</p>";
                        
    $class 'class="yellow"'
                        
    $cell_contents "$client_name [view] [<a href=\"booking.php\">edit</a>]";
                    } else { 
                        
    $class '';
                        
    $cell_contents '&nbsp;';
                        }
                    }
                } 
    // close for x < $count_bookings
            
            
    ?>
            <td <?php echo $class?>><?php echo $cell_contents?></td>
            <?php // close for looping 0-24 ?>
            
          </tr>
          <?php // close while looping through records ?>
    What I can't work out for the life of me is how to both show all 25 cells, and show all the arrays for each row, which is what "method-2.gif" should look like.

    I have this slightly different method, which seems more logical except the second for loop messes up the number of cells and breaks the layout.
    PHP Code:
         <tr <?php bgcolor(); ?>>
            
            <td class="bold"><?php echo "$resource_name ($count_bookings)"?></td>
            <?php 
            
            
    for ($i=0$i<24$i++) 
            { 
                
    // check each start time in bookings to see if it is in the current timespan
                
    for ($x=-1$x<$count_bookings$x++) 
                {
                    
    $start $data[$x]['start'];
                    
    $finish $data[$x]['finish'] - 1;
                    
    $client_name $data[$x]['client_name'];
                    
                    if (!empty(
    $client_name)) 
                    {
                        if (
    $start <= $i and $finish >= $i) {
                        
    //echo "<p class='bold'>$start <= $i and $finish >= $i (x = $x, $client_name, f=$finish)</p>";
                        
    $class 'class="yellow"'
                        
    $cell_contents "$client_name [view] [<a href=\"booking.php\">edit</a>]";
                        echo 
    "<td $class>$cell_contents</td>\r\n";
                    } else { 
                        echo 
    "<td>&nbsp;</td>\r\n";
                        }
                    }
                } 
    // close for x < $count_bookings
            
    // close for looping 0-24 
            
    ?>
          </tr>
          <?php // close while looping through records ?>
    Really struggling on this one!
    Attached Images Attached Images
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  7. #7
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i wonder can anyone see the pics I attached before they were approved?
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •