how to display dynamic array values to view from model in codeigniter - php

here is my model code:
foreach ($query->result() as $row)
{
$data = array(
'ptitle' =>$row->ptitle,
'technology' => $row->technology,
'description' => $row->description,
);
$this->session->set_userdata('project',$data);
}
here is my View code:
<?php
if (isset($this->session->userdata['project']))
{
$ptitle = ($this->session->userdata['project']['ptitle']);
$technology = ($this->session->userdata['project']['technology']);
$description = ($this->session->userdata['project']['description']);
}
?>
when i print array it displays
Array ( [ptitle] => opmp [technology] => hbh [description] => kg ) Array ( [ptitle] => icicse [technology] => vv [description] => bhjv ) .can someone help me to print this values in view

Setting session should be like this
$data = array(
'ptitle' => $row['ptitle'],
'technology' => $row['technology'],
'description' => $row['description'],
);
$this->session->set_userdata($data);
To retrive them use
if (isset($this->session->userdata['project']))
{
$ptitle = $this->session->userdata('ptitle');
$technology = $this->session->userdata('technology');
$description = $this->session->userdata('description');
}

If your are storing multiple as array in SESSION then you surely need foreach in your view too.
Change from
if(isset($this->session->userdata['project']))
{
$ptitle = ($this->session->userdata['project']['ptitle']);
$technology = ($this->session->userdata['project']['technology']);
$description = ($this->session->userdata['project']['description']);
}
into
if(isset($this->session->userdata['project']))
{
foreach($this->session->userdata['project'] as $project)
{
$ptitle = $project['ptitle'];
$technology = $project['technology'];
$description = $project['description'];
echo $ptitle.'<br>'.$technology.'<br>'.$description.'<br>';
}
}

Try setting your array like bellow and then pass it to session set data.
$project = array("project" =>
array(
'ptitle' => "ptitle",
'technology' => "technology",
'description' => "description",
)
);
$this->session->set_userdata($project);

Related

how to cek array php

I have an array that I will insert into the item table, here I use multiple inserts
Array
(
[0] => Array
(
[id_service] => 2
[tracking_number] => RJC219384044389234035
)
[1] => Array
(
[id_service] => 1
[tracking_number] => RJC749944771469498146
)
)
in the item table, there is already id_service: 2
how to make validation, when one of the input is already in the item table, then all the input is false so it fails?
my code
public function nyobain()
{
$resi = $this->input->post('kode'); //tracking_number
$expld = explode(' ', $resi);
$data = $this->M_outbound_destinasi->dbGet($expld); //get 'id_service' where tracking_number
// $db = $this->db->query("SELECT id_service FROM `tabel_items`")->result();
// foreach ($db $key) {
// $temp=array($key->id_service);
// }
// how to cek ? if id_service already in the item table
foreach ($data as $key) {
$idnya = $key['id_service'];
$id_outbound = $key['id_outbound'];
$id_indes = $key['id_indes'];
$id_outbag = $key['id_outbag'];
$data_insert = array(
'jenis' => 'outbound-destinasi',
'id_service' => $idnya,
'id_indes' => $id_indes,
'id_outbound' => $id_outbound,
'id_outbag' => $id_outbag,
'id_admin' => $this->session->userdata('ses_id')
);
$this->db->insert('tabel_items', $data_insert);
}
echo json_encode($data);
}
You can use in_array to achieve this
public function nyobain(){
$resi = $this->input->post('kode');
$expld = explode(' ', $resi);
$data = $this->M_outbound_destinasi->dbGet($expld);
$db = $this->db->query("SELECT id_service FROM `tabel_items`")->result();
if(isset($data) && $data !== ''){//check if $data has a value and it not null
if (in_array($data ,$db, true)) {
return $data.'already exists in the DB!';
}else{
foreach ($data as $key) {
$idnya = $key['id_service'];
$id_outbound = $key['id_outbound'];
$id_indes = $key['id_indes'];
$id_outbag = $key['id_outbag'];
$data_insert = [
'jenis' => 'outbound-destinasi',
'id_service' => $idnya,
'id_indes' => $id_indes,
'id_outbound' => $id_outbound,
'id_outbag' => $id_outbag,
'id_admin' => $this->session->userdata('ses_id')
];
$this->db->insert('tabel_items', $data_insert);
}
return json_encode($data);
}
}
}

ForEach loop inside associative array PHP

I have following foreach loop
$selectedids = "1255;1256;1257";
$selectedidsarr = explode(';', $selectedids);
$idstand = '1';
foreach ($selectedidsarr as $item) {
$output1 = $idstand++;
echo "<li>product_id_$output1 = $item,</li>";
}
I want to add the output of the above loop inside following associative array
$paramas = array(
'loginId' => $cred1,
'password' => $credpass1,
'orderId' => $orderid,
'offer' => $offerid,
'shipid' => $shipcharge
)
So that the final array will look like this;
$paramas = array(
'loginId' => $cred1,
'password' => $credpass1,
'orderId' => $orderid,
'offer' => $offerid,
'shipid' => $shipcharge,
'product1_id' => 1255,
'product2_id' => 1256,
'product3_id' => 1257,
)
I tried creating following solution but its not working for me
$selectedids = $boughtitem;
$selectedidsarr = explode(';', $selectedids);
$idstand = '1';
foreach ($selectedidsarr as $item) {
$idoutput1 = $idstand++;
$paramas [] = array (
'product$idoutput1_id' => $item,
);
}
Need advice.
You don't need to define a new array, just set the key of the current array to the value you want, in the form of $array[$key] = $value to get an array that looks like [$key=>$value], or in your case...
$paramas['product' . $idoutput1 . '_id'] = $item;

