SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Formatting Json Key, Value Pairs

    I am querying mysql and trying to convert my results into json keyvalue pairs.

    For whatever reason, my formatting is not coming back as desired.





    Code PHP:
    if (!$result = mysql_query($sql)){
     
    		echo 'Could not query database'.$sql;
    	}else{
    		$thedata = array();
     
     
    		echo '<table id="datatable"><thead>
    <th>Date</th><th>Total Leads</th></thead>';
     
    		$i = 0;
    		while($row = mysql_fetch_assoc($result)){
     
    		$strDate =  'Date.UTC('.$row['theyear'].',  '.$row['themonth'].',  '.$row['theday'].')';
     
    		$data[$i] =$strDate.', '. $row['num_leads']; 
    			$i++;
    		};
     
    		$data = json_encode($data);
     
     
     <?php echo $data; ?>
    Which returns JSOn result of
    Code PHP:
    [
      "Date.UTC(2011,  5,  4), 111",
      "Date.UTC(2011,  5,  3), 103",
      "Date.UTC(2011,  5,  2), 73",
      "Date.UTC(2011,  5,  1), 29",
      "Date.UTC(2011,  4,  30), 26",
      "Date.UTC(2011,  4,  29), 77",
      "Date.UTC(2011,  4,  28), 147",
      "Date.UTC(2011,  4,  27), 122"
    ]

    I need this to return the json in the following format.

    Code PHP:
    [Date.UTC(1970,  9,  9), 0   ],
                [Date.UTC(1970,  9, 14), 0.15],
                [Date.UTC(1970, 10, 28), 0.35],
                [Date.UTC(1970, 11, 12), 0.46],
                [Date.UTC(1971,  0,  1), 0.59],
                [Date.UTC(1971,  0, 24), 0.58]

    Any ideas?

  2. #2
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. I got a step further by changing the value of $data[$i] in the loop.

    Code PHP:
    $data[$i] =array($strDate=> $row['num_leads']);

    encoding $data with json produced


    Code HTML4Strict:
        	[
      {
        "Date.UTC(2011,  5,  4)": "111"
      },
      {
        "Date.UTC(2011,  5,  3)": "103"
      },
      {
        "Date.UTC(2011,  5,  2)": "73"
      },
      {
        "Date.UTC(2011,  5,  1)": "29"
      },
      {
        "Date.UTC(2011,  4,  30)": "26"
      },
      {
        "Date.UTC(2011,  4,  29)": "77"
      },
      {
        "Date.UTC(2011,  4,  28)": "147"
      },
      {
        "Date.UTC(2011,  4,  27)": "122"
      }
    ]

    I guess I can use str_replace to get the formatting I want from this but there must be a better way?

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    What you're trying to write is not JSON, it's just JavaScript. You'll need to write that out "manually" as you would with HTML, etc.; PHP's JSON functions are not appropriate for this.
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •