Jump to content

Help with Adding to an array


PHPNEWTOIT

Recommended Posts

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}. However it may not always have a keyword so if it does not it needs to just use the nid array.

 

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, and if it does have a nid&keywordod 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

 

Thats what I am looking to achieve. Is this possible and how do I change the array. I have had a little attempt but not sure what I am doing and just got coding errors.

 

<?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.

 

Roy

Link to comment
Share on other sites

Hi

 

This is the part I am stuck on, how to add into the array and php code the following:

 

1) The nid part on its own is working fine and has a default value to return if a nid does note exist of '0844 000 3000'

2) I then want to add to the array and php code the ability to show the nid if its in the incoming url and then extend the code to say if it has a nid&kw to look into the array and find where nid=1@kw=help

3) So I assume the array would look something like 1 "test" => '0844 000 0350', and if this was in the incoming url (http://website.com/?nid=10&kw={keyword} it would display a different phone number from the array being 0844 000 0350. Obviously I have not added this part to the code as I am stuck on how to ammend the PHP and the array.

 

Thanks

Roy

Link to comment
Share on other sites

Hi

 

If the nid=1 with no keyword coming in via the URL I would like it to return the result from just the nid=1 so from the code I already have it would return '0844 000 0300'. (This part works fine)

 

Then I want to extend the array to use a totally different set of numbers if the incoming URL has a nid=1&kw=test. So in the array I would want to add a new number for this for example 0844 000 0350. Again if the nid=1&kw=new in the array this would have a number of 0844 000 0351 for example and so on.

 

So in the array nid=1 would exist once for nid like it does now but could have multiple nid=1 with various kw options.

 

If the incoming URL does not contain a nid or a kw it would defualt like it does now to 0 => '0844 000 3000'

 

I am stuck on how to make the array do want I want and the php changes.

 

So any help is much appreciated.

 

Roy

Link to comment
Share on other sites

Ignoring the use of $GLOBALS for now, I'd use a multi-dimensional array for this:

 

// 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'),
); 

 

Then where you check the "nid" exists within the numbers array, add in a conditional to check if the keyword is set and there's a matching index:

 

        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']]; 
        }

 

The code, in all honesty, is very ugly. If you want help rewriting it and to get away from the use of $GLOBALS, I'm happy to help.

Link to comment
Share on other sites

Correction to last code:

 

        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]; // <-- here
        }

Link to comment
Share on other sites

Hi

 

Thanks for the code help. I will have a little play with it and see how it goes.

 

Currently the nid array is around 1800 lines long and with the keywords added it will be about 3600+ lines long.

 

Would I be better removing this from the script and try adding the nid's into keyword combinations in a database. Would this help reduce the overhead.

 

Thanks

Roy

Link to comment
Share on other sites

Hell yes! 3600 would be a ridiculous length for an array.

 

Using a relational database this would be easy.  Just create your initial table containing the "nid" and phone number (and what every else is needed), then create a second table for the keywords - use the "nid" as the foreign key to link the keywords to the other table. That's a very brief over view, but should give some food for thought.

Link to comment
Share on other sites

Hi

 

I have played 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

 

<?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['ct_get_parameter_name1']];

}

 

 

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(); ?>

 

Also for some reason it would not let me post the php so excuse the text version.

 

Many thanks

Roy

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.