[SOLVED] Array headache

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cateyes
Forum Commoner
Posts: 63
Joined: Mon Jun 14, 2004 5:06 pm

[SOLVED] Array headache

Post by Cateyes »

Hi guys I am having a problem passing an array with post here is my first page code

Code: Select all

<form action="add_student_db.php" method="post" ENCTYPE="multipart/form-data"> 
<table cellpadding="2" cellspacing="2" border="1" width="100%"> 
<? 
   for ($n=1;$n<13;$n++) 
   { 
?> 
<tr> 
   <td><b>First Name</b>:</td> 
   <td><input type="text" name="first_name[<? echo $n ?>]" size="45"></td> 
   </tr> 
     <td valign="top"><b>Last Name </b>:</td> 
       <td><input type="text" name="last_name[<? echo $n ?>]" size="45"></td> 
</tr> 
<tr> 
   <td><b>Password(min 4 Digits </b>:</td> 
   <td><input type="text" name="pass_4[<? echo $n ?>]" size="45"></td> 
</tr> 
<tr> 
    <td colspan="2">&nbsp;</td> 
</tr> 
<? 
  
   } 
  
?> 
<tr> 
   <td colspan="2" align="center"><input type="submit" name="submit" value="Enter"></td> 
</tr> 
</table> 
</form>
this is just a basic page I created to see that it was passing properly and it wasn't here is the page it transfers to

Code: Select all

<?php
$count = 0;
for ($n=1;$n<13;$n++)
	{
	
$count = $count+1;
$first_name[$n] = $_POST['first_name[$n]'];
if ($first_name[$n] != "") 
		{ 
echo $first_name[$n];
$last_name[$n] = $_POST['last_name[$n]'];
$sin_4[$n] = $_POST['sin_4_$n'];
$username[$n] = $last_name[$n].$first_name[$n].date("my");
$userlength[$n] = strlen($username[$n]);
if ($userlength[$n] > 20)
			{
$adjust[$n] = $userlength[$n] - 20;
$new_length[$n] = strlen($first_name[$n]) - $adjust[$n];
$new_first[$n] = substr($first_name[$n], 0, $new_length[$n]);
$final_username[$n] = $last_name[$n].$new_first[$n].date("my");
$final_username[$n] = strtolower($final_username[$n]);
			}
else 
			{
$final_username[$n] = strtolower($username[$n]);
			}
		}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Student Username</title>
</head>
<h3>Here is the studend ID</h3>
<?php
for ($m=1;$m<$count;$m++)
	{
print "Your username is $final_username[$m]"; ?><br /><?php
}
?>
<body>
</body>
</html>
Basically the variables dont seem to be getting picked up by the second page when I tested just passing one name and 1 last name no problem simple variables if your wondering what the second page does is it creates a user name from the first and last name not exceeding 20 chars(windows limit on logins).

I thought I was finally getting a grasp on arrays but I am starting to think I am missing something simple or I have made a stupid mistake somewhere.
Last edited by Cateyes on Fri Mar 24, 2006 10:13 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$_POST['first_name[$n]']
et al to

Code: Select all

$_POST['first_name'][$n]
Cateyes
Forum Commoner
Posts: 63
Joined: Mon Jun 14, 2004 5:06 pm

Post by Cateyes »

Thanks Feyd you seem to always have the answer I guess I do understand arrays just not how to retrieve them thank you for the help much appreciated.
Post Reply