...

View Full Version : $ Globals Array



ROYW1000
12-10-2010, 04:34 PM
Hi

I currently pass a URL to our website as follows: http://website.com/?nid=10 but want to extend this to include a keyword from google as well. For example http://website.com/?nid=10&kw={keyword}

This is the current script, how do I go about changing it so that if it just contains a nid and no keyword it will do one search of the array and return the result or if is has the keyword as well it returns something different.

For example if it had nid=1 it would return 0844 000 0300 and if it had nid=1&kw=help it would return 0844 000 0350



<?php
$GLOBALS['ct_get_parameter_name'] = 'nid';
$GLOBALS['ct_default_number_id'] = 0;

// source_id => phone number
$GLOBALS['numbers'] = array(
0 => '0844 000 3000',
1 => '0844 000 0300',
2 => '0844 000 0301',
3 => '0844 000 0302',
);
session_start();

if (array_key_exists($GLOBALS['ct_get_parameter_name'], $_REQUEST))
{
$_SESSION['ct_source'] = $_REQUEST[$GLOBALS['ct_get_parameter_name']];
}

function get_phone_number()
{
$numbers =& $GLOBALS['numbers'];
$default_number_id = $GLOBALS['ct_default_number_id'];

if (isset($_SESSION['ct_source']))
{
if (array_key_exists($_SESSION['ct_source'], $numbers))
{
return $numbers[$_SESSION['ct_source']];
}
else
{
// otherwise return first number
return $numbers[$default_number_id];
}
}
else
{
// return first number
return $numbers[$default_number_id];
}
}
?>



Many thanks in advance for your help
Roy

Fumigator
12-10-2010, 10:06 PM
This is pretty straight forward, use the additional query string value and expand the logic (and that numbers array) that assigns a phone number. Maybe if you were more specific on which part of the task you're having trouble with, or you were to show us what you've tried so far to get it working, we could be more helpful.

ROYW1000
12-11-2010, 10:29 AM
Hi

The bit I am stuck on is the array part (How to create it and does it become part of the existing one or a new one) as at the moment they are for the nid, so if the nid=1 it will display the number from array 1.

I am not sure how to go incorprate the next part in which will be a nid and a kw into the script as well as the array part and how to format that.

If the incoming link just contains a nid, this part is already working fine. Now I want to add a new part where a incoming link could contain a nid=1&kw=test. Then it would need to look for an exact match for nid=1&kw=test.

Therefore currently if nid=1 0844 000 3000 would display. However the extra additional to the arrray would be if nid=1&kw=test a new number would be in the array for that combination for example 0844 000 3050.

Thanks
Roy


Thanks
Roy

ROYW1000
12-14-2010, 03:33 PM
Hi

This is hwo far I have got with the code and cant get it to work with the kw included but its working fine for the nid using the following incoming url. When I add in the kw it returns array so it I think its nearly there but not sure what happened.

http://www.website.com/?nid=1
http://www.website.com/?nid=1&kw=test

If there is no nid in the incoming URL it should display array 0.


<?php

// get or post parameter name
$GLOBALS['ct_get_parameter_name'] = 'nid';
$GLOBALS['ct_default_number_id'] = 0;
// Sitelinks
$GLOBALS['keywords'] = 'kw';


// source_id => phone number
$GLOBALS['numbers'] = array(
0 => array ('0844 000 3000'),
1 => array(
'0844 000 0300',
'keywords' => array(
'test' => '0844 000 0350',
'new' => '0844 000 0351'
)
),
2 => array('0844 000 0301'),
3 => array('0844 000 0302'),
);

session_start();

if (array_key_exists($GLOBALS['ct_get_parameter_name'], $_REQUEST))
{
$_SESSION['ct_source'] = $_REQUEST[$GLOBALS['ct_get_parameter_name']];
}
if (array_key_exists($GLOBALS['keywords'], $_REQUEST))
{
$_SESSION['ct_source'] = $_REQUEST[$GLOBALS['keywords']];
}


function get_phone_number()
{
$numbers =& $GLOBALS['numbers'];
$default_number_id = $GLOBALS['ct_default_number_id'];

if (isset($_SESSION['ct_source']))
{
if (array_key_exists($_SESSION['ct_source'], $numbers))
{
if (!empty($_REQUEST['kw']) && array_key_exists($_REQUEST['kw'], $numbers[$_SESSION['ct_source']]['keywords']))
{
return $numbers[$_SESSION['ct_source']]['keywords'][$_REQUEST['kw']];
}
return $numbers[$_SESSION['ct_source']][0];
}
else
{
// otherwise return first number
return $numbers[$default_number_id];
}
}
else
{
// return first number
return $numbers[$default_number_id];
}
}
?>
<?php echo get_phone_number(); ?>

Many thanks
Roy

Fumigator
12-14-2010, 08:48 PM
A few questions about your script:

Do you really need to use $_REQUEST instead of $_GET?

Is there really a pressing need to assign your query string variable names ("nid" and "kw") to other variables? I'm all for flexible code but when you hardcode "kw" anyway as you're doing, it defeats the purpose of the use of a variable so you may as well use the constant.

Is there really a need to define your $GLOBALS['numbers'] array outside the get_phone_numbers() function?

As for the format of your array, what if you were to use the keywords as index values on the second level, and include a default index to indicate no keyword was provided? Maybe like:



<?php
function get_phone_number() {
$numberArray[0]['default_number'] = '0844 000 3000';
$numberArray[1]['default_number'] = '0844 000 0300';
$numberArray[1]['test'] = '0844 000 0350';
$numberArray[1]['new'] = '0844 000 0351';
$numberArray[2]['default_number'] = '0844 000 0301';
$numberArray[3]['default_number'] = '0844 000 0302';

if (isset($_GET['nid']) && isset($_GET['kw'])) {
if (isset($numberArray[$_GET['nid']][$_GET['kw']])) {
return $numberArray[$_GET['nid']][$_GET['kw']];
} else {
if (isset($numberArray[$_GET['nid']]['default_number'])) {
return $numberArray[$_GET['nid']]['default_number'];
} else {
return $numberArray[0]['default_number'];
}
}
} else {
if (isset($_GET['nid']) && isset($numberArray[$_GET['nid']]['default_number'])) {
return $numberArray[$_GET['nid']]['default_number'];
} else {
return $numberArray[0]['default_number'];
}
}
}
?>

ROYW1000
12-15-2010, 07:55 AM
Hi Fumigator

I was actually given that code by our old phone company who have since gone out of business, so I am not really sure why is was designed the way it was.

Anyway I have just had the pleasure of testing your code you kindly supplied and it works perfectly.

Many thanks
Roy

barkermn01
12-15-2010, 09:14 AM
@Fumigator that is the best code for it i have seen,

You should not be so hevy on the globals array one it makes your code a mess and 2 the Globals includes a recusion of it's self and the Globabs hold all vars in php

E.G

$test = "helloWorld";
echo $GLOBALS['test']; // would echo helloWorld

So i's point less to use it it should ne just standard vars alot less memory used than serching the globals array for a key and value



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum