Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. how to change PHP code

how to change PHP code

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
databasehelptutorialphpdata-structures
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wartotojas
    wrote on last edited by
    #1

    Hi everybody, I was implementing an image gallery from one of tutorials. In this tutorial PHP opens a directory and loops through it and outputs all image files. I am looking for code solution how to change this loop to output only those images that belongs to particular image album. There should be way to run SQL query and select files and then output them, problem is I have no idea how. When I upload image I store some data to following table:

    CREATE TABLE tbl_image (
    im_id INT NOT NULL AUTO_INCREMENT,
    im_user_name VARCHAR(25) NOT NULL,
    im_album_id INT NOT NULL,
    im_title VARCHAR(64) NOT NULL,
    im_description TEXT NOT NULL,
    im_type VARCHAR(30) NOT NULL,
    im_image VARCHAR(60) NOT NULL,
    im_date DATETIME NOT NULL,
    PRIMARY KEY(im_id)
    );

    here is the code piece that should be changed:

    <?php

    /* Configuration Start */

    $thumb_directory = 'img/thumbs';
    $orig_directory = 'img/original';

    $stage_width=600; // How big is the area the images are scattered on
    $stage_height=400;

    /* Configuration end */

    $allowed_types=array('jpg','jpeg','gif','png');
    $file_parts=array();
    $ext='';
    $title='';
    $i=0;

    /* Opening the thumbnail directory and looping through all the thumbs: */

    $dir_handle = @opendir($thumb_directory) or die("There is an error with your image directory!");

    $i=1;
    while ($file = readdir($dir_handle))
    {
    /* Skipping the system files: */
    if($file=='.' || $file == '..') continue;

    $file\_parts = explode('.',$file);
    $ext = strtolower(array\_pop($file\_parts));
    
    /\* Using the file name (withouth the extension) as a image title: \*/
    $title = implode('.',$file\_parts);
    $title = htmlspecialchars($title);
    
    /\* If the file extension is allowed: \*/	
    if(in\_array($ext,$allowed\_types))
    {
    	/\* Generating random values for the position and rotation: \*/
    	$left=rand(0,$stage\_width);
    	$top=rand(0,400);
    	$rot = rand(-40,40);
    	
    	if($top>$stage\_height-130 && $left > $stage\_width-230)
    	{
    		/\* Prevent the images from hiding the drop box \*/
    		$top-=120+130;
    		$left-=230;
    	}
    	
    	/\* Outputting each image: \*/
    	
    	echo '
    	<div id="pic-'.($i++).'" class="pic" style="top:'.$top.'px;left:'.$left.'px;background:url('.$thumb\_directory.'/'.$file.') no-repeat 50% 50%; -moz-transform:rotate('.$rot.'deg); -webkit-transform:rotate('.$rot.'deg);">
    	<a class="fancybox" rel="fncbx" href="'.$orig\_directory.'/'.$file.'" target="\_blank">'.$title.'</a>
    	</div>';
    }
    

    }

    /* Closing the directory */
    closedir($dir_ha

    H 1 Reply Last reply
    0
    • W wartotojas

      Hi everybody, I was implementing an image gallery from one of tutorials. In this tutorial PHP opens a directory and loops through it and outputs all image files. I am looking for code solution how to change this loop to output only those images that belongs to particular image album. There should be way to run SQL query and select files and then output them, problem is I have no idea how. When I upload image I store some data to following table:

      CREATE TABLE tbl_image (
      im_id INT NOT NULL AUTO_INCREMENT,
      im_user_name VARCHAR(25) NOT NULL,
      im_album_id INT NOT NULL,
      im_title VARCHAR(64) NOT NULL,
      im_description TEXT NOT NULL,
      im_type VARCHAR(30) NOT NULL,
      im_image VARCHAR(60) NOT NULL,
      im_date DATETIME NOT NULL,
      PRIMARY KEY(im_id)
      );

      here is the code piece that should be changed:

      <?php

      /* Configuration Start */

      $thumb_directory = 'img/thumbs';
      $orig_directory = 'img/original';

      $stage_width=600; // How big is the area the images are scattered on
      $stage_height=400;

      /* Configuration end */

      $allowed_types=array('jpg','jpeg','gif','png');
      $file_parts=array();
      $ext='';
      $title='';
      $i=0;

      /* Opening the thumbnail directory and looping through all the thumbs: */

      $dir_handle = @opendir($thumb_directory) or die("There is an error with your image directory!");

      $i=1;
      while ($file = readdir($dir_handle))
      {
      /* Skipping the system files: */
      if($file=='.' || $file == '..') continue;

      $file\_parts = explode('.',$file);
      $ext = strtolower(array\_pop($file\_parts));
      
      /\* Using the file name (withouth the extension) as a image title: \*/
      $title = implode('.',$file\_parts);
      $title = htmlspecialchars($title);
      
      /\* If the file extension is allowed: \*/	
      if(in\_array($ext,$allowed\_types))
      {
      	/\* Generating random values for the position and rotation: \*/
      	$left=rand(0,$stage\_width);
      	$top=rand(0,400);
      	$rot = rand(-40,40);
      	
      	if($top>$stage\_height-130 && $left > $stage\_width-230)
      	{
      		/\* Prevent the images from hiding the drop box \*/
      		$top-=120+130;
      		$left-=230;
      	}
      	
      	/\* Outputting each image: \*/
      	
      	echo '
      	<div id="pic-'.($i++).'" class="pic" style="top:'.$top.'px;left:'.$left.'px;background:url('.$thumb\_directory.'/'.$file.') no-repeat 50% 50%; -moz-transform:rotate('.$rot.'deg); -webkit-transform:rotate('.$rot.'deg);">
      	<a class="fancybox" rel="fncbx" href="'.$orig\_directory.'/'.$file.'" target="\_blank">'.$title.'</a>
      	</div>';
      }
      

      }

      /* Closing the directory */
      closedir($dir_ha

      H Offline
      H Offline
      Hesham Amin
      wrote on last edited by
      #2

      You need to read image names from database instead of scanning the directory, using a query like:

      SELECT * FROM tbl_image WHERE im_album_id = XXX

      Reading a basic PHP/MySQL Tutorial[^] will help.

      Hesham A. Amin My blog twitter: @HeshamAmin

      W 1 Reply Last reply
      0
      • H Hesham Amin

        You need to read image names from database instead of scanning the directory, using a query like:

        SELECT * FROM tbl_image WHERE im_album_id = XXX

        Reading a basic PHP/MySQL Tutorial[^] will help.

        Hesham A. Amin My blog twitter: @HeshamAmin

        W Offline
        W Offline
        wartotojas
        wrote on last edited by
        #3

        I do understand that bit, but how to output query results the same way as they are in while loop?

        F 1 Reply Last reply
        0
        • W wartotojas

          I do understand that bit, but how to output query results the same way as they are in while loop?

          F Offline
          F Offline
          fly904
          wrote on last edited by
          #4

          wartotojas wrote:

          how to output query results the same way as they are in while loop?

          Many of the most basic tutorials demonstrate this. This Tizag Tutorial[^] is a good example.

          If at first you don't succeed, you're not Chuck Norris.

          W 1 Reply Last reply
          0
          • F fly904

            wartotojas wrote:

            how to output query results the same way as they are in while loop?

            Many of the most basic tutorials demonstrate this. This Tizag Tutorial[^] is a good example.

            If at first you don't succeed, you're not Chuck Norris.

            W Offline
            W Offline
            wartotojas
            wrote on last edited by
            #5

            Problem solved, thanks for suggestions everybody :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups