Here is the code
$i=0;
while ($row = mysql_fetch_array($result))
{
$output.="idno".$i."=".urlencode($row['usr_id']).
'&'."res".$i."=".urlencode($row['responce']).
'&'."sex".$i."=".urlencode($row['sex']).
'&'."com1".$i."=".urlencode($row['com1']).
'&'."com2".$i."=".urlencode($row['com2']);
$i++;
}
OUTPUT i get idno is part of com2 string how do I seperate them.
You need to add an & when $i is not zero:
$i=0;
while ($row = mysql_fetch_array($result))
{
$output .= ($i ? '&' : '') . "idno".$i."=".urlencode($row['usr_id']).
'&'."res".$i."=".urlencode($row['responce']).
'&'."sex".$i."=".urlencode($row['sex']).
'&'."com1".$i."=".urlencode($row['com1']).
'&'."com2".$i."=".urlencode($row['com2']);
$i++;
}
Another solution would be using an array and join its elements afterwards:
$array = array();
$i = 0;
while ($row = mysql_fetch_array($result)) {
$array[] = "idno$i=".urlencode($row['usr_id']);
$array[] = "res$i=".urlencode($row['responce']);
$array[] = "sex$i=".urlencode($row['sex']);
$array[] = "com1$i=".urlencode($row['com1']);
$array[] = "com2$i=".urlencode($row['com2']);
$i++;
}
$output .= implode('&', $array);
Furthermore you could use the argName[] declaration that PHP will convert into an array when receiving such a request query string.
Or you could do this:
$array = array();
$i = 0;
while ($row = mysql_fetch_array($result)) {
$array["idno$i"] = $row['usr_id'];
$array["res$i"] = $row['responce'];
//etc.
$i++;
}
$output = http_build_query($array);
Related
I'm trying to get the following output from mysql for Google Line Chart API:
[["product","diameter","width"],["Product 1","2","4"],["Product 2","4","8"]]
I have set up several input checkboxes to send field names (e.g width,diameter) to the database via $_POST["info"] and retrieve the values from those fields. Here's the part that generates the data from mysql:
$result = $users->fetchAll();
$comma = "";
$data="";
$data[0] = array_merge(array(product),$info);
$i = 1;
foreach ($result as $r)
{
foreach($_POST["info"] as $p)
{
$d .= $comma.$r[$p]; // trying to get "$r["width"],$r["diameter"]"
}
$comma = ",";
$data[$i] = array($r["name"], $d);
$i++;
}
echo json_encode($data);
My desired output should be like this:
[["product","diameter","width"],["Product 1","2","4"],["Product 2","4","8"]]
But that code is generating duplicated results like this
[["product","diameter","width"],["Product 1","24"],["Product 2","24,4,8"]]
I guess I shouldn't be using the nested foreach to loop over $_POST. Can anyone tell me how to fix that?
Full PHP Code:
$info = $_POST["info"]; // It contains an array with values like width,diameter,thickness etc...
$comma = "";
foreach($info as $in)
{
$field .= "".$comma."b.".$in."";
$comma = ",";
}
$sql = "
SELECT {$field},a.user_id,a.name
FROM `product_detail` a INNER JOIN
`attr` b ON a.model = b.model
WHERE a.user_id = ?
GROUP BY a.model
";
$users = $dbh->prepare($sql);
$users->bindValue(1, $_SESSION["user_id"]);
$users->execute();
$result = $users->fetchAll();
$comma = "";
$data="";
$i = 1;
$data[0] = array_merge(array(product),$info);
foreach ($result as $r)
{
foreach($_POST["info"] as $p)
{
$d .= $comma.$r[$p];
}
$comma = ",";
$data[$i] = array($r["name"], $d);
$i++;
}
echo json_encode($data);
$_POST["info"] Content:
Array
(
[0] => diameter
[1] => width
)
try it like this:
$result = $users->fetchAll();
$data="";
$data[0] = array_merge(array(product),$info);
$i = 1;
foreach ($result as $r)
{
$d[]=$r["name"];
foreach($_POST["info"] as $p)
{
$d[]= $r[$p];
}
$data[$i] = $d;
$d=array(); //set $d to empty not to get duplicate results
$i++;
}
echo json_encode($data);
The end result you are looking for, is valid JSON. You should not try to manually generate that.
Instead you should make an array of arrays in php and use json_encode($array) to get the result you are looking for.
Also note that by injecting your POST variables directly in your query, you are vulnerable to sql injection. When accepting fields, you should check them against a white-list of allowed values.
Try the below solution:
$result = $users->fetchAll();
$data="";
$data[0] = array_merge(array(product),$info);
$i = 1;
foreach ($result as $r)
{
$d = array();
foreach($_POST["info"] as $p)
{
$d[] = $r[$p]; // trying to get "$r["width"],$r["diameter"]"
}
$data[$i] = array($r["name"]) +$d;
$i++;
}
echo json_encode($data);
I want to store result of a query as associative array. Below is my code that generates the query result below.
<?php
$include('config.php') //mysql connection file
$result = mysql_query("SELECT daystime.*, Sprinkler_ID FROM daystime, scheduler WHERE daystime.id = scheduler.DaysTime_ID ORDER BY daystime.id, Sprinkler_ID") or trigger_error(mysql_error());
$data_array = array();
while($rs = mysql_fetch_assoc($result))
{
$key=$rs['id'];
$value=$rs['Sprinkler_ID'];
$data_array[$key] = [$value];
}
foreach ($data_array as $key => $value)
{
echo $key.'=>'.$value.'<br />';
}
?>
This gives me output as
19=>Array
20=>Array
21=>Array
27=>Array
29=>Array
But I should get
19 -> [4,5],
20 -> [5],
21=>[4,6],
// and so on
$value is an array, you can't use echo on arrays
Don't loop just do a var_dump()
$data_array = array();
while($rs = mysql_fetch_assoc($result)){
$data_array[$rs['id']][]=$rs['Sprinkler_ID'];
}
var_dump($data_array);//or print_r($data_array);
<?php
$include('config.php') //mysql connection file
$result = mysql_query("SELECT daystime.*, Sprinkler_ID FROM daystime, scheduler WHERE daystime.id = scheduler.DaysTime_ID ORDER BY daystime.id, Sprinkler_ID") or trigger_error(mysql_error());
$data_array = array();
while($rs = mysql_fetch_assoc($result))
{
$key=$rs['id'];
$value=$rs['Sprinkler_ID'];
$data_array[$key] = [$value];
}
$out = '';
$count = count($data_array);
$iter = 0;
foreach ($data_array as $key => $value)
{
$out.= $key.'=>[';
foreach ($value as $val) {
$out.=$val.',';
}
$out = rtrim($out, ",");
$out.= ']';
if ($iter < ($count-1)) {
$out.=',<br />';
}
$iter++;
}
echo $out;
You need an inner foreach loop to handle printing the array in $value as this is a multi dimensional array. The preceding is an example of how it could look to produce the exact output you requested. If you are looking for a dump of the values, then please use the var_dump solution provided by #meda.
There are two issues in your code. The first is that $value is an array, the second is that this array only contains one item.
Try this:
$data_array = array();
while($rs = mysql_fetch_assoc($result))
{
$key=$rs['id'];
$value=$rs['Sprinkler_ID'];
$data_array[$key][] = $value;
}
foreach ($data_array as $key => $values)
{
echo $key.'=> [';
foreach($values as $value)
echo $value . ',';
echo ']<br />';
}
I was trying something but I need to explode it twice because I store 2 variables in a string. Anyways, I used a while loop but I don't understand, I use cid++, but it does not appear to increase. ANyways, here's the code.
$cid = 0;
while($row = mysql_fetch_array($result)){
$comment = explode("-", $row['comments']);
$madeby = explode("///", $comment[$cid]);
$cid++;
echo $madeby[1];
}
Since you're setting a var and incrementing you can use it as a key for both arrays.
$cid = 0;
while($row = mysql_fetch_array($result)){
$comment[$cid] = explode("-", $row['comments']);
$madeby[$cid] = explode("///", $comment[$cid]);
echo $madeby[$cid][1];
$cid++;
}
Then you can do this:
foreach($madeby as $key=>$tempArr){
echo '"'.$madeby[$key][0].'" by "'.$madeby[$key][1].'"<br>';
}
To see the whole array:
print_r($madeby);
try this
while($row = mysql_fetch_array($result))
{
$comment = explode("-", $row['comments']);
foreach($comment as $each_comment)
{
$madeby = explode("///", $each_comment);
echo $madeby[1];
}
}
or if you really want to use cid then
while($row = mysql_fetch_array($result))
{
$comment = explode("-", $row['comments']);
for($cid=0;$cid<count($comment);$cid++)
{
$madeby = explode("///", $comment[$cid]);
echo $madeby[1];
}
}
try this:
while($row = mysql_fetch_array($result)){
$comment = explode("-", $row['comments']);
$cnt = count($comment);
$madeby = array();
for($cid=0; $cid<$cnt; $cid++){
$madeby[] = explode("///", $comment[$cid]);
}
print_r($madeby);
}
There is some problem, the code below assigning the last pr_name to all keys.
$arr = array();
while($row = mysql_fetch_array($results)) {
$keys[] = $row['pr_code'];
$items = array_fill_keys($keys, $row['pr_name']);
}
Simply with this:
$items = array();
while($row = mysql_fetch_array($results)) {
$items[$row['pr_code']] = $row['pr_name'];
}
function tableOne() {
$query = mysql_query("SELECT valor FROM grafico") or die(mysql_error());
$i = 0;
while($row = mysql_fetch_assoc($query)) {
$arr[] = array($row[valor]);
++$i;
}
echo json_encode($arr);
}
}
the output will be
[["15573"],["31978"],["11227"],["5752"],["20817"],["32182"]]
i need something like:
["15573","31978","11227","5752","20817","32182","10935"]
i tried some changes in the code but the output is not what i want.
thanks
You are placing sub-arrays in each element of your array. You should replace
$arr[] = array($row[valor]);
with
$arr[] = $row[valor];
The [] in $arr[] already adds each entry as an element of the array.
$query = mysql_query("SELECT valor FROM grafico") or die(mysql_error());
$arr = array();
while ($row = mysql_fetch_assoc($query)) {
$arr[] = $row['valor']; // get rid of the array() wrapper
}
echo json_encode($arr);