How to create an array and save them into database? - php

Based on the code below, how would I create an array and then save the arrays to the database?
<?php
$all_userid = '';
if (isset($_POST['userid'])) {
foreach($_POST['userid'] as $userid) {
// Add filtering here
$all_userid .= $userid;
}
}
echo $all_userid;
?>
Output from echo;
003210032100321

Use json_encode http://www.php.net/json_encode
$string = json_encode($array);
Then when retriving it from database, use json_decode http://php.net/json_decode

<?php
$all_statuses = '';
if (isset($_POST['status'])) {
foreach($_POST['status'] as $status) {
// Add filtering here
$all_statuses .= $status;
}
}
echo($all_statuses);
To Save in database serialize this array
serialize($all_statuses)
?>

you can implode them with an - or / or _ and save them.Like
$i = 0;
$cnt = count($_POST['status']);
foreach($_POST['status'] as $status) {
// Add filtering here
$all_statuses .= $status;
if($i++ < $cnt) {
$all_statuses .= '_';
}
}
echo $all_statuses;
Will gives you like
id1_id2_id3_id4....
And while retriving you simply explode them with the imploded string,you will get all the result in form of an array.

Related

Decode the array values

I want to decode array values for passing it into json. I want to pass the values in contentvalue baaed on content type into json. Now It shows null. I want to move array value as $zip_num=$content->zip; based on content type.
while ($ee = mysql_fetch_array($query)) {
$key_val = $ee['CONTENT_TYPE'];
$content = json_decode($ee['CONTENT_VALUE']);
if ($key_val == 'stat_sum') {
$stat = $content;
}
if ($key_val == 'zip_stats') {
$zip[] = $content;
$zip_num=$content->zip;
$zip_cou=$content->count;
}
if ($key_val == 'qual_stats') {
$qual[] = $content;
}
}
$new = array('ID'=>$id,'zip'=>$zip_num);
echo $json = json_encode($new);
}
Replace $key_val (that one which is in if statement) with $ee['CONTENT_TYPE']

php fetch multiple array and serialize array from mysql database

I have this table for insert files data :
|id|url|author|post_id|post_type|timestamp|
In url field i insert PHP serialize data and fetch with this function:
function _is_files_list_($id,$type){
$files = mysqli::fetch("SELECT * FROM " . NEWS_FILES . " WHERE news_id = ? AND type = ? ",$id , $type );
if (count($files) > 0) {
$list_files = unserialize($files[0]['url']);
$count = count($list_files[0]);
for($i=0; $i<$count; $i++){
$files_show[] = array($list_files[0][$i]);
}
return $files_show;
}
else
{
return FALSE;
}
}
and result :
if(($list = _is_files_list_($id,"download")) !== false){
foreach ($list as $key => $data) {
echo urldecode($data[0]);
}
}
This worked for me and print/show url field form table but I need to show other table field ie author or timestamp using this function.
How can I generate this ?
did you tried something like?
foreach ($list as $key => $data) {
echo $data[key]['url'];
echo $data[key]['author'];
echo $data[key]['timestamp '];
}

how do i create post values as a session array?

How do I create post values as a session array passing session to another pages? I'm working on a code it is showing only Array on the other pages when i echo $_SESSSION['size'];?
Here My Both Functions:
$size = array('size');
$_POST['size'];
$_SESSION['size'] = array();
foreach ($size as $key)
{
if (isset($_POST[$key]))
{
$_SESSION['size'][$key] = $_POST[$key];
}
}
$size=$_POST['size'];
$max= count($_POST['size']);
for($i=0; $i<$max; $i++)
{
$_SESSION['size']= $_POST['size'];
}
Both Function Are Showing Only Array On The Other Pages...
When you echo $_SESSION['size'] it will show Array because its an array, not single value.
If you want to check values, use this construction:
foreach($_SESSION['size'] as $key=>$val) {
echo $key . '=>' . $val . '<br />';
}
Instead of
echo $_SESSION['size'];
You can also use:
var_dump($_SESSION['size']);
or
print_r($_SESSION['size']);
On the first page simply store the value(s):
$_SESSION['size'] = $_POST['size'];
Now, if your POST contained only a single value for size, you simply use this:
echo $_POST['size'];
// or this:
some_function($_POST['size']);
However, if your POST really had an array of values for size (which is possible), you would also have an array in your session. You then need a simple loop:
foreach ($_SESSION['size'] as $size) {
echo $size;
// or this:
some_function($size);
}
Combined code that handles both cases:
if (is_array($_SESSION['size'])) {
foreach ($_SESSION['size'] as $size) {
echo $size;
// or this:
some_function($size);
}
} else {
echo $_SESSION['size'];
// or this:
some_function($_SESSION['size']);
}

