1

I have my jquery code which connects to getdata.php. Here the val is dynamic value and the function is called every time an option is selected from dropdown.

    function getMoleculeData(val){
      var molval=val;
      var url = 'getmoldata.php';
     $.ajax({ 
      url: url,
      data: 'molval='+molval,
      method:'POST',
      success: function(moldata) {        
      alert(moldata);
        }
     });
    }

My getdata.php code goes here

    <?php  
      $molval = $_POST['molval']; //  consider $_POST['molval']=ABC 
      $dbhost = "localhost";
      $dbuser = "root";
      $dbpass = "";
      $dbname= "userdata";
      $conn = mysql_connect($dbhost, $dbuser, $dbpass);
      mysql_select_db($dbname) or die ();   
       $query = "SELECT cm_id, CORP_RANK, CORP_MKT_SHARE, COMBINED_MOLECULE FROM PRODUCT_UNIVERSE_D WHERE COMBINED_MOLECULE ="."'$molval'";

       $result = mysql_query($query) or die(mysql_error());
       $i = 1; 
         $array = array();
         while($row = mysql_fetch_array($result) { 
         $array[] = $row;
         echo $row['CORP_RANK']; // etc
         } 

       ?>

Here is my table PRODUCT_UNIVERSE_D

   cmid  |  CORP_RANK | CORP_MKT_SHARE |  COMBINED_MOLECULE  |    DATE
     1   |     10     |       30       |         ABC         |  01-04-2013
     2   |     5      |       50       |         ABC         |  03-06-2013
     3   |     8      |       40       |         ABC         |  23-09-2013
     4   |     3      |       10       |         XYZ         |  05-01-2014

when I select ABC in dropdown, the same value is given in where clause and I am able to fetch the result. FOr the above code written in getdata.php the output that I got is 1058 i.e the values of CORP_RANK where COMBINED_MOLECULE = 'ABC'. But the requirement is I have to get the result in the form of [10,5,8]. How do I get the result in this array format and also how to fetch all the columns separately like $row['CORP_MKT_SHARE'] = [10,50,40] and display them separately in success function of index.php as

    [10,5,8] and [10,50,40] for furthur use

Please help me out, as I am not perfect in fundamentals of arrays I am unable to get the result. Thanks in advance

rji rji
  • 697
  • 3
  • 17
  • 37

1 Answers1

1

If you're aiming to output the data into json format.

example:
$array = array(1,2,3);
var_dump(json_encode($array));

You'll get this output:

string(7) "[1,2,3]" 

It's better to change your loop as this:

for($array = array();$row = mysql_fetch_array($result);$array[] = $row);
json_encode($array);

This is what the best way when you want to retrieve data from php file using ajax.

using your code:

while($row = mysql_fetch_array($result)) { 
   $rank[] = $row['CORP_RANK'];
   $share[] = $row['CORP_MKT_SHARE'];
}
$array = array('CORP_RANK'=>$rank,'CORP_MKT_SHARE'=>$share);
json_encode($array);

Hope this helps.

DarkZ3ro11
  • 128
  • 7
  • I am getting the same output i.e. something like [{"0":"1","cm_id":"1","1":"5","CORP_RANK":"5","2":"50","CORP_MKT_SHARE":"50","3":"ABC","COMBINED_MOLECULE":"ABC"},{"0":"2","cm_id":"2","1":"8","CORP_RANK":"8","2":"50","CORP_MKT_SHARE":"50","3":"ABC","COMBINED_MOLECULE":"XYZ"}] but I want to get it in the format [ABC, ABC, ABC] for example for COMBIND_MOLECULE and [10, 5. 8] for CORP_RANK @Ronnel Padernal – rji rji Mar 12 '15 at 17:29
  • for($array = array();$row = mysql_fetch_array($result);$array[] = $row['CORP_RANK']); to get only the CORP_RANK data, – DarkZ3ro11 Mar 12 '15 at 17:30
  • Thanq.. it works :) I have one more doubt. How to get these value separately like if I want to append $row['CORP_RANK'] to a div and $row['CORP_MKT_SHARE'] to other div how do I append after fetching it from getdata.php to the jquery code – rji rji Mar 12 '15 at 17:37
  • you can convert the json data from getdata,php to javascript using JSON.parse(json) – DarkZ3ro11 Mar 12 '15 at 17:43
  • @ DarkZ3ro11 can you give the code please. I am very new to this field. please don't mind – rji rji Mar 12 '15 at 17:51
  • check this : http://stackoverflow.com/a/912247/4660348 or this: http://www.dyn-web.com/tutorials/php-js/json/parse.php – DarkZ3ro11 Mar 12 '15 at 17:56
  • I have tried the code $.ajax({ url: 'getdata.php', data: 'molval='+molval, method:'POST', dataType:'json', success: function(moldata) { alert(moldata.CORP_RANK); } }); // I am getting output as undefined – rji rji Mar 12 '15 at 18:06