Page Caunter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Page Caunter

Post by moiseszaragoza »

Hey everyone.
I have a Small problem I want to create a page counter to know how many people are visiting each page in my site. And I want to do this using php.

I added a column in my DB called Counter

I need to add one on a page event Load.
But this dose not always happened just when they see a specific section.

This is what I have

The Upload function that need to be called

Code: Select all

<?php 
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE CONTENT SET Counter=%s WHERE contentID=%s",
                       GetSQLValueString($_POST['Counter'], "int"),
                       GetSQLValueString($_POST['contentID'], "int"));

  mysql_select_db($database_DinamicPortfolio, $DinamicPortfolio);
  $Result1 = mysql_query($updateSQL, $DinamicPortfolio) or die(mysql_error());
} 
?>
How I’m calling the function

Code: Select all

&lt;body onload=&quote;&lt;?php echo $editFormAction; ?&gt;&quote;&gt;
The Form with my information

Code: Select all

&lt;form name=&quote;form1&quote; id=&quote;form1&quote; method=&quote;POST&quote;&gt;
			        
&lt;p&gt;XX This is invisible Page Counter   &lt;input name=&quote;Counter&quote; type=&quote;hidden&quote; id=&quote;Counter&quote; value=&quote;&lt;?php echo ++ $row_ShowContent&#1111;'Counter']; ?&gt;&quote; /&gt;
  &lt;input name=&quote;contentID&quote; type=&quote;hidden&quote; id=&quote;contentID&quote; value=&quote;&lt;?php echo $row_ShowContent&#1111;'contentID']; ?&gt;&quote; size=&quote;3&quote; /&gt;  XX
                      &lt;input type=&quote;hidden&quote; name=&quote;MM_update&quote; value=&quote;form1&quote;&gt;
					&lt;/form&gt;
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

onLoad is a JavaScript event. You're trying to use it to run PHP code?
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Post by moiseszaragoza »

Ok I have been trying some things.

My main goal id to update a record in my DB when the page loads.

I try calling the php function using a Javascript. But that did not work.

Now I'm trying to call the function using php and I must have something wrong but I don’t know what it is


This in before ant HTML is called

Code: Select all

function upload(){
	if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
 	 $updateSQL = sprintf("UPDATE CONTENT SET Counter=%s WHERE contentID=%s",
                       GetSQLValueString($_POST['Counter'], "int"),
                       GetSQLValueString($_POST['contentID'], "int"));

 	 mysql_select_db($database_DinamicPortfolio, $DinamicPortfolio);
 	 $Result1 = mysql_query($updateSQL, $DinamicPortfolio) or die(mysql_error());
	}
}
<html>
<head>
Now I'm calling the funcion from the body of my page

Code: Select all

<form name="form1" id="form1" method="POST" action="<?php echo $editFormAction; ?>">
			        
					<p align="left">XX This is invisible Page Counter   <input name="Counter" type="text" id="Counter" value="<?php echo ++ $row_ShowContent['Counter']; ?>" />
					  <input name="contentID" type="text" id="contentID" value="<?php echo $row_ShowContent['contentID']; ?>" size="3" />
					  XX
                      <input type="hidden" name="MM_update" value="form1">
					</form>
					 
					  <?php 
					  upload();
?>
I am thinking that this should call the function names upload and I would see that the field 'Counter' was updated with the new value of ++
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Post by moiseszaragoza »

Ok I went over my code and I remember I’m not passing the information on a post.

How ho I pass 2 parameters threw a function

And what should I change in my function 4 it to work
Thanks
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Post by moiseszaragoza »

Ok Now I have a funcion and I am passing 2 Parametters $Counter and $ contentID

Here is my Funcion

Code: Select all

function upload($Counter,$contentID){
	
 	 $updateSQL = sprintf("UPDATE CONTENT SET Counter=%s WHERE contentID=%s",
                       GetSQLValueString($Counter, "int"),
                       GetSQLValueString($contentID, "int"));

 	 mysql_select_db($database_DinamicPortfolio, $DinamicPortfolio);
 	 $Result1 = mysql_query($updateSQL, $DinamicPortfolio) or die(mysql_error());
	
}
if some one can tell me why this dose not work
I would be happy

Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

add

Code: Select all

echo $updateSQL;
on line 6 and post it's result.
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Post by moiseszaragoza »

Ok I made some changes to the code becase I was not getting it from a post value.

New Code

Code: Select all

$updateSQL = sprintf("UPDATE CONTENT SET Counter=%s WHERE contentID=%s",
                       GetSQLValueString($row_ShowContent['Counter'], "int"),
                       GetSQLValueString($row_ShowContent['contentID'], "int"));
I added the echo $updateSQL;

Code: Select all

echo $updateSQL;
and it Retures

*****UPDATE CONTENT SET Counter=NULL WHERE contentID=NULL****

So I cant pass the variables that way
Post Reply