How to access array fields in codeigniter? - php

I'm trying to pass two arrays ($a_1 and $a_2) from my controller to my view like so:
$this->load->view('confirm_data_v', $a_1, $a_2);
In my view I want to print the value of one of them doing this:
<p><?php echo $name ?></p>
<p><?php echo $mail ?></p>
when I print each array I get this:
Array
(
[name] => jon
)
Array
(
[mail] => blabla#server.com
)
$name is a field inside $a_1 and $mail is a field inside $a_2, but it seems like the view doesn't know where these fields are, I mean, it doesn't know in wich array is $name and $mail, wether $a_1 or $a_2. How do I do that?.

the codeigniter wiki sais this
$data = array(
'name' => $a_1['name'],
'mail' => $a_2['mail'],
);
$this->load->view('confirm_data_v', $data);
https://www.codeigniter.com/user_guide/general/views.html

You're passing the arrays in an incorrect way. You can only pass one data array as a second parameter while loading the view.
You could instead put each array in the data array in your controller:
$data['a_1'] = $a_1;
$data['a_2'] = $a_2;
$this->load->view('confirm_data_v', $data);
Then in your view you can access $a_1 and $a_2 as you like
Name: <?php echo $a_1['name']; ?>
Email: <?php echo $a_2['mail']; ?>

Related

Can't pass variable from controller to view in CodeIgniter

I am having a problem trying to pass a variable from the controller to the view. I think I am doing a mistake somewhere, I can't find it since I am new to it.
My controller Method is :
public function paymentdetail($order_code)
{
$this->load->model('Payment_Model');
$paymentDetail = $this->Payment_Model->getpaymentdetail($order_code);
$orderDetail = $this->Payment_Model->getOrderDetail($order_code);
// echo print_r($paymentDetail);
$datas['content'] = $this->load->view('payment/paymentDetail',
array('paymentDetail' => $paymentDetail,
'orderDetail' => $orderDetail), true);
$this->load->view('layouts/main_template', $datas);
}
The following model function getpaymentdetail() returns an array (result_array()) and this is the one I am concerned about. If I can work this one out then I can also work with other model methods.
$this->Payment_Model->getpaymentdetail($order_code);
When I type <?php echo $paymentDetail['column_name']; ?> in view file(PaymentDetail.php) I get an error
Undefined index: column_name
Why do I get this error?
model function getpaymentdetail() returns an array (result_array())
those arrays are normally structured like this:
Array
(
[0] => Array
(
[ID] => 3120
[column_name] => col1
)
)
hence you cannot access column_name via <?php echo $paymentDetail['column_name']; ?> as this index doesn't exist in your array structure.
if you move one index deeper, then it will work: <?php echo $paymentDetail[0]['column_name']; ?>
Attention, if you expect more than 1 row to be returned, above will only access the first result (indexed 0) row! You'd need a foreach loop to get all results, see Generating Query Results - result arrays

codeigniter Pass array from view to controller

View:
user_data coming from the controller (Database middleware values)
<?php
$ID['IDS'] = array_column($user_data, 'ID');
print_r($ID);
// print_r($ID)=Array( [IDS] => Array([0] => 0 [1] => ABCD [2] => EFG ) )?>
Controller
$postData = $this->input->post();
$payment_code=$postData['IDS'];
$payment_code=$ID;
$postData = $this->input->post();
echo "<b>Name :</b> ".$postData['IDS']."<br/>";
$this->load->view('Demo/code_Send_data', ['code_key'=>$payment_code]);
[ Questions ]
can not see value in code_Send_data view
how to pass array value from view to controller
Is my <?php echo form_open('login/send_validated_Code_To_final',$ID['IDS']); ?> syntax correct ??
If you want to send additional hidden input data from the view to the controller, add a third parameter containing the data array (the second parameter is for the attribute, you can leave it empty) :
$ID['IDS'] = array_column($user_data, 'ID');
<?php echo form_open('login/send_validated_Code_To_final', '', $ID); ?>
Refs : Form Helper Documentation

If an Object can hold an Array, how is the Array accessed?