update batch in codeigniter

I have data on db_temporary here I will update based on id_service, but why doesn't the update work?
public function updateUpload() {
$db = $this->M_order->db_temporary_service(); //db_temporary
$data = array();
foreach($db AS $key => $val){
$data[] = array(
"id_service" => $_POST['id_service'][$key],
"id_destination" => $_POST['id_destination'][$key],
"id_sub_destination" => $_POST['id_sub_destination'][$key],
"charges_order" => $_POST['charges_order'][$key],
"weight_order" => $_POST['weight_order'][$key],
"jenis_service" => $_POST['jenis_service'][$key],
"service_order" => $_POST['service_order'][$key],
"charges_order" => $_POST['charges_order'][$key],
);
echo '<pre>', print_r($data);
//update to db_service where id_service
$this->db->update_batch('service', $data, 'id_service');
redirect('backend/.....');
}
}
The data doesn't seem to be updated because you've used update_batch as well as redirect inside foreach loop, I'm writing the rectified code below, comments are mentioned wherever necessary. See if it resolves your issue.
public function updateUpload(){
$db = $this->M_order->db_temporary_service(); //db_temporary
$data = array();
foreach($db AS $key => $val){
$data[] = array(
"id_service" => $_POST['id_service'][$key],
"id_destination" => $_POST['id_destination'][$key],
"id_sub_destination" => $_POST['id_sub_destination'][$key],
"charges_order" => $_POST['charges_order'][$key],
"weight_order" => $_POST['weight_order'][$key],
"jenis_service" => $_POST['jenis_service'][$key],
"service_order" => $_POST['service_order'][$key],
"charges_order" => $_POST['charges_order'][$key],
);
}
// echo '<pre>', print_r($data); // all the data should be here
$this->db->update_batch('service', $data, 'id_service'); // update the table with all the data
redirect('backend/.....');
}

PHP Save array in database and miss data

I need to save in database category like "a & b" but the model save in database just "a" miss the blank space and the &.
This is the array:
$data = array('avenvu' => $_POST['avenvu'],
'brand' => $_POST['brand'],
'category' => $_POST['category'],
'description' => $_POST['description'],
'man_women_shop'=> $_POST['man_women_shop'],
'postdatetime' => date("Y-m-d H:i:s",time()),
'publish' => 1,
'user_id' => $this->session->userdata('user_id'));
$result = $this->personal_closest->insertCloset($data);
And this is the model:
function insertCloset($data) {
$this->db->insert('personal_closest',$data);
}
Change your insertCloset function:
function insertCloset($data) {
$personal['avenvu'] = $data['avenvu'];
$personal['brand'] = $data['brand'];
$personal['category'] = $data['category'];
$personal['description'] = $data['description'];
$personal['man_women_shop'] = $data['man_women_shop'];
$personal['postdatetime'] = $data['postdatetime'];
$personal['publish'] = $data['publish'];
$personal['user_id'] = $data['user_id'];
$this->db->insert('personal_closest',$personal);
}

How to convert this array to fuelphp acl format

Hi I'm a newbie of FuelPHP. I'm makin' a demo use ACL. I have fetched all roles from database with format as the code below
$data = array(
array('admin'=>array(
'none' => array(
'crudform' => array('create','index')
)
)),
array('admin'=>array(
'none' => array(
'cruddept' => array('create','view')
)
)),
);
And now I want to convert that array to format as
$data = array(
'admin' => array(
'none'=>array(
'crudform' => array(
'create',
'index'
),
'cruddept'=>array(
'create',
'view'
)
)
)
)
How I can do that ?
After you retreive your data from database, you can convert your array using a function just like this one
function change_array($data)
{
$result = array('admin' => array('none' => array()));
foreach ($data as $key => $value)
foreach ($data[$key]['admin']['none'] as $key_child => $value_child)
$result['admin']['none'][$key_child] = $value_child;
return $result;
}
All you need to do, is to use it like this
$data = change_array($data);
Thanks Mr Khalid, your idea has provide a way for me how to resolve my solution. I have found the way to build array as I want. This is my code
function build_role_array($roles){
$final = array();
foreach($roles as $row){
foreach($row as $role=>$value){
if(!isset($final[$role])){
$final[$role] = array();
}
foreach($value as $module=>$area){
if(!isset($final[$role][$module])){
$final[$role][$module] = array();
}
foreach($area as $controller=>$rights){
$final[$role][$module][$controller] = $rights;
}
}
}
}
return $final;
}
Thanks for support

Categories