homepage Welcome to WebmasterWorld Guest from 54.237.98.229
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Put result of sql query into javascript array
I'm in a bind and learning the hard way
androo

5+ Year Member



 
Msg#: 3573562 posted 5:47 am on Feb 13, 2008 (gmt 0)

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.

 

androo

5+ Year Member



 
Msg#: 3573562 posted 6:01 am on Feb 13, 2008 (gmt 0)

I think I've got a handle on it now so never mind.

androo

5+ Year Member



 
Msg#: 3573562 posted 7:38 am on Feb 13, 2008 (gmt 0)

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++;
}
?>

androo

5+ Year Member



 
Msg#: 3573562 posted 8:04 am on Feb 13, 2008 (gmt 0)

<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]

phparion

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3573562 posted 8:07 am on Feb 13, 2008 (gmt 0)

can you use php code like this i javascript ? I am a little confused.

androo

5+ Year Member



 
Msg#: 3573562 posted 8:46 am on Feb 13, 2008 (gmt 0)

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.

whoisgregg

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



 
Msg#: 3573562 posted 2:06 pm on Feb 13, 2008 (gmt 0)

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>';
?>

androo

5+ Year Member



 
Msg#: 3573562 posted 2:35 pm on Feb 13, 2008 (gmt 0)

That looks much cleaner. Much obliged.

androo

5+ Year Member



 
Msg#: 3573562 posted 3:59 am on Feb 14, 2008 (gmt 0)

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).')';

androo

5+ Year Member



 
Msg#: 3573562 posted 5:04 am on Feb 14, 2008 (gmt 0)

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'.

androo

5+ Year Member



 
Msg#: 3573562 posted 6:04 am on Feb 14, 2008 (gmt 0)

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]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved