Results 1 to 10 of 10

Thread: Dynamically populating array

  1. #1
    Join Date
    Jan 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dynamically populating array

    Hi. I'm using this script http://www.dynamicdrive.com/dynamici...geselector.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.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    description[0]="<?php echo($row_Recordset1['desc']; ?>";
    Or am I misunderstanding you?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jan 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    for/while loop, for the number of items in the array, echoing like twey had, but also with the array thing.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Aha, I see.
    Code:
    <?php while($row_Recordset1 = mysql_fetch_array($desc)) { ?>
      description[0]="<?php echo($row_Recordset1['desc']; ?>";
    <?php } ?>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The array number needs to change as well, though...

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

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes... of course it does. Sorry.
    Code:
    <?php for($i=0;$row_Recordset1 = mysql_fetch_array($desc);$i++) { ?>
      description[<?php echo($i); ?>]="<?php echo($row_Recordset1['desc']); ?>";
    <?php } ?>
    Last edited by Twey; 05-03-2006 at 05:16 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Jan 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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:

    Code:
    <?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.

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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?

  10. #10
    Join Date
    Jan 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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:

    Code:
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •