Update DB column every time a link is clicked via PHP - php

I have searched online for this issue, but the results are mostly based around Excel.
I have a webpage that has a list of videos. Every time someone clicks on a link, they are taken to a separate page where they can view a video. I have a table in my mysql database that links video id to views. I would like to increment the views column every time someone clicks on the link.
I know how to do this on the database side, but I am trying to figure out how to trigger the DB operation via PHP. I thought about having a script at the top of the page that calls the stored procedure to update the tally everytime the page is loaded, but the problem with this is that the video doesn't start automatic ally when the page loads. Users have to manually click on "play".

Why not write an Update query on the table which gets called everytime a link is clicked?

It is a little confusing where you want to increment the counter. You give three situations:
When a user clicks a link to a particular video's page
When a user loads a particular video's page
When a user clicks play on a video
1 & 2 wouldn't give an accurate count of those who actually watched the video, so I'll choose that you meant 3.
It depends on your video player then. If the player has a way to hook a javascript function to the start of a video, then you can use that to make an ajax request back to a server page that updates your database. If the player has no hooks then you're limited to on page load, which will be less reliable.
Does this help? If I miss understood the question, please clarify by updating the question or adding comments. PS. adding code of what you've tried goes a long away as well.

Related

Creating unique pages of a website by passing an sql key

The title may be a bit awful as I'm really not sure how to describe what I'm trying to achieve without using lots of words.
I'm currently building an events website. This website has the option for users to create their own events which can be added to a big list of upcoming events that other users have submitted.
What I want to do is be able to have a user create an event and have the details of the event stored in an SQL database (which I've successfully done), I want to be able to then have a large list of events on a separate page the details of which are pulled from the server dynamically using PHP and SQL (I've also successfully done this). The part I am struggling with however is being able to create unique pages (or a single dynamic page) that displays a more detailed look at the event, such as age restrictions etc etc, which cannot be displayed on the big list which displays all the available events.
I figured that this could be done using a unique eventID, which is something the SQL database already includes and autoincrements with each new event entry. What I'm really not sure about is how I'd be able to pass that eventID through the link to the more detailed page so that the detailed page can be created displaying all of the details of the event.
I'm not even sure if this idea will work at all. Would having a page with a unique URL that is created on event submission that has a more in-depth look at the details of the event be a better idea? How would I go about creating unique URL webpages that correspond to each event on event submission?
I'm really not sure how I need to go about this at all, so I need some help and pointing in the right direction.
Here's a kind of TLDR:
1) Events website
2) All events on the website are displayed on a single page that has minimal detail about each event (title, date, location) BUT has a link to a page that displays the event in more detail.
3) User can select an event from the list and click on that event's unique HTML link to more detailed page
4) More detailed page displays event in more detail by pulling event's unique information from the database
Feel free to ask any questions at all as I know this probably isn't the easiest idea to get across
When creating the links from the general listing page to the detail page, simply include the id in the link. For example:
<A HREF="mysite.com/detail.php?id=3>Big Event</A>
In detail.php then, you'll find the id in $_GET['id'].

Creating x amount of pages equal to x ID's in database?

The title may be a little vague, but I'll try to explain the concept here.
I have a site in which daily new items ( sports games ), get published, now what I would love is that my users would be able to click on a certain game and get redirected to a new page with more detailed information about that one game.
The thing is, creating every php page seperate for every game each day is a lot of work because also a lot details in the page need to be changed.
Now I was wondering if it is possible in some kind of way that there's a script that reads, OK you have 5 games today, page 1 = id number, title of the page is the matches name, extra info is the info that stands with that id.
I don't know how else to explain this so I hope this was good enough.
Thanks for your time and reading this :)
You can use requests to select the page (the best way is to use $_GET as suggested by gbestard) to select the ID. Then you can have some static content (like url or short article or link to screen shot or something similar) stored in your database under the same ID as the game, and upon clicking on the link to the game, the page is reloaded and you populate it with the new content, that was stored in the database.
You can even store entire pages with static html (as in article or short game description in your case) content and etc in your database and simply call them to populate a div.
If you have something like a custom made admin section of your site, you can add a page to edits it with a js plugin like ckeditor and the creation/editing of articles will be a piece of cake.
It's pretty much a standard case of a CMS(Content management system).

Functions and parameters - is there a better method?

I'm looking at neatening my file structures and code. At the moment I'm making an image gallery, it has a management page which allows the user to upload, edit and delete images/information.
To upload an image, the user would select their files, sumbit the form and arrive at uploaded.php.
To delete an image, the user would click a link taking them to delete.php?id=IMAGEID.
To edit an image, the user would be taken to a page with an editor. Upon making any revisions they'd then be taken to edit.php?id=IMAGEID.
Each page runs either a function. I feel it's unecessary to have three different pages to run three different functions. Is there any way I could neaten this process?
Well, it just depends on how you like to manage your files and how you are using your IDE when you've to edit something.
If you're working alone, it's clearly up to you. If you know someone will, maybe, have to rework your code someday, having three files could be a plus, somehow.
I guess you're not concerned about SEO for these pages, but just in case the problem appears again, it could be better to have actions in parameters, rather than in files name.
/manage.php?edit or /manage.php?delete or /manage.php?upload
But, if your code is really really big, using three files could still result in a lower use of memory (Well, very slight improvement, but still)
Exactly you don't need 3 pages for each function
1)Delete ,instead of redirection the user to a different page and delete it ,you can add an image button which delete icon and when user click on it ,it call a specific function in your php and delete it then refresh your page.
2)Edit and Insert : these are 1 page ,like make your insert page if it doesnt have a query string with the Image ID then it's insert function ,else it contains and ID then load the content of this Image inside your inputs and while saving check if there is ID then update else insert new record
So,you moved from 3 pages to 1 page only.Hope this helped you.

