2012-01-20 60 views
1

每個頁面都列出了特定零售商可用的所有優惠券。我在數據庫中查詢標題中的所有優惠券代碼,因爲我計算返回的行數,並在頁面元標題中使用該信息。我現在也想顯示陣列中前2個優惠券的標題。我將如何去解壓數組中的前2個結果而不再查詢數據庫?如何從mysql數組中檢索前2個結果而不再查詢?

這是我到目前爲止有:

$retailer_coupons = "select C.couponid,C.fmtc_couponid,C.merchantid,C.exclusive,C.label,C.shoppingtip,C.restrictions,C.coupon,C.custom_order,C.link,C.image,C.expire,C.unknown,M.name,M.approved,M.homepageurl,M.category from tblCoupons C,tblMerchants M where C.merchantid=M.merchantid and C.begin < ".mktime()." and C.expire > ".mktime()." and C.merchantid=".$merchantid." and M.display='1' and C.user_submitted='' order by C.custom_order desc, C.coupon desc"; 
$retailer_coupons_result = mysql_query($retailer_coupons) or die(mysql_error()); 
$count_coupons=mysql_num_rows($retailer_coupons_result); 
$meta_title = ''.$name.' Coupon Codes ('.$count_coupons.' coupons available)'; 
+1

['或死亡()'必須死](http://www.phpfreaks.com/blog/or-die-must-die)。另外,你是什麼意思*再次查詢數據庫*?你還沒有取得任何結果。 – Phil

回答

1

假設我在我的表有3條記錄。如果我執行下面的查詢,我會得到2結果不過count(*)會給我3作爲輸出

SELECT count(*) FROM temp.maxID limit 2

你的情況,這將是

$retailer_coupons = 
    "select C.couponid,C.fmtc_couponid,C.merchantid,C.exclusive,C.label,C.shoppingtip,C.restrictions,C.coupon,C.custom_order,C.link,C.image,C.expire,C.unknown,M.name,M.approved,M.homepageurl,M.category 
    from tblCoupons C,tblMerchants M 
    where C.merchantid=M.merchantid 
    and C.begin < ".mktime()." and C.expire > ".mktime()." 
    and C.merchantid=".$merchantid." and M.display='1' 
    and C.user_submitted='' 
    order by C.custom_order desc, C.coupon desc 
    limit 2"; 

limit 2會做的魔力......乾杯!!!

祝你好運!!!

+0

我試過你的代碼,但它不工作。我認爲你爲我寫的代碼中缺少count(*)?我如何輸出總數作爲一個PHP變量?謝謝!! – PaperChase

+0

'limit 2'只會顯示前兩個記錄...如果要計算總行數,count(*)將會執行此操作... 您能否更新您的問題並顯示要輸入的內容什麼是輸出? –

0

像這樣:

$res = mysql_fetch_assoc($retailer_coupons_result); 
$i = 0; 
while ($i < 2){ 
    echo $res[$i]['label']."\n"; 
    $i++; 
} 
相關問題