Code Igniter Dynamic Checkbox - php

Hi I am trying to echo out check boxes, and then determine if they are checked in my controller and add them to an array if they are. I have followed a guide from SO but still having issues. Any insight much appreciated!
Controller Code:
foreach ($content['options'] as $option) {
$id = $option['id'];
$checked = (isset($_POST[$id])) ? true : false;
if ($checked == TRUE) {
array_push($recipientGroups, $id);
}
}
View Code:
foreach ($options as $option) {
echo br(1);
echo $option['name'];
$checkboxattr = array(
'name' => $option['name'],
'value' => $option['name'],
'id' => $option['id']
); //'checkbox_'.
echo form_checkbox($checkboxattr);
echo "<span id='total_".strtolower($option['name'])."'></span>";
}

The blank array you mean is from this variable $checkboxattr?
In your view, did you get the value of array variable ($options) from the controller?
If so, have you passed it from the controller ? Because I don't see any codes in your controller that pass the array to your view.

Related

Problem when displaying array session data [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 4 years ago.
I have a problem showing the field of the array 'notes' since it does not print the numbers generated by the 'addNotes' function.
The code loads the preloaded data and assigns random numbers with the 'addNotes' function to the 'notes' fields of the array and finally shows the data of the array in showStudentsList ().
<?php session_start();?>
<html>
<body>
<?php
if(!existDataInSession()){
initializePreloadedData();
}
function existDataInSession(){
return $_SESSION['data'] != NULL;
}
function initializePreloadedData(){
$person1= [
'name' => 'person1',
'notes' => []
];
$person2= [
'name' => 'person2',
'notes' => []
];
$data=[$person1,$person2];
$_SESSION['data'] = $data;
}
function addNotes(){
foreach ($data as $key => $value) {
$data[$key]['notes'] = random_int(0,100);
}
}
addNotes();
showStudentsList();
function showStudentsList(){
$data = $_SESSION['data'];
foreach ( $data as $student ) {
echo $student['name'] . " ";
echo $student['notes'];
echo implode($student['notes']);
echo "<br>";
}
}
//Result
//person1 Array
//person2 Array
?>
</body>
</html>
There are several problems in your code.
You change between array and integer notes. The plural implies an array.
You write to a copy of $_SESSION
$_SESSION['data'] != null throws a notice when not defined
Fixed version using notes array:
if(!existDataInSession()){
initializePreloadedData();
}
addNotes();
showStudentsList();
function existDataInSession(){
// we check if data was set. just to make double sure we check for type array as well
return isset($_SESSION['data']) && is_array($_SESSION['data']);
}
function initializePreloadedData(){
$person1= [
'name' => 'person1',
'notes' => []
];
$person2= [
'name' => 'person2',
'notes' => []
];
$_SESSION['data'] = [ $person1, $person2 ];
}
function addNotes(){
// operate on $_SESSION referencing the value since we want to ALTER the session data
foreach ($_SESSION['data'] as &$value) {
// since notes was defined as an ARRAY, we append here
$value['notes'][] = random_int(0,100);
}
}
function showStudentsList(){
$data = $_SESSION['data'];
foreach ( $data as $student ) {
echo $student['name'] . " ";
echo implode(', ', $student['notes']);
echo "<br>", PHP_EOL;
}
}
If I am not wrong to understand your requirement, $data is not defined in addNotes() function:
function addNotes(){
$data = $_SESSION['data']; // fetch data from session
foreach ($data as $key => $value) {
$data[$key]['notes'] = random_int(0,100);
}
$_SESSION['data'] = $data; // assign into session again
}
now use just echo without implode to get notes like echo $student['notes']
Hope this help.

Passing data in yii dependent dropdown

Am creating a depenednt dropdown in yii1 but i always get an error of htmlspecialchars() expects parameter 1 to be string, object given
This is the controller action code
public function actionDistrictList() {
$id = (int)$_POST['province'];
$data = Tblsudistricts::model()->findAll('province_id=1');
Yii::app()->session['districtlist'] = $data; //save created list to session
echo CHtml::tag('option', array('value' => ''), CHtml::encode('[select one]'), true);
foreach ($data as $value => $name){
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
//echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
}}
What could be wrong
Here, In your code
foreach ($data as $value => $name){
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
$value is a index and $name is a object, you have to use its parameters
For eg:
Chtml::encode($name->district_name)
Instaed of:
CHtml::encode($name)
Abhishek's answer is correct. However, you also have an issue with value. $value is the index of the object in the $data array not the id.
It looks like you are expecting a value => name kind of array for $data. If this is the case you should pass $data through the CHtml::listData function.
You can also avoid the foreach loop by using CHtml::listOptions
$data = Tblsudistricts::model()->findAll('province_id=1');
$data = CHtml::listData($data, "id", "name");
Yii::app()->session['districtlist'] = $data;
echo CHtml::listOptions(null, $data, array('prompt' => '[select one]'));

Change anchor class when navigating through pages using Codeigniter

Hello guys just want to ask a simple question. It is all about creating a navigation link.
Because I have a navigation link. And if the user choose a link the link will be highlighted and if the user click another link that link will be highlighted and the previous link will not. In creating my link I use an array and i loop it to call the action.
Here's my code
MY CONTROLLER
public function homepage(){
$data['title'] = "Welcome";
$data['copyright'] = date('Y');
$data['queryViewEntries'] = $this->category_model->viewAllEntry();
$data['link'] = "category";
$this->load->view('common/header_common',$data);
$this->load->view('common/navigation',$data);
$this->load->view('User/contents/homepage');
$this->load->view('common/footer_common',$data);
}
MY VIEW
<li class="nav-header"></li>
<?php
$highlight = $link;
$section = array(
'CATEGORIES' => 'user_controller/homepage',
'ITEMS' => 'item_controller/index',
'SUPPLIERS' => 'supplier_controller/index'
);
foreach($section as $key => $value){
echo "<li class='active'>".anchor($value,$key)."</li>"; //this is the problem how can i set the cliked link to active and the other will be not.
}
?>
</li>
I hope guys you can help me. Thanks.
https://www.codeigniter.com/user_guide/libraries/uri.html
//add to your controller
$data['url_link'] = $this->uri->segment(1, 0); //your URL segment /www.website/items
//view
foreach($section as $key => $value){
//class name
$className = ($key === $url_link) ? 'active' : 'no-active';
echo "<li class='$className'>".anchor($value,$key)."</li>";
}
first, you probably want to enclose your list in a < ul> and not close your list right after you open it. (your editor probably did you that favour).
In your loop, you need to check each key against the current link and apply a style accordingly. something like ....
<ul class="nav-header">
<?php
$highlight = $link;
$section = array(
'CATEGORIES' => 'user_controller/homepage',
'ITEMS' => 'item_controller/index',
'SUPPLIERS' => 'supplier_controller/index'
);
foreach($section as $key => $value){
if ($key == $link){
echo "<li class='active'>";
} else{
echo "<li class='inactive'>";
}
echo anchor($value,$key)."</li>";
}
?>
</ul>
You have to make sure that your 'section' array in your view and your $data['link'] have the same value. i.e. rename them to match e.g. in your controller
$data['link'] = 'CATEGORIES'
OR in your view
`$section = array(
'category' => 'user_controller/homepage',
.....
);

How to pass an array as a variable to a new function php

I'm trying to get my head around arrays. This a is a followup to previous question.
PHP How can I pass values from one array to another?
So, I've figured out how to pass data from one array to another thanks to: Explosion Pills but now i'm trying to take the array and pass it through to a new function and then create the new array within the new function. I'm guessing this is easier than it appears to be to me right now.
Here is the array:
$theSetup = array(
'option1' => array('title'=>'my_title','label'=>'The Big Title','val'=>'NoneyoBizness'),
'option2' => array('title'=>'my_watch,'label'=>'Big Watch','val'=>'Seiko'),
'option3' => array('title'=>'facebook','label'=>'Facebook Page','val'=>'http://facebook.com/bigwatch'),
);
Here is my test function:
function testArray($theSetup) {
foreach($theSetup as $v) {
$defaultOptions[$v['title']] = $v['val'];
}
foreach($defaultOptions as $k=>$v) {
echo $k .' : '.$v .'<br>';
}
}
testArray($theSetup);
After running the code I get an error: undefined variable theSetup
You need to use a text editor that shows you when you have a mistake in your PHP code.
This works fine:
<?php
$theSetup = array(
'option1' => array('title'=>'my_title','label'=>'The Big Title','val'=>'NoneyoBizness'),
'option2' => array('title'=>'my_watch','label'=>'Big Watch','val'=>'Seiko'),
'option3' => array('title'=>'facebook','label'=>'Facebook Page','val'=>'http://facebook.com/bigwatch'),
);
function testArray($theSetup) {
foreach($theSetup as $v) {
$defaultOptions[$v['title']] = $v['val'];
}
foreach($defaultOptions as $k=>$v) {
echo $k .' : '.$v .'<br>';
}
}
testArray($theSetup);
?>
This does not:
<?php
$theSetup = array(
'option1' => array('title'=>'my_title','label'=>'The Big Title','val'=>'NoneyoBizness'),
'option2' => array('title'=>'my_watch,'label'=>'Big Watch','val'=>'Seiko'),
'option3' => array('title'=>'facebook','label'=>'Facebook Page','val'=>'http://facebook.com/bigwatch'),
);
function testArray($theSetup) {
foreach($theSetup as $v) {
$defaultOptions[$v['title']] = $v['val'];
}
foreach($defaultOptions as $k=>$v) {
echo $k .' : '.$v .'<br>';
}
}
testArray($theSetup);
?>
The difference is I closed your quote mark after "my_watch."
Output
This works fine: my_title : NoneyoBizness
my_watch : Seiko
facebook : http://facebook.com/bigwatch

Does it exist any function to dynamically create variables from the key value pair of an array?

Let's say I have this array,
$array = array(
'name' => 'hermet',
'emails' => array ('hermet#example.com',
'hermet#example.net');
);
So this way echo $array ['name'] == 'hermet' prints true. I would like to know if there is a function already embedded in PHP that let me do this:
echo $name == 'hermet'; // obviously 'false'
foreach ($array as $key => $value) {
$aux = $key;
$$aux = $value;
}
echo $name == 'hermet'; // now prints 'true'
It seems to work even with a multidimensional array but I don't know if PHP has already any function to do that.
Thank you in advance.
You might be looking for extract
$array = array(
'name' => 'hermet',
'emails' => array ('hermet#example.com',
'hermet#example.net')
);
extract($array);
var_dump($emails);
echo $name;
-- EDIT: If you are concerned about Paul's remark, supply EXTR_SKIP to the second argument of extract, that way it won't overwrite variable in case you've already defined it prior to calling extract.
$name = 'jason';
extract($array, EXTR_SKIP);
echo $name; // still 'jason'

Categories