Comment system breaks upon opening a new browser tab

I have sort of design problem with my site. Here's the introduction:
I have a simple, dynamic page which contains a videoplayer and next to it a bunch of videos to play. Under the videoplayer there's a comment section that is powered with jquery form plugin because naturally I don't want the page to refresh and reset the video if someone enters a comment while watching.
So, the page fetches the video ID to play via a $_GET variable and then gets another 10 or so video links based on the ID from MySQL database and plays the first one and lists the rest as links next to the player.
The comment system is simple. It fetches all the comments from MySQL table having the same ID as the video playing and lists them via the jquery form plugin mentioned before. Upon entering a new comment it adds the comment to the table again by targeting the same ID.
Since the ajax form plugin fetches the comments from external PHP files containing the comment showing/adding code - I have to use a $_SESSION variable to pass the info from the main page to those files.
And here's the problem:
Everything works as I expected when a client browses the page normally.. but when he decides to open another video to a new tab - the comment system breaks down.
So what happens is that I pass the video ID from the main page's $_GET variable to a $_SESSION variable and use it to let the external show/add-comments-PHP-files know which comments to show. This works fine when a client is using only one tab but when he opens another video in a new tab the $_SESSION variable of course updates to match the new tab's currently playing video's comments and since this variable is same in both tabs - the original tab now points wrongly to the new tab's comments.
So.. to hopefully clarify a bit:
browser tab 1:
$_SESSION['now_playing'] = video 1
[..client opens another video into a 2nd tab]
browser tab 2:
$_SESSION['now_playing'] = video 2
..and from this point on the tab 1's comments are of course the same
as tab 2's.
Would there be any tricks to start a new session upon a new tab creation or something like that? I'm rather new to all this so there might be obviously silly involved.. any pointers how to get this fixed would be greatly appreciated - even if it's a better way to implement the commenting - adding comments without a page refresh is a must tho.
Regards, kitsu
You are hitting the classic problem that the Session data is controlled by a cookie, and is therefore per browser not per screen.
One approach is to have a hidden field on the screen. When handling the get to create the screen, set this to a unique value, and use that value as a key into the session data.
Then pass the value back with the ajax requests - so each screen's requests are handled with a different part of the session data.

Keeping track of more than one level of page referrers

The scenario (all happening within the administration area/backend):
From the listing page, the user clicks a link to view an article (on the backend).
From the article view page, the user clicks a link to edit that article.
In the article edit page, form is submitted to the current uri.
If validation succeeds or user cancels, user is redirected to the article view page.
From the article view page, the user click a 'back' link to return to the listing page.
List <--> View <--> Edit
Right now, I'm only able to track referring url from a previous page. In the edit form, I'm using a hidden field to maintain referral to the view page, lest it be changed during failed form POST submission to itself and user remains in the edit page.
Problem is that when the user returns to the view page from edit, the 'back' link to the listing page is now linked to the edit page.
FYI,
The listing page url is dynamic as the user should return to the listing on the same page and sort order (stored in query strings); therefore a fixed url is out of the question.
In the past, I've tried using sessions (e.g. SESSION['view_to_list_ref'] SESSION['edit_to_view_ref']), but it messed up with multiple tabs.
I could transition between view/edit via ajax, but I'm hoping to keep the app simple and ajaxless at this point of time.
I'm using PHP + Kohana 3.2 Framework
The only solution I can think of is to have the list page url encoded and appended to the 'view article' link via query string. This way, the location of the listing page is preserved even while in the edit page; as the referring url back to view page would also contain the listing page url in the query string. However I don't really like the idea of 'dirtying' the url with long parameter values (encoded or not).
I'm really hoping there is a more elegant solution to this problem of generally tracking multiple levels of page referrals; not just specifically to solving the scenario I've mentioned.
EDIT: Oh and the solution should be able to support multiple tabs performing the same scenario.
You could track the pages by using a unique identifying code in a PHP session, a temporary variable, and using a temporary database table that tracks page loads by these temporary values.
The database structure might be:
+-------------+-------------------+---------------------+
| Unique ID | Page Referral | Time of page load |
+-------------+-------------------+---------------------+
Tracking time of page load would allow you to selectively wipe loads older than X minutes, and keep the table relatively small.
Anyway, this would allow you to keep as many levels as you'd like, and if you wanted to add an auto incrementing counter field, or your own counter field, you could even keep a simple to use number system that tracks page loads, though I believe the time of page load would suffice for that scenario.

Categories