Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am Posting my values from database here, code:

<?php 
  $host = "localhost";
  $user = "root";
  $pass = "";

  $databaseName = "ani";
  $tableName = "balls";

  //--------------------------------------------------------------------------
  // 1) Connect to mysql database
  //--------------------------------------------------------------------------
include 'DB.php';
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);

  //--------------------------------------------------------------------------
  // 2) Query database for data
  //--------------------------------------------------------------------------
$result = mysql_query("SELECT * FROM $tableName");         
$array = mysql_fetch_row($result);                          

    //echo json
echo json_encode($array);

?>

Then I am trying to write out all rows of my array but can't get to understand how. However, this is the code from my script. Hope you can help somehow. Thanks.

<script id="source" language="javascript" type="text/javascript">

        setInterval("yourAjaxCall()",1000);
        function yourAjaxCall() 
      {

        $.ajax({                                      
          url: 'api.php',                  //the script to call to get data          
          data:  "id=1",          //you can insert url argumnets here to pass to api.php
                                           //for example "id=5&parent=6"
          dataType: 'json',                //data format
          success: function(data)          //on recieve of reply
          { 
            var id1 = data[0];              // get id of first row
            var vcolor1 = data[1];              //get color of first row            
            $("#square").css({"background-color" : vcolor1});   
            $color1 = vcolor1; //saving value
            var $color1;

            //HERE I WANT TO BE ABLE TO GET NEXT ROW OF MY JSON ARRAY. HOW?

          } 

        });
      };


    </script>

I can't find a solution so I am very greatful for good and simple answers!

share|improve this question
    
If you just want to pull one row at a time using Ajax then you'll probably need to look into the LIMIT command on the mySQL, in order to state which row you need, and look at general data-paging techniques. You basically keep a counter of where you are within the results on the client side and pass that through to the SELECT statement using LIMIT to give a certain row from the data. –  Darren Crabb Sep 26 '13 at 11:57
    
Well, I want all rows to be wrote out, but I just succeed with one. So do not understand how... @DarrenCrabb –  PeterP Sep 26 '13 at 12:01

1 Answer 1

In PHP file do it:

$result = mysql_query("SELECT * FROM $tableName");         
$dataArray = array();
while($array = mysql_fetch_assoc($result)){
    $dataArray[] = $array;
} 

echo json_encode($dataArray);

AND in JS:

success: function(data)
{ 
   var obj = JSON.parse(data);
   $.each(obj,function(index,value)){
      alert(index+" : "+value); //here you can get all data
   }
} 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.