i have a mysql database storing uploaded photos , i am trying to generate a photo album using php , assuming i have 20 photos i need to put every 5 images in one row then start a new row with the 6th image .
i succesfully did it but i also need to attach the image id from the mysql database to the image displayed in the album so when i click the image the image id should be forwarded along with the url so i can allow users to comment to the picture and store the comments in database along wih the image id , how can i do it please ?
u need 2 scripts: to send list of images and to send the image data
i need to put every 5 images in one row then start a new row with the 6th image
u can use something like
for($i=0;$i<count($images);$i++)
{
echo '<img src="/myLinkTogetImagedata?id='.$i.'"/>';
if($i%5==0)echo '<br/>';
}
to send image data u need to send mime type in header
http://php.net/manual/en/function.header.php
and data from DB
think you can save image path in db
yeh, saving filename match better then saving image data in DB
when you generate album you must generate link with get
for example you have address kind /showimage.php?id=21
and then in show image you can give comment and other things
.ps : i think you can save image path in db and then select 10 last image ...
Related
I'm working on a user management system that uses PHP and mysql.(XAMPP)
it has around 1000+ user records in A table called "users" with "id" as primary key.
one of the columns is "avatar"
I have a folder with 1000+ jpg images. all user images are named by their ids. for example if a user named Jon with id=222 his photo will be 222.jpg
what I want to do is upload all photos to their users using the image name to be put to the correct user id in the table
what's the best approach I can take?
hope its clear and thanks in advance
I have some steps that you might do :
Upload file as usual, but in this step you must include user id as parameter
Query user by id
Rename file before save image, example :
$user->id . time() . '.' . $file->getClientExtension();
Upload it to folder
Save to your table
I don't really understand properly the concept behind uploading multiple files with preview. I checked DropzoneJS and it looks nice and I feel I can do a lot of stuff with it.
I have this example: I have a user (id: 801) that creates a new post (id: TBD since the post is not yet created). I have a table image_post which holds the images of the specific post, and an image table which holds the image details. Now, I have the Dropzone form which is supposed to upload images to a folder that I specify in the .php file that is implemented to deal with the ajax request coming from Dropzone. Let's say the user uploads 2 images and I store them into an temp folder. The user submits the creating of the new post, it receives an ID: 10001. The temporary files are on the disk (but should they be stored in the DB as well?) but don't see how to link the post to the images.
What are the exact steps that are required to be able to (after the files were uploaded with Dropzone ajax request) link those images to the actual post id?
Your system will need to create a database record for the post prior to them uploading images. Consider the following table structure, where an i prefix indicates an INT column, s a VARCHAR/TEXT column, and dt a DATETIME.
Table posts: iPostId | iUserId | iStatus | sComments | dtCreated
Table photos: iPhotoId | iPostId | dtUploaded
When a user begins to start a new post, create a record in the posts table with their user id, an empty comments, a datetime such as '0000-00-00 00:00:00', and a status of 0, and then add a hidden form input to the photo upload form so that the assigned post id is known to your photo upload script, e.g.:
<input type="hidden" name="iPostId" value="22" />
The photo upload script could create a new record in the photos table using the post id and then save the photo in a particular folder using the post id in the filename, for example if the photo_id for the record created was 2821 you would save the file as 2821.jpg in your chosen folder.
When the user finishes their post and clicks to save/submit, you could use an UPDATE query to modify the original record on the posts table, assigning their entered text to the sComments column, the current date and time to the dtCreated column, and changing the iStatus to 1.
On your pages where you display a list of posts simply modify your query to something like this to exclude those currently in a draft status (0): SELECT * FROM posts WHERE status=1;, and then when viewing the post you can run a query such as SELECT * FROM photos WHERE post_id=22; to get an array of the photos to be displayed.
Obviously this can be expanded upon but hopefully gives you a good starting point.
before uploading an image, system has to know uploading path.
Image path is generated by image ID.
for example: image 123456 has this path images/123/456
Of course, image ID will be = the last inserted image ID + 1
How to get the ID of the last uploaded image (data is stored in database)
I could do
select max(image_id) from images
or
select image_id FROM images order by image_id desc limit 1
but them seems to be a lot of work every time some user wants to upload an image.
Is there some simpler solution to get last ID
You need to do it in a few steps.
user uploads the image
you insert a skeleton record in the database, so you can get this new record's ID
process/move the uploaded file, using this new id
update the skeleton record to 'activate' it
You cannot do this BEFORE the upload, because PHP does not start executing until AFTER the upload has been completed.
For maximum safety, you do the database operations in a transaction. If there's any problems with the upload (wrong file type, upload failed, can't process file, etc...) you simply roll back the transaction and everything's undone.
So I'm trying to create a system where the user can go to a page where a photo is displayed. The photos have a column in the MySQL db for views. How would I be able to view the top viewed photo with a link on the page for the photo with the 2nd most viewed photo, which has a link for the 3rd most viewed photo, etc. Each photo would have a dedicated page.
I'm using CakePHP, if there are any CakePHP specific strategies for doing this. Any suggestions?
I have never had the experience of using cakePHP, but with PHP and MySQL, you can use a query like this:
SELECT image_link, view_count
FROM `tablename`
ORDER BY view_count DESC
LIMIT 1
OFFSET $i;
and then, using a GET variable to pass on the incremental/decremental value of $i.
You can collect statistics for each image using
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$url = $_SERVER['HTTP_REFERER'];
?>
where $url - is a full path for your image.
well i don't have knowledge of cakephp, but i am telling you the logic to achieve that you want,
just send some parameter on the photo click and set a unique id of the photos, so that on the click event on the photo you will get the unique id of the photo and get which photo is viewed and update the database on every click.
beside that the link you will genrate according to your requiement to fetch the image on view filed on descending order
I have two tables here:
Gallery
ID | Name | Description
Image
ID | Title | Caption | GalleryID
I got the code all set to make a simple image gallery by listing all the thumbnails or paging through the thumbnails. However what I need to do is accomplish something like the gallery here only without the javascript and ajax:
http://gulfnews.com/pictures/news/abandoned-cars-around-uae-1.655965
I want a select number fo thumbnails to show beneath the main image and as and as teh user clicks on teh thumbnails it opens the page with the newly selected image as highlighted and the series of thumbnails on the botto moves ahead by one. if its not the last image that is.
How do I do this? I'd like a quick and dirty way to do this? I'm using php and mysql here for the database - just need the code to show the 'paginated'images below the main image. i know how to set up everything else i.e thumbnails resizing etc...
I don't know if MySQL has a ROWNUMBER() function like T-SQL for numbering your rows, but the smart way to do it is to have a sequential numbering of your images in the database. From there, you query your database by saying something like:
SELECT TOP [images_per_page] WHERE Row_Number > [images_per_page] * [page_number - 1].
That way you only pull back the data you need. You keep track of the page number, most likely in your query string.
That's pretty simplistic, but hopefully it gives you an idea of how to start.