Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: $ Globals Array

  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    161
    Thanks
    10
    Thanked 1 Time in 1 Post

    $ Globals Array

    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 Code:
    <?php
    $GLOBALS
    ['ct_get_parameter_name'] = 'nid';
    $GLOBALS['ct_default_number_id'] = 0;

    // source_id => phone number
    $GLOBALS['numbers'] = array(
        
    => '0844 000 3000',
        
    => '0844 000 0300',
        
    => '0844 000 0301',
        
    => '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

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jan 2010
    Posts
    161
    Thanks
    10
    Thanked 1 Time in 1 Post
    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
    Last edited by ROYW1000; 12-11-2010 at 11:05 AM. Reason: Added additional info

  • #4
    Regular Coder
    Join Date
    Jan 2010
    Posts
    161
    Thanks
    10
    Thanked 1 Time in 1 Post
    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 Code:
    <?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(
     
    => array ('0844 000 3000'),
     
    => array(
     
    '0844 000 0300',
     
    'keywords' => array(
     
    'test' => '0844 000 0350'
    'new' => '0844 000 0351'
     

    ), 
    => array('0844 000 0301'), 
    => 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

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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 Code:
    <?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'];
            }
        }
    }
    ?>

  • #6
    Regular Coder
    Join Date
    Jan 2010
    Posts
    161
    Thanks
    10
    Thanked 1 Time in 1 Post
    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

  • #7
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    @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
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •