problem in the output of query - php+json - php

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);

Related

Store data into array variable php

If i echo inside the while loop i get 4,2 values and if i echo outside the while loop then i only get 2. I want to get the data from $row into the values array. is something missing in this code?
$query = oci_parse($con, "SELECT count(*) FROM Counter GROUP BY Blog_name");
oci_execute($query);
while($row = oci_fetch_array($query))
{
$s = $row[0].',';
$values = explode(',', $s);
echo $values[0]; // 4
echo $values[1]; // 2
}
echo $values[0]; // 2
echo $values[1]; // 2
Try this,
$Values = array();
while($row = oci_fetch_array($query))
{
$Values[] = $row[0];
}
echo $Values[0];
First, $values as you're using it is just a string, and is not an array.
Try adding before the while loop.
$values = array();
Second, there's really no need to use explode here, it's an unnecessary step. If you only want the first column in the row, you can simply add that column to $values. (Currently you are overwriting the contents of $values at each iteration of the loop because you are missing the [].)
$values[] = $row[0];
If you're trying to put ALL of the columns in each row into values, try:
$values[] = $row;
If you're trying to individually put the contents of each column into it's own index in $values, try:
while($row = oci_fetch_array($query))
{
foreach($row as $column)
{
$values[] = $column;
}
}
Do like this:
$s = array();
while($row = oci_fetch_array($query)) {
$s[] = $row[0];
}
echo $s[0];
print_r($s);
$query = oci_parse($con, "SELECT count(*) FROM Counter GROUP BY Blog_name");
oci_execute($query);
$values = array();
while($row = oci_fetch_array($query))
{
$values[] = $row[0];
}
print_r($values);

PHP: When using explode() twice, how to auto increase

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);
}

Ignore value from assoc array when display

$result = mysql_query($sql_result);
$newArray = array();
$index=0;
while($row = mysql_fetch_assoc($result)){
$newArray[$index] = $row;
$index++;
}
I wanna ignore the value - from my assoc array when display. Help me please.
If you want to remove all columns containing a certain value you could use array_filter:
function removeEmpty($v){
return $v != '-';
}
while($row = mysql_fetch_assoc($result)){
$newArray[$index] = array_filter($row,"removeEmpty");
$index++;
}

php is assigning last result to all array keys

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'];
}

php string formatting?

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);

Categories