How to display image and comments together

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 3171 Views - Last Post: 11 June 2009 - 06:16 AM Rate Topic: -----

#1 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

How to display image and comments together

Posted 05 June 2009 - 08:49 AM

I'm having trouble to figure out. How the script can display image and comments when user click upload button. At moment the script only display image on brownser without the comments.Please could you help? Thank in advance.

<?php 
$idir = "C:/wamp/www/test2/images/";   // Path To Images Directory 
$tdir = "C:/wamp/www/test2/thumbs/";   // Path To Thumbnails Directory 
$twidth = "200";   // Maximum Width For Thumbnail Images 
$theight = "150";   // Maximum Height For Thumbnail Images 


if (!isset($_GET['subpage'])) 
  {   // Image Upload Form Below   ?>

  
  <form method="post" action="?subpage=upload" enctype="multipart/form-data"> 
   
  <input type="file" name="imagefile" class="form" id="imagefile"> 
  <br /> <br /> 
   Comments about image
   <br /> 
  <input type="text" name="comments"/>
   <br /><br /> 
  <input name="Upload" type="submit" value="Upload" class="form">
	
  </form>
 
<?php
 
  } else  if 
	
  (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') {   // Uploading/Resizing Script 
	$fileName = $_FILES['imagefile']['name'];
	$tmpName  = $_FILES['imagefile']['tmp_name'];
	$fileSize = $_FILES['imagefile']['size'];
	$fileType = $_FILES['imagefile']['type'];
	$filecomments = $_POST['comments'];

	
	
	
	
	 $filePath = $uploadDir . $fileName; 
	 $filePath = $tdir . $fileName;	

	
	include 'library/config.php';
	include 'library/opendb.php';

	if(!get_magic_quotes_gpc())
	{
		$fileName  = addslashes($fileName);
		$filePath  = addslashes($filePath);
	}  

	$query = "INSERT INTO upload2 (name, size, type, path, comments) ".
			 "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$filecomments')";

	mysql_query($query) or die('Error, query failed : ' . mysql_error());					

	include 'library/closedb.php';
	

  $url = $_FILES['imagefile']['name'];   // Set $url To Equal The Filename For Later Use 

  if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") { 
  $file_ext = strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php 
  $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location 
	
	if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location 

	  print 'Image uploaded successfully.<br />';   // Was Able To Successfully Upload Image 
	  $simg = imagecreatefromjpeg("$idir" . $url);   // Make A New Temporary Image To Create The Thumbanil From 
	  $currwidth = imagesx($simg);   // Current Image Width 
	  $currheight = imagesy($simg);   // Current Image Height
 
	  if ($currheight > $currwidth) {   // If Height Is Greater Than Width 
		 $zoom = $twidth / $currheight;   // Length Ratio For Width 
		 $newheight = $theight;   // Height Is Equal To Max Height 
		 $newwidth = $currwidth * $zoom;   // Creates The New Width 

	  } else {	// Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) 
		$zoom = $twidth / $currwidth;   // Length Ratio For Height 
		$newwidth = $twidth;   // Width Is Equal To Max Width 
		$newheight = $currheight * $zoom;   // Creates The New Height 
	  } 
	 $dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail 
	  imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete 
	  $palsize = ImageColorsTotal($simg); 
	  for ($i = 0; $i < $palsize; $i++) {   // Counting Colors In The Image 
	   $colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used 
	   ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use 
	  } 
	  imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It) 
	  imagejpeg($dimg, "$tdir" . $url, 100);   // Saving The Image 
	  imagedestroy($simg);   // Destroying The Temporary Image 
	  imagedestroy($dimg);   // Destroying The Other Temporary Image 
	  print 'Image thumbnail created successfully.';   // Resize successful 
	} else { 
	  print '<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed 
	} 
  } else { 
	print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is ';   // Error Message If Filetype Is Wrong 
	print $file_ext;   // Show The Invalid File's Extention 
	print '.</font>'; 
  }

  
 
  function createGallery( $pathToImages, $pathToThumbs ) 
{
 // echo "Creating gallery.html <br />";

  $output = "<html>";
  $output .= "<head><title>Upload</title></head>";
  $output .= "<body>";
  $output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
  $output .= "<tr>";

  // open the directory
  $dir = opendir($pathToThumbs );

  $counter = 0;
  // loop through the directory
  while (false !== ($fname = readdir($dir)))
  {
	// strip the . and .. entries out
	if ($fname != '.' && $fname != '..') 
	{
	  $output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname} \">";
	  $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
	  $output .= "</a><br />{$fname}</td>";
	  

	  $counter += 1;
	  if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
	}
  }
  // close the directory
  closedir($dir );

  
  $output .= "</tr>";
  $output .= "</table>";
  $output .= "</body>";
  $output .= "</html>";

  // open the file
  $fhandle = fopen( "gallery.html", "w" );
  // write the contents of the $output variable to the file
  fwrite( $fhandle, $output ); 
  // close the file
  fclose( $fhandle );
}

