Laravel database implode function - php

I have inserted values into database for multiple selection like this somevalue1, somevalue2, somevalue3 in Laravel using implode function.
Controller.php
$project_name=$request->project_name;
$project_id=$request->project_id;
$client_id=$request->client_id;
$project_status=$request->project_status;
$approver = implode(",",Input::get(array('approver')));
I want value in separately not like somevalue1, somevalue2. How can I solved this?

You can use json_encode during saving to database.
$save_approver = json_encode($request->approver);
and json_decode when retrieving from database.
$get_approver = json_decode($your_variable, TRUE);
foreach($get_approver as $value){
// operations
}

Related

How to retrieve data from a database as an array in Laravel?

Normally data retrieved as an object from database in Laravel when we use get() function. But I want to retrieved data as an array.
In CodeIgnitor we use get_array(). What we use in Laravel ?
I have already tried with toArray(). But no result.
$doctor_result = Doctor::select('*')->toArray();
How to solve that?
I got result. just change the code
$doctor_result = Doctor::all()->toArray();
You can also do like this.
$result = DB::table('tableName')->get();
$resultArray = $result->toArray();
second way, but some tricky.
$resultArray = json_decode(json_encode($result), true);

How to get only JSON object values from a JSON array in LARAVEL 5.2

My database query is below.
$beneficiary_id = DB::select('select Telephone from item ');
This returns a json array looks like this
[{"Telephone":"0111222333"},{"Telephone":"0112211223"},{"Telephone":"012345557"},{"Telephone":"0225545455"}]
For another database insertion operation I need only the telephone numbers.
I have used json_decode() function, it is working only if we enter the array manually.
How to get only those values to another array?
Thanks in advance.
Use the pluck function
If you would like to retrieve an array containing the values of a single column, you may use the pluck method.
$titles = DB::table('roles')->pluck('title');
foreach ($titles as $title) {
echo $title;
}
use $beneficiary_id->column_name to get the value from your object.

How to insert json array into mysql database with php

Hi I'm trying to insert the json array into my MySQL database.With array json data from android client.
[{"name":"peter","phone":"dsf","city":"sdfsdf","email":"dsf"},{"name":"111","phone":"222","city":"hn","email":"1#yahoo.com"}]
If you want to store the array as a string, you can use JSON.stringify():
$string = [{"name":"peter","phone":"dsf","city":"sdfsdf","email":"dsf"},{"name":"111","phone":"222","city":"hn","email":"1#yahoo.com"}];
$json = JSON.stringify($string);
The variable $json is then a simple string which can be inserted into MySQL easily.
You can then use:
var obj = JSON.parse($json);
To convert the string back to an array.
This method usually isn't recommended for performance reasons though, so you might alternatively want to break up the array and store each field individually.
Try this:
$json = serialize(json_array);
Use $jsonArray = json_decode($jsonStr);.
Then iterate the array as you want to save data in your mysql database.
you can use - serialize()
$json = '[{"name":"peter","phone":"dsf","city":"sdfsdf","email":"dsf"},{"name":"111","phone":"222","city":"hn","email":"1#yahoo.com"}]';
$newJson = serialize(json_decode($json));
$newJson is ready to be inserted. and after fetching -
$data = unserialize($fetchedData); and then json_encode($data);

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

How to store MySql values as Multidimensional Array using PHP

I have a database table as follows.
<table border='1'><th>Id</th><th>FirstName</th><th>last Name</th><tr><td>1</td><td>Tom</td><td>T</td></tr><tr><td>2</td><td>Jerry</td><td>J</td></tr></table>
I would like to store all values as a multi dimensional array using php(using a while loop to retrieve fields).That is,
I would like the data to be echoed as:
array(array(1,Tom,T),array(2,Jerry,J));
$result = mysql_query("SELECT * FROM tablename;");
while($result_ar = mysql_fetch_array($result)) {
$multid_array[] = $result_ar;
}
after which $multid_array will be an array of arrays.
You can use phps serialize function to convert any variable into a string representation
$string = serialize($dbData);
You can use unserialize() to convert the string back into arrays, objects, etc
$dbData = unserialize($string);
Once you have the data within a string, it's very easy to store in a file, db, etc. A disadvantage is that you won't be able to search your database easily for this data.

Categories