I am using CodeIgniter insert array value issue first array value insert fine but multiple value not insert proper so please share valuable idea sir. i am share all issue code here...
CodeIgniter models
public function savemedicine()
{
$db2 = $this->load->database('dpr',TRUE);
$medicine_typer = array("CAB", "TAB", "CAB","CAB");
$pricer = array("202", "100", "97","92");
$quantityr = array("2","2","1","3");
foreach ($item_namer as $key => $value){
//print_r($medicine_typer[$key]);//output CABA
//print_r($quantityr[$key]);//output 2
//print_r($pricer[$key]);//output 2020
$medicine_typer =$medicine_typer[$key];
$quantityr =$quantityr[$key];
$pricer =$pricer[$key];
$db2->query('INSERT INTO dpr_medicine_return(medicine_type,quantity,price)
VALUES ("'.$medicine_typer.'","'.$quantityr.'","'.$pricer.'")');
$i++;
}
}
you are revaluing variable in the loop so when the loop run for 2 time the
time it didn't find those array variable but now they are not array
use this code
$medicine_typer2 =$medicine_typer[$key];
$quantityr2 =$quantityr[$key];
$pricer2 =$pricer[$key];
$itemidr2 =$item_idr[$key];
now use these variable in your query
Related
I have a multi-select option and a function which i want it to loop through the array and pick one value and save in database one at a time, but now its saving the last item in the database. e.g. if someone selects five items at one the records entered in DB should be 5 records
Code
$kycs = json_decode($input['document_type_ids']);
foreach($kycs as $key => $kyc){
$input['kyc_type'] = CheckList::find($kyc)->slug;
$filledDocument->payload = json_encode($input);
$filledDocument->save();
}
i have used the both version of foreach i.e.
foreach($kycs as $kyc) && oreach($kycs as $key => $kyc)
where am i going wrong....thanks in advance
i did manage to get help from laracast...
foreach($kycs as $key => $kyc){
$filledDocument = new ClientFilledInvestmentApplicationDocument();
}
i have to create an instance of the $fileDocument within the loop...
https://laracasts.com/discuss/channels/eloquent/save-method-only-writing-last-record-in-foreach-loop?page=1
You need to create the instance of $filledDocument inside the foreach loop, for example:
foreach($kycs as $key => $kyc){
$input['kyc_type'] = CheckList::find($kyc)->slug;
$filledDocument = new FilledDocument(); // replace with your model here
$filledDocument->payload = json_encode($input);
$filledDocument->save();
}
i want to display data from database and also i have created function in model file which is showing data from database but all values are shown in the array format.
problem is that when i print echo $values['title']; in foreach loop it is showing only first letter from title array??
model code
function reviewcitypage()
{
$cacheKey = 'city_page';
GigaCache::set(array('duration'=>"+1 minutes",'path'=>CACHE));
$cachedCategoryData = GigaCache::read($cacheKey);
if($cachedCategoryData && !cr('DynamicPage.field'))
{
$recentactivity = $cachedCategoryData;
}else
{
$recentactivity= $this->find("list",array("conditions"=>array("status"=>1),'fields'=>array('title','body','rating'),'recursive'=>-1,'limit'=>10));
//dont't set cache if dynamic field
if(!cr('DynamicPage.field'))
{
GigaCache::set(array('duration'=>"+1 minutes",'path'=>CACHE));
GigaCache::write($cacheKey,$recentactivity);
}
}
return $recentactivity;
}
view file
$ReviewObj = cri('Review');
$recentactivity = $ReviewObj->reviewcitypage();
foreach ($recentactivity as $name => $value){
foreach($value as $values)
{
echo $values['title'];
}
}
**problem is solved now thanks for support **
i have changed the code in model file and it is woking now
$recentactivity= $this-
>find("all",array("conditions"=>array("status"=>1),'recursive'=>-1,
'limit'=>10));
Your find() query is preparing the data as a 'list'. in cake lists are always key => value pair arrays. so in your view when you use the second foreach loop you are saying foreach character in a string...do.....
in your example $value can only be a string. foreaching it can only make $values a single char.
Let me know if you still unsure what i mean. not the best at explaining what i mean
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-list
Because you are after 3 fields I suggest using either first or all in place of list as the first argument in the find() method.
I am a beginner in PHP. I am trying to make an operation in this array. I want to insert this array in my database like on to many in a table.But before the insertion i have to modify the array values.
this is my array.
$services=[0=>('id_e'=>91701,'id_s'=03),
1=>('id_e'=>'','id_s'=>01),
2=>('id_e'=>'','id_s'=>02)
];
It has to become like as follow.
$services=[0=>('id_e'=>91701,'id_s'=>03),
1=>('id_e'=>'91701','id_s'=>01),
2=>('id_e'=>'91701','id_s'=>02)
];
And then i want insert into the database. Any idea please?
Try this:
$id_e = null;
foreach ($services as &$row) {
if ($row['id_e']) $id_e = $row['id_e'];
else $row['id_e'] = $id_e;
}
unset($row);
demo
I am getting a value from an input box in array. I want to post all this into a database with a single insert query so that all relevent data are inserted together in MySQL
Here is my code
foreach ($_POST['stop'] as $stopIndex => $stopValue) {
echo $stopname=$stopValue;
}
foreach ($_POST['timing'] as $timingIndex => $timingValue) {
}
foreach ($_POST['ampm'] as $ampmIndex => $ampmValue) {
}
Can anyone help me with correct code where to write insert query
This is example if you use codeigniter's way of fetching data.
Just prepare one array with appropriate keys (relevant to database keys you defined) and import it with the update_batch CI db method.
This is actually method like the one you can use in your model.
function save_something() {
$post_data = $this->input->post();
foreach ($post_data as $key => $value) {
$settings[] = array( 'key' => $key, 'value' => $value);
}
}
$this->db->update_batch('settings',$settings,'key');
return true;
}
Yes sure.
Assumed that you have something like the below.
<input type="text" name="variable1['key1']">
<input type="text" name="variable1['key2']">
Here variable1 is an array. When you submit, instead of using "for" OR "foreach" serialize it directly as:
$s = serialize($_POST['variable1']);
echo $s;
Your $s will be something like this:
"a:2:{s:6:"'key1'";s:6:"value1";s:5:"'bcd'";s:6:"value2";}"
The $s can be stored in a single column in the database (type can be text in DB).
While retrieving you can unserialize that column which converts back to array and you can use it easily.
Advantages, less number of records in DB, and in your front end you there is no need to use
looping statements.
UPDATED - TO ADD INTO DATABASE:
foreach ($_POST as $key)
{
$s = serialize($key);
mysqli_query("INSERT INTO `test`.`abc` (`id` ,`data`) VALUES (NULL , $s)");
}
Serialize value acts as a string, so the resultant will be number of records == number of arrays.
Im trying to fetch my form date in one for loop and then put the data into the database. My problem is that i have'nt worked that much with PHP myself and i cant figgure out how to put the data into the database.
Here is the code
<?php
function get_post_information() {
$inf[] = array();
foreach($_POST as $field => $value) {
$inf[$field] = $value;
}
return $inf;
}
I execute the code like this:
get_post_information();
But then what? How do i get this into my database?
Hope anyone can help :)
If you want to store the complete array in one single database column, you should use the serialize() function to make a string from your array. There also is an unserialize() function to convert it to an array again.