Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy add PHP array content to a Javascript array

    Hi,
    i can't find the mistake in my little script hope someone can help me.

    PHP Code:
    <?php

    /* -------------------- read thumbfolder -------------------- */
        
        
    function isRdyPfD($filename){ 

            if (
    $filename == '.' || $filename == '..') {
            
    // To-Top-Dir
            
    return false;
        }
        
        
    $ext explode(".",$filename);
        
    $ext $ext[sizeof($ext) - 1];
        
    $allowedformats = array (
            
    'jpg'
            
    'png',
            
    'jpeg',
            
    'gif'
        
    );
        
        return 
    in_array($ext,$allowedformats);
        } 

        function 
    getPicsfromDir($dir){ 
        
    /* array with names of the pictures in  $dir */

            
    if (is_dir($dir)) {
            if (
    $dh opendir($dir)) {
                    
    $filearray = array();
            while ((
    $file readdir($dh)) !== false) {
                if (
    isRdyPfD($file) === true) {
                    
    $filearray[] = $file;
                }
            }
            
    closedir($dh);
                    return 
    $filearray;
            }
        }
        else {
            return 
    false;
        }
        } 
    // End Function


        
    $thumbs getPicsfromDir("./images/thumbs/");
        

    /* -------------------- thumbfolder  -------------------- */    
        
    echo "<div id='thumbslider'>\n";
        echo 
    "<ul id='thumbs'>\n";
        for(
    $i 0$i count($thumbs); $i++){
                echo 
    "<li><img src=\"./images/thumbs/$thumbs[$i]\" onclick=\"thumbClick($i)\"  /></li>\n";
                
        }
        echo 
    "</ul>\n";
        echo 
    "</div>\n";
        




    /* -------------------- big size images folder -------------------- */
        
    $bigSizeImages getPicsfromDir("./images/");
        
    //print_r($bigSizeImages);
        
        
    $jsValue '';
        for (
    $j=0$j count($bigSizeImages); $j++){
                    
    $jsValue $jsValue $bigSizeImages[$j];
                    if (
    $j < (count($bigSizeImages)-1)) {
                            
    $jsValue $jsValue ",";
                    }
       }
        
    ?> 


    <script type="text/javascript">
            images = new Array(<?php echo $jsValue ?>);
            
        

        
        
        function thumbClick(pos){
                
                //alert(pos);
                alert(images[pos]);
                
        }

    </script>
    I can't trace the images array values?
    thanks for a feedback!!!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,643 Times in 4,605 Posts
    You don't have "..." or '...' around your array elements, so JS will barf on your feet when it sees something like
    Code:
    images = new Array(abc.jpg, xyz.jpg);
    JS *needs* to see
    Code:
    images = [ "abc.jpg", "xyz.jpg" ];
    I don't code in PHP, but I would think that all you need is this:
    Code:
        $jsValue = '';
        for ($j=0; $j < count($bigSizeImages); $j++)
        {
            $jsValue .= ( $j > 0 ? "," : "" ) . "'" . $bigSizeImages[$j] . "'";
       }
    And then
    Code:
    var images = [<?php echo $jsValue ?>];
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,643 Times in 4,605 Posts
    But you surely could have also done:
    Code:
    <script type="text/javascript">
    var images = [
    <?php
        for ($j=0; $j < count($bigSizeImages); $j++)
        {
            echo ( $j > 0 ? ",\n" : "" ) . "'" . $bigSizeImages[$j] . "'";
        }
    ?>
        ];
    ...
    Which would possibly be a shade faster, since we all know how bad it can be to create long strings via concatenation.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    freshUser (12-16-2010)

  • #4
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ..it works fine. thank you so much for your help!!!!!
    have a great day!

  • #5
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    You can also use a php built-in function that converts all php arrays to json arrays that can be used in javascript, it can also encode multidimensional array for example.

    This code:
    PHP Code:
    <?php
    $phpArray1 
    = array('hello','world');
    $phpArray2 = array(
      
    "example" => array("hello"=>"world"),
      
    "names" => array("john"=>"doe")
    );
    ?>
    <script type="text/javascript">
    var jsArray1 = <?php print json_encode($phpArray1); ?>;
    var jsArray2 = <?php print json_encode($phpArray2); ?>;
    window.onload = (function(){
      alert(jsArray1);
      alert(jsArray2);
    });
    </script>
    Results in:
    Code:
    <script type="text/javascript">
    var jsArray1 = ['hello','world'];
    var jsArray2 = {"examples":['hello','world'],"names":['john','doe']};
    window.onload = (function(){
      alert(jsArray1);
      alert(jsArray2.examples);
      alert(jsArray2.names);
    });
    </script>
    Last edited by DJCMBear; 12-16-2010 at 05:10 PM.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P


  •  

    Posting Permissions

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