Displaying category heading

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Displaying category heading

Post by therat »

I have the following function that retrieves a category heading and all the sub-categories associated with the main one. This is working properly apart form not displaying the main category heading.
The function that retrieves the data is

Code: Select all

/***************************************************************************/
// Function     :   list_scats()
// Description  :   Gets a list of sub-categories to show
    function list_scats($id) {
        global $db;
        $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);
        return $result;
    }
To display it on the page I am using the following, starting by calling the function

Code: Select all

$list_scats = list_scats($id);
<table width="100%" cellpadding="3" cellspacing="1" border="0" class="tablebg">
            <tr>
                <th><?php echo list_scats($id) ?></th>
	        </tr>
	        <tr class="row1">
	            <td>
		        <?php while($row = $db->sql_fetchrow($list_scats)) {
                            echo '<a href=preview.'.$phpEx.'?id='.$row['subcat_id'].'>'.$row['subcat_name'].'</a>&nbsp;('.$row['total'].')<br />';
                } ?>
                </td>
            </tr>
        </table>
            </div>
        </td>
    </tr>
</table>
All of this is working perfectly apart form displaying the main category heading. I assume it is because this is outside of the loop as I only need this to be displayed once, where the sub-categories need to be looped. What I need is for the main category heading to be displayed once followed by however many sub-categories there are.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You are calling list_scats($id) (again) for the headings, but it just returns a query ID from the database. That's two queries. Do you have the headings in an array somewhere? Were you going to use the db field names?
(#10850)
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

arborint wrote:You are calling list_scats($id) (again) for the headings, but it just returns a query ID from the database. That's two queries. Do you have the headings in an array somewhere? Were you going to use the db field names?
Calling list_scats($id) for the headings was just me trying to get it working. There is only the one query I am using to show the heading and the sub-categories.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

But it returns a result ID, not HTML. Can you post what you want the final HTML to look like?
(#10850)
Post Reply