Home > Software engineering >  PHP if result equals this, then replace
PHP if result equals this, then replace

Time:04-17

I'm calling the 'Referring site URL' in a post array...

data34' => $lead->getReferringSiteUrl(),

But need to replace it with a friendly name.

i.e.

If www.a.com replace with 'A SITE'

If www.b.com replace with 'B SITE'

$data = array(
           //response data
           'data25' => (string) $lead->getId(),
           'data26' => $lead->getCommission(),
           'data27' => $lead->getCommissionBasis(),
           'data29' => $lead->getAcceptedPingtree(),
           'data33' => $lead->getMarketingSource(),
           'data34' => $lead->getReferringSiteUrl(),
           'data35' => $lead->getGclid() ? : '',
           'data36' => date('m/d/Y g:i:00 A'),
       ); 

I know it's something like...

if($ReferringSiteUrl == "www.a.com") {
    return 'Site A'
}
else if($ReferringSiteUrl == "www.b.com") {
    return 'Site B'
}
else if($ReferringSiteUrl == "www.c.com") {
    return 'Site C'
}

But I don't know how to work that into the above array.

Can anyone help please?

CodePudding user response:

You could call $lead->getReferringSiteUrl() before creating the $data array, then you can use a switch statement.

$ReferringSiteUrl = $lead->getReferringSiteUrl();
$RefferingSiteData = '';

switch ($ReferringSiteUrl) {
    case 'www.a.com':
        $ReferringSiteData = 'Site A';
        break;

    case 'www.b.com':
        $RefferingSiteData = 'Site B';
        break;

    case 'www.c.com':
        $RefferingSiteData = 'Site C';
        break;
}

$data = array(
    'data34' => $RefferingSiteDate
);

Alternative, you can use a closure as a IIFE (Immediately Invoked Function Expression):

$ReferringSiteUrl = $lead->getReferringSiteUrl();

$data = array(
    //response data
    'data25' => (string) $lead->getId(),
    'data26' => $lead->getCommission(),
    'data27' => $lead->getCommissionBasis(),
    'data29' => $lead->getAcceptedPingtree(),
    'data33' => $lead->getMarketingSource(),
    'data34' => (function() use ($ReferringSiteUrl) {
                    if ($ReferringSiteUrl == "www.a.com")
                        return 'Site A'

                    if ($ReferringSiteUrl == "www.b.com")
                        return 'Site B'

                    if ($ReferringSiteUrl == "www.c.com")
                        return 'Site C'  
                })(),
    'data35' => $lead->getGclid() ? : '',
    'data36' => date('m/d/Y g:i:00 A'),
); 

However, since both of those options can balloon out the code if there are many sites, I would go with the option of having an array that maps URLs to Site Names, and use that instead:

$ReferringSiteUrl = $lead->getReferringSiteUrl();

$referralMap = [
    'www.a.com' => 'Site A',
    'www.b.com' => 'Site B',
    'www.c.com' => 'Site C'
];

$ReferringSite = $ReferringSiteUrl;
if (in_array($ReferringSiteUrl, array_keys($referralMap))) {
    $ReferringSite = $referralMap[$ReferringSiteUrl];
}

$data = array(
    'data34' => $ReferringSite
);

The map method has the benefit that you should be able to build this array dynamically so you don't have to modify code at all if more sites are added.

CodePudding user response:

You can do it like this, but Jacob's version is better

$data = array(
       //response data
       'data25' => (string) $lead->getId(),
       'data26' => $lead->getCommission(),
       'data27' => $lead->getCommissionBasis(),
       'data29' => $lead->getAcceptedPingtree(),
       'data33' => $lead->getMarketingSource(),
       'data34' => ($lead->getReferringSiteUrl() == "www.a.com")?'Site A':(($lead->getReferringSiteUrl() == "www.b.com")?'Site B':(($lead->getReferringSiteUrl() == "www.c.com")?'Site C':'')),
       'data35' => $lead->getGclid() ? : '',
       'data36' => date('m/d/Y g:i:00 A'),
   ); 

CodePudding user response:

I'm not sure where $lead comes from, but you could just hardcode those:

function getReferringSiteUrl() {
    // Do whatever you had before in your getReferringSiteUrl function.
    $referringSiteURL = 'www.a.com';
    $sites = [
        'www.a.com' => 'Site A',
        'www.b.com' => 'Site B',
        'www.c.com' => 'Site C'
    ];

    return $sites[$referringSiteURL] ?? $referringSiteURL;
}

CodePudding user response:

A few semi colons and it's working like a dream...

            $ReferringSiteUrl = $lead->getReferringSiteUrl();

            $data = array(

            //response data

            'data25' => (string) $lead->getId(),
            'data26' => $lead->getCommission(),
            'data27' => $lead->getCommissionBasis(),
            'data31' => $lead->getLender(),
            'data32' => $lead->getTier(),
            'data33' => $lead->getMarketingSource(),
            'data34' => (function() use ($ReferringSiteUrl) {
                    if ($ReferringSiteUrl == "https://www.site1.co.uk")
                        return 'SITE1';

                    if ($ReferringSiteUrl == "https://site2.co.uk")
                        return 'SITE2';

                    if ($ReferringSiteUrl == "https://site3.com")
                        return 'SITE3';

                    if ($ReferringSiteUrl == "https://site4.co.uk")
                        return 'SITE4';

                    if ($ReferringSiteUrl == "https://www.site5.com")
                        return 'SITE5';
                })(),
            'data35' => $lead->getGclid() ? : '',
            'data36' => date('m/d/Y g:i:00 A'),
        );
  •  Tags:  
  • php
  • Related