Jump to content

using arrays or variables inside _POST


f.ismayil

Recommended Posts

I have the following loop in which I receive Parse error:

 

$letters = array ("A", "B", "C", "D","E", "F", "G", "H","I", "J", "K", "L",

"M", "N", "O", "P","Q", "R", "S", "T","U", "V", "W", "X",

"Y", "Z", "&#198", "&#216", "&#197");

 

 

for ($i = 1; $i < 29; $i++) {

  if ($_POST['$letters['$i']']){

    echo "You have selected the following products:" . "<br>" . $_POST['$letters['$i']];

  }

}

 

or is it possible to write something like that:

 

for ($i = 1; $i < 300; $i++) {

  if ($_POST['$i']){

    echo "You have selected the following products:" . "<br>" . $_POST['$i'];

  }

}

 

 

Please, help me. I need to use array and variables inside _POST[].

Link to comment
Share on other sites

If you want PHP to evaluate your strings (like turn $i into its value), you have to enclose them in double quotes. But in this case, you don't even need to use quotes. All the single/double quotes do is tell PHP that you're writing a string.

for ($i = 1; $i < 29; $i++) { 
  if ($_POST[$letters[$i]]){ 
    echo "You have selected the following products:" . "<br>" . $_POST[$letters[$i]]; 
  }
}

PS: I'm not so good at explaining these kinds of things, so if you don't understand maybe someone else could explain better.

Link to comment
Share on other sites

Probably a better approach to your overall logic, but how about:

 

$letters = array_merge(range("A", "Z"), array("&#198", "&#216", "&#197"));

foreach($letters as $letter) {
  if(isset($_POST[$letter])) {
    echo "You have selected the following products:" . "<br>" . $_POST[$letter];
  }
}

Link to comment
Share on other sites

dcro2 thanks for your comment. You are good enough in explaining. Just give code and I will understand. I wrote your code, but this time I have received 2 different notices:

Undifined index: B

Undifined index: C

Undifined index: D

.

.

.

.

Undifined index: Z

 

and

 

Undifined offset 30

Undifined offset 31

.

.

.

Undifined offset 304

 

As I understood this time problem is not in syntax. Any idea?

Link to comment
Share on other sites

dcro2 thanks for your comment. You are good enough in explaining. Just give code and I will understand. I wrote your code, but this time I have received 2 different notices:

Undifined index: B

Undifined index: C

Undifined index: D

.

.

.

.

Undifined index: Z

 

and

 

Undifined offset 30

Undifined offset 31

.

.

.

Undifined offset 304

 

As I understood this time problem is not in syntax. Any idea?

 

Because those don't exist.  Use isset() as in my example.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.