// call createThumb function and pass to it as parameters the path 
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width. 
// We are assuming that the path will be a relative path working 
// both in the filesystem, and through the web for links
//   createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path 
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that 
// the path will be a relative path working 
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");


} 

?>


Is This A Good Question/Topic? 0
  • +

Replies To: How to display image and comments together

#2 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: How to display image and comments together

Posted 05 June 2009 - 09:34 AM

What comments exactly? Can you explain a little more? Provide a screenshot or something? Link to a test page?

View PostGhost rider, on 5 Jun, 2009 - 05:49 PM, said:

I'm having trouble to figure out. How the script can display image and comments when user click upload button. At moment the script only display image on brownser without the comments.Please could you help? Thank in advance.

<?php 
$idir = "C:/wamp/www/test2/images/";   // Path To Images Directory 
$tdir = "C:/wamp/www/test2/thumbs/";   // Path To Thumbnails Directory 
$twidth = "200";   // Maximum Width For Thumbnail Images 
$theight = "150";   // Maximum Height For Thumbnail Images 


if (!isset($_GET['subpage'])) 
  {   // Image Upload Form Below   ?>

  
  <form method="post" action="?subpage=upload" enctype="multipart/form-data"> 
   
  <input type="file" name="imagefile" class="form" id="imagefile"> 
  <br /> <br /> 
   Comments about image
   <br /> 
  <input type="text" name="comments"/>
   <br /><br /> 
  <input name="Upload" type="submit" value="Upload" class="form">
	
  </form>
 
<?php
 
  } else  if 
	
  (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') {   // Uploading/Resizing Script 
	$fileName = $_FILES['imagefile']['name'];
	$tmpName  = $_FILES['imagefile']['tmp_name'];
	$fileSize = $_FILES['imagefile']['size'];
	$fileType = $_FILES['imagefile']['type'];
	$filecomments = $_POST['comments'];

	
	
	
	
	 $filePath = $uploadDir . $fileName; 
	 $filePath = $tdir . $fileName;	

	
	include 'library/config.php';
	include 'library/opendb.php';

	if(!get_magic_quotes_gpc())
	{
		$fileName  = addslashes($fileName);
		$filePath  = addslashes($filePath);
	}  

	$query = "INSERT INTO upload2 (name, size, type, path, comments) ".
			 "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$filecomments')";

	mysql_query($query) or die('Error, query failed : ' . mysql_error());					

	include 'library/closedb.php';
	

  $url = $_FILES['imagefile']['name'];   // Set $url To Equal The Filename For Later Use 

  if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") { 
  $file_ext = strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php 
  $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location 
	
	if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location 

	  print 'Image uploaded successfully.<br />';   // Was Able To Successfully Upload Image 
	  $simg = imagecreatefromjpeg("$idir" . $url);   // Make A New Temporary Image To Create The Thumbanil From 
	  $currwidth = imagesx($simg);   // Current Image Width 
	  $currheight = imagesy($simg);   // Current Image Height
 
	  if ($currheight > $currwidth) {   // If Height Is Greater Than Width 
		 $zoom = $twidth / $currheight;   // Length Ratio For Width 
		 $newheight = $theight;   // Height Is Equal To Max Height 
		 $newwidth = $currwidth * $zoom;   // Creates The New Width 

	  } else {	// Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) 
		$zoom = $twidth / $currwidth;   // Length Ratio For Height 
		$newwidth = $twidth;   // Width Is Equal To Max Width 
		$newheight = $currheight * $zoom;   // Creates The New Height 
	  } 
	 $dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail 
	  imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete 
	  $palsize = ImageColorsTotal($simg); 
	  for ($i = 0; $i < $palsize; $i++) {   // Counting Colors In The Image 
	   $colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used 
	   ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use 
	  } 
	  imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It) 
	  imagejpeg($dimg, "$tdir" . $url, 100);   // Saving The Image 
	  imagedestroy($simg);   // Destroying The Temporary Image 
	  imagedestroy($dimg);   // Destroying The Other Temporary Image 
	  print 'Image thumbnail created successfully.';   // Resize successful 
	} else { 
	  print '<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed 
	} 
  } else { 
	print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is ';   // Error Message If Filetype Is Wrong 
	print $file_ext;   // Show The Invalid File's Extention 
	print '.</font>'; 
  }

  
 
  function createGallery( $pathToImages, $pathToThumbs ) 
{
 // echo "Creating gallery.html <br />";

  $output = "<html>";
  $output .= "<head><title>Upload</title></head>";
  $output .= "<body>";
  $output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
  $output .= "<tr>";

  // open the directory
  $dir = opendir($pathToThumbs );

  $counter = 0;
  // loop through the directory
  while (false !== ($fname = readdir($dir)))
  {
	// strip the . and .. entries out
	if ($fname != '.' && $fname != '..') 
	{
	  $output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname} \">";
	  $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
	  $output .= "</a><br />{$fname}</td>";
	  

	  $counter += 1;
	  if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
	}
  }
  // close the directory
  closedir($dir );

  
  $output .= "</tr>";
  $output .= "</table>";
  $output .= "</body>";
  $output .= "</html>";

  // open the file
  $fhandle = fopen( "gallery.html", "w" );
  // write the contents of the $output variable to the file
  fwrite( $fhandle, $output ); 
  // close the file
  fclose( $fhandle );
}

