request for help cleaning up very small php code

Here you go, this works as i have fully tested it myself and works just fine.
I made a couple of tweaks to the database so be sure to look at the screenshot attached for new db structure.

A couple of things to note about the following script.
I couldn’t sit here and help you whilst using mysql, it goes against my nature (and most helpers on here)
Using mysql is a NO NO so stop asap - like yesterday!

So, with that said, this uses mysqli (other drivers also available :slight_smile: )
Place the class somewhere - ideally in it’s own file, then include it and use the few lines of code below the class to display the image, count the impressions and clicks (when they happen).

Go ahead and pop it into your scripts and let me know how you get on.
Don’t forget to put your connection details at the top of the script where they go.

Red :wink:

[php]
// Define MySQL connection details and database table name
$SETTINGS[“hostname”] =’’;
$SETTINGS[“mysql_user”] =’’;
$SETTINGS[“mysql_pass”] =’’;
$SETTINGS[“mysql_database”] =’’;
$SETTINGS[“data_table”] =‘graphics’;

class ImageClickTracker {
/**
* set container(s).
**/
public $dbc;
public $id;
public $table;

/**
* setup the class.
**/
public function __construct($SETTINGS) {
	// Setup a connection to the database.
	$this->dbc = new mysqli($SETTINGS["hostname"], 
							$SETTINGS["mysql_user"], 
							$SETTINGS["mysql_pass"], 
							$SETTINGS["mysql_database"]);
	
	// Connect to the database.
	if($this->dbc->connect_errno) {
		die('Error: Could not connect to database');
	}
	
	// clean the string.
	$this->table = $this->sql_escape_string($SETTINGS['data_table']);
	
} // End Method.


/**
* cleans the string.
**/
public function sql_escape_string($string) {
	return mysqli_real_escape_string($this->dbc, $string);
}

/**
* Get the image.
**/
public function get_image($id) {
	$this->_image_id($id);
	$this->_get_page_name();
	
	$graphic = false;
	$query = "SELECT url, graphic FROM " . $this->table . " WHERE (id=? AND sourcepage=?)";
	if($stmt = $this->dbc->prepare($query)) {
		if($stmt->bind_param("is", $this->id, $this->page)) {
			$stmt->execute();
			$stmt->store_result();
			if($stmt->num_rows > 0) {
				$stmt->bind_result($url, $graphic);
				$stmt->fetch();
			}
		}
	}
	$stmt->close();
	
	// update the impression count.
	$this->_increase_impressions();
	
	// return the url and image path in an array ready for use. 
	// Note: will need to use <img> tags in calling scripts.
	return array('url'=>$url, 'image'=>$graphic);
} // End Method.

/**
* ImageClicked.
**/
public function image_clicked($id, $url) {
	$query = "UPDATE " . $this->table . " SET clicks=clicks+1 WHERE id=?";
	if($stmt = $this->dbc->prepare($query)) {
		if($stmt->bind_param("i", $id)) {
			$stmt->execute();
			$stmt->close();
			header('Location: http://' . $url);
			exit();
		}
	}
	return false;
	//
} // End Method.

/**/

/**
* get the image id.
**/
private function _image_id($id) {
	$this->id = $id;
} // End Method.


/**
* set the pagename.
**/
private function _get_page_name() {
	$this->page = basename($_SERVER['REQUEST_URI']);
} // End Method.


/**
* Increase the impressions count by 1.
**/
private function _increase_impressions() {
	$query = "UPDATE " . $this->table . " SET impressions=impressions+1 WHERE (id=? AND sourcepage=?)";
	if($stmt = $this->dbc->prepare($query)) {
		if($stmt->bind_param("is", $this->id, $this->page)) {
			$stmt->execute();
			$return = true;
		}
	}
	$stmt->close();
	return $return;
} // End Method.

} // End Class.

/**

  • To use:
    **/
    // setup the class.
    $ImageTracker = new ImageClickTracker($SETTINGS);
    // check if link was clicked…
    if(isset($_GET[‘id’]) && isset($_GET[‘url’])) {
    $ImageTracker->image_clicked($_GET[‘id’], $_GET[‘url’]);
    }

// get an image.
$imageid = 20;
$img1 = $ImageTracker->get_image($imageid);
echo ‘’;
?>[/php]


thanks… i hope to test this here in a few minutes.
in the table snapshot, i see that you added a record to the bottom that has an empty name field, is this correct?

no that is my editor awaiting a command for a new line.
The last line is ‘graphic’ :wink:

hi red,

thanks for solving my problem.
it appears to be working perfectly.

rinasheville

You’re welcome, happy to help :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service