【问题标题】:PHP: Display rows value same with all union limitPHP:显示行值与所有联合限制相同
【发布时间】:2010-10-09 16:40:04
【问题描述】:
<?
    // 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>';
      }
    }
    }
    ?>

如何根据在 union all 中设置的限制在页面上显示每一行 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)

谢谢....

【问题讨论】:

    标签: php mysql rows union-all


    【解决方案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。

    【讨论】:

    • 不错。回声电流($limits);下一个($限制);可以合并以回显每个($limits);我认为。
    • 感谢您的回答,但我不知道如何实现我的脚本。请在下面查看我以前的脚本。谢谢秋葵。
    • whoaaahhhh 最后,我可以实施您的解决方案,现在我的脚本工作了!谢谢。
    【解决方案2】:

    我认为可以简化查询以简化 PHP:

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

    这将使您的 MySQL 数据按照您想要的 PHP 格式进行格式化。您可以只打印每一行。

    【讨论】:

    • 这并没有考虑到限制,我意识到。如果你想使用严格的限制,php 将不得不做这项工作。既然你有很好的分隔符,你可以在它上面爆炸 array_splice($input, $limit);
    【解决方案3】:
    $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);
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-17
      • 2021-06-03
      • 2014-12-08
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多