-3

I am getting two different arrays arrays of data from MSQL database, first array from my blog post table containing 15 rows/items, and the second array from advert table containing 3 rows/item. i want to foreach the first 5 set of data from blog post table(the first array), then foreach 1 (one) from advert table (second array) and continue from where i stopped in the first array with another 5 item and one from the first array, just like that. Please help me come up with PHP code on how to do that. thank you.

4

2 に答える 2

0

モジュロを使用します。

でカウントしてmssql_num_rowsから、カウンターを繰り返します。$i

次に foreach($rows)....:

$i = 0; $count = mssql_num_rows($result);
foreach($rows as $blog) {
    if($i > 0 && $i++ % 5 == 0) {
        $advert = array_shift($adverts_array);
        // process $advert
    }
    // process $blog here
}
于 2012-09-02T11:15:34.947 に答える
0

このコードは、配列が から始まるインデックス付けされていることを前提としています0

$i = 0;
foreach ($posts as $post) {
  // Output and/or otherwise process $post
  if (++$i % 5 == 0) {
    $advertisements[$i / 5 - 1] // Output and/or process advertisement
  }
}
于 2012-09-02T11:19:26.863 に答える