tlt2w
tlt2w

Reputation: 321

Fix PHP Notice: 'Undefined index' on Wordpress

I'm trying to fix the Undefined index PHP notice on a wordpress site.

At the moment this is what I got:

    <?php if($_SESSION['currency-select'] == "a") echo 'selected="selected"';?>

I tried to write this way but then the site goes down:

    <?php if(isset($_SESSION['currency-select'] == "a")) echo 'selected="selected"';?>

Also, I have a query which is also without isset and i'm trying to fix it:

                    if($_SESSION['currency-select'] == 'b') {

                if($_GET['pricing'] == '1') {

                    $args['meta_query'][] = array(

                        'key' => 'price',

                        'value' => array( '0', '250' ),

                        'compare' => 'BETWEEN',

                        'type' => 'numeric'

                    );

                }

I tried to write:

      if(isset($_GET['pricing'] == '1')) {
      if($_GET['pricing']) {

but it doesn't work as well.

Thanks for your support!

Upvotes: 3

Views: 19854

Answers (3)

makallio85
makallio85

Reputation: 1356

This should work if currency_select is set.

 <?php if(isset($_SESSION['currency_select'])) echo "selected='selected'";?>

Upvotes: 1

Rottingham
Rottingham

Reputation: 2604

Simply convert your faulty isset() check.

if (isset($_GET['pricing'])) {
    $pricing = $_GET['pricing'];
}

The short hand version would look like this, if you want to assign a default value.

$pricing = (isset($_GET['pricing'])) ? $_GET['pricing'] : 0;

Upvotes: 5

stUrb
stUrb

Reputation: 6832

Try looking if the variable isset and then check the contents of it.

if( isset($_GET['pricing']) && $_GET['pricing'] == '1' ){
   //- do some magic
}

With your if-clause you check if the clause itself isset() instead of the variable.

ps: why do you check the number 1 as a string 1?

Upvotes: 7

Related Questions