f.ismayil Posted April 13, 2011 Share Posted April 13, 2011 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", "Æ", "Ø", "Å"); 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 https://forums.phpfreaks.com/topic/233598-using-arrays-or-variables-inside-_post/ Share on other sites More sharing options...
dcro2 Posted April 13, 2011 Share Posted April 13, 2011 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 https://forums.phpfreaks.com/topic/233598-using-arrays-or-variables-inside-_post/#findComment-1201101 Share on other sites More sharing options...
AbraCadaver Posted April 13, 2011 Share Posted April 13, 2011 Probably a better approach to your overall logic, but how about: $letters = array_merge(range("A", "Z"), array("Æ", "Ø", "Å")); foreach($letters as $letter) { if(isset($_POST[$letter])) { echo "You have selected the following products:" . "<br>" . $_POST[$letter]; } } Link to comment https://forums.phpfreaks.com/topic/233598-using-arrays-or-variables-inside-_post/#findComment-1201254 Share on other sites More sharing options...
f.ismayil Posted April 13, 2011 Author Share Posted April 13, 2011 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 https://forums.phpfreaks.com/topic/233598-using-arrays-or-variables-inside-_post/#findComment-1201258 Share on other sites More sharing options...
kenrbnsn Posted April 13, 2011 Share Posted April 13, 2011 Please post the form that is being used. Link to comment https://forums.phpfreaks.com/topic/233598-using-arrays-or-variables-inside-_post/#findComment-1201263 Share on other sites More sharing options...
AbraCadaver Posted April 13, 2011 Share Posted April 13, 2011 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 https://forums.phpfreaks.com/topic/233598-using-arrays-or-variables-inside-_post/#findComment-1201265 Share on other sites More sharing options...
f.ismayil Posted April 16, 2011 Author Share Posted April 16, 2011 Thanks, AbraCadaver. Your code worked fine. I would like to thank everybody for contribution. You helped me very much in learning PHP. Link to comment https://forums.phpfreaks.com/topic/233598-using-arrays-or-variables-inside-_post/#findComment-1202404 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.