Using foreach to fetch $_POST elements - php

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.

Related

array defult value insert not proper insert data?

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

cakephp foreach loop only display the first letter from title

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.

get all data from a variable

if i want to see what data is available into a variable, i fetch the data like ---
get(0) to get the first data like 'www.hello.com/23/23', get(1) to get the second data like 'www.mydomain.com/it/12', and so on ....
$apartmentId = $user->getApartment()->get(0)->getId();
Now if there are more data available in the getApartment(), which method i should use to get all the id's which is available in getApartment() !
i Have tried getAll() method which dose not working in this case. Anyone have any idea how to solve this problem. Thanks in advanced .
One way to get all values, is to use a foreach() loop.
<?php
$var = array(
'value_One',
'value_Two',
'value_Three',
'value_Four',
'value_Five'
);
foreach($var AS $value){
echo $value . "<br>";
}
?>

How to manipulate this array?

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

PHP function not running

My page grabs some records from a mssql DB, reads them into an array called $rows, with 3 columns:
ComputerName, Room, time_in_days
From $rows a second array is created (called $graph) with two columns, with the key being time_in_days from $rows, and the value column being a count of how often time_in_days occurred in the first array. The $graph array is used to create a number of HTML divs to give the impression of a graph.
I then want to be able to click on each individual div and using the $key value from the $graphs array, want to look up the rest of the information associated with all records in $rows where $graph[$key] matches $rows['time_in_days'] and display those records on the page.
But I have got stuck! I don't know where to put the function, the if statement(see code below) and at the moment, it doesn't even seem to be running the function. I don't even think I have the function code right. So if you could help with any of that I will very much appreciate it!
This is the code where the divs/graph is created:
foreach($graph as $key => $value){
$width = $value * $factor;
$page .= '<div style="width:40px;display:inline-block;">'.$key.'</div><div class="bar '.$key.'" style="width:'.$width.'px;"></div><div style="display:inline-block;margin-left:2px;">'.$value.'</div><br/>';
}
This is the function so far:
function SearchDays($key, $page, $rows) {
$computers = array();
foreach ($rows as $arr){
if ($key == $arr["time_in_days"]){
$computerName = $arr["ComputerName"];
$computers[$computerName] = $arr;
}
}
$page .= '<p>computers array from function SearchDays(): <pre>'.print_r($computers,true).'</pre></p>';
return;
}
And this is the if statement that makes the if statement that should run the function:
if (isset($_GET['barclick'])){
SearchDays($key, $page, $rows);
}
$page is just a variable that holds everything that is printed out onto the HTML page. The page itself can be seen here: cems.uwe.ac.uk/~s3-gupta/histogram/index.php. The whole page code can be got here: https://www.dropbox.com/s/h2q7x9xxtjbktx9/index.php
Thanks in advance. Please let me know if you need me to clarify things. I try and be as clear as possible on here, but usually don't manage it, so let me know if you need more.

Categories