LINE Flex message ใช้ while loop ข้อมูลเข้ามาใส่ใน carousel ได้ยังไงครับ

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา LINE Flex message ใช้ while loop ข้อมูลเข้ามาใส่ใน carousel ได้ยังไงครับ

LINE Flex message ใช้ while loop ข้อมูลเข้ามาใส่ใน carousel ได้ยังไงครับ
คือตอนนี้ผมลูปข้อมูลออกมากจาก db มาใส่ใน carousel ได้แล้ว แต่มันขึ้นอันเดียว(ตามโค้ด)ในแชท Line ถ้าอยากได้หลายอันผมต้องลูปข้อมูลยังไงครับ
จากโค้ดด้านล่าง สมมติมีรายการแจ้ง 10 รายการ ผมอยากให้ลูปข้อมูลมาทั้ง 10 รายการ แล้วใส่ใน carousel ครับ ขอบคุณครับ
if (mysqli_num_rows($result) > 0) { //ถ้ามี
		// output data of each row
		while ($row = mysqli_fetch_assoc($result)) {
			$repairID = $row['repairID'];
			$dis = mb_substr($row["discrition"], 0, 15); //รายละเอียด
			$date = $row["dt_add"]; // วันที่
			$description = $row['description']; //รายการแจ้งซ่อม
			//ตรวจสอบรูป
			if ($row["image"] == '') { //ถ้าไม่มีรูป
				$img = 'www.xxx.com';
			} else { // ถ้ามีรูป
				$img = 'www.xxx.com?img=' . $row["image"];
			}
		}
		//ข้อมูลที่ตอบกลับไปยัง line ผู้ใช่ เมื่อตรวจสอบว่ามีข้อมูลในระบบ
		$messages = [
			"type" => "flex",
			"altText" => "รายการแจ้งซ่อมของคุณ",
			"contents" => [
				"type"  => "carousel",
				"contents"  => [
					[
						"type"  => "bubble",
						"hero"  => [
							"type"  => "image",
							"size"  => "full",
							"aspectRatio"  => "20:13",
							"aspectMode"  => "cover",
							"url"  => $img
						],
						"body"  => [
							"type"  => "box",
							"layout"  => "vertical",
							"spacing"  => "sm",
							"contents"  => [
								[
									"type"  => "text",
									"text"  => "แจ้งซ่อมล่าสุด",
									"wrap"  => true,
									"weight"  => "bold",
									"size"  => "xl"
								],
								[
									"type"  => "box",
									"layout"  => "baseline",
									"contents"  => [
										[
											"type"  => "text",
											"text"  => $dis . "...",
											"wrap"  => true,
											"weight"  => "bold",
											"size"  => "xl",
											"flex"  => 0
										]
									]
								]
							]
						],
						"footer"  => [
							"type"  => "box",
							"layout"  => "vertical",
							"spacing"  => "sm",
							"contents"  => [
								[
									"type"  => "button",
									"style"  => "primary",
									"action"  => [
										"type" => "message",
										"label" => "Track",
										"text" => "Track"
									]
								],
								[
									"type"  => "button",
									"style"  => "primary",
									"color"  => "#9c4924",
									"action"  => [
										"type"  => "uri",
										"label"  => "ประเมินผล",
										"uri"  => "https://www.tu-library.com/gentelella-master/repair_1/line-form/survey-user.php?getid=" . $repairID
									]
								]
							]
						]
					],
					[
						"type"  => "bubble",
						"body"  => [
							"type"  => "box",
							"layout"  => "vertical",
							"spacing"  => "sm",
							"contents"  => [
								[
									"type"  => "button",
									"flex"  => 1,
									"gravity"  => "center",
									"action"  => [
										"type"  => "uri",
										"label"  => "See more",
										"uri"  => "https://tu-library.com/gentelella-master/repair_1/line-form/tables_all-line.php?getuserId=" . $str
									]
								]
							]
						]
					]
				]
			]
		];


Samada 203.131.208.xxx 12-09-2020 17:20:38

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1
ให้ดูโครงสร้าง flex ที่ถูกต้อง ทีสามารถแสดง carousel ได้
แล้วให้มองหาส่วนที่เป็นโครงสร้าง array ของ flex ที่เราจะใช้ค่าจาก loop ข้อมูล db
 
ถ้าโครงสร้างที่ถูกต้องประมาณนี้

//ข้อมูลที่ตอบกลับไปยัง line ผู้ใช่ เมื่อตรวจสอบว่ามีข้อมูลในระบบ
$messages = [
    "type" => "flex",
    "altText" => "รายการแจ้งซ่อมของคุณ",
    "contents" => [
        "type"  => "carousel",
        "contents"  => [
            [
                "type"  => "bubble",
            ],
            [
                "type"  => "bubble",
            ]
        ]
    ]
];
 
loop ที่ใช้ คือ array ของ bubble ให้เรากำหนดเป็นตัวแปร
 
$messages = [
    "type" => "flex",
    "altText" => "รายการแจ้งซ่อมของคุณ",
    "contents" => [
        "type"  => "carousel",
        "contents"  => [
            [
                "type"  => "bubble",
            ],
            [
                "type"  => "bubble",
            ]
        ]
    ]
];
 
กำหนด array แยก
 
$arrBubble = [
    [
        "type"  => "bubble",
    ],
    [
        "type"  => "bubble",
    ]    
];

$messages = [
    "type" => "flex",
    "altText" => "รายการแจ้งซ่อมของคุณ",
    "contents" => [
        "type"  => "carousel",
        "contents" =>  $arrBubble
    ]
];
 
สุดท้ายเอา สร้าง array ของ bubble ใน loop ของ db
 
 
$arrBubble = [];
while(){
    $arrBubble[] = [
        "type"  => "bubble",
    ]
}

$messages = [
    "type" => "flex",
    "altText" => "รายการแจ้งซ่อมของคุณ",
    "contents" => [
        "type"  => "carousel",
        "contents" =>  $arrBubble
    ]
];
 


ninenik 27.55.64.xxx 12-09-2020
 ความคิดเห็นที่ 2

โอ้วววววววว ได้แล้ว สุดยอดมาก

ขอบพระคุณมากครับ ผมนั่งงมตั้งหลายวัน



Samada 203.131.208.xxx 12-09-2020 19:26






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