Laravel overwrite value in request - laravel

I trouble to overwrite existing request value.
Below example info is my input array and product is my input array key.
e.g,
HTML
<input type="text" name="info[product]" value="10" />
PHP
echo request('info.product');
OUTPUT
10
Edit
print_r(request()->all());
Array
(
[info] => Array
(
[product] => 10
)
[_method] => PUT
[info.product] => 20
)
Question : But now I am required to overwrite this default 10 value to 20 using laravel.

Use the merge() method:
$array['input']['product'] = 20;
request()->merge($array);
Or do this when you'll need the data from request:
$data = request()->all();
$data['input']['product'] = 20;

Try this:
Input::merge(['input.product' => 'new value']);
and don't forget to import Input facade at the top.( use Input;)

Related

how to extract a specific link value (page number) from a pagination in codeigniter?

i would like to extract the current link value from my codeigniter pagination in the controller and put it in a own variable.
the usual pagination code...
$str_links = $this->pagination->create_links();
$data["links"] = explode(' ',$str_links );
with print_r($data["links"]); i printed the array to check what i find inside.
result:
( Array ( [0] => [1] => 1234NextLast › )
( Array ( [0] => Previous1 [1] => 2345NextLast › )
( Array ( [0] => Previous12 [1] => 3456NextLast › ) and so on
i would like to extract the current page number and add it to a different variable.
... [1] => 1
... [1] => 2
... [1] => 3 and so on
You can get current page number from the url:
$this->uri->segment(3);
If it is in the third segment, you can count the segments or i prefer this:
$segment_array = $this->uri->segment_array();
$last_segment = end($segment_array);
And last segment should be your current page number.

Laravel how to update multiple id values

Laravel how to update multiple id valus in table
print_r($test); exit();
Array ( [0] => 185 [1] => 216 )
$report = Test::find($test);
$report->status = "A";
$report->save();
Test::whereIn('id', $test)->update(['status' => "A"]);
Good luck

Codeigniter echo session Multidimensional Array variable

Okay im using codeigniter and i got a session where i want to echo [user_id], i can see its an "Multidimensional Array", but i cant seem to get it right...
This is the print_r from all session userdata:
Array (
[session_id] => b7e721332248
[ip_address] => ::1
[user_agent] => Mozilla/5.0
[last_activity] => 1409104940
[user_data] => [name] => 2 [uniq] => 2
[flexi_auth] => Array ( [user_identifier] => mail.com [user_id] => 2 [admin] => 1
[group] => Array ( [3] => Master Admin )
[privileges] => Array ( )
[logged_in_via_password] => 1
[login_session_token] => 11017021313jhbjh1h2j3b213mab913269d95 ) )
if i type:
print_r($this->session->all_userdata()); // i will get all
echo $this->session->userdata('uniq'); //returns 2
echo $this->session->userdata('user_id'); //returns nothing(THIS IS THE PROBLEM)
Ive tried something like this:
$this->session->userdata(1,1); //1,1 would be array 2 and number 2 which would be user_id
Is it because i can only print out if I loop through them somehow?
You're trying to call the data directly, if you're want to access user_data or flexi_auth you need to assign the data to a variable then target it since they're arrays.
$session_data = $this->session->all_userdata();
echo $session_data['user_data']['uniq']; // 2
$user_data = $this->session->userdata('user_data');
echo $user_data['uniq']; /// 2
$flexi_auth = $this->session->userdata('flexi_auth');
echo $flexi_auth['user_id']; /// 2
If you're trying to target a string directly
echo $this->session->userdata('last_activity');
You can simply access user name stored in session like this
echo $this->session->userdata['user_data']['name'];
Be sure you have some userdata in session or use like this to avoid any errors
echo #$this->session->userdata['user_data']['name'];
adding # does not generate any errors. hope you got the answer you were looking for.

Query error in code igniter ( wrong escape )

I try to insert a row in the table on code igniter from a Array, but something is going wrong.
That's the array:
Array
(
[Date] => 2001-08-15
[Number] => 962883
[Time] => 17:40
[Etc1] => 0
[Etc2] => 0
)
And this the insert:
$this->db->insert('mytable', $myarray);
A new line is inserted, but all columns are empty!
Trying to find de error, I printed the last query by
echo $this->db->last_query() ." <br>";
And I got:
INSERT INTO `mytable` (`Date`, `Number`, `Time`, `Etc1`, `Etc2`)
VALUES
('\02\00\00\01\0-\00\08\0-\01\05\0', '\09\06\02\08\08\03\0', '\01\07\0:\04\00\0', '\00\0', '\00\0')
For some reason I can not get, the codeigniter ( or PHP ) is wrongly escaping the values.
Any Idea?
Firstly your change your array like this:
$data_array = Array
(
'Date'=> 2001-08-15
'Number' => 962883
'Time' => 17:40
'Etc1' => 0
'Etc2' => 0
);
$this->your_model->insert_data($data_array);
and inside your model write function like this
function insert_data($data=array())
{
$this->db->trans_start();
$this->db->insert('your_table_name',$data);
$this->db->trans_complete();
return TRUE;
}
i hope this will solve your problem
try this , this may help.
$this->db->set('Date', '2001-08-15', FALSE);
$this->db->set('Number', '962883', FALSE);
$this->db->set('Time', '17:40', FALSE);
$this->db->set('Etc1', '0', FALSE);
$this->db->set('Etc2', '0', FALSE);
this->db->insert('mytable');
I tried emulating your problem and was able to get the correct SQL statement genrated.
But I am pasting here the code that worked for me:
$data = array
(
'Date' => '2001-08-15',
'Number' => '962883',
'Time' => '17:40',
'Etc1' => '0',
'Etc2' => '0'
);
$this->db->insert('mytable', $data);
Let me know if this works - and if not, what the error message is.

Fetch list data from database in Joomla component

I am creating custom component in joomla and it saves list data in database like these:
["2013-05-04","2013-05-05","2013-05-06","2013-05-07","2013-05-08","2013-05-09","2013-05-10","2013-05-11","2013-05-12"]
Is there any way so that i can fetch these data nicely on module ?
Thanks.
If i got your question you want to show these out put in you module.See the below example:
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
$output = json_decode($json , true);
print_r($output);
?>
Output:
Array
(
[a] => 1
[b] => 2
[c] => 3
[d] => 4
[e] => 5
)
Then you can use this output as below:
<?php
foreach ($output as $value){
echo $value;
}
?>
For more info check the this : http://php.net/manual/en/function.json-decode.php
Hope this helps you.
It is done through json_decode() function.

Resources