I am pulling data from database and would like to save it into session variables. I would like to name the keys the same that my table cells are named. So for example:
I have a cell named "EMAIL", and I would like to get $_SESSION["EMAIL"]
I already have the data from database saved in an array ($data), which has array keys named after the cells, but I would like to move that data to SESSION array, with same keys...
How can I do this dynamically?
You could either do it like this:
foreach($myArr as $k=>$v) {
$_SESSION[$k] = $v;
}
Or,
$_SESSION['user'] = $myArr;
In the first case, you will access email by doing $_SESSION['EMAIL'], and in the second case, $_SESSION['user']['EMAIL'];
Another alternative is
$_SESSION = array_merge($_SESSION, $myArr);
I'm not sure if this is a good practice though.
Related
I need to put a variable within an array.
For example.
$anarray = array("1","3","4");
foreach($anarray as $value){ // do such and such
I need the array() to include a variable instead. To be exact, I need it to contain a variable from my table in my database.
In effect, I would want my array to look like this.
Column 'example' in my users table contains - "1","3","4"
When I rewrite the code to look like this ...
$anarray = array($users['example']);
foreach($anarray as $value){ // do such and such
It doesn't work. What do I need to do to that variable to make it read the exact same as if it were the actual characters?
$anarray=array("1","2","3");
$new_array['example']=array();
for($i=0;$i<count($anarray);$i++){
array_push($new_array['example'],$anarray[$i]);
}
I want to set a value in a Codeigniter session array, something like:
$this->session->userdata['xxx']['yyy'] = $some_value;
But it didn't work. Also, I've tried:
$this->session->sess_write();
But that did not work either for me. Does anyone know how I can assign a value to this kind of session?
You can set multi dimensional array with this example.
$row => array(
'basket_id'=>'1',
'order_id'=>'1',
'cus_id'=>$cusid,
'product_id'=>$pro_id,
'bas_quantity'=>1
)
$cart = array($row);
$this->session->set_userdata('cart',$cart);
You can set multi-dimensional array as below.
$this->session->set_userdata(array("index_1"=>array("val1","val2","val3")));
Then you can use
$sess_data = $this->session->userdata("index_1");
echo $sess_data[0];
echo $sess_data[1];
echo $sess_data[2];
You cannot directly unset specific value of multidimensional array in session of codeingiter. You have to take all session value to variable. Unset the specific value and again set the variable in session as shown below:
$items_session = $this->session->userdata('item_names');
unset($items_session[$key][$app_no]);
$this->session->set_userdata('item_names',$items_session);
Hope this will help.
I am using values to be submitted to the next page through post method. The input type fields have dynamic names which are created like this name="fob-$id". Now when submitted through post, there are 4 - 5 variables such as fob-89, fob-29, fob-65 etc...
How can i assign these values to a new variable ???
NOTE: I know what numbers will be attached with the fob, so it will not be a problem, the only problem i am facing is about how to assign these values to a variable..
$fob=$_POST['fob-$id'];
Above code is not working.
Thanks
The best way would be to name your HTML fields so that they create an array when sent back to PHP:
echo "<input type='text' name='fob[$id]' />";
And then in PHP you can just iterate over $_POST['fob']:
foreach ($_POST['fob'] as $id => $value) {
// Do stuff
}
As per the comment updates, assuming $id is an integer gotten from the $_SESSION var, you can store it to an array:
$fob[$id] = $_POST['fob-' . $id];
You should use an array on the inputs name instead. Like this: name="fob[$id]"
Then you can simple do a foreach($_POST["fob"] as $id => $value) to get the values
I'm currently using the following format to save a value from an HTML form $item_name=$_POST['item_name'];
This saves the value, but how to I also save the name attribute in a variable?
Thanks in advance!
Assuming you want to store each element of $_POST variable as a key-value pair, then you can try:
$var = array();
foreach($_POST as $key => $val) {
$var[$key] = $val;
}
I'm saving a lot of values and want to avoid typing each one out.
Please, make your mind first.
Global variables are intended to be typed by hand.
If you want some automated processing - just keep them in a form of array.
Looks like rdt.exe's answer is what you're looking for.
maye you noticed you have to use the name to access the $_POST-array and get the value. if you want to store the name in a variable, too, just do:
$item_name_name = 'item_name';
$item_name_value = $_POST[$item_name_name];
you could also use some kind of loop to dynamically create variables with the according names like this:
foreach( $_POST as $name => $value ){
$$name = $value;
}
both ways are some kind of unnecessary and useless in my opinion, but you havn't stated what exactly you're trying to achive - so maybe this helps.
An alternative approach:
$keysarray = array_keys ( $_POST);
print_r( $keysarray);
This will give you all the keys in array
The function you are looking for is called extract.
This will create variables for all the $key=>$val pairs in the array.
$_EXAMPLE = array('bird' => 'chicken', 'dog' => 'greyhound');
export($_EXAMPLE);
echo $bird; # prints "chicken"
echo $dog; # prints "greyhound"
Watch out though - this is a huge security risk. So are the solutions described in some of the other answers.
The problem with doing something like this is that a user can tamper with the POST data, and set parameters other than the ones she is supposed to set. If they set variables that are actually variable names in your application, those variables can be overwritten.
$is_admin = false;
$_EXAMPLE = array('bird' => 'chicken', 'dog' => 'greyhound', 'is_admin' => 'true');
export($_EXAMPLE);
if ($is_admin) { # this will now evaluate to true.
# do sensitive stuff...
}
I have a table with a whole bunch of fields and records in it (40+fields).
I use my MySQL result like so:
<?php
$field1 = $row['field1'];
$field2 = $row['field2'];
$field3 = $row['field3'];
?>
As you see, the variable name is the same as the field name.
How can I do this for all fields, without having to type em all out? Would be really awesome for when I add more fields!
I know about the eval function, however I am not sure of how to use it in this case.
Also, how can I generate an array with the results? Like so:
<?php
$arr = array(
'field1'=>$row['field1']
);
?>
You can use extract():
extract($row);
However, take great care you don't pollute your namespace with this function. Say you already have a variable named $car, and you have a field in your database called car. You could be unwittingly overwriting your existing variables!
It may be safer, then, to use:
extract($row, EXTR_SKIP); // don't extract variables that already exist in the namespace
EDIT: In regards to your edit, you don't need to create an array for the results... your $row array is the result. If you construct the array as in your edit, $arr['field1'] = $row['field1'], so why not bypass the construction of this array altogether and just use the original $row?
The extract() function does exactly this:
extract($row);
By default it will override any existing variable (e.g. if you already have a field1 variable, it will override it). You can disable this by passing EXTR_SKIP as second parameter:
extract($row, EXTR_SKIP);
Or you can prefix all variables:
extract($riw, EXTR_PREFIX_ALL, 'row');
Also, how can I generate an array with the results?
$arr = $row;