For each PHP multi dimension array returns 1 character

For some reason the both code upon killing the query var returns:
SELECT client_fname FROM client WHERE c=:l AND f=:n
Instead of:
SELECT client_fname FROM client WHERE client_id=:id AND client_id=:fname
Notice that only the first character of the column name strings is output.
Where am I going wrong? :S
PHP 5.4, will be using PDO SQL.
public function getField($_field, $_id, $_type) {
$_args = array(
array($_type.'_id', 'id'),
array($_type.'_fname', 'fname')
);
//var_dump($_args);
echo $this->dbSelect($_type.'_'.$_field, $_type, $_args);
}
protected function dbSelect($_select, $_from, $_args) {
$i = 0; //var_dump($_args);
$query = 'SELECT '.$_select.' FROM '.$_from.' WHERE ';
foreach ($_args as $_where) {
if($i == 0) {
$query .= $_where[$i][0] .'=:'. $_where[$i][1];
} else {
$query .= ' AND '.$_where[$i][0] .'=:'. $_where[$i][1];
}
$i++;
}
die($query);
}
$_args was a 2D array. However, your foreach is using $_where as its iteration variable. $_where is itself a one-dimensional array, so you don't need $i here at all. Instead just use $_where[0]
$_where[0] should refer to the column, and $_where[1] refers to its bound placeholder. $i is unrelated to the contents of $_where.
foreach ($_args as $_where) {
if($i == 0) {
$query .= $_where[0] .'=:'. $_where[1];
} else {
$query .= ' AND '.$_where[0] .'=:'. $_where[1];
}
$i++;
}
Otherwise, you are getting the result of an array key access of a string. For example:
$str = "Hello world";
echo $str[0];
// "H"

Store information in a array with a while loop inside function

How can I store information in a array?
I'm trying to get information from a database and then store HTML code in the array.
Is this possible? If so how?
function echothatshit() {
$sqll=mysql_query("select id,data from produkter where weight='1'");
$alla = array();
while($row=mysql_fetch_array($sqll))
{
$idd=$row['id'];
$data=$row['data'];
$alla[] = '<option value="'.$idd.'">'.$data.'</option>';
}
}
$test2 = echothatshit();
echo $test2;
You just need return $alla; at last of your function, also you can not echo an array directly, you need to loop it.
function echothatshit() {
$sqll=mysql_query("select id,data from produkter where weight='1'");
$alla = array();
$i=0;
while($row=mysql_fetch_array($sqll))
{
$idd=$row['id'];
$data=$row['data'];
$alla[$i] = '<option value="'.$idd.'">'.$data.'</option>';
$i++;
}
$yourVariable="";
for ($j=0;$j<$i;$j++)
{
$yourVariable.=$alla[$j];
}
return $yourVariable;
}
$test2 = echothatshit();
echo $test2;
Why not having $alla as string, that is (with a few formatting / renaming / simplification):
function getProductsHTML()
{
$sql = mysql_query("select id,data from produkter where weight='1'");
$html = '';
while($row = mysql_fetch_array($sql))
$html .= '<option value="'.$row['id'].'">'.$row['data'].'</option>'."\n";
return $html;
}
echo getProductsHTML();
function echothatshit() {
$sqll=mysql_query("select id,data from produkter where weight='1'");
$alla = array();
while($row=mysql_fetch_array($sqll))
{
$idd=$row['id'];
$data=$row['data'];
$alla[] = '<option value="'.$idd.'">'.$data.'</option>';
}
return $alla;
}
$test2 = echothatshit();
echo $test2;
If you want that $test2 gets array $alla, return that.
And then, in order to print array correctly, use
echo "<pre>";
print_r($test2);
echo "</pre>";

Categories