i seem to be stuck at this problem for quiet a few time, basically i have used cookie in codeigniter, and passed an array with different names to different functions, the code to set the cookie is
$data = array (
'client_block_ID' => $client_block_ID,
'client_unit_ID' => $client_unit_ID,
'blockUnits' => $blockUnits
);
$cookieName ='tab'.$counter;
$cookie = array(
'name' => $cookieName,
'value' => $data,
'expire' => '86500',
);
$this->input->set_cookie($cookie);
now i just dont know how to get the variables inside the array i.e what will be the syntax to get client_block_ID??
Now your cookie array will look like this.........
$cookie = array(
'name' => $cookieName,
'value' => array(
'client_block_ID' => $client_block_ID,
'client_unit_ID' => $client_unit_ID,
'blockUnits' => $blockUnits
);
'expire' => '86500',
);
So to get your client_block_ID from your $cookie array you have to loop through that array like below.
foreach($cookie as $c)
{
echo $c['name'];
foreach($c['value'] as $v)
{
echo $v['client_block_ID'];
}
}
You did not expect array as value;
I did and I see:
A PHP Error was encountered
Severity: Warning
Message: setcookie() expects parameter 2 to be string, array given
Filename: core/Input.php
Line Number: 404
use
$this->input->cookie()
Lets you fetch a cookie. The first parameter will contain the name of the cookie you are looking for (including any prefixes)
The function returns FALSE (boolean) if the item you are attempting to retrieve does not exist.
try this,
$cookievalue= $this->input->cookie('value');
if($cookievalue){
//cookie exists
foreach($cookievalue as $cookie){
echo $cookie['client_block_ID'];
}
}else{
//cookie doesnot exists
}
Related
I have an array..
$file=array(
'uid' => '52',
'guarantee_id' => '1116',
'file_id' => '8',
'file_category' => 'test',
'meta' =>'{"name":"IMAG0161.jpg","type":"image\/jpeg","tmp_name":"\/tmp\/phpzdiaXV","error":0,"size":1749244}',
'FileStorage' => array()
)
and I am trying to extract the name using
$fileName = $file['meta'['name'];
which gives me a Illegal string offset 'name' error.
The value of $file['meta'] is a string, not an array. That means your approach to access the value does not work.
It looks like that meta value string is a json encoded object. If so you can decode it and then access the property "name" of the resulting object.
Take a look at this example:
<?php
$file = [
'uid' => '52',
'guarantee_id' => '1116',
'file_id' => '8',
'file_category' => 'test',
'meta' =>'{"name":"IMAG0161.jpg","type":"image\/jpeg","tmp_name":"\/tmp\/phpzdiaXV","error":0,"size":1749244}',
'FileStorage' => []
];
$fileMeta = json_decode($file['meta']);
var_dump($fileMeta->name);
The output obviously is:
string(12) "IMAG0161.jpg"
In newer version of PHP you can simplify this: you do not have to store the decoded object in an explicit variable but can directly access the property:
json_decode($file['meta'])->name
The output of this obviously is the same as above.
This is happening because your meta is a json, so you should decode and then access whatever you need, not that I placed true as second parameter becuase i wanted to decode as an associative array instead of an object
$decoded = json_decode($file['meta'],true);
echo $decoded['name'];
//print IMAG0161.jpg
You can check a live demo here
But you can easily access as an obect
$decoded = json_decode($file['meta']);
echo $decoded->name;
//print IMAG0161.jpg
You can check a live demo here
<?php
$file=array(
'uid' => '52',
'guarantee_id' => '1116',
'file_id' => '8',
'file_category' => 'test',
'meta' =>'{"name":"IMAG0161.jpg","type":"image\/jpeg","tmp_name":"\/tmp\/phpzdiaXV","error":0,"size":1749244}',
'FileStorage' => array()
);
$meta=$file['meta'];
$json=json_decode($meta);
echo $json->name;
?>
Trying to make $emo_word a global variable so I can call it in another function. But whenever I put global $emo_word, it gives me the error below. If I remove global, it is OK, but I want to call the global, what can I do?
Warning: Illegal string offset 'cat_id' in /home2/898992on708/public_html/mysitel.com/wp-content/plugins/gallery/gallery.php on line 212
<?php
function name_1(){
global $emo_word;
$emo_word = array(
1 => array(
'word' => 'LOL !',
'id_name' => 'lol',
'cat_id' => '1'
),
2 => array(
'word' => 'COOL !',
'id_name' => 'cool',
'cat_id' => '2'
)
);
foreach($emo_word as $word){
$word['cat_id'];
}
}
?>
Your code is valid but the error is telling you that there is a value in $emo_word that isn't an array but is instead a string.
If you were to run var_dump($emo_word); where you get that error you will see.
Essentially when you are accessing the cat_id index something like this is happening:
$emo_word = array(
1 => array('cat_id' => 1, 'word' => 'LOL'),
2 => 'string',
);
In the loop when it evaluates $word['cat_id'] on the string value it throws the warning Illegal string offset.
Check your array to make sure it is contains what you expect.
This is driving me mad.. I have a PHP script that returns an array in the form $key => $value and I want to rename the key so that I can display it in a table header. I saw there are several ways of doing this but I'm not sure they are what I need... Either that or I haven't understood the examples correctly which is the likely problem.
Basically my array keys differ each time I iterate over a foreach loop and also some can be blank. How can I get round this?
The first output might look like this:
'_can_chaccess' => false,
'_can_chown' => false,
'_can_delete' => false,
'_can_modify' => false,
'_can_read' => true,
'assigned_to_name_879' => 'Unassigned',
'id' => 1,
'type' => 'Private::Reporting::DataViewModel::DataView_223_42858',
'type_877' => 'Email',
The next run through, I might get this:
'_can_chaccess' => false,
'_can_chown' => false,
'_can_delete' => false,
'_can_modify' => false,
'_can_read' => true,
'assigned_to_name_793' => 'Consultants',
'id' => 1,
'object_reference_794' => 'CASE-1004',
'summary_795' => 'Deployment of New System for HQ (Project)',
'type' => 'Private::Reporting::DataViewModel::DataView_200_42858',
),
As you can see, some keys rename the same e.g. id, type. But the most important ones that I am interested in change each time e.g. Assigned To Name.
Any ideas?
Where do you receive your data from?
You can either somehow modify the source of your data, so if it were a query (what I do not assume here), you have the SELECT ... AS ... statement.
First you do need to know how to interpret the changing keys. If e.g. "assigned_to_name_879" and "assigned_to_name_793" is the same field, you can define a canonical function, which mapps both inputs to a unique output.
The output of the cannonical function and as well the other array keys can serve as keys for an additional array, which contains the table headers of your output.
So your current array is the value's array, and by hand you define a header's array:
array(
'assigned_to_name_879' => 'Name assignment'
);
This dynamic way of storing the table headers in an array only makes sense if you are using the array twice. Otherwise you could simply write the header in the html-code which you do output.
I've managed to figure it out using the below:
$mappings_array = array();
foreach ($report['data'][0] as $key => $value) {
$workbooks->log('Old Key', $key);
preg_match_all('([^_\d]+)', $key, $new_key);
$workbooks->log('New Key', $new_key);
$str = implode(" ", $new_key[0]);
$capitalised = ucwords($str);
array_push($mappings_array,$capitalised);
}
Maybe it's not the best solution but it works :) I get the following output:
> New array: «array (
0 => 'Can Chaccess',
1 => 'Can Chown',
2 => 'Can Delete',
3 => 'Can Modify',
4 => 'Can Read',
5 => 'Id',
6 => 'Total Type',
7 => 'Type',
8 => 'Type',
)
I am able to set the viewVars for a single record and mail it successfully. A problem occurs when I want to send an email containing more than one record. I find the correct records and I am able to pass them to my mail function. The problem comes in that when I debug the array passed to the mail template, I get a
Notice (8): Undefined variable: vars [APP\View\Emails\html\latest_projects.ctp, line 1]
However, just below the error, it does show me the information I want:
(int) 0 => array(
'Project' => array(
'id' => '809',
'created' => '2014-04-23',
'project_number' => 'SPN00000809',
)
),
(int) 1 => array(
'Project' => array(
'id' => '810',
'created' => '2014-04-23',
'project_number' => 'SPN00000810',
)
)
*Certain fields omitted for brevity.
How do I loop through this array in the email template? I have tried the standard foreach loop as you would in the view, but then I get the undefined variable supplied foreach problem. Any advice or suggestions?
//Pass your variable
$Email->viewVars(array('projects' => $projects));
//In your email body or template
<ul>
<?php foreach ($projects as $project) { ?>
<li><?php echo $project['Project']['project_number']; ?></li>
<?php } ?>
</ul>
like said in documentation :-
$Email->viewVars(array('value' => 12345));
you will be able to use it as $value in mail template.
just like that set your array to 'value' you will be able to use $value as array.
The problem was that the array passed, $dataForView, which is generated by cake, was a combination(?) array - meaning that some keys were associative such as $dataForView['content'] => '', while the other keys were (int)0 => array();
The array received looked as such:
array(
content => '',
(int) 0 => array(
Project => array(
fieldName1 => value,
fieldName2 => value
)
),
(int) 1 => array(
Project => array(
fieldName1 => value,
fieldName2 => value
)
)
)
I found that if I unset the associative keys (content) I am able to loop through the normalized array as per usual. I did it in this way, it might not be the best way, but it works.
//remove associative key
unset($dataForView['content']);
//loop through array and output values
foreach($dataForView as $key=>$val):
echo $val['Project']['id']; //echo other info as well
endforeach;
debug($dataForView);
Thank you all for helping.
I cannot seem to get CI's session library to function the way I want it to. Essentially, I am storing 2 different categories of data within the sessions. The data within the 2 categories may contain the same value. Right now my attempt to add a key => value pair to the session is failing, as it is only allowing 1 key => value pair to be associated with the array. It overrides itself each time I do a post.
$arr = array(
'favorite_products' => array(),
'viewed_products' => array()
);
$arr["favorite_products"][] = $fav_id;
$this->session->set_userdata($arr);
This is what the array looks when I print_r it:
Array ( [favorite_products] => Array ( [4f1066c2b7fff] => 1648406 ) [viewed_products] => Array ( ))
Am I doing something wrong, or is this just the way CI's session library works?
Make sure you are destroying your session between attempts, but this code should work just fine...
$arr = array(
'favorite_products' => array(),
'viewed_products' => array()
);
$arr["favorite_products"][] = $fav_id;
$arr["favorite_products"][] = 033333; // another id
$this->session->set_userdata($arr);
should give you...
Array (
[favorite_products] => Array (
[0] => 1648406,
[1] => 033333
),
[viewed_products] => Array ()
)
If you are trying to do this between requests...
// if it doesn't already exist in the session, create an empty array.
if( ! ($favorite_products = $this->session->get_userdata("favorite_products")))
{
$favorite_products = array();
}
$favorite_products[] = "new id or info";
$this->session->set_userdata("favorite_products", $favorite_products);