![](/img/trans.png)
[英]PHP - Adding a div tag after a certain amount of results in a foreach loop
[英]Is it possible to fetch data from other tables inside a foreach loop after a certain amount of results in PHP?
假設您的數據如下所示:
$table1 = [
'Row 1 from table_1',
'Row 2 from table_1',
'Row 3 from table_1',
'Row 4 from table_1',
'Row 5 from table_1',
'Row 6 from table_1',
'Row 7 from table_1',
'Row 8 from table_1',
'Row 9 from table_1',
'Row 10 from table_1',
];
$table2 = [
'Row 1 from table_2',
'Row 2 from table_2'
];
$table3 = [
'Row 1 from table_3',
'Row 2 from table_3'
];
解決此問題的一種方法是執行循環並參考索引號來決定是否 append 基於模運算符(即除法時的余數)來自其他表之一的數組元素。
編寫此類邏輯的一種方法如下:
$out = [];
foreach($table1 as $k=>$v)
{
$out[] = $v;
if( $k % 4 === 1 && count($table2) > 0)
{
$out[] = array_shift($table2);
}
if( $k % 4 === 3 && count($table3) > 0)
{
$out[] = array_shift($table3);
}
}
print_r($out);
對我來說,上面給了我一個 output,我知道這是你想要的 output:
Array
(
[0] => Row 1 from table_1
[1] => Row 2 from table_1
[2] => Row 1 from table_2
[3] => Row 3 from table_1
[4] => Row 4 from table_1
[5] => Row 1 from table_3
[6] => Row 5 from table_1
[7] => Row 6 from table_1
[8] => Row 2 from table_2
[9] => Row 7 from table_1
[10] => Row 8 from table_1
[11] => Row 2 from table_3
[12] => Row 9 from table_1
[13] => Row 10 from table_1
)
旁注:array_shift 將破壞 table2 和 table3 的數組。 如果這是不希望的副作用,可能會嘗試稍微修改代碼,例如先克隆數組。
或者,您也可以在 mysql 級別解決此問題。 基本上,您可以通過聯合或聯接將表格連接在一起。 然后,您需要一種以特殊方式對其進行排序的方法。 這樣做的一個好方法是動態引入一個新列,它表示基於主鍵的某種計算。
假設您的表格如下所示:
CREATE TABLE `table1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
INSERT INTO `table1` VALUES (1,'Row 1 from table_1'),(2,'Row 2 from table_1'),
(3,'Row 3 from table_1'),(4,'Row 4 from table_1'),
(5,'Row 5 from table_1'),(6,'Row 6 from table_1'),
(7,'Row 7 from table_1'),(8,'Row 8 from table_1'),
(9,'Row 9 from table_1'),(10,'Row 10 from table_1');
CREATE TABLE `table2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `table2` VALUES (1,'Row 1 from table_2'),(2,'Row 2 from table_2');
CREATE TABLE `table3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `table3` VALUES (1,'Row 1 from table_3'),(2,'Row 2 from table_3');
您可以使用如下查詢:
SELECT data FROM (SELECT (id + floor((id - 1) / 2)) AS order_id, data FROM table1
UNION
SELECT ( (6 * (id-1)) +3) AS order_id, data FROM table2
UNION
SELECT ( (6 * (id-1)) +6) AS order_id, data FROM table3) t4
ORDER BY order_id;
這里的邏輯基本上是由於表具有相同的結構,我們可以將它們的結果聯合在一起。 然后基本上我們希望表一具有這樣的排序索引:
1,2, 4,5, 7,8, 10,11, 13,14。
通過這樣做,我們為每個第三個索引留出了空間來指向其他東西。
至於差距,我們希望 3、6、9 和 12 指向其他兩張桌子。 3 和 9 應指向表 2。 6 和 12 應指向表 3。 然后我們只是根據主鍵和表做一些數學運算,最后得到一個唯一的 order_id。 然后只需按 order_id 排序。
結果:
Row 1 from table_1
Row 2 from table_1
Row 1 from table_2
Row 3 from table_1
Row 4 from table_1
Row 1 from table_3
Row 5 from table_1
Row 6 from table_1
Row 2 from table_2
Row 7 from table_1
Row 8 from table_1
Row 2 from table_3
Row 9 from table_1
Row 10 from table_1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.