SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast therat's Avatar
    Join Date
    May 2004
    Location
    London
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Display info from an array

    I have the following function that creates an array of info from a DB
    PHP Code:
    function get_scats($id) {
    global 
    $db;
       
    $products false;

            
    $sql "SELECT COUNT(skinz_images.subcat_id) AS total
                    , skinz_subcats.subcat_id
                    , skinz_subcats.subcat_name
                    , skinz_cats.cat_name
            FROM 
                    skinz_subcats
            LEFT JOIN
                    skinz_cats
            ON
                    skinz_cats.cat_id = skinz_subcats.cat_id
            LEFT JOIN
                    skinz_images
            ON
                    skinz_images.subcat_id = skinz_subcats.subcat_id
            WHERE
                    skinz_subcats.cat_id = '"
    .$id."'
            AND
                    skinz_subcats.status = '1'
            AND
                    skinz_images.subs_status = '1'
            GROUP BY
                     skinz_subcats.subcat_id
                     , skinz_subcats.subcat_name"
    ;

       
    $result $db->sql_query($sql);

       while (
    $row mysql_fetch_array($result))
       {
           
    $sub_cats[$row['cat_name']][] = array(
                                                    
    'subcat_name'   =>  $row['subcat_name'],
                                                    
    'subcat_id'     =>  $row['subcat_id'],
                                                    
    'total'         =>  $row['total']);
       }

       return 
    $sub_cats;

    This produces the following array
    Code:
    Array
    (
        [Wallpapers] => Array
            (
                [0] => Array
                    (
                        [subcat_name] => People - Female
                        [subcat_id] => 1
                        [total] => 19
                    )
    
                [1] => Array
                    (
                        [subcat_name] => People - Male
                        [subcat_id] => 2
                        [total] => 2
                    )
    
            )
    
    )
    What I want to do is display the name of the array, in this case Wallpapers, followed by the rest of the info in this table. Where it says NAME OF ARRAY needs to be Wallpapers and the rest looped underneath.
    PHP Code:
    $sub_cats = get_scats($id);
    <table width="100%" cellpadding="3" cellspacing="1" border="0" class="tablebg">
                <tr>
                    <th>NAME OF ARRAY</th>
                </tr>
                <tr class="row1">
                    <td>
    <?php for ($i 0$i count($sub_cats); $i++) { 
            echo 
    '<a href="preview.'.$phpEx.'?id='.$sub_cats[$i]['subcat_id'].'">'.$sub_cats[$i]['subcat_name'].'</a>&nbsp;('.$sub_cats[$i]['total'].')<br />';
            }
    ?>
                    </td>
                </tr>
            </table>
                </div>
            </td>
        </tr>
    </table>
    Is this the correct way of doing this, or is there a more efficient method.

  2. #2
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should work if I understand the array correctly - not sure its the most efficient way of doing it as it is trying to loop through an array that will always only have 1 value.

    PHP Code:
    $sub_cats = get_scats($id);
    foreach($sub_cats as $sub_cat_name => $sub_cat)
    {
    ?>
    <table width="100%" cellpadding="3" cellspacing="1" border="0" class="tablebg">
                <tr>
                    <th>$sub_cat_name</th>
                </tr>
                <tr class="row1">
                    <td>
    <?php foreach ($sub_cat as $value) {
            echo 
    '<a href="preview.'.$phpEx.'?id='.$value['subcat_id'].'">'.$value['subcat_name'].'</a>&nbsp;('.$value['total'].')<br />';
            }
    ?>
                    </td>
                </tr>
            </table>
                </div>
            </td>
        </tr>
    </table>
    }


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
  •