/    Sign up×
Community /Pin to ProfileBookmark

Repeating array problem

Hi All, this looks like the place to be!

I am fairly new to programming and I have come to a problem that I know someone will have the simplest answer in the world to.

I am building a real estate website in php(ish) and drawing my property data from a Mysql database. On the property viewing page I have set up a repeating table that works fine, it pulls in the pictures fine BUT…

I have the following mouseover script that also works fine if there is only one property but changes only the “CurrentImage” on the last property when it is repeated no mater which thumbnail you cover.

Did I explain that well? No I agree, take a look at the page:

[url]www.test.pinsapoestates.com/props.php[/url]

[B]Here is the script:[/B]

<script language=”JavaScript”>
var imageArray = Array(
‘<?php echo $row_Recordset1[‘pic1′]; ?>’,'<?php echo $row_Recordset1[‘pic2′]; ?>’,'<?php echo $row_Recordset1[‘pic3′]; ?>’,'<?php echo $row_Recordset1[‘pic4′]; ?>’
);
var imageID = 1;

function SwitchImage(id) {
imageID = id;
DisplayImage();
}
function DisplayImage() {
if(document.images)
document.images[‘currentImage’].src = imageArray[imageID-1];

}
</script>

[B]Here is the framework with the script:[/B]

<?php do { ?>
<script language=”JavaScript”>
var imageArray = Array(
‘<?php echo $row_Recordset1[‘pic1′]; ?>’,'<?php echo $row_Recordset1[‘pic2′]; ?>’,'<?php echo $row_Recordset1[‘pic3′]; ?>’,'<?php echo $row_Recordset1[‘pic4′]; ?>’
);
var imageID = 1;

function SwitchImage(id) {
imageID = id;
DisplayImage();
}
function DisplayImage() {
if(document.images)
document.images[‘currentImage’].src = imageArray[imageID-1];

}
</script>

<table width=”92%” height=”409″ border=”0″ cellpadding=”2″ cellspacing=”2″>
<tr>
<td height=”405″ align=”left” valign=”top”> <table width=”510″ height=”406″ border=”0″ align=”left” cellpadding=”2″ cellspacing=”0″>
<tr align=”center” valign=”middle”>
<td colspan=”4″ rowspan=”2″ bordercolor=”#333333″ bgcolor=”#333333″ class=”globalboxfilled”>
<div align=”center”><img src=”<?php echo $row_Recordset1[‘pic1’]; ?>” alt=”Image Viewer” width=”236″ height=”207″ border=”0″ align=”middle” id=”currentImage” title=””><br>
<font color=”#FFFFFF” size=”1″ face=”Verdana”><strong>PASS
OVER SMALL PHOTOS TO ENLARGE</strong></font> </div></td>
<td width=”3″ rowspan=”2″ bgcolor=”#333333″>&nbsp;</td>
<td height=”109″ colspan=”2″ bordercolor=”#333333″ bgcolor=”#333333″ class=”globalboxfilled”><img onMouseOver=”SwitchImage(1);” width=”118″ height=”105″ border=”0″ alt=”Image Viewer” title=”” src=”<?php echo $row_Recordset1[‘pic1’]; ?>” style=”cursor: hand”></td>
<td width=”126″ height=”109″ bordercolor=”#333333″ bgcolor=”#333333″ class=”globalboxfilled”><img onMouseOver=”SwitchImage(2);” width=”118″ height=”105″ border=”0″ alt=”Image Viewer” title=”” src=”<?php echo $row_Recordset1[‘pic2’]; ?>” style=”cursor: hand”></td>
</tr>
<tr align=”left” valign=”top”>
<td height=”109″ colspan=”2″ align=”center” valign=”middle” bordercolor=”#333333″ bgcolor=”#333333″ class=”globalboxfilled”><img onMouseOver=”SwitchImage(3);” width=”118″ height=”105″ border=”0″ alt=”Image Viewer” title=”” src=”<?php echo $row_Recordset1[‘pic3’]; ?>” style=”cursor: hand”></td>
<td height=”109″ align=”center” valign=”middle” bordercolor=”#333333″ bgcolor=”#333333″ class=”globalboxfilled”><img onMouseOver=”SwitchImage(4);” width=”118″ height=”105″ border=”0″ alt=”Image Viewer” title=”” src=”<?php echo $row_Recordset1[‘pic4’]; ?>” style=”cursor: hand”></td>
</tr>

I have tried several ways and put the script in several places with different and sometimes interesting effect! but none of them correct. All other details pulled onto the page are correct.

Any Offers?

Sorry if I have posted incorrectly, I have never needed to ask for help before on a notice board (all self taught you know, and I am an old fella) so please excuse me.

Cheers

Andy

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Dec 28.2005 — You copy and pasted a script multiple times that was not meant to be used more than once on the page. You can either change the variable names and image names each time, or you are going to have to find someone nice enough to make it work with your page. I am too busy to do that right now!

Eric
Copy linkTweet thisAlerts:
@Andy_in_SpainauthorDec 29.2005 — Thanks, I have tried several ideas now with more or less the same results so I wonder if and where I might change the variable names, it may work.

What does surprise me is that it actually pulls the correct pic out of the database but only changes the last one, how could I introduce a variable into the code or is there another way to do this in either javascript or php for instance?

Again any ideas are more than welcome
Copy linkTweet thisAlerts:
@Andy_in_SpainauthorDec 29.2005 — So, as there are no more suggestions from the experts out there, does that mean it can't be done?

Are you telling me that this is actually impossible? Well I Never!!!
Copy linkTweet thisAlerts:
@A1ien51Dec 29.2005 — an example of WHY your code does not work. Lets take you code and bunch it up. I am too lazy to do some copy and pasting so I will just act like the hi is your array.

<script>

var hi="1";

var hi="2";

var hi="3";

var hi="4";

alert(hi);

</script>

Now why does it alert show 4? It is because the variables are being overwritten each time you declare it. The script has no clue what you are doing. It is as only as smaert as the programmer makes it.

So to get around your problem you need to avoid the overwriting of the variables. One way is to use an array to hold your different sets of data.

<i>
</i>var arrImages = new Array();
arrImages["group1"] = new Array("1.gif","2.gif","3.gif","4.gif");
arrImages["group2"] = new Array("1.gif","2.gif","3.gif","4.gif");
arrImages["group3"] = new Array("1.gif","2.gif","3.gif","4.gif");
arrImages["group4"] = new Array("1.gif","2.gif","3.gif","4.gif");

function SwitchImage(group,id) {
if(document.images)document.images[group].src = arrImages[group][id];
}


now you name your main images "group1","group2","group3",etc and the mouseovers look like SwitchImage('group1',0);

Hopefull you can figure out the rest from there.

Eric
Copy linkTweet thisAlerts:
@Andy_in_SpainauthorDec 29.2005 — Thanks Eric.

This in effect then means that, although this info is being pulled from a Mysql database on a repeated region (table) of a dynamic php page, each time the database is updated I would have to put a new set of variables into the page which negates the advantages of using a database in the first place and I may as well just update an html page instead. Or am I reading this wrong?

Oh yes, I changed the script a little to add a 5th picture and also moved the script into the head section.

Sorry for being a wus but I just can't figure it. thanks anyway and have a peaceful new year ?
×

Success!

Help @Andy_in_Spain spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...