Skip to main content
Inspiring
December 14, 2010
Answered

Counter field in database table

  • December 14, 2010
  • 19 replies
  • 3011 views

Am just thinking about how to solve a little problem which is new to me in PHP, but I'm hoping not to complex.

Basically, I have a database of holiday resorts, and my client would like to keep track of how many people click through to the resorts own website from our website.

So I was thinking if I have a field ClickCounter, initially set to 0, would there be some way to have a link that, once clicked on, would increment the ClickCounter field by 1.

If anyone could help me get started with this, that would be much appreciated.

Thanks.

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer

OK - I think the issue there was it needed to be == rather than = - so that's working.

So now I'm trying to just pull the LodgeURL from the the table using the query :

mysql_select_db($database_databaseConnection, $databaseConnection);

$query_rsLodgeURL = "SELECT LodgeURL FROM lodges WHERE LodgeID = '$the_resort'";

$rsLodgeURL = mysql_query($query_rsLodgeURL, $connSafari) or die(mysql_error());

$row_rsLodgeURL = mysql_fetch_assoc($rsLodgeURL);

$totalRows_rsLodgeURL = mysql_num_rows($rsLodgeURL);

Should that be LodgeID, or id? (LodgeID is the unique ID in the table)

But I'm not sure what the syntax needs to be for the actual link in the header part at the bottom...

So the lines that were :

if ($_GET['LodgeID'] == "1") {

header ("Location: http://www.elsaskopje.com");

}

if ($_GET['LodgeID'] == "2") {

header ("Location: http://www.behobeho.com");

}

if ($_GET['LodgeID'] == "7") {

header ("Location: http://www.singita.com");

}

What should that be changed to?


Change this:

mysql_select_db($database_databaseConnection, $databaseConnection);

$query_rsLodgeURL = "SELECT LodgeURL FROM lodges WHERE LodgeID = '$the_resort'";

$rsLodgeURL = mysql_query($query_rsLodgeURL, $connSafari) or die(mysql_error());

$row_rsLodgeURL = mysql_fetch_assoc($rsLodgeURL);

$totalRows_rsLodgeURL = mysql_num_rows($rsLodgeURL);

To this:

mysql_select_db($database_databaseConnection, $databaseConnection);

$query_rsLodgeURL = sprintf("SELECT LodgeURL FROM lodges WHERE LodgeID = %s",

GetSQLValueString($the_resort, "int"));

$rsLodgeURL = mysql_query($query_rsLodgeURL, $connSafari) or die(mysql_error());

$row_rsLodgeURL = mysql_fetch_assoc($rsLodgeURL);

echo $reLodgeURL['LodgeURL']; should output the entire lodge URL like http://www.singita.com or http://www.behobeho.com etc. depending on the URL parameter for LodgeID = 2 or 7 or whatever.

Then change this:

if ($_GET['LodgeID'] == "1") {

header ("Location: http://www.elsaskopje.com");

}

if ($_GET['LodgeID'] == "2") {

header ("Location: http://www.behobeho.com");

}

if ($_GET['LodgeID'] == "7") {

header ("Location: http://www.singita.com");

}

To this:

if ($row_rsLodgeURL['LodgeURL'] != "") {

header("Location: $row_rsLodgeURL['LodgeURL']");

} else {

header ("Location: http://www.google.com");

}

Cheers!

19 replies

December 14, 2010

database has a table 'resorts' with columns for resort and hits. On resort list page have links to resorts go to your_website.com/hit_counter.php?resort=resort_name

on hit_hounter.php enter a query to update the databse with +1 where the URL parameter = the resort in the resort column of the database.


$the_resort = "-1";
if (isset($_GET['resort'])) {
$the_resort = $_GET['resort'];
$query_hits = sprintf("UPDATE resorts
SET hits = hits+1
WHERE resort = %s",
GetSQLValueString($the_resort, "text"));}

Then redirect the page to the resort name in the header location. Use the URL parameter of resort_name to redirect to the resort_name.com URL. You can setup a conditional array where if resort_name = this then go to this.com or if resort_name = that go to that.com etc.

Inspiring
December 14, 2010

Duplicate post