PHP+MySQL: Creating a photo gallery based on views - php

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

Related

Count Page views each separate

I want to make a count views system ,I have one for the site count views , but that's in general for the main site , dunno how to do it individually but it seems like this one will not be easy !
So I will show you my old system and in what way does the new should work, so I got this Running so far
$guest_ip = $_SERVER['REMOTE_ADDR'];
$visits = mysql_query("INSERT INTO visitors(ip) VALUES('".$guest_ip."')");
$visitoronsite = mysql_query("SELECT * FROM visitors");
$onlinevisits = mysql_num_rows($visitoronsite);
?>
Visitors: <?PHP echo $onlinevisits; ?>
Of Course It is great to keep track of who and how many , but As my site is with videos and each video has link with php ID which is something like site/index.php?id=8 and that page , What I want to do is to put a counter on that page saying that many people watched using this link and I already have a teory but Has Few Issues
So I created a table called pageviews with 2 columns one ip and the other one page , now here's my question , I cannot use unique because I want to show that that IP has also watched this link but it wount insert it because it will be unique so what do I do to keep one ip unique as for that page only?
On your pageviews table, you should create a unique index based on two columns (ip and video or link id).
You add a unique on your table like this:
ALTER TABLE `pageviews` ADD UNIQUE INDEX `ip_video_unique` (`ip`, `video_id`);
Than you would be able to store unique video views per IP.
On the page, you can get the count of how many IPs have viewed that video by doing a query such as:
SELECT * FROM `pageviews` WHERE video_id = x
where x in this query is the id of the video. In case of site/index.php?id=8 it would be 8.
Its also better if you separate the IPs on a separate table where you store visitors with a unique id and their respective IP and on pageviews table you reference the unique visitor id instead of the IP. Later on, this would allow you to count video views on a table and something else's views on another table but have the visitors(and their IPs) aggregated on a single table.

Linking to previous/next PHP/MySQL

I am creating a PHP/MySQL site which has records giving details about images.
Each record contains the field image_id (the key) and image_name.
I want to create a link on each page to go to the next record based on image_name rather than ship_id.
I have the current image_id and image_name as parameters in the URL. I've got as far as this line for the link but it just links to the current page... any help????
Previous

"Centering" mysql result

I am trying to make a photo gallery in php + mysql.
The URL viewphoto.php?id=143 shows photo 143.
In the navigation menu, I want to display the previous two photos in that album, and the next two.
I thought of using:
$temp_id = $id - 2;
mysql_query("SELECT id, photo FROM photos WHERE album = {$album} AND id >= {$temp_id} LIMIT 5");
But that didn't work, because photo's from different albums could have the ids preceding and succeeding the one of the viewed photo.
Does anyone know how I could do this?
Thanks.
Using the id to sort your pictures is a bad idea. Imagine you delete one picture. Your chain of pictures will be broken. In order to fix it, you would need to change the id column, which is an even worse idea.
Either use a seperate index column for the sorting within one album. This index could be rebuilt easily in the case of a picture getting deleted.
Or you define a "previous" and "next" column in your table. This would then contain the id of the last and next picture. If a picture gets deleted, you only need to change these columns for the previous and the next picture.
With both methods, you could for example use a nested self join to access your previous and next 2 pictures. Comment on this post if you need help on that.
I've been dealing with this issue myself and I never found an elegant solution to the problem, I ended up doing it in two queries:
// next
mysql_query("SELECT id, photo FROM photos WHERE album = {$album} AND id > {$id} ORDER BY id ASC LIMIT 2");
// prev
mysql_query("SELECT id, photo FROM photos WHERE album = {$album} AND id <= {$id} ORDER BY id DESC LIMIT 3");
Now the first item in "prev" once fetched would be the current photo.
This solution does has to be amended with a check that the first item in "prev" is in fact the requested id, because it doesn't make sure it's the proper id, in case of gaps in the series.

Facebook API: How do you get the url for the last created picture?

How can I get the url of the picture I just uploaded to facebook through the API?
I need the user to make it its profile picture and I want to give him a fast link to go there.
Alright, so using the Graph API you need to do this:
Retrieve a list of of all albums
Determine which album was changed most recently
Retrieve a list of photos in that album
Determine which photo was changed most recently
Here's how you do it:
First you call: https://graph.facebook.com/me/albums
Which gives you a list a list of all the albums, each album has a key called "updated_time". Loop through all the albums and compare them to see which one is the most recent one.
When you have found the most recent one you take the id of that album and call: https://graph.facebook.com/{ the album id }/photos
This will give you a list of all the photos in that album. Each photo has a key called "created_time". Use that to determine which photo is the most recently created. When you have done that you can just take the value in the key "source" which is an url to the full scale of the picture.
If you want a smaller picture there's a key called "images" which contains a list of 4 different sizes; 590x480, 221x180, 130x105 and 92x75. Each of those items in the list has the "source" key.
You can use FQL to do this very fast by issuing the following query:
SELECT object_id FROM photo WHERE aid IN(SELECT aid FROM album WHERE
owner = me() ORDER BY modified DESC LIMIT 1) ORDER BY modified DESC
LIMIT 1
This will return just the graph object id, which you can then simply issue a graph API request to https://graph.facebook.com/object_id
If you want more fields than just the object_id, check these docs: http://developers.facebook.com/docs/reference/fql/photo/

PHP and MySQL: Formatting URLs correctly when using pagination to display images of multiple galleries

I've set up a simple gallery system in PHP that is built to display one image per page. My URL parameter simply uses a gallery variable and a page variable and does not use the image iD in any way to pull the image. This is because I'm setting my htaccess settings to make a friendly URL:
http://mysite.com/images/1/
^ Would pull the first image in my MySQL query
or
http//mysite.com/images/12/
^ Would pull the 12th image in my MySQL query
The PHP page would then look like:
images.php?gallery=images&page=1
and
images.php?gallery=images&page=12
The queries (simplified) would then look like this for each of the above:
For the first image:
SELECT id, img_src
FROM pictures
WHERE gallery = images
ORDER BY date_added DESC
LIMIT 0, 1
and for the 12th image:
SELECT id, img_src
FROM pictures
WHERE gallery = images
ORDER BY date_added DESC
LIMIT 11, 1
It's been working great but I ran into a problem now that I want to add a feature. I was hoping to display thumbnails of the ten most recently added images to the database no matter which gallery they belong to… i.e. this query:
SELECT id, gallery, img_src
FROM pictures
ORDER BY date_added DESC
LIMIT 10
Is there any way I can know which 'position' or 'page' each image would be for the specific gallery so that I can create the link correctly?
For example, say the ten most recent thumbnails return 4 pictures from the gallery 'images', then 2 pictures from the gallery 'weddings', then 3 pictures from the gallery 'portraits' and then one more image from the gallery 'images', so my links should then be:
http://mysite.com/images/1/
http://mysite.com/images/2/
http://mysite.com/images/3/
http://mysite.com/images/4/
http://mysite.com/weddings/1/
http://mysite.com/weddings/2/
http://mysite.com/portraits/1/
http://mysite.com/portraits/2/
http://mysite.com/portraits/3/
http://mysite.com/images/5/
Thanks for any help. I'm sure I'm overlooking something stupid easy here but I'm hoping to do it most efficiently as far as programming goes. So far my thoughts are that when I'm looping through the output I have to somehow retain each gallery's 'image count' and add one to this count each time an image of that gallery is added.
If you're still building the site I would change the URL's to show the actual image ID's and not a pseudo ID based on pagination.
This is good for several reasons:
SEO. You're pages stay the same. Google indexes them and when a visitor comes to the site at example.com/wedding/4234 he will actually find what he's looking for and not what Google indexed last time your site was scanned
Simplicity. You're already facing a problem that would be very easilly resolved if you were using real image ID's on the first place.
Analytics. You'll know what content is driving people to your site. There is no guessing here. URL's will remain the same always (no matter if new content comes in or not).
Quick and dirty:
$sql = "SELECT id, gallery, img_src
FROM pictures
ORDER BY date_added DESC
LIMIT 10";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
if(!$$row['gallery']) $$row['gallery'] = array();
array_push($$row['gallery'], $row['id']);
$pagenumber = count($$row['gallery']);
echo "<a href=\"http://mysite.com/{$row['gallery']}/$pagenumber/\">
<img src=\"{$row['img_src']}\" alt=\"\"></a><br>";
// alt attribute for w3 validation (http://validator.w3.org/)
}
Edit:
Fixed code error. Now will dynamically create the array if it doesn't already exist.
(Originally, it illegally used the [ ] operator.)
I'm going to add to Frankie's answer, because I also believe the OP's current method is going to bite him the butt again one day. The OP may not find this useful, but it could help someone else.
You can use http://site.com/images/20, but the 20 represents an image ID instead of a page number. Of course the query to get the image is simple:
SELECT id, img_src
FROM pictures
WHERE gallery = images
AND id = 20
LIMIT 1
As a benefit this query is more efficient than using an offset with the LIMIT clause. But the OP said in a comment that he likes his current system because it doesn't require additional database queries to help build pagination links. Well, that's not a problem here.
Lets say you're on the page http://site.com/images/20. Your page links would be the following:
Prev - http://site.com/images/prev/20
Next - http://site.com/images/next/20
Obviously it didn't take any additional queries to build those links. They're just a slightly different URL showing the same image ID. But how do they work to move to the next or prev image? Simple. Instead of the above query, you would use one of these:
If the prev link was clicked, you use this query:
SELECT id, img_src
FROM pictures
WHERE gallery = 'images'
AND id < 20
ORDER BY id DESC
LIMIT 1;
If the next link was clicked, you use this query:
SELECT id, img_src
FROM pictures
WHERE gallery = 'images'
AND id > 20
ORDER BY id ASC
LIMIT 1;
The first query gives the image that came before the current one, and the second query gives the image that comes after the current one.

Categories