// call createThumb function and pass to it as parameters the path 
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width. 
// We are assuming that the path will be a relative path working 
// both in the filesystem, and through the web for links
//   createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path 
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that 
// the path will be a relative path working 
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");


} 

?>

Was This Post Helpful? 0
  • +
  • -

#3 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: How to display image and comments together

Posted 05 June 2009 - 10:16 AM

Ok. Here the script upload image and text(comments) on server. Meant when user want to upload the image and make litte comments about the image that user is uploading on server. Look below in script !

if (!isset($_GET['subpage'])) 
  {   // Image Upload Form Below   ?>

  
  <form method="post" action="?subpage=upload" enctype="multipart/form-data"> 
   
  <input type="file" name="imagefile" class="form" id="imagefile"> 
  <br /> <br /> 
   Comments about image
   <br /> 
  <input type="text" name="comments"/>
   <br /><br /> 
  <input name="Upload" type="submit" value="Upload" class="form">
	
  </form>




Here the code to display images gallery that user uploaded in server. At moment it can only display image in gallery wihout the comment the user made about image. How can I dispaly image and comments the user uploaded in gallery? Thank you.

  function createGallery( $pathToImages, $pathToThumbs ) 
{
 // echo "Creating gallery.html <br />";

  $output = "<html>";
  $output .= "<head><title>Upload</title></head>";
  $output .= "<body>";
  $output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
  $output .= "<tr>";

  // open the directory
  $dir = opendir($pathToThumbs );

  $counter = 0;
  // loop through the directory
  while (false !== ($fname = readdir($dir)))
  {
	// strip the . and .. entries out
	if ($fname != '.' && $fname != '..') 
	{
	  $output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname} \">";
	  $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
	  $output .= "</a><br />{$fname}</td>";
	  

	  $counter += 1;
	  if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
	}
  }
  // close the directory
  closedir($dir );

  
  $output .= "</tr>";
  $output .= "</table>";
  $output .= "</body>";
  $output .= "</html>";

  // open the file
  $fhandle = fopen( "gallery.html", "w" );
  // write the contents of the $output variable to the file
  fwrite( $fhandle, $output ); 
  // close the file
  fclose( $fhandle );
}

