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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script works on its own but not in my page

Status
Not open for further replies.

dongbamage

Programmer
Nov 3, 2004
32
GB
Hi peeps,

I made a little script that calls photo's from my MySQL db, then passes them from a PHP array to JS.
The problem I'm having is although it works if run directly, when i add it to my mainpage, it doesn't work...

Anyone got any ideas?

Here's my code..

<?
include_once
$arraycount=0;
$counter++;
$counter2=($counter+1);
$counter3=($counter+2);
$sql= "select StockID, Photo, StockName from stock";
$result = mysql_query($sql) or die ("dead");
while($row=mysql_fetch_array($result))
{

$image=$row["Photo"]."catthumb.gif";

$StockID=$row["StockID"];

$linkStockID=$stockID;
$StockName=$row["StockName"];

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

}

?>

<script language="JavaScript">

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); ?>");


var ad_num = getAdNum();


document.write('<CENTER><TABLE CELLPADDING=0 CELLSPACING=1 BORDER=0><TR><TD '
+'ALIGN=CENTER><FONT SIZE=2 FACE=Arial><B>Click for details! '
+'</FONT></TD><TR></TR><TD><A HREF="'+ads[ad_num].href+'"><IMG SRC="'+ads[ad_num].src+'" '
+'WIDTH="100" HEIGHT="60" BORDER=0 name=js_ad></A></TD></TR><TR></TR><TD><A HREF="'+ads[ad_num+1].href+'"><IMG SRC="'+ads[ad_num+1].src+'" '
+'WIDTH="100" HEIGHT="60" BORDER=0 name=js_ad1></A></TD></TR><TR></TR><TD><A HREF="'+ads[ad_num+2].href+'"><IMG SRC="'+ads[ad_num+2].src+'" '
+'WIDTH="100" HEIGHT="60" BORDER=0 name=js_ad2></A></TD></TR></TABLE></CENTER>');
link_num = document.links.length-1;
document.js_ad.src = ads[ad_num].src;
document.js_ad1.src = ads[ad_num+1].src;
document.js_ad2.src = ads[ad_num+2].src;
document.links[0].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num].href;
document.links[1].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+1].href;
document.links[2].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+2].href;

function rotateSponsor() {
if (document.images) {
ad_num = (ad_num+1)%ads.length;
document.js_ad.src = ads[ad_num].src;
document.js_ad1.src = ads[ad_num+1].src;
document.js_ad2.src = ads[ad_num+2].src;
document.links[0].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num].href;
document.links[1].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+1].href;
document.links[2].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+2].href;


setTimeout("rotateSponsor()",5000);

}
}
setTimeout("rotateSponsor()",5000);
// -->
</script>
 
What happens? Do you get an error? Can you post the code that gets sent to the client after it's parsed by PHP?

Adam
"Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done." Andy Rooney.
 
Maybe this statement creates an array with one element.
Code:
var ads=new adArray("<?=implode("\",\"", $array); ?>");
ads[0] = the string generated by implode("\",\"", $array)


How does it work without the quotemarks?
Code:
var ads=new adArray(<?=implode("\",\"", $array); ?>);


Remember that the PHP merely generates a stream of text; it does not in any way communicate with the browser, nor with the Javascript which runs on the client.

 
No, that wouldn't fix it:
Code:
Contents of $array:
Array
(
    [0] => "a"
    [1] => "b"
    [2] => "c"
)

var x = new Array(<? implode("\",\"", $array); ?>);

returns

var x = new Array(a","b","c);

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
ahah! its actually changing the other links in the page rather than the links i want to change

document.links[0].href

so how how can I make the above code work with just the links I want to change?

I have tried:

document.js_ad.links[0].href

but that doesn't work...
 
ok so i am trying this:


<?
include_once("/files/home3/munchexpress/Includes/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 ("dead");
while($row=mysql_fetch_array($result))
{

$image=$row["Photo"]."catthumb.gif";

$StockID=$row["StockID"];

$linkStockID=$stockID;
$StockName=$row["StockName"];

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

}

?>

<script language="JavaScript">

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); ?>");


var ad_num = getAdNum();


this.form.write('<CENTER><TABLE CELLPADDING=0 CELLSPACING=1 BORDER=0><TR><TD '
+'ALIGN=CENTER><FONT SIZE=2 FACE=Arial><B>Click for details! '
+'</FONT></TD><TR></TR><TD><A HREF="'+ads[ad_num].href+'"><IMG SRC="'+ads[ad_num].src+'" '
+'WIDTH="100" HEIGHT="60" BORDER=0 name=js_ad></A></TD></TR><TR></TR><TD><A HREF="'+ads[ad_num+1].href+'"><IMG SRC="'+ads[ad_num+1].src+'" '
+'WIDTH="100" HEIGHT="60" BORDER=0 name=js_ad1></A></TD></TR><TR></TR><TD><A HREF="'+ads[ad_num+2].href+'"><IMG SRC="'+ads[ad_num+2].src+'" '
+'WIDTH="100" HEIGHT="60" BORDER=0 name=js_ad2></A></TD></TR></TABLE></CENTER>');
link_num = this.form.links.length-1;
this.form.js_ad.src = ads[ad_num].src;
this.form.js_ad1.src = ads[ad_num+1].src;
this.form.js_ad2.src = ads[ad_num+2].src;
this.form.links[0].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num].href;
this.form.links[1].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+1].href;
this.form.links[2].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+2].href;

function rotateSponsor() {
if (document.images) {
ad_num = (ad_num+1)%ads.length;
this.form.js_ad.src = ads[ad_num].src;
this.form.js_ad1.src = ads[ad_num+1].src;
this.form.js_ad2.src = ads[ad_num+2].src;
this.form.links[0].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num].href;
this.form.links[1].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+1].href;
this.form.links[2].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+2].href;


setTimeout("rotateSponsor()",5000);

}
}
setTimeout("rotateSponsor()",5000);
// -->
</script>

</form>
 
this gives me:

this.form.links is null or not an object

<?
include_once("/files/home3/munchexpress/Includes/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 ("dead");
while($row=mysql_fetch_array($result))
{

$image=$row["Photo"]."catthumb.gif";

$StockID=$row["StockID"];

$linkStockID=$stockID;
$StockName=$row["StockName"];

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

}

?>

<script language="JavaScript">

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); ?>");


var ad_num = getAdNum();


document.write('<form><CENTER><TABLE CELLPADDING=0 CELLSPACING=1 BORDER=0><TR><TD '
+'ALIGN=CENTER><FONT SIZE=2 FACE=Arial><B>Click for details! '
+'</FONT></TD><TR></TR><TD><A HREF="'+ads[ad_num].href+'"><IMG SRC="'+ads[ad_num].src+'" '
+'WIDTH="100" HEIGHT="60" BORDER=0 name=js_ad></A></TD></TR><TR></TR><TD><A HREF="'+ads[ad_num+1].href+'"><IMG SRC="'+ads[ad_num+1].src+'" '
+'WIDTH="100" HEIGHT="60" BORDER=0 name=js_ad1></A></TD></TR><TR></TR><TD><A HREF="'+ads[ad_num+2].href+'"><IMG SRC="'+ads[ad_num+2].src+'" '
+'WIDTH="100" HEIGHT="60" BORDER=0 name=js_ad2></A></TD></TR></TABLE></CENTER></form>');
link_num = this.form.links.length-1;
document.src = ads[ad_num].src;
document.js_ad1.src = ads[ad_num+1].src;
document.js_ad2.src = ads[ad_num+2].src;
this.form.links[0].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num].href;
this.form.links[1].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+1].href;
this.form.links[2].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+2].href;

function rotateSponsor() {
if (document.images) {
ad_num = (ad_num+1)%ads.length;
document.js_ad.src = ads[ad_num].src;
document.js_ad1.src = ads[ad_num+1].src;
document.js_ad2.src = ads[ad_num+2].src;
this.form.links[0].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num].href;
this.form.links[1].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+1].href;
this.form.links[2].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+2].href;


setTimeout("rotateSponsor()",5000);

}
}
setTimeout("rotateSponsor()",5000);

// -->
</script>

</form>
 
Can you just post the code after the PHP? (i.e. View Source on the page)

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
ok heres the view source version:

Code:
<HTML>
<HEAD>

<style type="text/css">
<!--
body,td,th {
border-color:#FFFF66;

}
body {
	 background-image: url(/Images/blankpanelscrews.gif);
		color: #00CC66;

}
-->
</style></HEAD>
<TITLE>mysite.co.uk</TITLE>

<BODY style="overflow-x:hidden;" >
<P ALIGN=center>
<div  style=" background-image:url(/Images/cartoonscreen.gif); LEFT: 209px; WIDTH: 65%; border-style:ridge; POSITION: absolute; TOP: 31px; HEIGHT: 479px; overflow-y:hidden;"></div>

</P>




    <div align="center" style="background-image:url(/Images/Titlepanel.gif); width:20%; position:absolute; top:-10px; left: 10px; background-repeat:no-repeat; background-position:center; height:20%; vertical-align:center;"><strong>Planet Poi.co.uk</strong>
	</div>


    <div align="center" style="height:22%; position:absolute; top:103px; width:20%; vertical-align:middle; background:url(/Images/logpanel.gif); background-repeat: no-repeat; left: 2px;">
	 You are not logged in</div>
	<div style="background:url(/Images/Messagepanel.gif); z-index:1; background-repeat: no-repeat; height:22%; position:absolute; left:212px; top:6px; width:689px;">
	</div>
	<div align="center" style=" vertical-align:center; background:url(/Images/Shoppanel.gif); background-repeat: no-repeat; position:absolute; height:5%; width:12.5%; top:34px; left:868px; border-style:dashed;"><font size="2">Shopping Cart</font>
	</div>


		