I currently have an Object which holds a series of Arrays, when I do print_r it does show the arrays however, when I want to get these values out I seem to be getting an error.
print_r($Obj->Example);
Returns:
Object => Example ( 'Username' => 'Example' )
My code to query through the Object is:
foreach($Obj as $single):
echo $Example['Username'];
endif;
Is it possible to query through like this because it isn't working and I get an error saying that:
$Obj is not defined as an Array
So how can I access the Example array and echo all the Usernames ?
As Example is an array that belongs to an object, the code would be like this
foreach($Obj->Example as $single):
echo $single['Username'];
endif;

Detect how many array exist?

I am using CodeIgniter framework. I am sending an array to my view, but I could not get the array in my view.
This is my controller code:
public function edit($id)
{
$record = $this->Start_model->get_entry($id);//receiving array from model
$this->load->view('edit',$record);//send array to my view
}
This is my array on controller that I send:
Array
(
[0] => Array
( [id] => 1 [name] => Hamza [age] => 20 [address] => audit and account [phone] => 03000000000 )
)
But when I call this array view I get this error:
Undefined variable: record
This is how I am getting my array in view:
<?php
echo '<pre>';
print_r($record);
echo '</pre>';
?>
Now I know I am sending an array to my view but I want to know If there is array in my view or not. I can get record through another method but I think it is not a good practice. So anyone can help me how I can detect if there is an array in my view?
In your controller, send a parent array instead:
public function edit($id)
{
$data = array();
$data['record'] = $this->Start_model->get_entry($id); // provided this is not empty
$this->load->view('edit', $data);
}
Then in your view:
foreach($record[0] as $key => $value) {
echo $value['id'];
// the rest blah blah
}
Codeigniter extracts the array passed to a view, creating variables based on the keys of the array. To work as you want, pass an array with a key or record and a value of your array:
public function edit($id)
{
$data = array('record' => $this->Start_model->get_entry($id));
$this->load->view('edit',$data);//send array to my view
}
Then this will work in your view:
<?php
echo '<pre>';
print_r($record);
echo '</pre>';
?>
The way you are currently sending the data, it will be extracted into individual variables for each element in the array, however as your array is numerically indexed, and php variable name rules prevent numeric variable names, you cannot access the data.

Yii: How to get list of checkboxes?

I have a table called Table, it has id and name as attributes.
For each entry in Table, I would like to generate a checkbox.
How can I do this?
I am using the Yii-Boostrap plugin, which I'm expecting I would need use something like this:
foreach(...)
echo $form->checkBoxRow($model, 'name');
Which I got from the Yii-Bootstrap Documentation.
Try this simple one
And in this for precheck to work just pass the array as second parameter
as shown below
<?$select=array('2','3');?>
<?php echo CHtml::checkBoxList(
'TableValues',
'$select',//you can pass the array here which you want to be pre checked
CHtml::listData(Table::model()->findAll(),'id','name'),
array('checkAll'=>'Select all tasks', 'checkAllLast'=>true)
); ?>
And you can get the selected checkbox values in the controller using
print_r($_POST['TableValues']);
UPDATED
For this the precheck to work u have to assign the array to the model attribute as shown below
<?php $model->modelAttributename=array('3','5')//respective checked values as of yours
<?php echo $form->checkBoxList(
$model,
'modelAttributename',
CHtml::listData(Table::model()->findAll(),'id','name'),
array('checkAll'=>'Select all tasks', 'checkAllLast'=>true)
); ?>
You should see your result array form sql query and see how to access any string you want from result array and then you create array of string that contain list of name.
e.g. your result query is $result["name"] = array("a","b","c");
<?php /** #var BootActiveForm $form */
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'horizontalForm',
'type'=>'horizontal',
));
?>
<fieldset>
<legend>Legend</legend>
<?php
$result["name"] = array("a","b","c");
echo $form->checkBoxListRow($model, 'checkboxes', $result["name"]);
?>
</fieldset>
Check this example:
Book Model:
'authors' => array(self::MANY_MANY, 'Author', 'authorbook(book_id,author_id)'),
Author Model:
'books' => array(self::MANY_MANY, 'Book', 'authorbook(author_id, book_id)'),
Checkbox List in form:
$books = CHtml::listData(Book::model()->findAll(), 'id', 'name');
$selected_keys = array_keys(CHtml::listData( $model->books, 'id' , 'id'));
echo CHtml::checkBoxList('Author[books][]', $selected_keys, $books);

Categories