Jump to content

[SOLVED] 1st Time trying to use Arrays ...


bsamson

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

	<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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
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.