<div align="center" style="background:url(../Images/Navback.gif); background-repeat: no-repeat; POSITION: absolute; LEFT: 0px;  TOP: 131px;  WIDTH: 22%; height:44.7% ;">
			<p align="center"><a href="?linkdata=LogIn">LogIn</a></p>

<p align="center"><a href="?linkdata=Lessons">Lessons</a></p>
<p align="center"><a href="?linkdata=HomePage">Home</a></p>
<p align="center"><a href="?linkdata=BrowseStockCategories">Shop</a></p>
<p align="center"><a href="?linkdata=Forums">Forums</a></p>
<p align="center">Search</p>
<p align="center">
<input name="txtSearch" type="text" id="txtSearch" value="Search" maxlength="255" size="8%">
</p>
<p align="center"><a href="?linkdata=Links">Links</a></p></td>



</div>
	
   <div style="Z-INDEX: 1; POSITION: absolute; LEFT: 217px;  TOP: 36px; HEIGHT: 469px; WIDTH: 63%; margin:margin-bottom:200px; overflow-x:hidden; overflow-y:auto; scrollbar-base-color: #3366CC">
   <div><a href='?linkdata=HomePage'>Home</a>/<a href='?linkdata='></a></div>		<div style="height:105%; width:100%; border-style:solid; margin=0; align:center; valign:center;">
   Welcome to planet poi the most groovy poi website EVER made! mwuhahahahah! 	</p>
	</div>
</div>
</div>
	

		
		<div align="center" style="background:url(/Images/Shoppanelmain.gif); border-style:ridge; background-repeat: no-repeat; vertical-align:middle; position:absolute; left:86%; width:13%; top:71px; height:16%; font-size:9px; ">
		<br><br>your cart <br> is empty!	</div>




   <div style="position:absolute; height:50%; width:13%; top:211px; left:862px; border-style:ridge; background:url(/Images/adpanel.gif); background-repeat: no-repeat;">

<script language="JavaScript">

function adArray() {
 for (i=0; i*2<adArray.arguments.length; i++) {
  this[i] = new Object();
  this[i].src = adArray.arguments[i*2];
  this[i].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("/Images/Logocatthumb.gif","10","/Images/nacatthumb.gif","3","/Images/na.gifcatthumb.gif","11","/Images/na.gifcatthumb.gif","12","na.gifcatthumb.gif","5","na.gifcatthumb.gif","8","na.gifcatthumb.gif","7");


var ad_num = getAdNum(); 


document.write('<form><CENTER><TABLE CELLPADDING=0 CELLSPACING=1 BORDER=0><TR><TD '
+'ALIGN=CENTER><FONT SIZE=2 FACE=Arial><B>Click for details! '
+'</FONT></TD><TR></TR><TD><A HREF="'+ads[ad_num].href+'"><IMG SRC="'+ads[ad_num].src+'" '
+'WIDTH="100" HEIGHT="60" BORDER=0 name=js_ad></A></TD></TR><TR></TR><TD><A HREF="'+ads[ad_num+1].href+'"><IMG SRC="'+ads[ad_num+1].src+'" '
+'WIDTH="100" HEIGHT="60" BORDER=0 name=js_ad1></A></TD></TR><TR></TR><TD><A HREF="'+ads[ad_num+2].href+'"><IMG SRC="'+ads[ad_num+2].src+'" '
+'WIDTH="100" HEIGHT="60" BORDER=0 name=js_ad2></A></TD></TR></TABLE></CENTER></form>');
link_num = this.form.links.length-1;
 document.src = ads[ad_num].src;
  document.js_ad1.src = ads[ad_num+1].src;
  document.js_ad2.src = ads[ad_num+2].src;
  this.form.links[0].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num].href;
  this.form.links[1].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+1].href;
  this.form.links[2].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+2].href;

function rotateSponsor() {
 if (document.images) {
  ad_num = (ad_num+1)%ads.length;
  document.js_ad.src = ads[ad_num].src;
  document.js_ad1.src = ads[ad_num+1].src;
  document.js_ad2.src = ads[ad_num+2].src;
  this.form.links[0].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num].href;
  this.form.links[1].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+1].href;
  this.form.links[2].href = "../fargle2.php?Prodlink=ProductShow?id="+ads[ad_num+2].href;
  
  
  setTimeout("rotateSponsor()",5000);
  
 }
}
setTimeout("rotateSponsor()",5000);

// -->
</script>

</form>   </div>

</div>
 
		
		<div align="left" style=" position:absolute; width:30%; left:0%; top:90%;">Webdesign - sic Graffix</div> 
 		<div align="center" style=" position:absolute; left:35%;width:30%; top:90%;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<style type="text/css">
<!--
body,td,th {
	color: #FFCC00;
}
body {
	background-color: #000099;
}
-->
</style></head>

<body>
<div align="center">
  <p>&nbsp;</p>
  <p>(c)unlesss otherwise stated, All rights reserved </p>
</div>
</body>
</html>
 </div>
		<div align="right" style=" position:absolute; left:70%; width:30%; top:90%;"> wonder what goes here?</div>
		</div>

</BODY>
</HTML>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top