Fix Notice: Undefined index

Hi

I added this to my pages but it gives errors “Notice: Undefined index kw” on every page load.

How can I fix it?

<h1>
<?php if ($_GET['kw']): ?>
<?php echo ucwords($_GET['kw']); ?>
<?php else: ?>
<?php echo $_helper->productAttribute($_product, $_product->getHeadline(), 'headline') ?>
<?php endif; ?>
</h1>

Thank you

What does $_GET contain?

Not sure what $_GET contains. It gets the kw URL Parameters if they are there.

?kw=something+something+else

var_dump it before your if.

If I add this <?php echo var_dump($_GET['kw']); ?> before it without any URL Parameters it returns NULL

That’s not the way to use var_dump. You don’t echo it, you just call it see the manual and was suggesting dumping the entire $_GET array - we already know $_GET['kw'] doesn’t exist.

In any event, you need to look at where you think you set $_GET['kw'] as it is not getting set.

Try this:


<?php if ( isset($_GET['kw']) && $_GET['kw']) : ?>

Edit:

Vers-002:


<?php
if ( if ( isset($_GET['kw']) && $_GET['kw'])  :
  $tmp = ucwords($_GET['kw']); 
else: 
  $tmp = $_helper->productAttribute($_product, $_product->getHeadline(), 'headline') ;
endif; 
echo '<h1>' .$tmp .'</h1>';
?>

I changed it to:

<?php if (isset($_GET['kw'])): ?>
<?php echo ucwords($_GET['kw']); ?>
<?php else: ?>
<?php echo $_helper->productAttribute($_product, $_product->getHeadline(), 'headline') ?>
<?php endif; ?>

It seems to work fine without errors this way.

1 Like

[off-topic]
You know you don’t need an opening <?php and closing ?> tag on every line, don’t you? One opening tag at the beginning and a closing one at the end will be enough.

4 Likes

Yes, but I understand it a little better. : )

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.