codeigniter two dimensional superglobal _post - php

As you know We can use something like $_POST['name'][1]
How can we use it in $this->input->post('name') ?
Codeigniter post function isn't two dimensional. I think we can not pass second argument to post function.
Thank you.

It is not supported in Php(<v5.4) just to $this->input->post('name')[0], so there are several ways to get the values:
-use list function:
list($day) = $this->input->post("name");
-use loop, for exampleforeach:
$foreach($this->input->post("name") as $nameData){
echo $nameData;
}
- just to set array into variable and take value:
$name = $this->input->post("name");
echo $name[0];

Related

PHP get user id in url 4

I want to get the id from my url like below
http://localhost/cpanel-ar/stage_one/reports.php?temp=temp_21day
How can GET only the "temp"?
I assume that you mean this:
$getVars = array_keys($_GET);
print_r($getVars);
This will return an array with your get parameters.
for example:
http:example.com?getparameter=getvalue
returns:
Array ( [0] => getparameter )
why you dont try to use substr function?
$data = $_GET['temp'];
echo substr($data,0,4);
its will only catch temp
i dont know if i understood but you can use predefined variables using:
$temp = $_GET['temp'];
but if you are using a framework maybe they already is handle that.
if you want the 'temp' name and you know witch parameters are passed to URL you can use
array_keys($_GET)[i]
Where i i the index of parameter

Explode string into foreach

I have the following GET parameters:
?data=prop_123&data=prop_124&data=prop_125&data=prop_126&data=prop_129&data=prop_127
I was wondering if anybody knew a quick method of getting each value into a foreach? Thanks!
This won't work, because all of your parameters have the same name, so only the value "prop_127" is going to be passed to your script. You can create this as an array instead, like this:
?data[]=prop_123&data[]=prop_124&data[]=prop_125&data[]=prop_126&data[]=prop_129&data[]=prop_127
And then you can use foreach() to loop through them like this:
foreach ( $_GET['data'] as $data ) {
// This loops once for each instance of data[] in your URL
}
If you have no control over the URL, you'd have to read it in manually with $_SERVER['QUERY_STRING'], then parse through it to pull the values out.
Each data will overwrite the previous in $_GET or with parse_str so something like this maybe:
$s = 'data=prop_123&data=prop_124&data=prop_125&data=prop_126&data=prop_129&data=prop_127';
$s = str_replace('=', '[]=', $s);
parse_str($s, $a);
foreach($a as $something) { }
If you are actually trying to put the vars in a GET request and can get them from $_GET, then use the form data[] in the query string and then just foreach over $_GET.
You can use the PHP function: parse_str
http://www.php.net/manual/en/function.parse-str.php

How to set an array in session in codeigniter?

I am little bit struggling in setting array in session.
Here is my code:-
Controller:
function taketest(){
$this->load->model('', '');
$questions_for_test = $this->session->userdata('questions_for_test');
$difficulty_level = $this->session->userdata('difficulty_level');
$question_id = $this->session->userdata('question_id');
if($question_id==""){
$question_id = $this->myjcat->returnRandomQuestion($questions_for_test,$difficulty_level);
$this->session->set_userdata('question_id',$question_id);
}
$question_details = $this->myjcat->getQuestion($question_id);
}
Model:
function returnRandomQuestion($questions_for_test, $difficulty_level){
$question_id = array_rand($questions_for_test[$difficulty_level], 1);
$used_question=array();
$used_questions=$questions_for_test[$difficulty_level][$question_id];
$this->session->set_userdata('used_questions',$used_questions);
return $questions_for_test[$difficulty_level][$question_id];
}
But when I call:
$used_questions = $this->session->userdata('used_questions');
in controller
in the controller it will not return me an array.It gives me last value stored in it.
I could be misreading things, but it looks like you are only storing one value.
// this code:
$used_questions=$questions_for_test[$difficulty_level][$question_id];
$this->session->set_userdata('used_questions',$used_questions);
// is the same as this code
$this->session->set_userdata('used_questions',$questions_for_test[$difficulty_level][$question_id]);
You're probably looking for this:
// fetch the stored copy first.
$used_questions = $this->session->userdata('used_questions');
if(!is_array($used_questions)) $used_questions = array();
// note the []
$used_questions[] = $questions_for_test[$difficulty_level][$question_id];
$this->session->set_userdata('used_questions',$used_questions);
You can set array values in session data like this :-
$this->session->set_userdata('used_questions', json_encode($used_questions));
And retrieve the data as :-
json_decode($this->session->userdata('used_questions'));
If you want the retrieved array data as associative array :-
json_decode($this->session->userdata('used_questions'), true);
Hope it hepls you :)
The problem is that $used_questions is storing the value stored in $questions_for_test[$difficulty_level][$question_id] and not the array.
So do this $this->session->set_userdata('used_questions',$questions_for_test);
This is because the data you pass to the $used_questions is a value.
You might want to do something like this:
array_push($used_questions, $questions_for_test[$difficulty_level][$question_id]);
*appending / adding a new value to an array

db table names as alias in php GET-paramter?

I am looking for a possibility to pass db table name and column name via php and GET parameter.
I have a data grid with following structure:
table_name1.column1, table_name2.column1, table_name2.column1.
There is a search function for the grid, where I need those parameters.
From the url "?table_name1.column1=22" I am getting through the $_GET only table_name1_column1=22
How would you solve that?
Encode the variable with base64 and decode before you use.
I know that's dirty.
But php variables doesn't support periods (dots)
This is a documented feature of PHP. Its basically because PHP cannot have variable names with dots in them.
$x.y = 1 // is an invalid variable name
MANUAL Convert dots to _ in GET & POST
You can use serialize function of php to pass the text "table_name1.column1" in url. But for that you need to do few coding to get the result. Below I have given code which you can use:
Page1.php
<?php
$test = serialize('table_name1.column1=11::table_name2.column2=22');
?>
<a href='Page2.php?qs=<?php echo $test?>'>test</a>
Use this $test to pass as query string. In above example, I am passing on anchor tag click.
Page2.php
use following code to Unserialize the query string and use the values.
<?php
$test2 = unserialize($_GET['qs']);
$ex = explode('::',$test2);
$new_arr = array();
foreach($ex as $val)
{
$ex2 = explode('=',$val);
$new_arr[$ex2[0]] = $ex2[1];
}
echo $new_arr['table_name1.column1']; //print 11
echo $new_arr['table_name2.column2']; //print 22
?>
Hope this will help you :)
You need to seperate each parameter with &.
So: ?table=table_name1&column=column1

PHP populate single array with no keys

I have an indexed array like this:
$indexed = array(0=>2,1=>7,3=>9)
But i need a single array, without indexes, like this:
$notIndexed = array(2,7,9)
Zend_Form does not accept $indexed as parameter for the function populate() for multi checkboxes but works fine with $notIndexed
How can i dynamically transform $indexed to $notIndexed
Thanx for answers
$notIndexed = array_values($indexed);
Are you serious? Use, array_values($indexed).
http://php.net/manual/en/function.array-values.php
$notindex = array_values($indexed)

Categories