开发者

how to echo a hr on each 4

  'products' => 
    array
      0 => 
        array
          'pid' => string '3' (length=1)
          'name' => string '#122232' (length=7)
          'information' => string 'DSDSD' (length=5)
          'image_tumb' => string '1tumbc4ad490017dc211002cfef359204ca7e154467_VisualVoicemail_Android_original.jpg' (length=80)
          'tag' => string 'Sepatu' (length=6)
          'price' => string '12000000.00' (length=11开发者_运维百科)
      1 => 
        array
          'pid' => string '4' (length=1)
          'name' => string 'Jam Bagus' (length=9)
          'information' => string 'DSDSD' (length=5)
          'image_tumb' => string '1tumbc4ad490017dc211002cfef359204ca7e_48447427_diaspora_dandy_logo.jpg' (length=70)
          'tag' => string 'Jam' (length=3)
          'price' => string '12000.00' (length=8)
      2 => 
        array
          'pid' => string '6' (length=1)
          'name' => string '#122232' (length=7)
          'information' => string 'awdwad' (length=6)
          'image_tumb' => string '1tumbc4ad490017dc211002cfef359204ca7e10408_710.jpg' (length=50)
          'tag' => string 'Sepatu' (length=6)
          'price' => string '140000.00' (length=9)

unlimited.

how do i display them like

name
name
name
name
<hr/>
name
name
name
name
<hr/>

or insert a hr after 4 times fetching ( currently useing PDO fecthall and foreach ) ?

         // displays name loops like "namenamenamenamenamenametounlimiteddata"
        foreach ($db['products'] as $product) {

            echo "<div id='product'>";
            echo $db[product][name];
            echo "</div>";
        }

but still no clue of doing the above. my friends say to use % sign in php? is that the best way ? if it is please give me an example how do it the right way, if there is a better way please describe it.

Thanks for looking in. Adam Ramadhan


    $i = 1;
    foreach ($db['products'] as $product) {
        echo "<div id='product'>";
        echo $db[product][name];
        echo "</div>";
        if ($i % 4 == 0)
              echo "<hr>";
        $i++;
    }


You can do:

$i = 0;
foreach ($db['products'] as $product) {
        if($i && $i%4 == 0) {
                echo "<hr />";
        }
        // your existing echo here        

        $i ++;
}

See it


Your friend is right you need to use the % operator called as modulus operator which gives the reminder after division.

$i = 0;
foreach ($db['products'] as $product) {
        if($i%4 == 0)
                echo "<hr>";

        echo "<div id='product'>";
        echo $db[product][name];
        echo "</div>";     

        $i++;
}


Use a counter variable $i and increment it each time and check if

$i%4 == 0

then echo hr


From your example, I think you meant the 5th one. Try something along the line of:

for($i = 0; $i < 100; $i++) {
    if($i && $i%5 == 0) {
        // the fifth
    }
}


ops it's suppose to be 1

$a=1;
foreach ($db['products'] as $product) {

            echo "<div id='product'>";
            echo $db[product][name];
            echo "</div>";
if($a%4==0){
echo "<hr/>";
}
$a++;
        }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