Jump to content

Writing a simple statement with an index

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
makamo66

makamo66

    Newbie

  • Members
  • PipPip
  • 18 posts
I can write this and get the right results:

if ( $_SESSION['addId'] == 'add[4]' )
$productId = 1;
if ( $_SESSION['addId'] == 'add[5]' )
$productId = 2;
if ( $_SESSION['addId'] == 'add[6]' )
$productId = 3;

But if I try to streamline it by writing it this way, it doesn't work:

if ( $_SESSION['addId'] == 'add[$i]' )
$productId = $i;

I think the two methods should be equivalent so why doesn't the second way work?

#2
ksemeks

ksemeks

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
Play with the quotes. Try using double iinstead.
// d-_-b+

#3
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
I'd use an preg match

preg_match("/add\[(\d)\]/", $_SESSION['addId'], $match);
$productId = $match[1];

__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#4
makamo66

makamo66

    Newbie

  • Members
  • PipPip
  • 18 posts
All of the problems that I've been writing about lately had to do with the fact that I was using nine image buttons and just one form. Once I put form tags around all of my image buttons individually the problems went away.