PDA

View Full Version : Dynamically populating array



So_Cute
05-02-2006, 07:30 PM
Hi. I'm using this script http://www.dynamicdrive.com/dynamicindex4/imageselector.htm.

I've stored the image names and descriptions in a MySQL database. I've got it pulling the image names and descriptions to populate the select menu, but I can't seem to get the results into the array for the description beneath the photo.

Here is the part I need help with:

My variable is:

$desc = $row_Recordset1['desc'];

var description=new Array()
description[0]="$desc"; //looped results here.

Any help would be greatly appreciated.

Twey
05-02-2006, 07:46 PM
description[0]="<?php echo($row_Recordset1['desc']; ?>";Or am I misunderstanding you?

So_Cute
05-02-2006, 08:07 PM
Hi Twey,

Thanks. That's close, and it's not far from what I was using before, when I had a specific #of results returned from the query.

I could write it like
description[0] = "$desc1";
description[1] = "$desc2";
description[3] = "$desc3";
and so on...

But since I've changed the way the table is set up in the database, I need to be able to loop through the recordset and output it into the javascript array.

I think what I'm looking for is how to write the loop, something like

$desc = the recordset

var description=new Array()
description[$i]="$desc"; //looped results here.

I just don't know how to write it.

djr33
05-02-2006, 08:39 PM
for/while loop, for the number of items in the array, echoing like twey had, but also with the array thing.

Twey
05-02-2006, 10:00 PM
Aha, I see.
<?php while($row_Recordset1 = mysql_fetch_array($desc)) { ?>
description[0]="<?php echo($row_Recordset1['desc']; ?>";
<?php } ?>

djr33
05-02-2006, 10:53 PM
The array number needs to change as well, though...

description[1], [2], ...

Twey
05-02-2006, 10:59 PM
Yes... of course it does. Sorry.
<?php for($i=0;$row_Recordset1 = mysql_fetch_array($desc);$i++) { ?>
description[<?php echo($i); ?>]="<?php echo($row_Recordset1['desc']); ?>";
<?php } ?>

So_Cute
05-03-2006, 12:05 AM
OK, I understand the logic of your code, but I'm still having trouble getting it inside the script. I'm sure I'm doing something stupid.

Here's what I have now:


<?php for($i=0;$row_Recordset1 = mysql_fetch_array($Recordset1);$i++) { ?>
<script>
//Dynamic Image selector Script- Dynamic Drive (www.dynamicdrive.com)
//For full source code, installation instructions,
//100's more DHTML scripts, visit dynamicdrive.com

//enter image descriptions ("" for blank)
var description=new Array()
description[<?php echo($i); ?>]="<?php echo($row_Recordset1['desc']; ?>";

var ie4=document.all
var ns6=document.getElementById
var tempobj=document.dynamicselector.dynamicselector2
if (ie4||ns6)
var contentobj=document.getElementById? document.getElementById("dynamic3"): document.all.dynamic3
function generateimage(which){
if (ie4||ns6){
contentobj.innerHTML='<center>Loading image...</center>'
contentobj.innerHTML='<center><img src="'+which+'"><br><br>'+description[tempobj.options.selectedIndex]+'</center>'
}
else if (document.layers){
document.dynamic1.document.dynamic2.document.write('<center><img src="'+which+'"><br><br>'+description[tempobj.options.selectedIndex]+'</center>')
document.dynamic1.document.dynamic2.document.close()
}
else
alert('You need NS 4+ or IE 4+ to view the images!')
}

function generatedefault(){
generateimage(tempobj.options[tempobj.options.selectedIndex].value)
}

if (ie4||ns6||document.layers){
if (tempobj.options.selectedIndex!=-1){
if (ns6)
generatedefault()
else
window.onload=generatedefault
}
}
</script>
<?php } ?>

Thanks for all your help so far guys.

djr33
05-03-2006, 06:02 AM
Ha. Wow.

What you've got there is a for loop. That REPEATS 'for' the particular settings.

What you are doing is EVERY time it repeats, outputting that ENTIRE javascript. (over and over)

Note that php is interpreted BEFORE javascript, so you can use it INSIDE the JS.

You want to move those commands INTO the javascript... just surrounding the PARTICULAR line(s) that need to be repeated.

Make sense?

So_Cute
05-03-2006, 05:14 PM
YES!! Thank you so much. I FINALLY got it!

What I ended up doing was using PHP to echo the Javascript and breaking it (the Javascript code) up so that I could insert the PHP code where I needed it.

Here's my array/loop:


echo "var description = new Array();\n";
if($totalRows_Recordset1 > 0) {
mysql_data_seek($Recordset1,0);
for($i=0;$row_Recordset1 = mysql_fetch_array($Recordset1);$i++) {
echo "description[" . $i . "]=\"" . $row_Recordset1['description'] . "\"\n";
}
}


I had to make a few other changes, like adding in mysql_data_seek to reset the pointer to the first row of the recordset and fix an error (an extra opening parenthesis) in the PHP code, but I couldn't have done it without your help.

Thank you very much. :D