foreach 2 ชั้น จะส่งค่า Array ไป controller ยังไงครับ แล้วเรียกใช้ ในหน้า View ยังไงครับ ใช้ Codeigniter ครับผม
ถามอย่างตอบอย่าง งง ผมถามหลักปฎิบัติครับ ไม่ได้ ถามMVC ไหนทำให้ดูหน่อยครับ
ประวัติการแก้ไข 2018-10-25 07:19:21
Date :
2018-10-25 06:58:47
By :
teedesign
- Controller
เรียกใช้งาน Model
- Model
เขียน model function 2 ชุด ขณะที่กำลังเรียกใช้งาน fn ชุดแรก ก็วนลูปเอาค่าที่คิวรีมาได้ มาใส่ในการเรียก model ชุดที่ 2
public function get_photos_advertise($id_home="",$id_u="") {
$results = array();
$this->db->select('*');
$this->db->from('photos_advertise');
$this->db->where('ref_id_form_advetise', $id_home);
$this->db->where('ref_id_users', $id_u);
$query = $this->db->get();
$results = $query ->result();
return $results;
}
public function show_advertise_img($id_users) {
$this->db->select('*');
$this->db->from('home_advertise');
$this->db->join('main_advertise', 'main_advertise.ref_id_form_advetise = home_advertise.id_home');
$this->db->where('home_advertise.ref_id_users', $id_users);
$this->db->where('main_advertise.ref_id_users', $id_users);
$this->db->order_by('id_home','ref_id_form_advetise', 'DESC');
$query = $this->db->get();
$value = $query->result();
foreach ($value as $val)
{
$val->data = $this->get_photos_advertise($val->id_home,$val->ref_id_users,);
}
unset($val);
return $value;
}
- View print_r($show_advertise_img) ดูแล้ววนลูปตามสบาย
Date :
2018-10-25 09:34:24
By :
apisitp
ในหน้า view หัวข้อ home_title ออกครับผม ส่วน image_advertise ไม่ออก ลอง print_r($data['show_advertise_img']); ก็ออกหมดนะครับ
View image_advertise ไม่ออกครับ
<?php foreach($show_advertise_img as $show_ad_img){?>
<tr>
<td><?php echo $show_ad_img->home_title;?></td>
<td><?php echo $show_ad_img->image_advertise;?></td>
</tr>
<?php } ?>
Controller
$data['show_advertise_img'] = $this->guest_model->show_advertise_img($id_users);
Model
public function get_photos_advertise($id_home="",$id_u="") {
$results = array();
$this->db->select('*');
$this->db->from('photos_advertise');
$this->db->where('ref_id_form_advetise', $id_home);
$this->db->where('ref_id_users', $id_u);
$query = $this->db->get();
$results = $query ->result();
return $results;
}
public function show_advertise_img($id_users) {
$this->db->select('*');
$this->db->from('home_advertise');
$this->db->join('main_advertise', 'main_advertise.ref_id_form_advetise = home_advertise.id_home');
$this->db->where('home_advertise.ref_id_users', $id_users);
$this->db->where('main_advertise.ref_id_users', $id_users);
$this->db->order_by('id_home','ref_id_form_advetise', 'DESC');
$query = $this->db->get();
$value = $query->result();
foreach ($value as $val)
{
$val->data = $this->get_photos_advertise($val->id_home,$val->ref_id_users);
}
unset($val);
return $value;
}
ประวัติการแก้ไข 2018-10-25 11:27:13
Date :
2018-10-25 11:00:02
By :
teedesign
คห 1 เขาตอบถูกแล้ว ให้คูณศึกษาวิธีการใช้งานก่อน
เพราะ controller จะเป็นตัวเรียกใช้ ค่าตัวแปร หรือ function method ต่างๆ จาก model library helper thirdparty ฯลฯ
แล้ว ส่งต่อให้ ส่วนของ view เพื่อแสดงผล
ส่วนการกำหนดค่าต่างๆ ใน ใน model library อาจจะกำหนดค่าเริ่มต้นต่างๆ ไว้ในตัวแปรต่างๆ ด้วย constant
หรือ จากการใช้ function __construct เพื่อคำนวณหรือ โหลดอะไรต่าง มาเป็นค่าเริ่มต้น ให้ส่วน controller มานำไปใช้
แต่ในกรณีที่คุณกำลังทำงาน ในส่วนของ model หนึ่ง แล้วอยากเรียกใช้ อีก model คุณก็เรียกใช้โมเดลนั้นได้เลย
ข้อแม้ต้องถูกโหลดมาแล้วนะ หรือจะโหลดตรงนั้นเลยก็ได้
แต่ถ้าอยากเรียกใช้ function ที่อยู่ใน controller ที่ถูกเรียกครั้งแรก ก็ต้องอ้างอิง คลาส controller นั้น
หรือส่ง function นั้น ในรูปของ call_back แล้วเรียกใช้ด้วย call_user_func_array($callback, $argument );
แต่ไม่ใช่ การทำงานที่ ดี เพราะงานมันควรจะจบใน model นั้น และส่งค่าตัวแปรกลับไป
ก็เลือกเอานะครับว่าจะทำยังไง
เอาคำสั่ง $this-load->view() ที่เรียกใช้งาน view มาดูหน่อยครับ เขียนไว้ยังไง
ปล. อีกที
Code (PHP)
<td><?php echo $show_ad_img->image_advertise;?></td>
image_advertise มันอยูใน $show_ad_img->data[0]->image_advertise
ก็เรียกใช้งานให้ถูกครับ
ประวัติการแก้ไข 2018-10-25 11:53:16 2018-10-25 11:59:32
Date :
2018-10-25 11:47:28
By :
Chaidhanan
พอลอง print_r($data['show_advertise_img']); ใน Controller มันออกครับผม แต่พอใส่
<td><?php echo $show_ad_img->data[0]->image_advertise;?></td>
มันออกแค่รูปเดียวอ่ะครับ
หน้า load view ครับพี่
public function index() {
if ($this->session->userdata('logged_in')) {
$login = $this->session->userdata('logged_in');
if (($login['type_user'] == 'guest') or ( $login['type_user'] == 'administrator')) {
$id_users = $this->session_user['id'];
$data['title'] = 'ข้อมูลประกาศ';
$data['session_user'] = $this->session_user;
$data['authUrl_fb'] = $this->facebook->login_url();
$data['authUrl_gg'] = $this->googleplus->loginURL();
$data['categories'] = $this->Category_admin->category_get_all();
$data['advertise'] = $this->Advertise_admin->advertise_get_all();
$data['countries'] = $this->user->getCountryRows();
$data['show_advertise_img'] = $this->guest_model->show_advertise_img($id_users);
/*echo "<pre>";
print_r($data['show_advertise_img']);
echo "</pre>";*/
$this->load->view('templates/header_guest', $data);
$this->load->view('guest/index', $data);
$this->load->view('templates/footer');
$this->load->view('templates/model', $data);*/
}
} else {
redirect(base_url('page/login'));
exit;
}
}
print_r($data['show_advertise_img']); ใน Controller มันออก (PHP)
Array
(
[0] => stdClass Object
(
[id_home] => 146
[ref_id_users] => 375
[home_title] => หัวข้อ1
[data] => Array
(
[0] => stdClass Object
(
[id_photos_advertise] => 282
[image_advertise] => 22102018214840.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 146
)
[1] => stdClass Object
(
[id_photos_advertise] => 283
[image_advertise] => 221020182148401.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 146
)
[2] => stdClass Object
(
[id_photos_advertise] => 284
[image_advertise] => 221020182148402.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 146
)
[3] => stdClass Object
(
[id_photos_advertise] => 285
[image_advertise] => 221020182148403.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 146
)
[4] => stdClass Object
(
[id_photos_advertise] => 286
[image_advertise] => 221020182148404.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 146
)
[5] => stdClass Object
(
[id_photos_advertise] => 287
[image_advertise] => 221020182148405.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 146
)
)
)
[1] => stdClass Object
(
[id_home] => 147
[ref_id_users] => 375
[home_title] => หัวข้อ2
[data] => Array
(
[0] => stdClass Object
(
[id_photos_advertise] => 288
[image_advertise] => 22102018215707.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 147
)
[1] => stdClass Object
(
[id_photos_advertise] => 289
[image_advertise] => 221020182157071.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 147
)
[2] => stdClass Object
(
[id_photos_advertise] => 290
[image_advertise] => 221020182157072.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 147
)
[3] => stdClass Object
(
[id_photos_advertise] => 291
[image_advertise] => 221020182157073.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 147
)
[4] => stdClass Object
(
[id_photos_advertise] => 292
[image_advertise] => 221020182157074.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 147
)
[5] => stdClass Object
(
[id_photos_advertise] => 293
[image_advertise] => 221020182157075.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 147
)
)
)
[2] => stdClass Object
(
[id_home] => 148
[ref_id_users] => 375
[home_title] => หัวข้อ3
[data] => Array
(
[0] => stdClass Object
(
[id_photos_advertise] => 294
[image_advertise] => 22102018221938.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 148
)
[1] => stdClass Object
(
[id_photos_advertise] => 295
[image_advertise] => 221020182219381.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 148
)
[2] => stdClass Object
(
[id_photos_advertise] => 296
[image_advertise] => 221020182219382.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 148
)
[3] => stdClass Object
(
[id_photos_advertise] => 297
[image_advertise] => 221020182219383.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 148
)
[4] => stdClass Object
(
[id_photos_advertise] => 298
[image_advertise] => 221020182219384.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 148
)
[5] => stdClass Object
(
[id_photos_advertise] => 299
[image_advertise] => 221020182219385.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 148
)
)
)
[3] => stdClass Object
(
[id_home] => 149
[ref_id_users] => 375
[home_title] => หัวข้อ4
[data] => Array
(
[0] => stdClass Object
(
[id_photos_advertise] => 300
[image_advertise] => 22102018233702.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 149
)
[1] => stdClass Object
(
[id_photos_advertise] => 301
[image_advertise] => 221020182337021.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 149
)
[2] => stdClass Object
(
[id_photos_advertise] => 302
[image_advertise] => 221020182337022.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 149
)
)
)
[4] => stdClass Object
(
[id_home] => 150
[ref_id_users] => 375
[home_title] => หัวข้อ5
[data] => Array
(
[0] => stdClass Object
(
[id_photos_advertise] => 303
[image_advertise] => 22102018234007.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 150
)
[1] => stdClass Object
(
[id_photos_advertise] => 304
[image_advertise] => 221020182340071.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 150
)
[2] => stdClass Object
(
[id_photos_advertise] => 305
[image_advertise] => 221020182340072.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 150
)
)
)
[5] => stdClass Object
(
[id_home] => 161
[ref_id_users] => 375
[home_title] => หัวข้อ6
[data] => Array
(
[0] => stdClass Object
(
[id_photos_advertise] => 313
[image_advertise] => 25102018001720.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 161
)
[1] => stdClass Object
(
[id_photos_advertise] => 314
[image_advertise] => 251020180017201.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 161
)
[2] => stdClass Object
(
[id_photos_advertise] => 315
[image_advertise] => 251020180017202.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 161
)
[3] => stdClass Object
(
[id_photos_advertise] => 316
[image_advertise] => 251020180017203.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 161
)
[4] => stdClass Object
(
[id_photos_advertise] => 317
[image_advertise] => 251020180017204.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 161
)
[5] => stdClass Object
(
[id_photos_advertise] => 318
[image_advertise] => 251020180017205.JPG
[ref_id_users] => 375
[ref_id_form_advetise] => 161
)
)
)
[6] => stdClass Object
(
[id_home] => 162
[ref_id_users] => 375
[home_title] => หัวข้อ7
[data] => Array
(
[0] => stdClass Object
(
[id_photos_advertise] => 319
[image_advertise] => 25102018002102.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 162
)
[1] => stdClass Object
(
[id_photos_advertise] => 320
[image_advertise] => 251020180021021.jpg
[ref_id_users] => 375
[ref_id_form_advetise] => 162
)
)
)
[7] => stdClass Object
(
[id_home] => 164
[ref_id_users] => 375
[home_title] => หัวข้อ8
[data] => Array
(
[0] => stdClass Object
(
[id_photos_advertise] => 321
[image_advertise] => 25102018003650.jpeg
[ref_id_users] => 375
[ref_id_form_advetise] => 164
)
[1] => stdClass Object
(
[id_photos_advertise] => 322
[image_advertise] => 25102018003650.png
[ref_id_users] => 375
[ref_id_form_advetise] => 164
)
[2] => stdClass Object
(
[id_photos_advertise] => 323
[image_advertise] => 251020180036501.jpeg
[ref_id_users] => 375
[ref_id_form_advetise] => 164
)
)
)
)
ประวัติการแก้ไข 2018-10-25 13:26:00 2018-10-25 13:26:47 2018-10-25 13:27:23
Date :
2018-10-25 13:23:41
By :
teedesign
- Controller
$data['show_advertise_img'] = $this->guest_model->show_advertise_img($id_users); //มันไม่จบแค่นี้
//ต้องดูว่าคุณเรียกวิวแบบไหน ต้องส่งค่าทั้งหมดไปตอนเรียกวิว
$this->load->view('frontend/news', $data ,TRUE);
- View
print_r($show_advertise_img); //ดูค่าทั้งหมด
//ใช้งานผ่านลูป ลูปแรก
foreach($show_advertise_imgas as $value) :
//อยากจะวนอะไรก่อนก็ว่ากันไป ส่วน ตัวในที่ต้องการก็ประมาณ
//ลูป 2
foreach ($val->data as $xx) {
echo $xx->id_photos_advertise;
}
endforeach;
---บางที่ก็สับสน "หน้า load view ครับพี่" จริงๆ มันคือ Controller หรือเปล่า
สรุปก็ยังไม่เข้าใจการทำงานของ MVC จริงๆ...หรือเปล่า
ประวัติการแก้ไข 2018-10-25 13:55:12
Date :
2018-10-25 13:54:22
By :
apisitp
ยังไม่ออกเลยคร๊าบบบ..มันออกแค่รุปเดียวในแต่ล่ะแถว
<?php foreach($show_advertise_imgas as $value) {?>
<?php echo $value->title_home; ?>
<?php foreach ($val->data as $xx) {
echo $xx->image_advertise;
}
?>
<?php } ?>
ประวัติการแก้ไข 2018-10-25 14:33:21 2018-10-25 14:34:01
Date :
2018-10-25 14:24:24
By :
teedesign
Code (PHP)
<?php
// view/filename.php ลองใช้ smarty ดูบ้าง เพื่อโค๊ดวิว มันจะดู clean และเข้าใจได้ง่าย
?>
{show_advertise_img}
<tr>
<td>{home_title}</td>
<td>{data}<img src="/path_image/{image_advertise}">{/data}</td>
</tr>
{/show_advertise_img}
ประวัติการแก้ไข 2018-10-25 17:24:06
Date :
2018-10-25 14:50:29
By :
Chaidhanan
ขอบคุณครับผม ขอให้สุขภาพแข็งแรงกัน ทุกท่านครับผม
เดี๋ยวไปลองก่อนนะครับ
Date :
2018-10-25 20:25:12
By :
teedesign
ทำได้ล่ะครับผม เอาตามความเข้าใจผมนะครับ
Model
public function show_advertise($id_users) {
$this->db->select('*');
$this->db->from('home_advertise');
$this->db->join('main_advertise', 'main_advertise.ref_id_form_advetise = home_advertise.id_home');
$this->db->where('home_advertise.ref_id_users', $id_users);
$this->db->where('main_advertise.ref_id_users', $id_users);
$this->db->order_by('id_home','ref_id_form_advetise', 'DESC');
$query = $this->db->get();
return $query->result_array();
}
////////////////////////////////////////////////////////////////////////////////////////////////
public function show_advertise_img($id_users) {
$this->db->select('*');
$this->db->from('home_advertise');
$this->db->join('main_advertise', 'main_advertise.ref_id_form_advetise = home_advertise.id_home');
$this->db->join('photos_advertise', 'photos_advertise.ref_id_form_advetise = home_advertise.id_home');
$this->db->where('home_advertise.ref_id_users', $id_users);
$this->db->where('main_advertise.ref_id_users', $id_users);
$this->db->where('photos_advertise.ref_id_users', $id_users);
$this->db->order_by('id_home','ref_id_form_advetise', 'DESC');
$query = $this->db->get();
return $query->result_array();
}
Controller
public function index() {
$id_users = $this->session_user['id'];
$data['session_user'] = $this->session_user;
$data['show_advertise'] = $this->guest_model->show_advertise($id_users);
$data['show_advertise_img'] = $this->guest_model->show_advertise_img($id_users);
$this->load->view('guest/index', $data);
}
View
<table width="100" border="1">
<?php foreach($show_advertise as $show_ad){?>
<tr>
<td>
<?php echo $show_ad['home_title'];?>
</td>
</tr>
<tr>
<td>
<?php foreach($show_advertise_img as $show_ad_img){
if(($show_ad_img['ref_id_users'] == $show_ad['ref_id_users']) && ($show_ad_img['ref_id_form_advetise'] == $show_ad['id_home'])){ ?>
<img src="<?php echo base_url(); ?>upload/advertiseimg/thumb/<?php echo $show_ad_img['image_advertise']; ?>">
<?php }
} ?>
</td>
</tr>
<?php } ?>
</table>
ประวัติการแก้ไข 2018-10-25 21:33:09
Date :
2018-10-25 21:30:35
By :
teedesign
Load balance : Server 04