Use php include for certain div of the page

Status
Not open for further replies.

igordr

Member
22
2012
1
0
Hello,

I have this syntax:

Code:
if ( $CountryCode == RS || $CountryCode == ME ) {
	require $_SERVER['DOCUMENT_ROOT']."/ads/et300x250rsTEST.html ";  

}

I would like to access certain div o the above page for example


Code:
if ( $CountryCode == RS || $CountryCode == ME ) {
	require $_SERVER['DOCUMENT_ROOT']."/ads/et300x250rsTEST.html#nameovDiv1";  

}

than

Code:
if ( $CountryCode == RS || $CountryCode == ME ) {
	require $_SERVER['DOCUMENT_ROOT']."/ads/et300x250rsTEST.html#nameovDiv2";  

}

Code:
if ( $CountryCode == RS || $CountryCode == ME ) {
	require $_SERVER['DOCUMENT_ROOT']."/ads/et300x250rsTEST.html#nameovDiv3";  

}

And make just that div visible... On page ads/et300x250rsTEST.html i would add all my advertisment in divs, and return right one when user from specific country visits the site.

Qusetion is - How to use php include to include just part of the page which is on mine server, so i have full control of it, i can add whatever code there too [et300x250rsTEST.html] ?

Thanks
 
9 comments
Without seeing the rest of the code it would be hard to say.

You will have to use javascript to turn the div back to display from none.

Are you writing the entire html from the php program? If not just add the javascript in html to display the div you want. How are you determining the country code?
 
Hi mate, thanks for reply.

This is the full code:

Code:
<?php
//connect to db
$con = mysql_connect('localhost', 'xxxxxxx_user', 'passss') or die(mysql_error());

mysql_select_db('xxxxxxx_base', $con);

//Get the parameters
if ($_GET["ip"]){
  $ip = mysql_real_escape_string(trim($_GET["ip"]), $con);
} else {
  $ip = getenv('REMOTE_ADDR');
}

//Run the IP location query
$ipQuery = mysql_query("SELECT * FROM `ip_group_city` where `ip_start` <= INET_ATON('$ip') order by ip_start desc limit 1;", $con);
$ipData = mysql_fetch_array($ipQuery);
$nbResults = (bool)mysql_num_rows($ipQuery);

$CountryCode = $ipData['country_code'];

if ( $CountryCode == RS || $CountryCode == ME ) {
	require $_SERVER['DOCUMENT_ROOT']."/ads/et300x250rsTEST.html";  

} 

else if ( $CountryCode == HR || $CountryCode == BA ) {
	include $_SERVER['DOCUMENT_ROOT']."/ads/et300x250hr.html"; 

}

else if ( $CountryCode == DE || $CountryCode == AU || $CountryCode == CH ) {
	include $_SERVER['DOCUMENT_ROOT']."/ads/sp300x250deAUch.html"; 
}

else {  
	include $_SERVER['DOCUMENT_ROOT']."/ads/300x250else.html"; 

}


?>

This is code for showing desired advertisement based on user location... This is just one part of ads on my site, as you see 300x250 banner. Am i going to copy and paste this code for every ad unit, and how to avoid that? Also, i want to connect to database just once...
May you help me with that?

Thanks
 
Or you could shorten it to

PHP:
if (file_exists($_SERVER['DOCUMENT_ROOT']."/ads/et300x250".$CountryCode.".html")) {
include($_SERVER['DOCUMENT_ROOT']."/ads/et300x250".$CountryCode.".html");
} else {
include $_SERVER['DOCUMENT_ROOT']."/ads/300x250else.html"; 
}

Or even better just wrap it into a function and call it wherever you need to call an ad
 
Thanks for reply bluefrogx!

May you help me with wrap all this into the function and call it, how code should look like?

Thanks a lot.
Igor
 
Isnt the geoip SQL database outdated now?


You should change to the binary version there are free updates every month & its far easier to update & 0 queries.
http://www.maxmind.com/app/geolite

Usage
PHP:
	include("geoip.inc");
	$gi = geoip_open("/home/xxx/GeoIP.dat",GEOIP_STANDARD);
	$ip =$_SERVER['REMOTE_ADDR'];
	$CountryCode = geoip_country_code_by_addr($gi, "$ip");
 
Gavo, your post is very interested, but where i may find and download GeoIP.dat and geoip.inc ?
Also, how i can use statements as

if ( $CountryCode == RS || $CountryCode == ME ) {
require $_SERVER['DOCUMENT_ROOT']."/ads/et300x250rsTEST.html";

}

else if ( $CountryCode == HR || $CountryCode == BA ) {
include $_SERVER['DOCUMENT_ROOT']."/ads/et300x250hr.html";

}

else if ( $CountryCode == DE || $CountryCode == AU || $CountryCode == CH ) {
include $_SERVER['DOCUMENT_ROOT']."/ads/sp300x250deAUch.html";
}

else {
include $_SERVER['DOCUMENT_ROOT']."/ads/300x250else.html";

}

May you write me code please, or help me with that?

Thanks
 
Hi,
I would say, change the page you are including to a php file too (as you probably already know, php can contain html or you can use php to echo it).
That way you don't even need an if statement in your main page, you just move those if statements to the page containing the ads, echo'ing the right div based on your "CountryCode" variable.
Good luck.
 
make functions for each ads,and call those ads function(country based);
like:

if ( $CountryCode == RS || $CountryCode == ME ) {
functionnameRSorME;

}

else if ( $CountryCode == HR || $CountryCode == BA ) {
functionnameHR;

}

else if ( $CountryCode == DE || $CountryCode == AU || $CountryCode == CH ) {
functionnameRSorME;
}

else {
functionname;

}

and add those ads in functions.if its not cleared to you,then maybe i can help but later.
 
Status
Not open for further replies.
Back
Top