Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Member TheGardener's Avatar
    Join Date
    Jun 2006
    Posts
    62
    Member #
    13332
    Hey everyone,

    I'm a beginning PHP coder with just one last hurdle to pass before I'm finished coding for my site. Below is part of a script that walks through a directory full of images, thumbnails and image captions, posting links to them on an iframe in the main page.

    PHP Code:

            
    for($k=0;$kcount($files);$k+=3) {
                            echo 
    "<a href='#".$files[$k]."' name='".$files[$k]."' onclick='javascript:switchPicNCaption(path.concat(this.name), invDivName.concat(".$k.")); return false;'><img src='".$files[$k+1]."' height='100px' border='0'></a>&nbsp;&nbsp;";
                            echo 
    "<div class='inv' id='caption".$k."'>";
                            include 
    $files[$k+2];
                            echo 
    "</div>";
                            
                            
    /* Display the first pic in the gallery in the frame above. */
                            
    if ($k==0) { echo "<script type='text/javascript'> switchPicNCaption(path.concat('".$files[$k]."'), invDivName.concat('".$k."')); return false; </script>";    }
                        } 
    The naming convention for images is like "1.jpg," "1 thumbnail.jpg," "1.txt," thus I process the files in groups of three with this for loop.

    Everything you see above works correctly except for the last "if" statement. The purpose of that statement is to automatically display the first image in a section of the gallery when that gallery section is selected. The syntax for the second switchPicNCaption declaration looks like it should be right compared to what's above, but for some reason it doesn't work. I'm sure there's some obvious mistake I'm making here. Does anyone know what it is? Thanks.

  2.  

  3. #2
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    A wild guess would be that you're passing $k in as a string the second time, though that shouldn't make too much of a different in Javascript... Unless you're dealing with concatenation vs addition later on in the script.

    The real question is, is the code that is generated different? If not, this isn't a PHP issue but rather a Javascript one, and the bug shifts to something else entirely.

  4. #3
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    As a pointless nitpick, it's Javascript, not Java. The two are completely different.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  5. #4
    Member TheGardener's Avatar
    Join Date
    Jun 2006
    Posts
    62
    Member #
    13332
    Ok, I figured it out by looking at the code output. Looks like it was a syntax problem with the PHP. The correct syntax for the line is:

    PHP Code:
                    if ($k==0) { echo "<script type='text/javascript'>switchPicNCaption(path.concat(\"$files[$k]\"), invisibleDivName.concat(".$k."));</script>"; } 
    However, having come further along with the site I've run into a very daunting Javascript problem, so I'll head to that forum now...

  6. #5
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Well, at a glance, it looks like what really got fixed there was what I mentioned about using single quotes around the value of $k. You switched .concat('".$k."') to remove the single quotes. Putting the $file[] stuff inside the quotations should have made no difference; in fact, when you use array notation inside a double-quoted string, it's a good ideato surround the whole thing with {} (i.e. `"This is a string with an {$arr['index']}"'). It works fine without it in your case, but there are subtle cases where it might work strangely, so it's a better idea to do all arrays like above or with separate concatenation using `.'.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 09:13 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com