Forum Moderators: coopster

Message Too Old, No Replies

If country selected echo = X

         

solokron

7:14 am on Mar 3, 2010 (gmt 0)

10+ Year Member



Hello guys and gals.

I am looking for a script where I can do a country select. Each having a unique set of variables. What I would like to do is a drop down where if a user selects Europe for example, I can say load this image and load this particular URL. I don't want to do a bunch of URL redirects to different versions of the page. I have looked through resourceindex but I don't believe I am looking in the right places. Any help would be appreciated!

solokron

7:19 am on Mar 3, 2010 (gmt 0)

10+ Year Member



To clarify, when I say "load this particular URL" I mean place this link on the web page, not a redirect.

Zipper

3:32 pm on Mar 3, 2010 (gmt 0)

10+ Year Member



If you want to update an already loaded page using server-side data, you have to either reload it or use something like AJAX to grab the data and add it to the current page.

and do you already have a plan for managing the set of variables you mentioned? How and where are they stored? server-side or client-side?

and not that it really matters, but by countries do you mean continents? because Europe is a continent! :)

solokron

6:00 pm on Mar 3, 2010 (gmt 0)

10+ Year Member



I meant U.K. or Ireland etc. lol, I am surprised I wrote it that way.

I'll start looking for AJAX for it. Any direction or pointers?

CyBerAliEn

7:46 pm on Mar 3, 2010 (gmt 0)

10+ Year Member



This is really more of a Javascript issue than PHP.

If you can load out ALL the info/data into a single HTML page, you can get by with simple JS (no AJAX).

If this is a low amount of data (data being just images and associated URLs, which is what it sounds like)... you could possibly get done what you want by having your server side PHP load out your HTML page stuffed with Javascript code (arrays, containing countries and URLs). You could then setup JS on your page such that when a user does your desired action (clicks on something), JS will look up what it needs to get from the arrays and manipulate the HTML to replace or add content. This could be very straight forward if you're talking literally just loading an image and link.

But, if it requires a lot of data, or more intensive server side processing to handle a request... AJAX is the way to go for certain! I recommend you try moving or posting your question in this forum's javascript category, or even try searching about AJAX examples/tutorials... you may quickly learn or see how to accomplish what you desire to do.


But what is AJAX?
It's basically asynchronous javascript. Huh? In websites, a user sends a request for a page; the server returns the page; and the user gets the page they wanted. To do anything else with the server, the user would need to send a new request to the server... which in HTML/web servers, essentially meant that you have to reload the page or request a new page.

This is an issue if you want to do something small, quick, etc. Hence the dominance of AJAX. All it really does, is it allows you to send a request to the server, via javascript. So what's the big deal? Instead of reloading a page or sending to a new page, you can now simply tell JS to handle the request and then JS can return the results of the request and manipulate the currently loaded page.

To think of this "in action", consider Google's gmail. It is heavily loaded/based in JS/AJAX and really pioneered it into mainstream. When you open gmail, it automatically reloads your emails, magically attaches files to your current email, allows you to select a person to send the email to by simply typing in a few letters of their name or email, and much more. All of this is done behind the scenes, continuously, via JS (a la AJAX). AJAX is not a THING... it is a culmination of existing technologies (namely HTML, JS, and a backend server to handle/manipulate requests such as PHP).

solokron

8:17 pm on Mar 3, 2010 (gmt 0)

10+ Year Member



I was thinking about javascript as well but I want to do this cross site so PHP would be the way to go I think. I would like to set if country=England set England.

country1=U.S.
country2=England
country3=Australia

if country2 then echo countryimg1 ... img src=
if country2 then echo countryurl3 ... a href=

etc.

solokron

9:31 pm on Mar 3, 2010 (gmt 0)

10+ Year Member



"cross site" should be "across the site"

Matthew1980

9:39 pm on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there soloKron,

Just skimming the messages here, country selection, are you providing language packs for people who would like the choice? or is it just images at this point? Or have I misread, its good accessibility to provide more than one language IMHO.

MRb

solokron

10:06 pm on Mar 3, 2010 (gmt 0)

10+ Year Member



Basically if they select England, it would save that setting and show an image particular for English users and a different URL in links in the web pages.

Zipper

10:48 pm on Mar 3, 2010 (gmt 0)

10+ Year Member



Ok, then comes the question, do you want to remember their choice for a single visit or for multiple visits?

Because, if it's just for a single visit you can just use PHP sessions (else, there are many options including cookies, db etc.)


<?php
session_start();
if(<check for the user country here> )
$_SESSION['USER_COUNTRY'] = <user country>;
?>


and in your pages you can refer back to it like,


<?php
if($_SESSION['USER_COUNTRY'] == 'UK')
// do something
?>


Hope it helps.

solokron

1:31 am on Mar 4, 2010 (gmt 0)

10+ Year Member



Many visits including cookie. I would like to do a drop down Country select and then use something like you provided there, or even a reference to an include file with all those reference lines for img src and a href placed.

Zipper

2:59 pm on Mar 5, 2010 (gmt 0)

10+ Year Member



OK, I think someone asked this earlier too. But how many countries are you dealing with here? If it's a fairly small number you can store them in a php array (as oppose to a file or db).

Here's a simple example of how you can store you're image sources and links in a multi-dimensional array.


$countries = array(
"UK" => array(
"link" => "http://url",
"image" => "path-to-image/image.jpg"),
"US" => array(
"link" => "http://url2",
"image" => "path-to-image/image2.jpg")
);


once you got that setup, you will have your drop-down list of countries with the values assigned in the array. (you can dynamically generate this as well)


foreach($countries as $country => $value){
echo "<option value='$country'>$country</option>";
}


then you will have to deal with cookies. when a user selects a country, you can create a cookie to store the country. like,


setcookie( "USER_LOCATION", $<your php variable> ) ;


and you will also need to check in each page if the cookie already exists, and if so choose the appropriate links/image etc.


if (isset($_COOKIE['USER_LOCATION'])) {
$country = $_COOKIE['USER_LOCATION'];
$link = $countries[$country]["link"];
$image = $countries[$country]["image"];
// now you have the geo-specific link and image
}


Hope this helps.

solokron

6:19 am on Mar 7, 2010 (gmt 0)

10+ Year Member



Thank you Zipper! I'll see what I can do with this.