473,383 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

trouble with arrays + sessions

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
3 1541
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Avinash Korwarkar | last post by:
Hi Guys, I am using sessions to store the login name and then retrieve it on every page. Here are the details of the php settings. 1) I have PHP 4.2.2 installed on a Apache 1.3.27 server. 2)...
2
by: Phil Powell | last post by:
I have a form that will be preserving form data prior to processing the form data. Upon clicking a certain submit button you will go to another PHP script that will contain the following code: ...
0
by: Mountain Man | last post by:
Hi, I'm having trouble with sessions. Text fields from a form aren't getting updated when the form is resubmitted. On the other hand, my radio buttons are updated every time. Many thanks to...
6
by: Fnark! | last post by:
I am creating a shopping cart using PHP Version 4.1.2. I am creating and registering a cart object in a session. The cart object contains an array of arrays called $order whose elements are a...
3
by: John | last post by:
Hi all, I've got an issue with session variables in asp pages. I need to make a decision that I think you can hopefully help me out here. When a session starts on the iss server I need to...
0
by: David Staschover | last post by:
I'm trying to set up sessions in perl. The session is initialized fine in session1.cgi In session2.cgi, the correct session id is returned from the cookie, but when I initialize the session, a...
6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
1
by: ferraro.joseph | last post by:
Hi, I'm querying Salesforce.com via their AJAX toolkit and outputting query results into a table. Currently, their toolkit does not possess the ability to do table joins via their structured...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.