I'm relatively new to PHP and I am in the process of developing a site similar to fukung.net. The site shows random images one at a time and you basically click the image to view the next one. The issue I'm trying to resolve is that I'd like each image to have a unique url or identification number in the url so that people can share a link to my site and see the particular image they were just viewing.
Is there anyway this can be achieved through PHP without manually creating a new page for every single image, since i have several thousand images?
Automatic Page Creation For Images
Moderator: General Moderators
Re: Automatic Page Creation For Images
Probably easiest is to have a single page and use GET to specify the image. yoursite.com/image.php?id=1234
Re: Automatic Page Creation For Images
Will i need to create a database with a simple table containing the url to each individual image? It should be easy enough to figure out how to set something like this up, but the problem that I have with doing this is that I don't want to manually enter every image into the table. I have several thousand images in a directory, the names have no organization, and i'm constantly adding more.
I believe the easiest way would be to create a table in sql and use get with sql queries to display a random image, but is there anyway to create/update table rows automatically corresponding to files(in my case images) in a folder?
Like I said I'm relatively new to all this so I could very well be over complicating what i'm trying to achieve, which is to display a random image from a directory on my page with it's own unique url that pertains to that particular image.
The code below is a rough example of what i'm trying to accomplish and should work fine, but the biggest hurdle I see is creating and updating table rows with url links to each image. Please let me know if it's possible to generate these rows automatically, or if there is a simpler way to achieve this desired effect.
I believe the easiest way would be to create a table in sql and use get with sql queries to display a random image, but is there anyway to create/update table rows automatically corresponding to files(in my case images) in a folder?
Like I said I'm relatively new to all this so I could very well be over complicating what i'm trying to achieve, which is to display a random image from a directory on my page with it's own unique url that pertains to that particular image.
The code below is a rough example of what i'm trying to accomplish and should work fine, but the biggest hurdle I see is creating and updating table rows with url links to each image. Please let me know if it's possible to generate these rows automatically, or if there is a simpler way to achieve this desired effect.
Code: Select all
<?php
$image_id=$_GET['imageid']; //The ID of the image
$get_image=mysql_query("SELECT * FROM `images` WHERE `image_id`='{$image_id}'"); //Check if the ID exists in the database
$array_image=mysql_fetch_array($get_image); //Array the query
$get_all_images=mysql_query("SELECT * FROM `images`"); //count all images so that we can check if the "next" button should show or not
$count_images=mysql_num_rows($get_all_images); //Count all the images
if(mysql_num_rows($get_image)==0) //If the imageID is not found in the database, echo an error
{
echo "Image not found";
}
else //Else, show the image
{
?>
<div class="image">
<img src="images/<?php echo $array_image['image_name']; ?>" alt="<?php $array_image['image_name']; ?>" /> <!-- I set the directory as "images" as you can se, and then the name of the picture, which I assume is, for example, test.jpg -->
</div>
<?php
}
if($image_id>1)//If the current image_id is greater than 1, show the previous button
{
?>
<a href="?imageid=<?php echo $imageid--; ?>">Previous</a>
<?php
}
if($image_id<$count_images) //If the current image_id is smaller than the total amount of images in the database, show the next button
{
?>
<a href="?imageid=<?php echo $imageid++; ?>">Next</a>
<?php
}
?>Re: Automatic Page Creation For Images
What I would suggest is creating a script that will loop through each image, giving it an ID number, and formatting the titles in a systematic way: image-1.jpg, image-2.jpg, etc.
Once you do this, I would create an admin page that you can upload new images from. This would check the folder for the last image, grab the ID from that image, increment it, and rename the uploaded images based on this. You could even store an SQL row about the image.
You could also store the image directly in the database. You could then use a second PHP page to grab the image binary data from the DB table, and show it as if it were a regular .jpg file in the browser itself.
There are many different ways to do what you're looking to do.
Once you do this, I would create an admin page that you can upload new images from. This would check the folder for the last image, grab the ID from that image, increment it, and rename the uploaded images based on this. You could even store an SQL row about the image.
You could also store the image directly in the database. You could then use a second PHP page to grab the image binary data from the DB table, and show it as if it were a regular .jpg file in the browser itself.
There are many different ways to do what you're looking to do.
Re: Automatic Page Creation For Images
So i've had some time to work on this again recently. I've created an admin login to upload images, which are uploaded to an image directory. As the images are uploaded the image name and url(pointer) are inserted into a table, and the image name is simply the table's maxid++.
I'm unsure how to get each image to be displayed on it's own url. So if image 1 is selected it would display something like, "mydomain.com/imageid=1" and if someone entered this url it would direct them to this image.
Even though i haven't added it yet selecting a random image seems pretty straightforward, it's just making that image id a url. Here's how I started the page so far:
If what i'm asking is unclear just let me know, thanks in advance.
I'm unsure how to get each image to be displayed on it's own url. So if image 1 is selected it would display something like, "mydomain.com/imageid=1" and if someone entered this url it would direct them to this image.
Even though i haven't added it yet selecting a random image seems pretty straightforward, it's just making that image id a url. Here's how I started the page so far:
Code: Select all
<?php
$host="localhost"; // Host name
$username="****"; // Mysql username
$password="****"; // Mysql password
$db_name="****"; // Database name
$tbl_name="uploaded_images"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$query = mysql_query(" SELECT max(id) AS id FROM uploaded_images ");
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
}
$imgid = rand(6,$id);
?>Re: Automatic Page Creation For Images
Just an update: I got the random images to work and be displayed from the urls stored in the database with no problem. The last thing i'm trying to get to work is adding the id to the URL. I think this can be achieved by modifying the .htaccess and SEF URLs, but i need to read up more on this to be sure and find out how.