Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass a php array into Javascript

Status
Not open for further replies.

dongbamage

Programmer
Nov 3, 2004
32
GB
Hey guys, I'm having trouble passing a php array into a javascript array.

I am trying to use implode to pass the php array, but its not working properly.

var ads=new adArray("<?=implode(",", $array); ?>");

Can anyone see where I'm going wrong?


Here is my code:

<?
include ('db_connect.php');
$arraycount=0;
$counter++;
$counter2=($counter+1);
$counter3=($counter+2);
$sql= "select StockID, Photo, StockName from stock";
$result = mysql_query($sql) or die ("shit");
while($row=mysql_fetch_array($result))
{
echo ARRAYCOUNT;
echo $arraycount;

$image=$row["Photo"]."catthumb.gif";
echo " IMAGE:";
echo $image;
$StockID=$row["StockID"];
echo " STOCKID:";
$linkStockID=$stockID;
$StockName=$row["StockName"];
echo NAME;
echo $StockName;
$array[$arraycount]["src"]=$image;

$array[$arraycount]["href"]=$StockID;
#echo ("<a href='?Prodlink=ProductShow?id=$StockID'>".
#"<img src='$image'> </a>");



echo " HREF: ";
echo $array[$arraycount][href];
echo " SRC: ";
echo $array[$arraycount][src];

$arraycount++;
}

print_r($array);

?>

<script language="JavaScript">
// Copyright 1996, Infohiway, Inc. (// Courtesy of SimplytheBest.net - <!--

function adArray() {
for (i=0; i*2<adArray.arguments.length; i++) {
this = new Object();
this.src = adArray.arguments[i*2];
this.href = adArray.arguments[i*2+1];
}
this.length = i;
}
function getAdNum() {
dat = new Date();
dat = (dat.getTime()+"").charAt(8);
if (dat.length == 1)
ad_num = dat%ads.length;
else
ad_num = 0;
return ad_num;
}

var ads=new adArray("<?=implode(",", $array); ?>");

document.write (ads[1].href);
document.write ("booo");
document.write(ads[1].src);
document.write(ads[1].href);

var ad_num = getAdNum();


document.write('<CENTER><TABLE CELLPADDING=0 CELLSPACING=1 BORDER=0><TR><TD '
+'ALIGN=CENTER><FONT SIZE=2 FACE=Arial><B>SimplytheBest DHTML Scripts & JavaScripts is sponsored by: '
+'</FONT></TD><TR></TR><TD><A HREF="'+ads[ad_num].href+'"><IMG SRC="'+ads[ad_num].src+'" '
+'WIDTH="468" HEIGHT="60" BORDER=0 name=js_ad></A></TD></TR></TABLE></CENTER>');
link_num = document.links.length-1;
function rotateSponsor() {
if (document.images) {
ad_num = (ad_num+1)%ads.length;
document.js_ad.src = ads[ad_num].src;
document.links[link_num].href = ads[ad_num].href;
setTimeout("rotateSponsor()",5000);
}
}
setTimeout("rotateSponsor()",5000);
// -->
</script>
 
Try forum434. Incidentally, what your function looks like it prints is this:

Code:
// print_r($array) in my example
Array
{
    [0] => Hello
    [1] => world,
    [2] => How
    [3] => ya
    [4] => doin?
}

// result:
var ads=new adArray("Hello,world,,How,ya,doin?");

// what you want
var ads=new adArray("Hello","world,","How","ya","doin?");

// Maybe
var ads=new adArray("<?=implode("[red]\",\"[/red]", $array); ?>");
// would work



--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top