开发者

Getting number of results by category

My code is below. Basically, it's for a "deals" site (similar to groupon). This page shows all active deals in a city, and sorts them by category. For example, it will show: "Dining" (then all deals under Dining), then "Adventure" (and all deals under adventure). Etc. What I want to do is shorten it up a bunch. I want to only show a max of 3 deals for each, but then a link that says: "View (x) more in this category". For example:

Dining 

<max of 3 deals>
   (if deals > 3) "View 9 more deals in Dining"

Adventure

<max of 3 dals
   (if deals > 3) "View 4 more deals in Adventure"

etc. I know I can separate it and have a bunch of SQL statements (get all deals in dining, show 3. get all deals in adventure. show 3, etc. I'm just wondering if it's possible to do it another way).

$deals = mysql_query("SELECT $db[ddd_companies].name as company_name, $db[ddd_companies].slug as company_slug, $db[ddd_deals].slug as slug, $db[ddd_deals].id, $db[ddd_deals].company_id, $db[ddd_deals].end_time, $db[ddd_deals].name, $db[ddd_deals].photo_alldeals, $db[ddd_categories].name as category
                  FROM $db[ddd_deals]
                  JOIN $db[ddd_cities_deals] as cd ON $db[ddd_deals].id=cd.deal_id
                  join $db[ddd_categories] on $db[ddd_categories].id=$db[ddd_deals].category_id 
                  join $db[ddd_companies] on $db[ddd_companies].id=$db[ddd_deals].company_id

                  WHERE cd.city_id='$_SESSION[city_id]' AND $db[ddd_deals].start_time<='$now' AND $db[ddd_deals].end_time>'$now' AND $db[ddd_deals].deal_status_id='2' ORDER BY $db[ddd_categories].name, $db[ddd_deals].end_time");

$section = '';
while($d = mysql_fetch_array($deals)) {
    if($section != $d['category']) {
        // Display Section title when there is a new one
        ?>
        <div style="clear: both;"&g开发者_Go百科t;&nbsp;</div>
        <h2><?php echo $d['category'];?></h2>
        <?php
    }
    // Show deal information
   $section = $d['category'];
}

Edited To Add: Limit would allow me to limit the # of results to 3, but I'd also need to pull up how many are in that category.


Try specifying a limit in your query.

After your where clause, try LIMIT 3;

Look here for more: http://www.1keydata.com/sql/sql-limit.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