By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,916 Members | 1,728 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,916 IT Pros & Developers. It's quick & easy.

trouble with arrays + sessions

P: n/a
I just started with php and I'm trying to make a simple interface as
follows:

- user is asked to input an integers, for example: how many students do
you want to enter?
- user is then shown a page with number of text boxes = number he gave
at the previous page
- user fills out the test boxes with names of students and clicks
submit
- the user is sent to another page where the above names are output to
the screen

here is the code for the relevant pages:
First i ask for how many text boxes the user would like...
================================================== ====================
<?
session_start();
?>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>ECSLAB Submit System Course Registration</title>
</head>

<body>
<table>
<tr>
<td>
<p>
<?
if($_SESSION['sizeerror']) {
echo "Inputs must be
integers<br><br>";
$_SESSION['sizeerror'] =
0;
}
?>
<form method="POST"
action="transition.php">
How many instructors (TAs,
professors, etc) will need access to the system?<br>
<input type="text"
name="numinstructors" size="20"><br><br>
How many students would you like
to add?<br>
<input type="text"
name="numstudents" size="20"><br><br><br>
<input type="submit"
value="Submit" name="B1">
</form>
</p>
</td>
</tr>
</table>
</body>

</html>
================================================== ========================

if the input was correct (both were integers) you get redirected to the
next page...this part works fine (or so it seems). it displays the
correct number of text-boxes...
================================================== ========================
<?
session_start();
?>
<HTML>

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>ECSLAB Submit System Course Registration</title>
</head>

<body>
<table align="center">
<form method="POST" action="submit.php">
<tr>
<p>
Coursename:&nbsp;&nbsp;&nbsp;<input type="text"
name="coursename" size="20"><br><br>
</p>
</tr>
<tr>
<p>
Instructor ECS Lab Accountnames:<br><br>

<?
for( $k = 0; $k <
$_SESSION['numinstructors']; $k++){
echo "<input type=\"text\"
name=\"instructor[$k]\" size=\"20\"><br><br>";
}
?>
</p>
</tr>
<tr>
<p>
<br><br>Student ECS Lab Accountnames:<br><br>

<?
for( $i = 0; $i <
$_SESSION['numstudents']; $i++ ){
echo "<input type=\"text\"
name=\"student[$i]\" size=\"20\"><br><br>";
}
?>

<input type="submit" value="Submit" name="B1">
</p>
</tr>
</form>
</table>
</body>

</html>
================================================== =======================

submitting the names sends you to the following page...
================================================== ========================
<?
session_start();

$coursename = $_POST['coursename'];
$_SESSION['coursename'] = $coursename;

for( $i = 0; $i < $_SESSION['numstudents']; $i++ ){
$idx = 'student[' . $i . ']';
$student[$i] = $_POST[$idx];
$_SESSION[$idx] = $student[$i];
}

for( $k = 0; $k < $_SESSION['numinstructors']; $k++ ){
$instructor[$k] = $_POST["instructor[$k]"];
$_SESSION["instructor[$k]"] = $instructor[$k];
}
?>
<HTML>

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>ECSLAB Submit System Course Registration Successful</title>
</head>

<body>
<?
for( $i = 0; $i < $_SESSION['numstudents']; $i++ ){
$x = 'student[' . $i . ']';
echo $_SESSION[$x];
echo "<br>";
}
echo "now instructors<br>";
for( $k = 0; $k < $_SESSION['numinstructors']; $k++ ){
$x = 'instructor[' . $k . ']';
echo $_SESSION[$x];
echo "<br>";
}
?>
</body>

</HTML>
================================================== ======================
i expect it to print something like:
student1
student2
student3
now instructors
teacher1
teacher2

but instead it prints:

now instructors
(so, 3 blank lines, "now instructors", then 2 blank lines)
Anyone have any idea what I'm doing wrong?
Thanks in advance,
--Lucas

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ugh... where to begin...
1st is this just a general "how do sessions work test"?
because sessions appear to be completely unnecessary here.

