|
 |
|
สอบถาม การเรียกข้อมูลจากฐานข้อมูลในตารางแบบ array สองตาราง ที่มี id เหมือนกัน ต้องทำอย่างไรครับ ขอรบกวนท่านผู้รู้ครับ |
|
 |
|
|
 |
 |
|
ห่างหายไปนานครับเรื่องโค๊ดพอมาทำก้ทำไม่ได้ จนต้องมาถามครับอ่านแล้วก้ไม่เข้าใจ และเวลาจำกัดด้วยครับ
1.อยากสอบถามครับว่า ผมเรียกข้อมูล จากตาราง "tb_1" ตามโคดด้านล่าง
$query_name_departments= \Model::createQuery()
->select()
->from('tb_1') // ดึงฐานข้อมูลจาก ตาราง tb_1
->where(array('category_id',$name_departments)); // โดยที่ ข้อมูลจาก $name_departments เท่ากับ ข้อมูลฟิล category_id
foreach ($query_name_departments->execute() as $item_name_departments) { echo $item_name_departments->departments; }
ผลลัพที่ได้คือ --->>> ,74814,74828,74821,74829,

2. แล้วที่นี้ผม อยากจะ นำข้อมูลที่ได้ มาเรียกข้อมูลจาก tb_2 ที่มี id เป็นเลขที่ได้ ด้านบน โดยค้นหาเป็น array ซึ่งต้อง ลบ เครื่องหมาย " , " ออก และไปเทียบ กับฟิวในตาราง tb_2

|74814 = นาย ก|
|74828 = นาย ข|
|74821 = นาย ค|
|74829 = นาย จ|
สมมติ ผมเรียก ได้ข้อมูลมาจาก tb_1 เป็น -->> ,74828,74829,
ก็ให้นำน้อมมูล ฟิว topic (นาย ข, นาย ง) มาเก็บในตัวแปร $show แล้วนำไปแสดงในหน้าที่ต้องการ ประมารนี้ครับ ต้อง
เป็นแนวทางไหนครับ ผม งง มาสองวันละครับ
Tag : PHP, MySQL, Web Hosting, FuelPHP, Apache, XAMPP

|
ประวัติการแก้ไข 2023-10-25 23:02:48 2023-10-25 23:10:28 2023-10-25 23:12:03 2023-10-25 23:14:17
|
 |
 |
 |
 |
Date :
2023-10-25 23:01:12 |
By :
steamlose |
View :
367 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$department_tb1 = explode(",","74814,74828,74821,74829,"); //ตัด string ตาม , ใส่ array ไว้
Code (SQL)
$result = $modelTB2->whereIn("category",$department_tb1)->get(); // where ด้วย array id จากข้างบน
print_r($result);
|
 |
 |
 |
 |
Date :
2023-10-26 16:40:14 |
By :
mongkon.k |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 1 เขียนโดย : mongkon.k เมื่อวันที่ 2023-10-26 16:40:14
รายละเอียดของการตอบ ::
มาถูกทางหรือป่าวไม่รู้นะคับ
id topic
|74814 = นาย ก|
|74828 = นาย ข|
|74860 = นาย ค|
|74853 = นาย ง|
$name_departments=explode(",","74814,74828,74860,74853,"); //ตัด string ตาม , ใส่ array ไว้
//foreach ($name_departments as $gNAME){ echo $gNAME; }
$query_name_departments= \Model::createQuery()
->select()
->from('category') // ดึงฐานข้อมูลจาก ตาราง category
->where(array('category_id',$name_departments));// โดยที่ ข้อมูลจาก name_departments เท่ากับ ข้อมูลตาราง category_id
foreach ($query_name_departments->execute() as $item_name_departments) { $gNAME=$item_name_departments->topic; }
ผลที่ได้คือ นาย ก อย่างเดียวครับ นาย ข ค ง ไม่ออกครับ
|
 |
 |
 |
 |
Date :
2023-10-28 15:40:28 |
By :
steamlose |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|