ผมต้องการวนลูปArrayชุดนี้โดยการเอาข้อมูลที่queryได้มาวนลูปสร้างเลเอ้าตัวนี้ไปทำไลน์OA รบกวนใครมีึความรู้ช่วยหน่อยครับ ไม่เข้าใจArrayจริงๆ
ผมต้องการวนลูปArrayชุดนี้โดยการเอาข้อมูลที่queryได้มาวนลูปสร้างเลเอ้าตัวนี้ไปทำไลน์OA รบกวนใครมีึความรู้ช่วยหน่อยครับ ไม่เข้าใจArrayจริงๆ
Code (PHP)
$sql = "select * from product order by product_id Asc";
$result = $conn->query($sql);
$menu = [
"type"=> "flex",
"altText"=> "Flex Message",
"contents"=> [
"type"=> "carousel",
"contents"=> [
while($result->fetch_assoc()){
[
"type"=> "bubble",
"hero"=> [
"type"=> "image",
"url"=> "https://www.run.ots.co.th/Medicaltour/api/".$result['product_img'],
"size"=> "full",
"aspectRatio"=> "20:13",
"aspectMode"=> "cover"
],
"body"=> [
"type"=> "box",
"layout"=> "vertical",
"spacing"=> "sm",
"contents"=> [
[
"type"=> "text",
"text"=> $result['product_name'],
"size"=> "xl",
"weight"=> "bold",
"wrap"=> true
],
[
"type"=> "box",
"layout"=> "baseline",
"flex"=> 1,
"contents"=> [
[
"type"=> "text",
"text"=> "ราคา",
"flex"=> 6,
"weight"=> "regular",
"wrap"=> true
],
[
"type"=> "text",
"text"=> $result['product_price']" บาท",
"flex"=> 2
]
]
]
]
]
],
}
]
]
];
อยากให้ออกมาเป็นประมาณนี้อะครับ
Code (PHP)
$sql = "select * from product order by product_id Asc";
$result = $conn->query($sql);
$menu = [
"type"=> "flex",
"altText"=> "Flex Message",
"contents"=> [
"type"=> "carousel",
"contents"=> [
[
"type"=> "bubble",
"hero"=> [
"type"=> "image",
"url"=> "https://www.run.ots.co.th/Medicaltour/api/".$result['product_img'],
"size"=> "full",
"aspectRatio"=> "20:13",
"aspectMode"=> "cover"
],
"body"=> [
"type"=> "box",
"layout"=> "vertical",
"spacing"=> "sm",
"contents"=> [
[
"type"=> "text",
"text"=> $result['product_name'],
"size"=> "xl",
"weight"=> "bold",
"wrap"=> true
],
[
"type"=> "box",
"layout"=> "baseline",
"flex"=> 1,
"contents"=> [
[
"type"=> "text",
"text"=> "ราคา",
"flex"=> 6,
"weight"=> "regular",
"wrap"=> true
],
[
"type"=> "text",
"text"=> $result['product_price']" บาท",
"flex"=> 2
]
]
]
]
]
],
[
"type"=> "bubble",
"hero"=> [
"type"=> "image",
"url"=> "https://www.run.ots.co.th/Medicaltour/api/".$result['product_img'],
"size"=> "full",
"aspectRatio"=> "20:13",
"aspectMode"=> "cover"
],
"body"=> [
"type"=> "box",
"layout"=> "vertical",
"spacing"=> "sm",
"contents"=> [
[
"type"=> "text",
"text"=> $result['product_name'],
"size"=> "xl",
"weight"=> "bold",
"wrap"=> true
],
[
"type"=> "box",
"layout"=> "baseline",
"flex"=> 1,
"contents"=> [
[
"type"=> "text",
"text"=> "ราคา",
"flex"=> 6,
"weight"=> "regular",
"wrap"=> true
],
[
"type"=> "text",
"text"=> $result['product_price']" บาท",
"flex"=> 2
]
]
]
]
]
],
]
]
];
Tag : PHP, MySQL, HTML, CakePHP, FuelPHP
Date :
2020-05-01 00:16:55
By :
sukitti
View :
737
Reply :
2
Code (PHP)
<?php
function content($v1, $v2, $v3){
return
[ "type"=>"bubble",
"hero"=>["type"=>"image","url"=>$v1,"size"=>"full","aspectRatio"=>"20:13","aspectMode"=>"cover"],
"body"=>["type"=> "box","layout"=> "vertical","spacing"=> "sm",
"contents"=> [
["type"=>"text", "text"=>$v2,"size"=>"xl","weight"=>"bold","wrap"=> true],
["type"=> "box","layout"=> "baseline","flex"=> 1,
"contents"=> [
["type"=>"text", "text"=> "ราคา", "flex"=> 6, "weight"=> "regular","wrap"=> true],
["type"=>"text", "text"=> $v3.' บาท', "flex"=> 2]
]
]
]
]
];
}
$menu = [
"type"=> "flex",
"altText"=> "Flex Message",
"contents"=> [
"type"=> "carousel",
"contents"=> []
]
];
$sql = "select * from product order by product_id Asc";
$result = $conn->query($sql);
while($ro=$result->fetch_object()){
$x=content(
"https://www.run.ots.co.th/Medicaltour/api/".$ro->product_img,
$ro->product_name,
$ro->product_price);
$menu['contents']['contents'][]=$x;
}
ประวัติการแก้ไข 2020-05-01 14:24:05 2020-05-01 18:46:17
Date :
2020-05-01 14:23:41
By :
Chaidhanan
รับทองไปแล้ว...มาลองศึกษาพื้นดินบ้าง
(เฉพาะ array)
Code (PHP)
<?php
// database
$_data = [
'img' => ["image1", "image2", "image3"],
'name' => ["name1", "name2", "name3"],
'price' => ["price1", "price2", "price3"]
];
//print_r($_data);
$loop = [];
for ($i = 0; $i < count($_data); $i++) { // fetch_assoc()
$loop[] = [ // แก้ pattern ข้อมูลซ้ำ ตามชอบ
'Image' => $_data['img'][$i],
'Name' => $_data['name'][$i],
'Price' => $_data['price'][$i]
];
}
//print_r($loop);
// ประกอบร่าง array
$arr = [
'level1' => [
'level2' => $loop
]
];
print_r($arr);
//var_dump($arr);
//echo json_encode($arr);
?>
Date :
2020-05-01 15:14:53
By :
PhrayaDev
Load balance : Server 03