// call createThumb function and pass to it as parameters the path 
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width. 
// We are assuming that the path will be a relative path working 
// both in the filesystem, and through the web for links
//   createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path 
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that 
// the path will be a relative path working 
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");


} 

?>

Was This Post Helpful? 0
  • +
  • -

#4 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: How to display image and comments together

Posted 05 June 2009 - 10:35 AM

Since you are storing it in a database, you only have to query the database to retrieve the comments for the specific file and output it along with the file! :)
$query = "SELECT comments FROM upload2 WHERE name = '".$filename."'";

Was This Post Helpful? 0
  • +
  • -

#5 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: How to display image and comments together

Posted 05 June 2009 - 03:51 PM

Thank you for your replay. I tried do you suggestion but it is not display the comments.

this where original image is display but tried it was select out the function gallery still not displaying. Please illustrate how to output the comments. Thank very much
function createGallery( $pathToImages, $pathToThumbs ) 
{
 // echo "Creating gallery.html <br />";

  $output = "<html>";
  $output .= "<head><title>Upload</title></head>";
  $output .= "<body>";
  $output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
  $output .= "<tr>";

  // open the directory
  $dir = opendir($pathToThumbs );

  $counter = 0;
  // loop through the directory
  while (false !== ($fname = readdir($dir)))
  {
	// strip the . and .. entries out
	if ($fname != '.' && $fname != '..') 
	{
	  $output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}{$comments} \">";
	  $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
	  $output .= "</a><br />{$fname}</td>";
	  

	  $counter += 1;
	  if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
	}
  }
  // close the directory
  closedir($dir );

  
  $output .= "</tr>";
  $output .= "</table>";
  $output .= "</body>";
  $output .= "</html>";

  // open the file
  $fhandle = fopen( "gallery.html", "w" );
  // write the contents of the $output variable to the file
  fwrite( $fhandle, $output ); 
  // close the file
  fclose( $fhandle );
}

// call createThumb function and pass to it as parameters the path 
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width. 
// We are assuming that the path will be a relative path working 
// both in the filesystem, and through the web for links
//   createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path 
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that 
// the path will be a relative path working 
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");


} 

?>

Was This Post Helpful? 0
  • +
  • -

#6 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: How to display image and comments together

Posted 05 June 2009 - 05:33 PM

Well something like this perhaps(?):
<?php

...

while(false !== ($fname = readdir($dir)))
{
	// strip the . and .. entries out
	if($fname != '.' && $fname != '..') 
	{
		$comments = "";
		// Change this query to fit your database/table
		$result = mysql_query("SELECT comments FROM table WHERE filename = '".$fname."'");

		// Check if the query succeeded and with a result
		if($result && mysql_num_rows($result) == 1)
		{
			$row = mysql_fetch_array($result);
			$comments = $row['comments'];	
		}

		$output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}\">";
		$output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
		$output .= "</a><br />{$fname}<br />{$comments}</td>"; // Added "<br />{$comments}"

		$counter += 1;
		if($counter % 4 == 0) 
		{
			$output .= "</tr><tr>";
		}
	}
}

...

?>




View PostGhost rider, on 6 Jun, 2009 - 12:51 AM, said:

Thank you for your replay. I tried do you suggestion but it is not display the comments.

