Issue with storing string with a backslash(\) in session - php

Here is a quick example:
function step1() {
$data = array(
'level_1' => array(
'level_2' => 'abc\def'
),
);
$this->session->set_userdata("data", $data);
}
function step2() {
echo '<pre>';
print_r($this->session->all_userdata());
}
As end result, data is not stored in session.

You can separate with '\' using '.'...Think this may work
function step1() {
$data = array(
'level_1' => array(
'level_2' => 'abc'.'\'.'def'
),
);
$this->session->set_userdata($data);
}
function step2() {
echo '<pre>';
print_r($this->session->all_userdata());
}

Related

recursion function on tree data to get path

I want to write function which receive me path to top element, but I can't figure out how it should work..
My example data:
$data = array(
3546456 => array(
5345345,
12312312,
56456546,
),
12312312 => array(
34534534,
5675675,
8678678,
),
567978 => array(
234,
756756,
8678678,
),
);
//I have function to return parent.
$parents = getParents(8678678); // eg. $object_id = 8678678 - return [12312312, 567978] , $object_id = 12312312 - return [3546456]
// and my recursion function I tried.
function getObjectPath($object_id) {
if ($object_id == null) {
return [];
}
$parents = getObjectParents($object_id);
foreach ($parents as $parent) {
return array($object_id => getObjectPath($parent->nid));
}
}
It doesn't work way I need, on the return in getObjectPath(8678678) I'd like to have return array like that:
array(
3546456 => array(
12312312 => array(
8678678
)
),
567978 => array(
8678678
)
);
I think you almost got it, need to have some other check before returning
$data = array(
3546456 => array(
5345345,
12312312,
56456546,
),
12312312 => array(
34534534,
5675675,
8678678,
),
567978 => array(
234,
756756,
8678678,
),
);
//I have function to return parent.
$parents = getObjectPath(8678678, $data); // eg. $object_id = 8678678 - return [12312312, 567978] , $object_id = 12312312 - return [3546456]
echo "<pre>";
print_r($parents);
// and my recursion function I tried.
function getParents($object_id, $data) {
$return = [];
foreach($data as $key => $value) {
if(in_array($object_id, $value)) {
$return[] = $key;
}
}
return $return;
}
// and my recursion function I tried.
function getObjectPath($object_id, $data) {
$return = [];
$parents = getParents($object_id, $data);
foreach($parents as $parent) {
$temp = getObjectPath($parent, $data);
if(!empty($temp)) {
$return[key($temp)][$parent] = $object_id;
} else {
$return[$parent] = $object_id;
}
}
return $return;
}

Why is my variable data getting dropped after the first foreach loop?

I am using CodeIgniter to insert records in my database. Everything is working perfectly, except I can't seem to understand why the value of a nested array is losing its value after the first foreach loop.
php
// $data is all data (array) passed from controller method.
public function create($data)
{
$my_data = array(
'name' => $data['name'],
....
);
if ($this->db->insert('myTable', $my_data)) {
$insert_id = $this->db->insert_id();
// $data['mySecondCheckbox'] has value here
foreach ($first_data['myFirstCheckbox'] as $cb1) {
$first_data = array(
'fk_foo_id' => $insert_id,
'fk_cb_id' => $cb1
);
$this->db->insert('mySecondTable', $first_data);
}
// $data['mySecondCheckbox'] has no value here
foreach ($data['mySecondCheckbox'] as $cb2) {
$second_data = array(
'fk_foo_id' => $insert_id,
'fk_cb_id' => $cb2
);
$this->db->insert('myThirdTable', $second_data);
}
return $insert_id;
} else {
return false;
}
}
SOLUTION
Thank you #John! Pretty obvious after sleeping on it. At the time, I was thinking that $data was specific to within each of the each scope.
php
// $data is all data (array) passed from controller method.
public function create($data)
{
$my_data = array(
'name' => $data['name'],
....
);
if ($this->db->insert('myTable', $my_data)) {
$insert_id = $this->db->insert_id();
// $data['mySecondCheckbox'] has value here
foreach ($data['myFirstCheckbox'] as $cb1) {
$first_data = array(
'fk_foo_id' => $insert_id,
'fk_cb_id' => $cb1
);
$this->db->insert('mySecondTable', $first_data);
}
// $data['mySecondCheckbox'] has no value here
foreach ($second_data['mySecondCheckbox'] as $cb2) {
$data = array(
'fk_foo_id' => $insert_id,
'fk_cb_id' => $cb2
);
$this->db->insert('myThirdTable', $second_data);
}
return $insert_id;
} else {
return false;
}
}
The first line in your foreach loop is resetting the subject in your loop "$data". Rename the variable and you should be good

PHP - Arrays and Foreach

I searched in Google and consulted the PHP documentation, but couldn't figure out how the following code works:
$some='name=Licensing Module;nextduedate=2013-04-10;status=Active|name=Test Addon;nextduedate=2013-04-11;status=Active';
function getActiveAddons($somet) {
$addons = array( );
foreach ($somet as $addon) {
if ($addon['status'] == 'Active') {
$addons[] = $addon['name'];
continue;
}
}
return $addons;
}
echo (count( getActiveAddons( $some ) ) ? implode( '<br />', getActiveAddons( $some ) ) : 'None');
The code always echo's None.
Please help me in this.
I don't know where you got this code from but you've initialized $some the wrong way. It is expected as an array like this:
$some = array(
array(
'name' => 'Licensing Module',
'nextduedate' => '2013-04-10',
'status' => 'Active'
),
array(
'name' => 'Test Addon'
'nextduedate' => '2013-04-11',
'status' => 'Active'
)
);
I guess the article you've read is expecting you to parse the original string into this format.
You can achieve this like this:
$string = 'name=Licensing Module;nextduedate=2013-04-10;status=Active|name=Test Addon;nextduedate=2013-04-11;status=Active';
$result = array();
foreach(explode('|', $string) as $record) {
$item = array();
foreach(explode(';', $record) as $column) {
$keyval = explode('=', $column);
$item[$keyval[0]] = $keyval[1];
}
$result[]= $item;
}
// now call your function
getActiveAddons($result);
$some is not an array so foreach will not operate on it. You need to do something like
$some = array(
array(
'name' => 'Licensing Module',
'nextduedate' => '2013-04-10',
'status' => 'Active'
),
array(
'name' => 'Test Addon',
'nextduedate' => '2013-04-11',
'status'=> 'Active'
)
);
This will create a multidimensional array that you can loop through.
function getActiveAddons($somet) {
$addons = array( );
foreach ($somet as $addon) {
foreach($addon as $key => $value) {
if ($key == 'status' && $value == 'Active') {
$addons[] = $addon['name'];
continue;
}
}
}
return $addons;
}
First, your $some variable is just a string. You could parse the string into an array using explode(), but it's easier to just start as an array:
$some = array(
array(
"name" => "Licensing Module",
"nextduedate" => "2013-04-10",
"status" => "Active",
),
array(
"name" => "Test Addon",
"nextduedate" => "2013-04-11",
"status" => "Active",
)
);
Now, for your function, you are on the right track, but I'll just clean it up:
function getActiveAddons($somet) {
if (!is_array($somet)) {
return false;
}
$addons = array();
foreach ($somet as $addon) {
if ($addon['status'] == 'Active') {
$addons[] = $addon['name'];
}
}
if (count($addons) > 0) {
return $addons;
}
return false;
}
And finally your output (you were calling the function twice):
$result = getActiveAddons($some);
if ($result === false) {
echo "No active addons!";
}
else {
echo implode("<br />", $result);
}

How to use PHP function parameter inside array?

I am trying to declare a function parameter inside my array, but I'm having trouble getting it to work. I've trimmed it down for simplicity purposes, and I have something like:
function taken_value($value, $table, $row, $desc) {
$value = trim($value);
$response = array();
if (!$value) {
$response = array(
'ok' => false,
'msg' => "This can not be blank."
);
} else if (mysql_num_rows(
mysql_query(
"SELECT * FROM $table WHERE $row = '$value'"))) {
$response = array(
'ok' => false,
'msg' => $desc." is already taken."
);
} else {
$response = array(
'ok' => true,
'msg' => ""
);
}
echo json_encode($response);
}
Notice the function parameter $desc trying to be used in the array here:
'msg' => $desc." is already taken.");
The whole function works fine EXCEPT when I try to add the $desc to the array results.
How could this be done?
Do you have an open resource handle to your database? You are not passing one to the query function.

PHP - Adding multiple database entries

I have a function that adds an entry to the database, the code i have at the moment is as follows:
function create_internal_role($rolename, $rolekey)
{
$data = array(
'name' => $rolename,
'key' => $rolekey.'1'
);
if (!is_null($res = $this->ci->internal_roles->create_role($data))) {
return $data;
}
return NULL;
}
What i want to do, is using the same function add another 2 data arrays with a 2 and 3 behind the $rolekey, so that with the one function, it adds 3 lots of data, rolekey1, rolekey2 and rolekey3
How would i go about doing that?
With out knowing about your structure and from the current phrasing of your question, the obvious answer would seem to be this:
function create_internal_role($rolename, $rolekey)
{
$ret = array();
$data = array(
'name' => $rolename,
'key' => $rolekey.'1'
);
if (!is_null($res = $this->ci->internal_roles->create_role($data))) {
$ret[] = $data;
}
$data = array(
'name' => $rolename,
'key' => $rolekey.'2'
);
if (!is_null($res = $this->ci->internal_roles->create_role($data))) {
$ret[] = $data;
}
$data = array(
'name' => $rolename,
'key' => $rolekey.'3'
);
if (!is_null($res = $this->ci->internal_roles->create_role($data))) {
$ret[] = $data;
}
return $ret;
}
If you give more detail in your question, I may be able to give you a better answer.
Perhaps something like this will work:
function create_internal_role($rolename, $rolekey)
{
// define role key indecies
$indexArray = array(1,2,3);
$ret = array(); // return array
// create roles
for(i=0; i<count($indexArray); $i++){
$data = array(
'name' => $rolename,
'key' => $rolekey.indexArray[$i]
);
if (!is_null($res = $this->ci->internal_roles->create_role($data))) {
$ret[] = $data;
}else{
$ret[] = null;
}
}
return $ret;
}

Categories