Array with values changed into sql or array directly to sql in colum per index => result
Well i found out how to print my global arrays, works great but now i have a problem, i want to print that array into sql
the code :
print_r($_SESSION['cart']);
returns :
Array ( [1] => 2 [2] => 1 [3] => 1 )
thats great, i know exactly what has been select, but the user wont know that...
so my question its how can i print that array in sql database but changing the index
example :
Array ( [1] => 2 [2] => 1 [3] => 1 )
for
Array ( [name1] => 2 [name2] => 1 [name3] => 1 )
based only if the index [x] exist in the current array results so in the database will look somehting like this :
[name1] => 2
[name2] => 1
[name3] => 1
and i can just call that line back, or how to print on diferent columns the same array
example :
from array : Array ( [1] => 2 [2] =开发者_如何学编程> 1 [3] => 1 )
to sql :
id quantity
[1] 2
[2] 1
[3] 1
is it posible to print the array in sql in this way ? and how to do that
Ty in advance
You can get data from sql:
$id2name=array();
$x=mysql_query("SELECT id,name FROM goods WHERE id IN(".implode(',',array_keys($array)).")");
while($y=mysql_fetch_assoc($x)){
$id2name[$y['id']]=$y['name'];
}
You can print this way:
foreach($array as $k=>$v){
print "[".$id2name[$k]."]\t".$v."\n";
}
...and i want to change the index for the name. It's easy
foreach($array as $k=>$v)
$new_arr[get_name($k)]=$v; //where get name = some function, that get name from id(you can get as I see in your site)
code for saving:
$str="";
foreach($array as $k=>$v){
$str.="[".$k."] ".$v."<br/>";
}
//now you can use $str
精彩评论