this where original image is display but tried it was select out the function gallery still not displaying. Please illustrate how to output the comments. Thank very much
function createGallery( $pathToImages, $pathToThumbs ) 
{
 // echo "Creating gallery.html <br />";

  $output = "<html>";
  $output .= "<head><title>Upload</title></head>";
  $output .= "<body>";
  $output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
  $output .= "<tr>";

  // open the directory
  $dir = opendir($pathToThumbs );

  $counter = 0;
  // loop through the directory
  while (false !== ($fname = readdir($dir)))
  {
	// strip the . and .. entries out
	if ($fname != '.' && $fname != '..') 
	{
	  $output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}{$comments} \">";
	  $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
	  $output .= "</a><br />{$fname}</td>";
	  

	  $counter += 1;
	  if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
	}
  }
  // close the directory
  closedir($dir );

  
  $output .= "</tr>";
  $output .= "</table>";
  $output .= "</body>";
  $output .= "</html>";

  // open the file
  $fhandle = fopen( "gallery.html", "w" );
  // write the contents of the $output variable to the file
  fwrite( $fhandle, $output ); 
  // close the file
  fclose( $fhandle );
}

// call createThumb function and pass to it as parameters the path 
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width. 
// We are assuming that the path will be a relative path working 
// both in the filesystem, and through the web for links
//   createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path 
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that 
// the path will be a relative path working 
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");


} 

?>

Was This Post Helpful? 0
  • +
  • -

#7 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: How to display image and comments together

Posted 06 June 2009 - 04:55 AM

Thank you very much and I apprecciated for your help. That what I've done but still not display the comments. Please could you check the script? Thank in advance.

<?php

...

  function createGallery( $pathToImages, $pathToThumbs ) 
{
 // echo "Creating gallery.html <br />";

  $output = "<html>";
  $output .= "<head><title>Upload</title></head>";
  $output .= "<body>";
  $output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
  $output .= "<tr>";

  // open the directory
  $dir = opendir($pathToThumbs );

  $counter = 0;
  // loop through the directory
  while (false !== ($fname = readdir($dir)))
  {
	// strip the . and .. entries out
	if ($fname != '.' && $fname != '..') 
	{
	  $comments = "";
	  
	  // Change this query to fit your database/table
		$result = mysql_query("SELECT comments FROM upload2 WHERE filename = '".$fname."'");
	  
	  // Check if the query succeeded and with a result
		if($result && mysql_num_rows($result) == 1)
		{
			$row = mysql_fetch_array($result);
			$comments = $row['comments'];	
		}

	  
	  $output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname} \">";
	  $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
	  $output .= "</a><br />{$fname}<br />{$comments}</td>";  // Added "<br />{$comments}"
	  

	  $counter += 1;
	  if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
	}
  }
  // close the directory
  closedir($dir );

  
  $output .= "</tr>";
  $output .= "</table>";
  $output .= "</body>";
  $output .= "</html>";

  // open the file
  $fhandle = fopen( "gallery.html", "w" );
  // write the contents of the $output variable to the file
  fwrite( $fhandle, $output ); 
  // close the file
  fclose( $fhandle );
}

// call createThumb function and pass to it as parameters the path 
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width. 
// We are assuming that the path will be a relative path working 
// both in the filesystem, and through the web for links
//   createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path 
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that 
// the path will be a relative path working 
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");


} 

?>

Was This Post Helpful? 0
  • +
  • -

#8 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: How to display image and comments together

Posted 06 June 2009 - 07:17 AM

Since you are running the queries inside a function you need to provide the function with a database connection or open a new connection inside the function, else the database stuff won't work! :)

View PostGhost rider, on 6 Jun, 2009 - 01:55 PM, said:

Thank you very much and I apprecciated for your help. That what I've done but still not display the comments. Please could you check the script? Thank in advance.

<?php

