bsamson Posted July 31, 2007 Share Posted July 31, 2007 Good Morning. I have been reading and racking my brain on how to best do this. Please any comments/suggestions ARE VERY MUCH APPRECIATED! Alright, on page 1 the end user selects has upto 15 options all of which are hard coded like: <input type="radio" value="TEXT-$5" name="op1"> And obviously the op(#) are hard coded. Once the user hits submit on the next page I want to build an array that will check for "null" or empty op(#) fields and only fill it w/ the fields that contain actual info. This is what I have ... // Load Options for ($i=1;$i<=15;$i++){ if ((!empty($_POST[op$i]) && ($_POST[op$i} != "null")) { $opt[] = array($_POST[$op$i]); } } And I get this error each time: Parse error: syntax error, unexpected T_VARIABLE, expecting ']' in /home/nnyserve/public_html/internal/ebd/addentry.php on line 29 Please remember this is my 1st time of working w/ arrays . I know that I am forgetting something, and again any assistance would be GREATLY appreciated! Best Regards, Brian Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/ Share on other sites More sharing options...
lightningstrike Posted July 31, 2007 Share Posted July 31, 2007 for ($i=1;$i<=15;$i++){ $x = "op" . $i; if ((!empty($_POST[$x]) && ($_POST[$x] != NULL)) { $opt[] = array($_POST[$x]); } } You were using invalid syntax. Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-311922 Share on other sites More sharing options...
Psycho Posted July 31, 2007 Share Posted July 31, 2007 A radio button group is treated as an array in the HTML page (for JavaScript purposes). But, once submitted, it is treated as a single field with the value that was selected. So, in this case you would have the variable $_POST['TEXT-$5'] with the value of the radio button that was selected. You cannot access all of the options and their value on the receiving page. EDIT: After rereading the issue I'm not sure I understand - do you have one radio group with 15 options? Or, do you have 15 different radio groups, each with different options? If it is one group with 15 options, then all the options need to have the same name. Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-311925 Share on other sites More sharing options...
bsamson Posted July 31, 2007 Author Share Posted July 31, 2007 Thanks! Yes, I understand how HTML treats the radio buttons. That's why I grouped all features. By the way, I have 15 DIFFERENT radio groups w/ different options. For Example: TEST MESSAGING (op1) value = "NULL" (SELECTED) value = "$5 pack" value = "$7 pacl" Insurance (op2) value = "YES" value = "No" ect ... ect ... As far as the code's concerned, I tried that code with one correction. Here is the code: Line 27: // Load Options Line 28: for ($i=1;$i<=15;$i++){ Line 29: $x = "op" . $i; Line 30: if ((!empty($_POST[$x]) && ($_POST[$x] != "null")) { Line 31: $opt[] = array($_POST[$x]); Line 32: } } And now I am getting this error: Parse error: syntax error, unexpected '{' in /home/nnyserve/public_html/internal/ebd/addentry.php on line 30 I know it's gotta be something small I am missing! I have been at this for over 2 hours, so please excuse my frustration. Thanks! Best Regards, Brian Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-311935 Share on other sites More sharing options...
lightningstrike Posted July 31, 2007 Share Posted July 31, 2007 More Invalid Syntax....which I overlooked (You did not close all of the parenthesis) Use this: for ($i=1;$i<=15;$i++){ $x = "op" . $i; if ((!empty($_POST[$x])) && ($_POST[$x] != NULL)) { $opt[] = array($_POST[$x]); } } Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-311937 Share on other sites More sharing options...
bsamson Posted July 31, 2007 Author Share Posted July 31, 2007 Thanks for all your help! Just one more thing. I used this code: // Load Options $optcount = 0; for ($i=1;$i<=15;$i++) { $x = "op" . $i; if ((!empty($_POST[$x])) && ($_POST[$x] != NULL)) { $opt[] = array($_POST[$x]); $optcount++; } } With no errors. Now I have this code: <tr> <td width="135"><b><font face="Arial" size="2">Option Count</font></b></td> <td><? echo $optcount; ?></td> </tr> <tr> <td width="135"> </td> <td> </td> </tr> <? for ($zz=1;$zz<=$optcount;$zz++) { echo " <tr> <td width='135'><b><font face='Arial' size='2'>Option $zz:</font></b></td> <td><font face='Arial' size='2'>$opt[$zz]</font></td> </tr>"; } ?> Which displays: Option Count: 2 Option 1: Array Option 2: Any Suggestions why it displays 'Array' for option one, and nothing for option 2? Thanks in advance! Best Regards, Brian Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-311950 Share on other sites More sharing options...
DeepakJ Posted July 31, 2007 Share Posted July 31, 2007 Make sure you close parentheses Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-311951 Share on other sites More sharing options...
DeepakJ Posted July 31, 2007 Share Posted July 31, 2007 Try putting your variables in single quotes: '$example' Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-311955 Share on other sites More sharing options...
DeepakJ Posted July 31, 2007 Share Posted July 31, 2007 For the record, If you get a parse error it generally means you missed a semicolon/parenthesis on one of the preceding lines. Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-311957 Share on other sites More sharing options...
bsamson Posted July 31, 2007 Author Share Posted July 31, 2007 hhhmmm ... no parse error. When I tried this: LINE 187: <td><font face='Arial' size='2'>$opt['$zz']</font></td> I get this error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/nnyserve/public_html/internal/ebd/addentry.php on line 187 W/O the double quotes I get: Option Count: 2 Option 1: Array Option 2: Am I missing something? Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-311967 Share on other sites More sharing options...
DeepakJ Posted July 31, 2007 Share Posted July 31, 2007 Post the code around that please Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-311979 Share on other sites More sharing options...
bsamson Posted July 31, 2007 Author Share Posted July 31, 2007 <tr> <td width="135"><b><font face="Arial" size="2">Option Count:</font></b></td> <td><? echo $optcount; ?></td> </tr> <tr> <td width="135"> </td> <td> </td> </tr> <? for ($zz=1;$zz<=$optcount;$zz++) { echo " <tr> <td width='135'><b><font face='Arial' size='2'>Option $zz:</font></b></td> <td><font face='Arial' size='2'>$opt[$zz]</font></td> </tr>"; } ?> Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-311980 Share on other sites More sharing options...
DeepakJ Posted July 31, 2007 Share Posted July 31, 2007 Oooh thats easy. you can't include variables in quotes: echo "I love ".$waffles."."; correct format for variables above. Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-311985 Share on other sites More sharing options...
DeepakJ Posted July 31, 2007 Share Posted July 31, 2007 Here Ill make it easier, copy/paste echo " <tr> <td width='135'><b><font face='Arial' size='2'>Option ".$zz.":</font></b></td> <td><font face='Arial' size='2'>".$opt[$zz]."</font></td> </tr>"; } ?> Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-311987 Share on other sites More sharing options...
bsamson Posted July 31, 2007 Author Share Posted July 31, 2007 Didn't make any difference. Still displays ARRAY for option 1, and nothing for option 2. This is weird! Any other suggestions? I am sorry just learning arrays. In fact, I think I should have learned about them long before now because they would have made my life a LOT easier! Anyway, thanks in advance for the direction! Best Regards Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-312007 Share on other sites More sharing options...
DeepakJ Posted July 31, 2007 Share Posted July 31, 2007 Where do you define the array opt Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-312014 Share on other sites More sharing options...
bsamson Posted July 31, 2007 Author Share Posted July 31, 2007 At the top of the page ... // Load Options $optcount = 0; for ($i=1;$i<=15;$i++) { $x = "op" . $i; if ((!empty($_POST[$x])) && ($_POST[$x] != NULL)) { $opt[] = array($_POST[$x]); $optcount++; } } Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-312017 Share on other sites More sharing options...
DeepakJ Posted July 31, 2007 Share Posted July 31, 2007 LOL its cause arrays start at 0, nto 1. make $zz = 0 at start and till 14 instead of 15 Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-312023 Share on other sites More sharing options...
Psycho Posted July 31, 2007 Share Posted July 31, 2007 Ok, a lot of misinformation and bad format going on here. The reason you are not getting a value for the 2nd element in the array is that you are doing to things in a poor format: $opt[] = array($_POST[$x]); $optcount++; You are starting optcount at 1, but your arrays start at 0. So, in your later code when you try and show $opt[1] and $opt[2], there is no value at index 2, there are values at 0 and 1. But, you should not be using $optcount at all. When you go to print the values of the array you should be using a foreach statement. echo "Option count: ".count($opt)."<br>"; foreach ($opt as $key=>$value) { echo "Option $key: $value<br>"; } Oooh thats easy. you can't include variables in quotes: echo "I love ".$waffles."."; correct format for variables above. Absolutely NOT true. When using double quotes for a string value, variables are parsed. The same is not true when using single quotes. Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-312025 Share on other sites More sharing options...
bsamson Posted July 31, 2007 Author Share Posted July 31, 2007 Changed it, and still same problem. Code: // Load Options $optcount = 0; for ($i=0;$i<=14;$i++) { $x = "op" . $i; if ((!empty($_POST[$x])) && ($_POST[$x] != NULL)) { $opt[] = array($_POST[$x]); $optcount++; } } Code @ bottom (w/ options): <? for ($zz=1;$zz<=$optcount;$zz++) { echo " <tr> <td width='135'><b><font face='Arial' size='2'>Option ".$zz.":</font></b></td> <td><font face='Arial' size='2'>".$opt[$zz]."</font></td> </tr>"; } ?> I cannot figure out why it says array instead of the option!. As always seems to be the case w/ PHP ... it's something small & stupid I'm missing. Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-312028 Share on other sites More sharing options...
DeepakJ Posted July 31, 2007 Share Posted July 31, 2007 for $zz=0 Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-312046 Share on other sites More sharing options...
Psycho Posted July 31, 2007 Share Posted July 31, 2007 You should ONLY start the initial loop at 0 if your field names start at 0. The array will start at 0, unless you specify the key. But, assuming your field names start at 0 try this: // Load Options for ($i=0;$i<=14;$i++) { $x = "op" . $i; if (!empty($_POST[$x]) && $_POST[$x] != NULL) { $opt[] = array($_POST[$x]); } } //Code @ bottom (w/ options): foreach ($opt as $key => $value) { echo " <tr> <td width='135'><b><font face='Arial' size='2'>Option ".$key.":</font></b></td> <td><font face='Arial' size='2'>".$value."</font></td> </tr>"; } If it still says array, I woul be interested in seeing the form. Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-312061 Share on other sites More sharing options...
bsamson Posted July 31, 2007 Author Share Posted July 31, 2007 MJ: Your code produced something differed! Option 1: ARRAY Option 2: ARRAY LOL, so that's good. Anyway, here is the html for the options page: <div align="center"> <table border="0" width="80%" id="table3"> <tr> <td> </td> </tr> </table> <table border="0" width="550" id="table2" cellspacing="0" cellpadding="0"> <tr> <td bgcolor="#000000"> <p align="center"><b> <font face="Arial" color="#FFE100" size="2"> Please select </font></b> <font face="Arial Black" color="#FFE100" size="2"> ALL</font><b><font face="Arial" color="#FFE100" size="2"> activated features to receive credit!!</font></b></td> </tr> <tr> <td> </td> </tr> </table> <table border="0" width="480" id="table1" cellspacing="0" cellpadding="0"> <tr> <td width="173" bgcolor="#FFE100"><b><font face="Arial" size="2"> Text Messaging</font></b></td> <td width="93"> </td> <td width="161" bgcolor="#FFE100"><b><font face="Arial" size="2"> Vision</font></b></td> <td> </td> </tr> <tr> <td width="173"><b><font face="Arial" size="2">None</font></b></td> <td width="93"><input type="radio" value="" name="op1" checked></td> <td width="161"><b><font face="Arial" size="2">None</font></b></td> <td> <input type="radio" value="" name="op2" checked></td> </tr> <tr> <td width="173"><font face="Arial" size="2">$5.00</font></td> <td width="93"><input type="radio" value="TEXT-$5" name="op1"></td> <td width="161"><font face="Arial" size="2">$15.00</font></td> <td> <input type="radio" value="Vision-$15" name="op2"></td> </tr> <tr> <td width="173"><font face="Arial" size="2">$10.00</font></td> <td width="93"><input type="radio" value="TEXT-$10" name="op1"></td> <td width="161"><font face="Arial" size="2">$20.00</font></td> <td> <input type="radio" value="Vision-$20" name="op2"></td> </tr> <tr> <td width="173"><font face="Arial" size="2">$15.00</font></td> <td width="93"> <input type="radio" value="TEXT-$15" name="op1"></td> <td width="161"><font face="Arial" size="2">$25.00</font></td> <td> <input type="radio" value="Vision-$25" name="op2"></td> </tr> <tr> <td width="173"><font face="Arial" size="2">$20.00</font></td> <td width="93"> <input type="radio" value="TEXT-$20" name="op1"></td> <td width="161"><font face="Arial" size="2">$30.00</font></td> <td> <input type="radio" value="Vision-$30" name="op2"></td> </tr> <tr> <td width="173"> </td> <td width="93"> </td> <td width="161"> </td> <td> </td> </tr> <tr> <td width="173" bgcolor="#FFE100"><b><font face="Arial" size="2"> Protection</font></b></td> <td width="93"> </td> <td width="161" bgcolor="#FFE100"><b><font face="Arial" size="2">GPS Services</font></b></td> <td> </td> </tr> <tr> <td width="173"><font face="Arial" size="2">TEP Insurance ($7)</font></td> <td width="93"><input type="checkbox" name="op3" value="TEP-$7"></td> <td width="161"><b><font face="Arial" size="2">None</font></b></td> <td><input type="radio" value="" name="op5" checked></td> </tr> <tr> <td width="173"><font face="Arial" size="2">Roadside Assistance ($4)</font></td> <td width="93"> <input type="checkbox" name="op4" value="RoadsideAssistance-$4"></td> <td width="161"><font face="Arial" size="2">Sprint Navigation ($9.99)</font></td> <td> <input type="radio" value="Sprint_Navigation-$9.99" name="op5"></td> </tr> <tr> <td width="173"> </td> <td width="93"> </td> <td width="161"><font face="Arial" size="2">Family Locator ($9.99)</font></td> <td><input type="radio" value="Family_Locator-$9.99" name="op5"></td> </tr> <tr> <td width="173"> </td> <td width="93"> </td> <td width="161"> </td> <td> </td> </tr> <tr> <td width="173" bgcolor="#FFE100"><b><font face="Arial" size="2"> Phone As Modem Plans</font></b></td> <td width="93"> </td> <td width="161" bgcolor="#FFE100"><b><font face="Arial" size="2"> Blackberry Data Plans</font></b></td> <td> </td> </tr> <tr> <td width="173"><b><font face="Arial" size="2">None</font></b></td> <td width="93"><input type="radio" value="" name="op6" checked></td> <td width="161"><b><font face="Arial" size="2">None</font></b></td> <td><input type="radio" value="" name="op7" checked></td> </tr> <tr> <td width="173"><font face="Arial" size="2">40MB Plan ($39.99)</font></td> <td width="93"> <input type="radio" value="PAM_40MB-$39.99" name="op6"></td> <td width="161"><font face="Arial" size="2">10MB Data ($39.99)</font></td> <td> <input type="radio" value="Blackberry_10MB-$39.99" name="op7"></td> </tr> <tr> <td width="173"><font face="Arial" size="2">Unlimited Plan ($39.99)</font></td> <td width="93"> <input type="radio" value="PAM_Unlimited-$39.99" name="op6"></td> <td width="161"><font face="Arial" size="2">Unlimited Data ($39.99)</font></td> <td> <input type="radio" value="Blackberry_Unlimited-$39.99" name="op7"></td> </tr> <tr> <td width="173"> </td> <td width="93"> </td> <td width="161"> </td> <td> </td> </tr> <tr> <td width="173" bgcolor="#FFE100"><b><font face="Arial" size="2"> Miscellaneous Addons</font></b></td> <td width="93"> </td> <td width="161"> </td> <td> </td> </tr> <tr> <td width="173"><font face="Arial" size="2">Mobile to Mobile ($5)</font></td> <td width="93"> <input type="checkbox" name="op8" value="Mobile2Mobile-$5"></td> <td width="161"> </td> <td> </td> </tr> <tr> <td width="173"><font face="Arial" size="2">Sprint to Home ($5)</font></td> <td width="93"> <input type="checkbox" name="op9" value="Sprint2Home-$5"></td> <td width="161"> </td> <td> </td> </tr> <tr> <td width="173"><font color="#FFFFFF" face="Arial" size="2">-- </font></td> <td width="93"> </td> <td width="161"> </td> <td> </td> </tr> </table> </div> Please ignore to code not being pretty ... This table was produced using frontpage. Generally I go back into code view and make it 'pretty'. Anyway, thanks for any assistance! Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-312069 Share on other sites More sharing options...
deadimp Posted July 31, 2007 Share Posted July 31, 2007 You're throwing the array keyword in places where it doesn't really need to be. Here's your problem with the display: $opt[] = array($_POST[$x]); You're making that element of $opt an array with only one element containing $_POST[$x], thus making $opt a multi-dimensional array. [i remember telling someone else of this on a post I made yesterday.] That's redundant. When you're using the []= operator with arrays, it pushes the right operand to the top (after the last) of the list. When you use += with arrays, it adds all of the elements in the right operand (which must be a list) to the list. So, you could change two characters in your code and it work as this: $opt+=array($_POST[$x]); But that isn't needed. Instead, you just type: $opt[]=$_POST[$x]; your code a little more neatly. The way you have it now isn't all that bad, but you might want to use few more lines for clarity. I've added a couple things to this code: And one way you can avoid syntax error's is formatting // Load Options //You don't need $optcount. You can just use count($opt). Then again, if you're using foreach, you don't even need that. for ($i=0;$i<15;$i++) { //<=14 and <15 have the same effect when dealing with integer iteration, and using <15 is simpler because you don't have to subtract one each time from the size. $x = "op$i"; //Another way to format your string. if (isset($_POST[$x]) { //The way you had it before was redundant. The two checks did the same thing. With this, you can also check key_exists($x,$_POST) $opt[]=$_POST[$x]; } } Also, look more into what mjdamato suggested. Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-312083 Share on other sites More sharing options...
bsamson Posted July 31, 2007 Author Share Posted July 31, 2007 One Thing: HOLY CRAP I HAVE A LOT TO LEARN! Thanks everyone for all the help! It's finially working ... =) Link to comment https://forums.phpfreaks.com/topic/62677-solved-1st-time-trying-to-use-arrays/#findComment-312099 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.