php_beginner_83 Posted June 6, 2009 Share Posted June 6, 2009 Hi I need some advice about the best way to go about something. I'm creating a website to display my photos. On one of my pages, at the top, I'm displaying my photo albums. This consists of a thumbnail and the name of the album. On the lower part of my page is going to be the photo gallery, where the user can browse through the pictures of the album they have just selected. So my problem is, when a user clicks an album how to requery my database so I can search for the pics in the album just selected. Currently, I have it as two separate pages. On the first page are the albums. When the user clicks on the name of the album (which is a link) another pages opens, the photo gallery, which displays the pictures one at a time from the album selected and the user can browse through these using next and previous buttons. To put these both on one page should I use a form and a button. Use the button where the link (Album title) used to be so now when the user clicks on the buttons (Album titles), I can run a different mysql query to return the pictures from that album. I'm new at php and mysql and was wondering if this way is doable or is there a better way to do it. This is the code that displays the photo albums.. <?php $username = "root"; $password = ""; $hostname = "localhost"; //connection to the database $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); //echo "Connected to MySQL<br>"; $selected = mysql_select_db("MyWebsite",$dbhandle) or die("Could not select examples"); $result = mysql_query("SELECT * FROM albums WHERE Place=2"); $row = mysql_fetch_array($result); echo "<table border='0' cellspacing='50'>"; $title = $row['Title']; $table1 = "<td><a href=DatabaseConnection.php?link=$title>" . $row['Title'] . "</a></td>" . $table1; $thumb = "images\\" . $row['AlbumCover']; $table2 = "<td><img src=\"$thumb\"></td>" . $table2; $table3 = "<td>" . $row['Description'] . "</td>" . $table3; echo "<td><a href=DatabaseConnection.php?link=$title>" . $row['Title'] . "</a><br/><br/><img src=\"$thumb\"><br/><br/>" . $row['Description'] . "</td>"; while($row = mysql_fetch_array( $result )) { $title = $row['Title']; $table1 = "<td><a href=DatabaseConnection.php?link=$title>" . $row['Title'] . "</a></td>" . $table1; $thumb = "images\\" . $row['AlbumCover']; echo "thumb " .$thumb; $table2 = "<td><img src=\"$thumb\"></td>" . $table2; $table3 = "<td>" . $row['Description'] . "</td>" . $table3; echo "<td><a href=DatabaseConnection.php?link=$title>" . $row['Title'] . "</a><br/><br/><img src=\"$thumb\"><br/><br/>" . $row['Description'] . "</td>"; } echo "</table>"; ?> And this is my code that'll show the pictures from the selected photo album... <?php $username = "root"; $password = ""; $hostname = "localhost"; //connection to the database $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); //echo "Connected to MySQL<br>"; $selected = mysql_select_db("MyWebsite",$dbhandle) or die("Could not select examples"); //code here to determine what link has been clicked. $getValue = $_GET['link']; //sql query to get records from that album. $result = mysql_query("SELECT * FROM Images INNER JOIN Images_In_Album ON Images.ID = Images_In_Album.PicID INNER JOIN Albums ON Images_In_Album.AlbumID = Albums.ID WHERE Albums.Title = \"$getValue\""); //arrays to hold the titles, descriptions and paths separately $titles = array(); $descriptions = array(); $paths = array(); $counter = 0; //fetch tha data from the database while ($row = mysql_fetch_array($result)) { $titles[$counter] = $row['Title']; $descriptions[$counter] = $row['Description']; $paths[$counter] = substr(strrchr($row['Path'],92),1); $counter++; } $imgIndex = $_GET['img']; if(!isset($paths[$imgIndex])) { $imgIndex = 0; } $currentImage = $paths[$imgIndex]; if ($imgIndex<=0) { $prev = "Previous"; } else { $prev = "<a href=\"".$_SERVER['SCRIPT_NAME']."?page=photo&img=".($imgIndex-1)."\">Previous</a>"; } if ($imgIndex>=(count($paths)-1)) { $next = "Next"; } else { $next = "<a href=\"".$_SERVER['SCRIPT_NAME']."?page=photo&img=".($imgIndex+1)."\">Next</a>"; } echo "<pre>$prev $next</pre><br>"; echo "<div id='photoBody'></br><h2>$titles[$imgIndex]</h2>"; echo "<img src=\"{$currentImage}\"></br></br>"; echo "<p>$descriptions[$imgIndex]</p></div>"; mysql_close($dbhandle); ?> Link to comment https://forums.phpfreaks.com/topic/161190-advice-on-how-to-display-pics-in-photo-gallerymaybe-using-form-and-buttons/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.