here's a working "transition.php"
<?php
session_start();
?>
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>ECSLAB Submit System Course Registration</title>
</head>
<body>
<form method="POST" action="submit.php">
Coursename:&nbsp;&nbsp;&nbsp;<input type="text" name="coursename"
size="20"><br><br>
<p>
Instructor ECS Lab Accountnames:<br>
<?php
for( $k = 0; $k < $_POST['numinstructors']; $k++)
echo '<input type="text" name="instructor['.$k.']" size="20"
/><br/>'."\n";;
?>
</p>
<P>
Student ECS Lab Accountnames:<br>
<?php
for( $i = 0; $i < $_POST['numstudents']; $i++ )
echo '<input type="text" name="student['.$i.']" size="20" /><br
/>'."\n";
?>
<INPUT type="hidden" name="numinstructors"
value="<?=$_POST['numinstructors']?>" />
<INPUT type="hidden" name="numstudents"
value="<?=$_POST['numstudents']?>" />
<?php
// or, if you insist on using sessions, you need to set your session
values here
// they don't magically set themselves
// $_SESSION['numinstructors'] = $_POST['numinstructors']
// $_SESSION['numstudents'] = $_POST['numstudents']
?>
<input type="submit" value="Submit" name="B1">
</p>
</form>
</body>

</html>

***********************************

in submit.php you've got a few probs

you can use:
for( $i = 0; $i < $_POST['numstudents']; $i++ )
// or $_SESSION['numstudents'] if you've defined it (you hadn't)
{
$name = 'student[' . $i . ']';
$_SESSION[$idx] = $_POST[$name];
}

but down below you're echoing
$_SESSION[1]
should be
$_SESSION['student[1]']
at least the way you've set previous pages.

and again, sessions weren't necessary... unless you're doing something
with these value in later pages...

Jul 17 '05 #2

P: n/a
I actually do have a transition.php. I guess I should have included it
instead of just assuming people would know that it is there. The page
that you thought was not transition.php but actually register.php. The
following is the page that should come between the first group of code
I put up and the second....sorry for the confusion

==================================================
<?
session_start();

$numstudents = $_POST['numstudents'];
$_SESSION['numstudents'] = $numstudents;

$numinstructors = $_POST['numinstructors'];
$_SESSION['numinstructors'] = $numinstructors;
?>
<HTML>

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>ECSLAB Submit System...</title>
</head>

<?
if(ctype_digit($numstudents) && ctype_digit($numinstructors)) {
echo "<meta http-equiv=\"refresh\"
content=\"0;URL=http://node1\
7/register.php\">";
}else {
$_SESSION['sizeerror'] = 1;
echo "<meta http-equiv=\"refresh\"
content=\"0;URL=http://node1\
7/classsize.php\">";
}
?>

<body>
</body>

</html>

Jul 17 '05 #3

P: n/a
I have modified the following lines:

echo "<input type=\"text\" name=\"instructor[$k]\"
size=\"20\"><br><br>";
and
echo "<input type=\"text\" name=\"student[$i]\" size=\"20\"><br><br>";

to
echo '<input type="text" name="student['.$i.']" size="20"><br>';
and
echo '<input type="text" name="instructor['.$k.']" size="20"><br>';

like you said....I also modified my final page (where I'm trying to
echo the names inputted by the user) to:

for( $i = 0; $i < $_SESSION['numstudents']; $i++ ){
$x = 'student[' . $i . ']';
echo $_SESSION['student[1]'];
echo "<br>";
}
echo "<br>now instructors<br>";
for( $k = 0; $k < $_SESSION['numinstructors']; $k++ ){
$x = 'instructor[' . $k . ']';
echo $_SESSION['instructor[1]'];
echo "<br>";
}

but it still does not print out any values...this leads me to believe
that maybe the problem is in the page where I actually have the form
for inputting the names into the arrays....Any ideas?

At the same time i'm not sure why you said:
but down below you're echoing
$_SESSION[1]
should be
$_SESSION['student[1]']
at least the way you've set previous pages.


I was fairly sure that the way I was printing it was infact trying to
print:
$_SESSION['student[1]'], $_SESSION['student[2]'], etc...since i had:

$x = 'instructor[' . $k . ']';
echo $_SESSION[$x];

Because if I echo $x it prints: instructor[0], instructor[1], etc....

Thanks
--Lucas

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.