...

  function createGallery( $pathToImages, $pathToThumbs ) 
{
 // echo "Creating gallery.html <br />";

  $output = "<html>";
  $output .= "<head><title>Upload</title></head>";
  $output .= "<body>";
  $output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
  $output .= "<tr>";

  // open the directory
  $dir = opendir($pathToThumbs );

  $counter = 0;
  // loop through the directory
  while (false !== ($fname = readdir($dir)))
  {
	// strip the . and .. entries out
	if ($fname != '.' && $fname != '..') 
	{
	  $comments = "";
	  
	  // Change this query to fit your database/table
		$result = mysql_query("SELECT comments FROM upload2 WHERE filename = '".$fname."'");
	  
	  // Check if the query succeeded and with a result
		if($result && mysql_num_rows($result) == 1)
		{
			$row = mysql_fetch_array($result);
			$comments = $row['comments'];	
		}

	  
	  $output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname} \">";
	  $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
	  $output .= "</a><br />{$fname}<br />{$comments}</td>";  // Added "<br />{$comments}"
	  

	  $counter += 1;
	  if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
	}
  }
  // close the directory
  closedir($dir );

  
  $output .= "</tr>";
  $output .= "</table>";
  $output .= "</body>";
  $output .= "</html>";

  // open the file
  $fhandle = fopen( "gallery.html", "w" );
  // write the contents of the $output variable to the file
  fwrite( $fhandle, $output ); 
  // close the file
  fclose( $fhandle );
}

// call createThumb function and pass to it as parameters the path 
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width. 
// We are assuming that the path will be a relative path working 
// both in the filesystem, and through the web for links
//   createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path 
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that 
// the path will be a relative path working 
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");


} 

?>

Was This Post Helpful? 0
  • +
  • -

#9 hadi_php  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 382
  • Joined: 23-August 08

Re: How to display image and comments together

Posted 06 June 2009 - 08:09 AM

@Ghost rider.........its not possible for me to fix ur error unless i run it on a wamp or xampp server......

i need Ur databse table name, fileds name... i mean whats the name of ur database ...tables name then fields name bla bla ....exactly match.

then library/config.php, library/opendb.php, library/closedb.php


Please post ur hole code with mysql details which u used. I mean i need ... all stuffs which u have to run that script.
Was This Post Helpful? 0
  • +
  • -

#10 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: How to display image and comments together

Posted 06 June 2009 - 10:50 AM

Hey, but when I uploaded image and its comments, it uploaded the comments in database and image in directory. The trouble is when gallery only displaying the image but not it comments.
anyway I tried your suggestion be still the same problem. Please check the code! Thank in advance

 function createGallery( $pathToImages, $pathToThumbs ) 
{
 
$con = mysql_connect("localhost", "root", "");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test2", $con);

 // echo "Creating gallery.html <br />";

  $output = "<html>";
  $output .= "<head><title>Upload</title></head>";
  $output .= "<body>";
  $output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
  $output .= "<tr>";

  // open the directory
  $dir = opendir($pathToThumbs );

  $counter = 0;
  // loop through the directory
  while (false !== ($fname = readdir($dir)))
  {
	// strip the . and .. entries out
	if ($fname != '.' && $fname != '..') 
	{
	  $comments = "";
	  
	  // Change this query to fit your database/table
		$result = mysql_query("SELECT comments FROM upload2 WHERE filename = '".$fname."'");
	  
	  // Check if the query succeeded and with a result
		if($result && mysql_num_rows($result) == 1)
		{
			$row = mysql_fetch_array($result);
			$comments = $row['comments'];	
		}

	  
	  $output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname} \">";
	  $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
	  $output .= "</a><br />{$fname}<br />{$comments}</td>";  // Added "<br />{$comments}"
	  

	  $counter += 1;
	  if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
	}
  }
  // close the directory
  closedir($dir );

  
  $output .= "</tr>";
  $output .= "</table>";
  $output .= "</body>";
  $output .= "</html>";

  // open the file
  $fhandle = fopen( "gallery.html", "w" );
  // write the contents of the $output variable to the file
  fwrite( $fhandle, $output ); 
  // close the file
  fclose( $fhandle );
  mysql_close($con);

}

