Not quite sure how descriptive that title was but this is what I want to do. I scripted a URL shortener today and it's working fine, I just want to add some stats on the bottom of it saying how many links there are and how many clicks there are. Now everytime a user clicks one of the links it +1 the column in the database then it redirects the user. I want to query that and add all of those numbers together from each row. I attempted a while loop which im not surprised didn't work:
while($rows = mysql_fetch_assoc($check_count)){
$clicks = $rows['clicks'];
$clicks = $clicks+$clicks;
}
If you don't understand please ask and if you do understand it means alot for any response!
Related
I am building an admin type script to view records within a mySQL database. This links in numerous tables and is all working perfect. I have now added a filter form to allow me to search/filter for records that match specific criteria. Again this works perfect.
The problem I cannot get my head around is this...
For each record I have a link that allows me to go into that record and view more information/perform tasks on the record.
Initially it was simple to have a 'Previous' and 'Next' link for cycling through the records which meant I didnt need to go Back to the search results to go to the previous/next record. It simply +1 or -1 from the current record ID to generate the link.
Now however I am filtering so the records ID's might be 1,3,4,5,8,10,15,30 etc
The problem is now that once I click into any one of the results to view that record I essentially lose the filtering and all of the results.
The only way I can think of achieving this is to pass the filter variables in with the link and redo the filtering every time the View page is loaded, working out where the current record is in the result set and creating a link based on the previous and next records in the result set.
Am I doing this correct or is there a better way?
I have searched but most answers are geared towards the basics of looping through mySQL query results which sounds the same but is very different.
Any advice would be greatlt appreciated!
I have a project for school that I am working on where I have a table of different parts. In the last column of each row is an "Order" where they all link to orders.php page. Each link is for a different part, though, and so I want my variables to echo on the orders.php page for the correct link clicked. I have no idea how to approach this, but it must be done this way for the class and my professor is rather useless in terms of helping students.
Basically, if I click the "Orders" link in the "Batteries" row, I want the orders.php page to show $partname = Batteries, $price = (Battery Price) ... I really can't even figure out how to get each variable to be set once that link is clicked.
Thanks a bunch for any help please.
i had a hard time trying to understand your question but i think this may help:
you can use GET method to give information to the orders.php lie this:
in the original page you can have something like:
Battery
And in the orders.php:
$partname = $_GET["partname"];
$price = $_GET["price"];
i really hope that this is waht youre looking for. for more info google PHP GET Method.
I'm trying to figure out how to count the number of views that have been sent to my site from another website. I have a banner advertised and I want to have the banner link directed to a php script that will count the number of times people from website abc.com vistied the site.
The question is how do I go about doing this? I was thinking about setting up a table in mysql with a different row for each site and then have that specific row increment the count by one.
Problem is I'm not sure how to use the function i++ (if thats even right function). I am new to php, sorry if what I'm asking is a basic thing
You can get the referrer from $_SERVER['HTTP_REFERER']. It doesn't always exist so you'd need to check, and if it does then perform an upsert.
I would do something like this:
if(isset($_SERVER['HTTP_REFERER'])){
$referrer = $_SERVER['HTTP_REFERER'];
$query = "INSERT INTO 'referrals'
('referrer', 'count')
VALUES
($referrer, 1)
ON DUPLICATE KEY UPDATE
'count' = 'count' + 1";
$result = mysql_query($query);
}
Usually banners point to a specific URL on your server from the referring website so you can track where things come from.
Eg: BigSite.com has a banner for you that links to MySite.com/links/01941731.htm
Using something like .htaccess on an Apache server you can parse the incoming "01941731" part, safely check it against your database and incremement the key it relates to so that it counts against an incoming link from BigSite.com
Thats how I would do it :)
I use a SQL database and a WHILE LOOP to display the contents of the database on the main page and am currently trying to incorporate a Facebook "like" button for each and every item on the page.
Currently every item listed in the database is displayed on Shopping.php
I need the server to be able to create a page with a unique address for each item in the database so that the "like" buttons don't all link back to the Shopping.php page.
What I am imagining is something like "/Shopping.php?item=0001" that will link to a page dedicated to that one single item.
Thanks for your time.
Please if you could give a code how to do it , it would be great help
if(isset($_GET["item"]))
{
// Fetch information about item with ID = $_GET["item"] from database and render a page for it.
}
Was that what you were looking for?
Yes this would work, only change is, you need to get the item number from the URL and refine your SQL query.
if(isset($_GET["item"]){
sql = "select * from items where item_id = $_GET['item']";
\\display results after executing this sql
}
But if you want to optimize it to search engine, i recommend you keep your urls as
shopping/item1
shopping/item2 etc
My site has a library full of games, nations, game scenarios, etc.
library.php is given a type=___ & id=___
for example library.php?type=scenario&id=ABCD001
library.php saves the id to a session variable and loads an include appropriate for the type
This all works just dandy. Now, I wanted to give my users the option of pulling up a random scenario. To do that, I added a special id to the logic within lib-scenario.php (the include) such that if given library.php?type=scenario&id=random the include knows to run an alternate query for a random record rather than for the actual id
This also works just dandy... unless someone hits the Random Scenario button two+ times in a row, and decides that the previous random scenario was way cooler, I want to go back to that.
Because the http address is always directory/library.php?type=scenario&id=random no matter how many times you click Random Scenario, as soon as you click back you'll be taken to the last page with an alternate address you visited.
So, if you start at the Home page, and hit Random Scenario 35 times, then decide the 34th one was what you wanted and click BACK, you'll be put back onto the Home page.
I must admit this was not a problem I had anticipated. One of my testers was the first to have the urge to back-up in the random scenario stream and here we are.
How can I add back-up functionality to my script?
Make the 'Random Scenario' button simply link to an actual (but random) scenario id. You'll probably have to construct this with an SQL query to get all the id's of your scenarios.
$result = mysql_query("SELECT id FROM scenarios");
while ($row = mysql_fetch_row($result)) {
$ids[] = $row[0];
}
$randomid = array_rand($ids);
Button:
<a href="directory/library.php?type=scenario&id=<?php echo $randomid; ?>Random Scenario</a>
If your scenario id's are all consecutive numbers you can simply use this instead:
$randomid = rand($min, $max);
you can resolve this by redirecting to the canonical url for the scenario, i.e.: id=random redirects to id=A92831 or whatever was selected. the final url will be stored in the history, rather than the id=random url.