How will i pass the value to the following format? - php

I am trying to pass the value to a function to get the following url:
search?channelIds=1,2,3&streamName=wew&page=0&size=10
code inside function:
$this->request->set("url","broadcasts/pubic/search");
$this->request->set_fields(array(
'channelIds'=>$channel_ids,
'streamName'=>$stream_name,
'page'=>$page,
'size'=>$size
));
$result = $this->request->send();
Here i pass the values in this format.How will give pass value for channelIds=1,2,3? i use http_build_query for url formation. $channel_ids=array("channelIds"=>1, "channelIds"=>2, "channelIds"=>3); $stream_name="wew"; $page=0; $size=10; $getlivebroadcast = $api-searchBroadcast($channel_ids,$stream_name,$page,$size);
How will i set the value?

You can use the urlencode function to encode the channel ids. Your function may look like:
$this->request->set("url","broadcasts/pubic/search");
$this->request->set_fields(array(
'channelIds'=>urlencode($channel_ids),
'streamName'=>$stream_name,
'page'=>$page,
'size'=>$size
));
$result = $this->request->send();

Related

Getting value from angularjs scope object

I have the following angularjs code for posting value to php:
$scope.aToUn = function(aName) {
//$scope.question.question_16 =UnionsId;
var outputType=[{type:'aName',action:'none',PassId:aName}];
$http.post('addressManagement.php',outputType).success(function(dataJsonUnions) {
//alert(dataJsonUnions);
$scope.question =dataJsonUnions;
//question or dataJsonUnions is an object that passed from json,
//something like question={{id="2",questionvalue="Name"}}
//calling another function
$scope.anotherFunction(questionvalue); // here i want to pass only value that from json eg. 'Name' only)
});
};
I am getting json object value, which i want to do something with that only single value. (object contain only one single value, dont want if/else or foreach)
Did you try
$scope.anotherFunction($scope.question[0].questionvalue);
As i understand your question, You first need to conver json response to object by using angular.fromJson() and then pass object's property for example:
$scope.question = angular.fromJson(dataJsonUnions);
then call function with argument that you want to pass:
$scope.anotherFunction($scope.question.questionvalue);
hope this helps you.
If the response is a JSON object and if you want to access the value of field 'questionValue' then you can try the below.
var json = JSON.parse(dataJsonUnions);
json.questionValue; // THis will fetch you the value
//OR
json['questionValue'];
// Then you can pass this value to any function that you need

Get the value from URL

In this URL: www.example.com/transaction/summary/10 I can get 10 by this:
$this->uri->segment(3);
But, along with some other GET params how can I get that value?
e.g.: www.example.com/transaction/summary?local_branch=1/10
PS: GET params could be more than 1.
If you still want to keep CI style than add segments:
URL:
www.example.com/transaction/summary/10/1/TEST
Segments:
$this->uri->segment(3); // 10
$this->uri->segment(4); // 1
$this->uri->segment(5); // TEST
Or if you want to use query string than you can add params in query string and get values by using $_GET:
www.example.com/transaction/summary/10/?local_branch=1&test=test
Ok, segments in Codeigniter are the content between slashs like mysite.com/page/page-name. To get the page-name value I get the second segment, or $this->uri->segment(1). To get the strings passed by query string ($_GET), you can simply use the $_GET['local_branch'] as in your example or in case of Codeigniter, as the #Tom answer: $this->input->get('local_branch').
In this case, you can to explode the values delimiting by /.
$localBranch = $_GET['local_branch'];
// Or
$localBranch = $this->input->get('local_branch');
// And split using the / as delimiter if is need.
$localBranch = explode('/', $localBranch);
// Output
// Array(0 => 1, 1 => 10)
This way many values can be passed in same query string.
To get these GET parameters you can just use:
$this->input->get('some_variable', TRUE);
As per the SO link here.
example.com/?some_variable=hey

How to get params from encoded current URL?

Encoded URL
www.example.com/apps/?bmFtZTE9QUhTRU4mbmFtZTI9TUFFREEmcGVyY2VudD02NQ
Decoded URL
www.example.com/apps/?name1=AHSEN&name2=MAEDA&percent=65
now i want to get params from encoded URL
ob_start();
$name1 = $_GET['name1'];
You probably have the decoded url somewhere in a variable. If you extract the query part from it (the part after the ?), you can feed that query string to the function parse_str.
If you use parse_str with just the query string as argument, it sets the appropriate variables automatically. For example
// whereever you get the query part of your decoded url from
$my_query_string = "name1=AHSEN&name2=MAEDA&percent=65";
// feed it into parse_str
parse_str($my_query_string);
would set the global variables
$name1
$name2
$percent
But I'd advise to make use of the second parameter that parse_str offers, because it provides you with better control. For that, provide parse_str with an array as second parameter. Then the function will set the variables from your query string as entries in that array, analogous to what you know from $_GET.
// whereever you get the query part of your decoded url from
$my_query_string = "name1=AHSEN&name2=MAEDA&percent=65";
// provide parse_str with the query string *and* an array you want the results in
parse_str($my_query_string, $query_vars);
echo $query_vars['name1']; // should print out "AHSEN"
UPDATE: To split your complete decoded url, you can use parse_url.
You can use the function urldecode.
This should get you started...
<?php
$query = "my=apples&are=green+and+red";
foreach (explode('&', $query) as $chunk) {
$param = explode("=", $chunk);
if ($param) {
printf("Value for parameter \"%s\" is \"%s\"<br/>\n",
urldecode($param[0]), urldecode($param[1]));
}
}
?>

json_decode on serialized form Symfony2

I submit my form by ajax, here what I gets in controller:
$request->getContent()
return
string 'comment[header]=vcvdfgdfg&comment[body]=dfgfdgdf&comment[_token]=nV0QYu82KWFb-wRIlIoY4MKM6-WUfeFoMidjBHfpupA' (length=120)
when I try
json_decode($request->getContent(), true) // it equal to null
What I am doing wron?
That's not a json string. If you want to parse that string and get an array you have to use the parse_str function, and you need to set the second parameter to have the data put in an array instead of into individual variables.
$get_string = "pg_id=2&parent_id=2&document&video";
parse_str($get_string, $get_array);
print_r($get_array);
Or, if you're using Symfony2 you can access them this way:
// $_GET parameters
$request->query->get('name');
// $_POST parameters
$request->request->get('name');

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

Categories