1

i have this script and it works, but not as i expected. I need to assign an array of values with differents names, now all $arr[] are named "valor"

{"valor":"20"},{"valor":"50"}

i need

{"valor1":"20"},{"valor2":"50"}

the script

    $query = mysql_query("SELECT valor FROM grafico") or die(mysql_error());

    $arr = array();
    while($row = mysql_fetch_assoc($query)) {
        $arr[] = $row;
    }
    echo json_encode($arr);


in ajax

    <script type="text/javascript">
       jQuery(document).ready(function(){
          jQuery("button").click(function(){

             jQuery.ajax({
                url: "chart.php",
                dataType: "json", 
                success: function(json){
                   var msg = "Nome: " + json.valor1+ "\n";
                   msg += "Sobrenome: " + json.valor2 + "\n";

                   alert(msg);
                }
             });
          });
       });

       </script>

the problem is: I need to create a loop that create uniques names, as value1, value2, value3

like

$arr['valor1'] = "Evandro";

i tried a loop- for, but i get an error of memory

thanks

2 Answers 2

2

Try this:

$query = mysql_query("SELECT valor FROM grafico") or die(mysql_error());

$arr = array();
$i = 1;
while($row = mysql_fetch_assoc($query)) {
    $arr[] = array("valor{$i}" => $row["valor"]);
    ++$i;
}
echo json_encode($arr);

Should work. Alternatively if you want to make it so it works with current callback change the $arr[] = line to the following:

    $arr["valor{$i}"] = $row["valor"];
Sign up to request clarification or add additional context in comments.

5 Comments

the result is [{"valor1":"20"},{"valor2":"50"}] ?> but the alert doesn't work. It is supposed work, right?
You sure it ought to be jQuery("button") and not jQuery("#button")? Also, try alert()-ing json var at the very top of callback function.
the alert works but says valor1: undefined and the same to the valor2. Thanks
the problem is the [] --- [{"valor1":"20"},{"valor2":"50"}], but if i remove in the [] in $arr only show the second value {"valor2":"50"}
Updated the answer to work with your callback. Just a side note - don't call your variable "valor" ("value" in Spanish, I'm guessing) - it could mean anything. It's like calling your dog "Dog". It's plain to see this is some kind of value. The question is what kind exactly? If it's name, call it "name". If it's user details call it accordingly, etc.
1
$index = 1;
while($row = mysql_fetch_assoc($query)) {
        $key = 'valor'.index;
        $arr[$key] = $row;
        $index++;
    }

Does that give you a memory error? It shouldn't.

1 Comment

Unless you have an unbelievably immense number of return values from your query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.