SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    json_encode() wrong result?

    Hi guys,

    I've tried converting PHP array into JavaScript array.

    Code:
    	<?php
    		//Get all data category from database.
    		$result = $this->classes_mdl->get_classes_name2();
    		
    		//convert PHP array to JS array.
    		echo "<script>"
    		    . "var classnames = " . json_encode($result) . ";"
    		    . "</script>";
    	?>
    
    	<script>
                    //print values for testing only
    		document.write(classnames);
    	</script>
    and the result was,
    Code:
    [object Object],[object Object],[object Object],[object Object]
    So what was my mistake?
    Quality codes are optimized and tested...
    Click here for inspiration..

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,268
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Try using console.log() instead of document.write. The objects are there, but document.write isn't good at showing them.
    "First make it work. Then make it better."

  3. #3
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @Jeff Mott ;

    I've changed the codes a little,
    Code:
    	<?php
    		//Get all data category from database.
    		$result = $this->classes_mdl->get_classes_name2();
    		
    		//convert PHP array to JS array.		
    		$js_array = json_encode($result);
    	?>
    		
    	<script>
    		//Converting to JS array.
    		var classnames =  <?php echo $js_array; ?>;
    	
    		//print classnames variable.
    		console.log(classnames);
    	</script>
    And it works and the result was this,
    Code:
    [Object { nameclass="chemistry"}, 
     Object { nameclass="class1"}, 
     Object { nameclass="class2"}, 
     Object { nameclass="Physics"}]
    Now how do I convert classnames array above into this array example below,
    Code:
     var classnames = [
          "chemistry",
          "class1",
          "class2",
          "Physics"
     ];
    Thanks in advance.
    Quality codes are optimized and tested...
    Click here for inspiration..

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,129
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by solidcodes View Post
    Now how do I convert classnames array above into this array example below,
    Code:
     var classnames = [
          "chemistry",
          "class1",
          "class2",
          "Physics"
     ];
    Thanks in advance.
    Or change how your JavaScript writes it
    Code JavaScript:
    for (item in classnames)
    {
      console.log(item.nameclass);
    }

  5. #5
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @cpradio ;

    Thanks for the hint dude.
    I already figured it out.
    Code:
    		//Converting to JS array.
    		var classnames =  <?php echo $js_array; ?>;
    	
    		var x;
    		var availableTags = [];
    		for (x in classnames) {
    		  //console.log(classnames[x].nameclass);
    		  availableTags[x] = classnames[x].nameclass;
    		}
    Problem solved.
    Quality codes are optimized and tested...
    Click here for inspiration..

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Are you going to be using that classnames variable elsewhere? If not, you could get PHP to spit out a JSON array of the names for you.
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @Salathe ;
    Can you show me the sample codes.
    Thanks in advance.
    Quality codes are optimized and tested...
    Click here for inspiration..


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
  •