Welcome to WebmasterWorld Guest from 54.162.141.212

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Put result of sql query into javascript array

I'm in a bind and learning the hard way

     
5:47 am on Feb 13, 2008 (gmt 0)

5+ Year Member



I need to figure out a way to make a rotating slideshow hyperlink to the current image's corresponding page. I'm a little green around the gills and new to this forum. Here's the beginning of my code:

<?php
include('connect.php');
$query = "SELECT * FROM products WHERE SlideNumber > '0' ORDER BY SlideNumber";
$result = mysql_query($query);
if (!$result)
{
die("Could Not Query Database: <br />".mysql_error());
}
while ($row = mysql_fetch_array($result)){
echo "?category=".$row["Category"]."&which=".$row["ID"];
}
?>

The query works fine and the echo statement looks as it should although I'm only using it view the results. Since I have a column in my database that stores the slideshow sequence number of a product, if I were to put the results of my query into an javascript array I think a javascript function (similar to the one that rotates the images) can also handle the hyperlink. Does anyone have any advice on this? Thanks.

6:01 am on Feb 13, 2008 (gmt 0)

5+ Year Member



I think I've got a handle on it now so never mind.
7:38 am on Feb 13, 2008 (gmt 0)

5+ Year Member



On second thought I still need to pass the php array to a javascript array for it to work in the javascript function. Can anyone give me a hint how?

Here's where I've left off:

<?php
include('connect.php');
$query = "SELECT * FROM products WHERE SlideNumber > '0' ORDER BY SlideNumber ASC";
$result = mysql_query($query);
if (!$result)
{
die("Could Not Query Database: <br />".mysql_error());
}
$counter = 0;
$dynalinks = array();
while ($row = mysql_fetch_array($result)){
$dynalinks[$counter] = "?category=".$row["Category"]."&which=".$row["ID"];
$counter++;
}
?>

8:04 am on Feb 13, 2008 (gmt 0)

5+ Year Member



<snip>

I've got it! Thanks to the code I found in the link pasted above.

Here's the solution at work in my code for any of you who might run into a similar problem:

<script type="text/javascript">
dynalink = new Array();
<?php
for($i=0;$i<count($dynalinks); $i++){
echo "dynalink[$i]='".$dynalinks[$i]."';\n";
}
?>
for(i=0;i<dynalink.length;i++);
</script>

[edited by: dreamcatcher at 8:47 am (utc) on Feb. 13, 2008]
[edit reason] no urls as per T.O.S [webmasterworld.com].Thanks [/edit]

8:07 am on Feb 13, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



can you use php code like this i javascript ? I am a little confused.
8:46 am on Feb 13, 2008 (gmt 0)

5+ Year Member



I guess so, if the php is at the top. I'm just learning a lot of this, but I've gotten my script to work. Actually, there is a kink or two to work out as the links aren't corresponding, but does that have anything to do with the code I've posted? I'm not sure.
2:06 pm on Feb 13, 2008 (gmt 0)

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member



You can also pass entire array structures directly to javascript using json_encode() [php.net]

<?php
$arr = array(
'red' => 2,
'blue' => 5,
'green' => array('purple' => 2)
);
echo '<script type="text/javascript">'.PHP_EOL;
echo 'var arr = '.json_encode($arr).';'.PHP_EOL;
echo 'alert(arr.red * arr.blue + arr.green.purple);'.PHP_EOL; // 12
echo '</script>';
?>
2:35 pm on Feb 13, 2008 (gmt 0)

5+ Year Member



That looks much cleaner. Much obliged.
3:59 am on Feb 14, 2008 (gmt 0)

5+ Year Member



Fatal error: Call to undefined function: json_encode()?

I think that may be a bit beyond me right now (not using 5.2), but thanks for the tip. I found something else that might be cleaner than what I've got going but I'm not sure how to catch the array in js yet.

echo 'new Array('.implode(', ', $dynalinks).')';

5:04 am on Feb 14, 2008 (gmt 0)

5+ Year Member



I might stick with what's sort of working but I've got an incrementing problem. While the image array increments by one, the link array increments by 2 (starting at 2)! I only land right once with 5 possible results: '0/2', '1/4', '2/1', '3/3', '4/0'.
6:04 am on Feb 14, 2008 (gmt 0)

5+ Year Member



It boils down to handling two incrementing arrays in js and that's a topic for another post. Thanks again for the eye opener.

[edited by: jatar_k at 2:07 pm (utc) on Feb. 14, 2008]
[edit reason] no urls thanks [/edit]

 

Featured Threads

Hot Threads This Week

Hot Threads This Month