views:

40

answers:

3
<?
    // 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>';
      }
    }
    }
    ?>

how to display on page with every single rows of php loops in accordance with a limit that has been set in the union all.

on php page result like this:

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

thanks....

+1  A: 

You could use a simple counter to keep track of rows that have been fetched:

$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;
    }
}

Here current is used to get the current value the internal array pointer is pointing to. So if the current limit is equal to the number of rows that have been fetched, the limit is printed, the pointer is advanced (see next) and the counter is reset to 0.

Gumbo
Nice. echo current($limits); next($limits); can be consolidated to echo each($limits); I think.
tandu
thanks for the answer, but i dont know how to implement to my script. please see my previous script below. thanks gumbo.
key
whoaaahhhh finally, i can implement your solutions, now my scripts worked! thanks.
key
ok, done... thanks.
key
A: 
$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);
}
Alec
thanks alec, please see my previous script below:
key
+1  A: 

I think the query can be simplifed to also simplify this for PHP:

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

This will get you the MySQL data formatted like you want it in PHP. You can just print each row.

tandu
thanks for the answer tandu.
key
This doesn't take into account the limits, I realize. If you want to use the strict limits, php will have to do the work. Since you have the nice separator, you can just explode on it take array_splice($input, $limit);
tandu