json tutorial for beginners part 5 learn Dynamic Ajax PHP Photo Gallery



Lesson Code: http://www.developphp.com/video/JavaScript/Dynamic-Photo-Gallery-JSON-Ajax-PHP-Tutorial
Part 5 of learning JSON for beginners with Adam. In this lesson we demonstrate how to build a very lightweight dynamic photo gallery using Ajax, PHP and JSON. JSON stands for JavaScript Object Notation. It is a lightweight format for creating and sending data objects between the server and your client-side applications. JSON is a part of JavaScript and not a separate entity, the notation syntax is built into the raw Javascript programming language as a subset.

source

20 thoughts on “json tutorial for beginners part 5 learn Dynamic Ajax PHP Photo Gallery”

  1. As much as I like your teaching style, why oh why are you using this hellish inconsistent pile of shite called PHP for the server side? I mean, PHP is just a glorified templating system that went megalomaniac. You should use Python.

  2. My code just outputs it to the browser. I'm using xampp on chrome browser I looked through my code for maybe 25 mins and can't see the problem.

    <?php
     header("Content-Type: application/json");//lets browser know this has official json data in it
     $folder = $_POST["folder"]; // getting the ajax request which will the gallery1 in our case
     //$folder = "gallery1";
     $jsonData = '{'; //setting upt to render as json
     $dir = $folder."/"; //specify where the folder is located
     // open specified directory using opendir() the function
    $dirHandle = opendir($dir); 

    // Create incremental counter variable if needed
    $i = 0;
    while ($file = readdir($dirHandle)) { 
          // if file is not a folder and if file name contains the string .gif  
          if(!is_dir($file) && strpos($file, '.JPG')){
             $i++; // increment $i by one each pass in the loop
             $src = "$dir$file"; //says gallery1/somefile.gif
             $jsonData .= '"img'.$i.'":{ "num":"'.$i.'","src:"'.$src.'", "name":"'.$file.'" },';  //we pack in all of the deeply nested objects
          } // close the if statement
    } // End while loop
    closedir($dirHandle); // close the open directory
    $jsonData = chop($jsonData, ",");

    $jsonData .= "}";
    echo $jsonData; 
    ?>

  3. Hi, this failed for me at 20 minutes, displaying the php file.  Finally found that strpos($file, '.jpeg') does not work, not sure if deprecated but what did work was:  pathinfo($file)["extension"] == "jpg" 

  4. If you get empty { } when you run your php file (testing) – change .jpg to capital .JPG – checking file extension in the if statement seems to be case sensitive. It solved my problem.

  5. Your php code works but the part about handling the folder and outputting the jpeg files won't work. Check if you have the directory correct, or if you appropriately have the jpeg files in the right folder, or if you have the directory handling do its job. Php is easy to debug by echoing different parts to see if they're working or not so you might as well as echo test if the while loop actually runs or if the if statement is right.

Leave a Reply

Your email address will not be published. Required fields are marked *