// call createThumb function and pass to it as parameters the path 
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width. 
// We are assuming that the path will be a relative path working 
// both in the filesystem, and through the web for links
//   createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path 
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that 
// the path will be a relative path working 
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");


} 

?>

Was This Post Helpful? 0
  • +
  • -

#11 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: How to display image and comments together

Posted 06 June 2009 - 02:24 PM

Try changing this line:
$result = mysql_query("SELECT comments FROM upload2 WHERE filename = '".$fname."'");

To:
$result = mysql_query("SELECT comments FROM upload2 WHERE filename = '".$fname."'") or die("Query failed");

if the "Query failed" doesn't show the error is elsewhere, also make sure the database connection actually get established! :)
Was This Post Helpful? 0
  • +
  • -

#12 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: How to display image and comments together

Posted 07 June 2009 - 12:39 AM

ok here is simple php and mysql code
first of all here is table
CREATE TABLE `image`.`img` (
`ID` INT NOT NULL ,
`image` VARCHAR( 50 ) NOT NULL ,
`comments` LONGTEXT NOT NULL 
) ENGINE = MYISAM ;


index.php page
<?php
$con = mysql_connect("localhost","root","");
$db = mysql_select_db("image");
$query = mysql_query("select * from img");
?>
<p><a href="upload.php">Upload New Image</a></p>
<table width="120" border="0" cellpadding="2" cellspacing="0">
<?php
while($fetch = mysql_fetch_assoc($query))
{
?>
  <tr>
	<td align="center"><img src="images/<?php echo $fetch['image']?>" width="100" height="100" /></td>
  </tr>
  <tr>
	<td align="center"><?php echo $fetch['comments']?></td>
  </tr>
<?php
}
?>
</table>


upload.php
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="upload_code.php">
  <table width="620" border="0" cellspacing="0" cellpadding="2">
    <tr>
      <td colspan="2" valign="top"><label>
        <input type="file" name="image" id="image" />
      </label>
        <label></label></td>
    </tr>
    <tr>
      <td width="148" valign="top">Comments</td>
      <td width="472" valign="top"><label>
        <textarea name="comments" id="comments" cols="45" rows="5">your comments</textarea>
      </label></td>
    </tr>
    <tr>
      <td valign="top"> </td>
      <td valign="top"><input type="submit" name="button" id="button" value="Upload" /></td>
    </tr>
  </table>
  <label></label>
</form>


and upload_code.php
<?php
$con = mysql_connect("localhost","root","");
$db = mysql_select_db("image");
$file = $_FILES['image']['tmp_name'];
$filename = $_FILES['image']['name'];
$comments = $_POST['comments'];
$strQuery  = "Insert into img (`image`,`comments`) Values ('$filename','$comments')";
if(!move_uploaded_file($file,'images/'.$filename))
{
header("Location: upload.php");
}
mysql_query($strQuery) or die(mysql_error());
header("Location: index.php");
?>


Was This Post Helpful? 0
  • +
  • -

#13 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: How to display image and comments together

Posted 07 June 2009 - 01:03 AM

Thanks,
I did change the code as you illustrated and the "Query failed" Message show in brownser. Also the image that I tried to upload it didn't display in gallery. Despite details of image and comments are uploaded in database and image in directory. Thank in advance
Was This Post Helpful? 0
  • +
  • -

#14 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: How to display image and comments together

Posted 07 June 2009 - 01:15 AM

are you using a template i mean i tutorial downloaded from some where?
Was This Post Helpful? 0
  • +
  • -

#15 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: How to display image and comments together

Posted 07 June 2009 - 02:28 AM

ok, then change die("Query Failed") to die(mysql_error()) and see what kind of an error message you get. Remember to remove the "mysql_error()" when you publish the thing on the web since it might give users more information than they should have access too! :(


View PostGhost rider, on 7 Jun, 2009 - 10:03 AM, said:

Thanks,
I did change the code as you illustrated and the "Query failed" Message show in brownser. Also the image that I tried to upload it didn't display in gallery. Despite details of image and comments are uploaded in database and image in directory. Thank in advance

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2