Passing PHP array to a JavaScript array

27 views
Skip to first unread message

rico harley

unread,
Mar 30, 2015, 1:30:56 PM3/30/15
to atlan...@googlegroups.com
<?php

include ("php_includes/db_conx.php");

$sql = "SELECT sun_zodiac, mon_zodiac, tue_zodiac, wed_zodiac, thur_zodiac, fri_zodiac, sat_zodiac  FROM horoscope WHERE zodiac_sign =  'Taurus' OR zodiac_sign =  'Virgo'OR zodiac_sign =  'Capricorn' LIMIT 0 , 30";
$result = mysqli_query($db_conx,$sql);
while($row = mysqli_fetch_array($result)) {
//Array is set up to capture values out the database by rows which will number 3.

$sunday_daily[] = $row['sun_zodiac']; 
$monday_daily[] = $row['mon_zodiac']; 
$tuesday_daily[] = $row['tue_zodiac']; 
$wednesday_daily[] = $row['wed_zodiac']; 
$thursday_daily[] = $row['thur_zodiac']; 
$friday_daily[] = $row['fri_zodiac'];
$saturday_daily[] = $row['sat_zodiac'];  

}


?>

<script>

var dailyTaurus =  ["<?php $sunday_daily[1]; ?>", "<?php $monday_daily[1]; ?>", "<?php $tuesday_daily[1]; ?>", "<?php $wednesday_daily[1]; ?>", "<?php $thursday_daily[1]; ?>", "<?php $friday_daily[1]; ?>", "<?php $saturday_daily[1]; ?>"];

var dailyVirgo =  ["<?php $sunday_daily[2]; ?>", "<?php $monday_daily[2]; ?>", "<?php $tuesday_daily[2]; ?>", "<?php $wednesday_daily[2]; ?>", "<?php $thursday_daily[2]; ?>", "<?php $friday_daily[2]; ?>", "<?php $saturday_daily[2]; ?>"];

var dailyCapricorn =  ["<?php $sunday_daily[0]; ?>", "<?php $monday_daily[0]; ?>", "<?php $tuesday_daily[0]; ?>", "<?php $wednesday_daily[0]; ?>", "<?php $thursday_daily[0]; ?>", "<?php $friday_daily[0]; ?>", "<?php $saturday_daily[0]; ?>"];


</script>




<?php
//Values return fine using PHP
echo $sunday_daily[0] . "<br><br>";
echo $sunday_daily[1] . "<br><br>";
echo $sunday_daily[2] . "<br><br>";
?>


<script>

alert(dailyVirgo[1]);

//Returns absolutely nothing.

/*
I have done some research about JSON encoding like this
*/
var dailyVirgo = <?php echo json_encode($sunday_daily[0]) ?>;
//alert(dailyVirgo[1]);


/*It would really benefit me to understand this technique well, I need to manage this information to finish a project Can someone help me understand what I am missing please?*/

 

</script>

Ralph Smith

unread,
Mar 30, 2015, 1:40:21 PM3/30/15
to rmhar...@gmail.com, Atlanta PHP
Your javascript needs to parse the json object. Your just feeding it a
text string.
> --
> You received this message because you are subscribed to the Google Groups
> "AtlantaPHP Discussions and Job Postings" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to atlantaphp+...@googlegroups.com.
> To post to this group, send email to atlan...@googlegroups.com.
> Visit this group at http://groups.google.com/group/atlantaphp.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/atlantaphp/67c38241-d3ee-4daf-97cb-ac31c6e1f994%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ralph Smith

unread,
Mar 30, 2015, 1:40:22 PM3/30/15
to rmharley66, Atlanta PHP
Example

var json = '<?php echo json_encode($sunday_daily[0]) ?>',
obj = JSON.parse(json);

Corey Dulecki

unread,
Mar 30, 2015, 1:40:22 PM3/30/15
to rmhar...@gmail.com, atlan...@googlegroups.com
<?php $monday_daily[1]; ?>


Needs to be

<?php echo $monday_daily[1]; ?>



Corey Dulecki
Professional Software Consulting
Co...@CoreysConsulting.com

Alphonso Clay

unread,
Mar 30, 2015, 1:41:37 PM3/30/15
to rmhar...@gmail.com, atlan...@googlegroups.com
Instead of this:

var dailyVirgo =  ["<?php $sunday_daily[2]; ?>", "<?php $monday_daily[2]; ?>", "<?php $tuesday_daily[2]; ?>", "<?php $wednesday_daily[2]; ?>", "<?php $thursday_daily[2]; ?>", "<?php $friday_daily[2]; ?>", "<?php $saturday_daily[2]; ?>"];

Try this:

var dailyVirgo =  ["<?php echo($sunday_daily[2]); ?>", "<?php echo($monday_daily[2]); ?>", "<?php echo($tuesday_daily[2]); ?>", "<?php echo($wednesday_daily[2]); ?>", "<?php echo($thursday_daily[2]); ?>", "<?php echo($friday_daily[2]); ?>", "<?php echo($saturday_daily[2]); ?>"];

Hope that helps



Ralph Smith

unread,
Mar 30, 2015, 1:45:23 PM3/30/15
to clayal...@yahoo.com, rmhar...@gmail.com, atlan...@googlegroups.com
Why build a json object by hand when you can build a array and have
php do it right everytime?

Also his question is about passing it to javascript, which as you see
he is passing a text string and not rendering it from text string to
object in javascript. Until it is parsed as a valid object it is a
text string and unusable in javascript. His php is fine for the most
part.
> https://groups.google.com/d/msgid/atlantaphp/1165377250.1390309.1427737088013.JavaMail.yahoo%40mail.yahoo.com.

Mike Schinkel

unread,
Mar 30, 2015, 1:53:45 PM3/30/15
to Atlanta PHP
> Why build a json object by hand when you can build a array and have
> php do it right everytime?

+1
> To view this discussion on the web visit https://groups.google.com/d/msgid/atlantaphp/CAEN5PigbZNQPgPxnBbvJdAo3e3u091iwP004vi6dYT64fcb18w%40mail.gmail.com.

Brad

unread,
Mar 31, 2015, 11:14:58 AM3/31/15
to atlan...@googlegroups.com
I haven't tested this, but it should work more or less: https://gist.github.com/BradEstey/410414e1b8c81012242e

In most cases, it makes more sense to request your JSON objects from a separate API call. But, if you're going to inline the JSON object, then put it inside it's own script tag with the type "application/json". Browsers won't try to execute script tags that have a type other than "text/javascript". Just add a unique ID to that script tag so that you can fetch it from your Javascript with a simple document.getElementById(). This helps to create a separation of concerns by allowing you to move that Javascript out into its own file, create reusable modules and keep from cluttering up your markup.

Ralph Smith

unread,
Mar 31, 2015, 11:42:47 AM3/31/15
to brad....@gmail.com, Atlanta PHP
Very nice and clean implementation Brad. Applause.
> --
> You received this message because you are subscribed to the Google Groups
> "AtlantaPHP Discussions and Job Postings" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to atlantaphp+...@googlegroups.com.
> To post to this group, send email to atlan...@googlegroups.com.
> Visit this group at http://groups.google.com/group/atlantaphp.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/atlantaphp/5800169a-c9de-483f-97a3-e2bc57d83f50%40googlegroups.com.

rico harley

unread,
Mar 31, 2015, 11:42:55 AM3/31/15
to atlan...@googlegroups.com, clayal...@yahoo.com, rmhar...@gmail.com
I have two questions?

the first,

Until it is parsed as a valid object it is a 
text string and unusable in javascript. His php is fine for the most 
part. 

var json = '<?php echo json_encode($sunday_daily[0]) ?>',
    obj = JSON.parse(json);  WHAT WILL THIS DO?  and how could I access it's values?


Second

In most cases, it makes more sense to request your JSON objects from a separate API call.
Brad, could you explain this technique more, or point me in the right direction?

Thanks for the code sample as well.

Ralph Smith

unread,
Mar 31, 2015, 11:46:45 AM3/31/15
to rmharley66, Atlanta PHP, clayalphonso
Hey Rico.

The function JSON.parse takes a properly formatted text string in JSON
format and converts it to a javascript object.

JSON is Javascript notation but it is not a javascript object until
you parse it.
> https://groups.google.com/d/msgid/atlantaphp/1c283eba-0957-4d85-97d4-d7013007b675%40googlegroups.com.

Ralph Smith

unread,
Mar 31, 2015, 11:49:18 AM3/31/15
to rmharley66, Atlanta PHP, clayalphonso
Oh to the second part you can access it just like any other Javascript
object variable after it's parsed with obj.var or obj[var]

Brad

unread,
Mar 31, 2015, 12:53:56 PM3/31/15
to atlan...@googlegroups.com, rmhar...@gmail.com, clayal...@yahoo.com
Rico,

For some basic debugging in Javascript, you can always do something like alert(typeof json); or console.log(json); to see if the variable is a string or an object.

As for the API part, I wouldn't worry about that as of right now. At some point you may want to check out phptherightway.com and catch up on the past 10 years or so of PHP best practices, but for right now, just stick to learning the basics.
Reply all
Reply to author
Forward
0 new messages