Page 1 of 1

Displaying category heading

Posted: Sun Apr 02, 2006 9:59 am
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.

Posted: Sun Apr 02, 2006 12:19 pm
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?

Posted: Sun Apr 02, 2006 3:22 pm
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.

Posted: Sun Apr 02, 2006 7:32 pm
by Christopher
But it returns a result ID, not HTML. Can you post what you want the final HTML to look like?