jeudi 25 février 2016

PHP str_replace not replacing correctly

ok this is my code :

<?php if(empty($_GET['keywords']))
    {
        echo "Latest jobs in ".$_GET['loc'];
    } 
    elseif(empty($_GET['loc']))
    {
        echo "Latest ".$_GET['keywords']." jobs";
    } 
    elseif(empty($_GET['keywords']) && ($_GET['loc']))
    {
        echo "BLAH";
    }
    else 
    {
        echo "Latest ".$_GET['keywords']." jobs in ".$_GET['loc'];
    }
    ?>

On the index page there are 2 form fields , keywords & loc Now, if the user hits the form without entering data so $_get['keywords'] & $_get['loc'] are both empty i want it to echo BLAH

If $_get['keywords'] is empty but $_get['loc'] isnt echo "Latest jobs in ".$_GET['loc'];

If $_get['loc'] is empty but $_get['keywords'] isnt echo Latest ".$_GET['keywords']." jobs";

and if they both contain data I want it to echo "Latest ".$_GET['keywords']." jobs in ".$_GET['loc'];

My .htaccess file contains this

RewriteRule jobs/(.*)$ jobs.php?keywords=&loc=$1
RewriteRule (.*)/jobs$ jobs.php?keywords=$1&loc=
RewriteRule (.*)/jobs-in/(.*)$ jobs.php?keywords=$1&loc=$2

jobs.php is the page where I want the data above to be echoed Its for the title formatting etc

Please help guys, Ive spent 3 hours on this so far and all I've got is code block now :(

** STR_REPLACE code below **

function search()
{
if(isset($_POST['keywords']) || ($_POST['loc']))
    {
    $replace_array = array(',',' ');
    if(empty($_POST['keywords'])) 
    { 
        $keywords = ''; 
        $loc = str_replace($replace_array,'-',$_POST['loc']);
        header("Location: jobs/$loc");
    } elseif(empty($_POST['loc']))
    {
        $keywords = str_replace($replace_array,'-',$_POST['keywords']); 
        $loc = '';
        header("Location: $keywords/jobs");
    }
    else 
    {
        $keywords = str_replace($replace_array,'-',$_POST['keywords']); 
        $loc = str_replace($replace_array,'-',$_POST['loc']);
        header("Location: $keywords/jobs-in/$loc");
    }
    }   
}

Aucun commentaire:

Enregistrer un commentaire