2010-10-09 58 views
1
<? 
    // Connect database 
    include("cat-config.php"); 

    // Get all records in all columns from table and put it in $result. 
    $result=mysql_query(" 

    (select * from stok_lukisan where `Ukuran` LIKE '20x25' AND `Kategori` LIKE '1' ORDER BY `Kode` ASC limit 3) 
    union all 
    (select * from stok_lukisan where `Ukuran` LIKE '30x40' AND `Kategori` LIKE '1' ORDER BY `Kode` ASC limit 4) 
    union all 
    (select * from stok_lukisan where `Ukuran` LIKE '20x50' AND `Kategori` LIKE '1' ORDER BY `Kode` ASC limit 5) 
    "); 

    /*Split records in $result by table rows and put them in $row. 
    Make it looping by while statement. */ 
    while($row=mysql_fetch_assoc($result)){ 

    echo '<table><tr>'; 
     echo "<td align='center'>$row[Kode]<br><a href='$GAMBAR_URL/$row[Gambar]' rel='lightbox' title='Kode Lukisan: $row[Kode]'><img src='$GAMBAR_URL/$row[Gambar]'><br> 

     Rp.$row[Harga]<a href=\"javascript:;\" onclick=\"simpleCart.add('name=$row[Kode]', 'price=$row[Harga]','size=tiny','quantity=1','thumb=$GAMBAR_URL/$row[Gambar]');\"><br><img src='./images/buy.gif'><p></a>"; 

    $rows = 1; 
    while ($row = mysql_fetch_assoc($result)) { 
     echo "<td align='center'>$row[Kode]<br><a href='$GAMBAR_URL/$row[Gambar]' rel='lightbox' title='Kode Lukisan: $row[Kode]'><img src='$GAMBAR_URL/$row[Gambar]'></a><br> 

    Rp.$row[Harga]<a href=\"javascript:;\" onclick=\"simpleCart.add('name=$row[Kode]', 'price=$row[Harga]','size=tiny','quantity=1','thumb=$GAMBAR_URL/$row[Gambar]');\"><br><img src='./images/buy.gif'><p></a>"; 

     ++$rows; 
     if ($rows %4 == 0) { 
     echo '</tr><tr></tr> 

    </table></tr><tr><table><tr>'; 
     } 
    } 
    } 
    ?> 

如何根據聯合全部設置的限制在頁面上顯示每一行php循環。PHP:顯示行數值與所有聯合限制相同

在PHP頁面的結果是這樣的:

1001 - 1002 - 1003 (<---- 3 record on rows) 
2001 - 2002 - 2003 - 2004 (<---- 4 record on rows) 
3001 - 3002 - 3003 - 3004 - 3005 (<---- 5 record on rows) 

感謝....

回答

1

你可以使用一個簡單的計數器來跟蹤行已被獲取的:

$limits = array(3, 4, 5); 
reset($limits); 
$counter = 0; 
while ($row = mysql_fetch_array($result)) { 
    var_dump($row); 
    $counter++; 
    if ($counter === current($limits)) { 
     echo current($limits); 
     next($limits); 
     $counter = 0; 
    } 
} 

這裏使用current來獲取內部數組指針指向的當前值。因此,如果電流限制等於已提取的行數,限制打印,指針是先進的(見next)和計數器復位爲0。

+0

不錯。回波電流($限制);下一個($限制);可以合併以回顯每個($限制);我認爲。 – 2010-10-09 16:51:47

+0

感謝您的答案,但我不知道如何實現我的腳本。請參閱下面我的前面的腳本。感謝秋葵。 – key 2010-10-09 17:24:59

+0

whoaaahhhh終於,我可以實現你的解決方案,現在我的腳本工作!謝謝。 – key 2010-10-09 19:31:06

0
$sql = "(select *, 3 AS limit 
    from stock 
    where `size` LIKE '25' 
    AND `category` LIKE '1' 
ORDER BY `code` ASC 
    limit 3) 
union all 
(select *, 4 AS limit 
    from stock 
    where `size` LIKE '30' 
    AND `category` LIKE '1' 
ORDER BY `code` ASC 
    limit 4) 
union all 
(select *, 5 AS limit 
    from stock 
    where `size` LIKE '45' 
    AND `category` LIKE '1' 
ORDER BY `code` ASC 
    limit 5)"; 
$res = mysql_query($sql); 
while ($r = mysql_fetch_object($res)) { 
    // assuming each record has a unique id 
    $data[$r->limit][$r->id] = $r->name; 
} 

foreach ($data as $limit => $names) { 
    echo implode(' - ',$names); 
}
+0

感謝alec,請看我以前的腳本如下: – key 2010-10-09 17:23:00

1

我想查詢可以被簡化爲也簡化了這個PHP:

SELECT 
    GROUP_CONCAT(name SEPARATOR ' - ') names, 
FROM 
    stock 
WHERE 
    category 
    AND size IN (25, 30, 45) 
GROUP BY 
    size 

這將讓你MySQL的數據格式化,你想它在PHP中。你可以打印每一行。

+0

謝謝你的答案tandu。 – key 2010-10-09 17:23:34

+0

我沒有考慮到這個限制。如果你想使用嚴格的限制,PHP將不得不做的工作。既然你有很好的分隔符,你可以用它爆炸array_splice($ input,$ limit); – 2010-10-09 17:54:09