2013-02-21 33 views
0

我如何顯示每個循環內的一半行,現在和其餘的? 對於每個循環將打印6行,所以我想打印現在的第一個3和後面的4-6。有沒有一種方法可以顯示每個循環的前半部分,後半部分?

我試過使用while循環,但它沒有工作,試圖把它放在裏面和3個重複的同一行。

$i = 1; 
while($i <= 3) { 
foreach ($items as $res) { 
    $image = $res['image_name']; 
    $id = $res['id']; 
    //echo '<img src="/e-com/images/'.$image.'" />'; ?> 

<tr> 
    <td><?php echo $res['id']; ?></td> 
    <td><?php echo $res['item_name']; ?> </td> 
    <td><?php echo "£ ".$res['price']; ?> </td> 
    <td><?php echo $res['category']; ?> </td> 
    <td><?php echo $res['date_added']; ?> </td> 
    <td><?php echo "<a href='http://localhost/e-com/index.php/product/to_edit/$id' >Edit</a>";  ?></td> 
    <td><?php echo "<a href='' onclick='dltCnfrm(\"$id\"); return false'>Delete</a>"; ?></td> 
</tr> 

    <?php $i++; 
    } } 
    ?> 
+1

以後你的意思是? – hjpotter92 2013-02-21 16:58:49

回答

0

這個怎麼樣?

$first = array(); 
$second = array(); 
foreach ($items as $i => $res) { 
    $image = $res['image_name']; 
    $id = $res['id']; 
    $html = " 
<tr> 
    <td>".$res['id']."</td> 
    <td>".$res['item_name']."</td> 
    <td>£ ".$res['price']."</td> 
    <td>".$res['category']."</td> 
    <td>".$res['date_added']."</td> 
    <td><a href='http://localhost/e-com/index.php/product/to_edit/".$id."' >Edit</a></td> 
    <td><a href='' onclick='dltCnfrm(".$id."); return false'>Delete</a></td> 
</tr>"; 
    if($i < 3) { 
     $first[] = $html; 
    } else { 
     $second[] = $html; 
    } 
} 

echo implode("", $first); 

///few lines below 

echo implode("", $second); 
+0

好吧,但我對這條線有問題​​ ethio 2013-02-21 17:32:24

+0

@AziebAssres我沒有看到你的原始代碼上的那一行,但我在猜測問題是圍繞着src值的雙引號。只需將其更改爲:​​ Deleteman 2013-02-21 17:51:57

相關問題