473,772 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="transit ion.php">
How many instructors (TAs,
professors, etc) will need access to the system?<br>
<input type="text"
name="numinstru ctors" size="20"><br>< br>
How many students would you like
to add?<br>
<input type="text"
name="numstuden ts" 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:&nbs p;&nbsp;&nbsp;< input type="text"
name="coursenam e" size="20"><br>< br>
</p>
</tr>
<tr>
<p>
Instructor ECS Lab Accountnames:<b r><br>

<?
for( $k = 0; $k <
$_SESSION['numinstructors ']; $k++){
echo "<input type=\"text\"
name=\"instruct or[$k]\" size=\"20\"><br ><br>";
}
?>
</p>
</tr>
<tr>
<p>
<br><br>Stude nt ECS Lab Accountnames:<b r><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 1558
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:&nbs p;&nbsp;&nbsp;< input type="text" name="coursenam e"
size="20"><br>< br>
<p>
Instructor ECS Lab Accountnames:<b r>
<?php
for( $k = 0; $k < $_POST['numinstructors ']; $k++)
echo '<input type="text" name="instructo r['.$k.']" size="20"
/><br/>'."\n";;
?>
</p>
<P>
Student ECS Lab Accountnames:<b r>
<?php
for( $i = 0; $i < $_POST['numstudents']; $i++ )
echo '<input type="text" name="student['.$i.']" size="20" /><br
/>'."\n";
?>
<INPUT type="hidden" name="numinstru ctors"
value="<?=$_POS T['numinstructors ']?>" />
<INPUT type="hidden" name="numstuden ts"
value="<?=$_POS T['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($nu minstructors)) {
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=\"instruct or[$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="instructo r['.$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
4614
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) I have register_globals off. 3) I use $_session= $loginname to store the loginname into the session and then retrieve it on other pages using a $HTTP_SESSION_VARS.
2
2975
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: if (strcmp(strtolower($_POST), 'apply') != 0) { // GO BACK TO grad_application with $_SESSION session_start(); if (sizeof($_POST) > 0) $_SESSION = $_POST; if (sizeof($_GET) > 0) $_SESSION = $_GET; header("Location:...
0
1613
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 anyone who can help with this. Mountain Man
6
2274
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 collection of $orderline associative arrays which, in turn, hold the global POST values key 'order_code' and value 'qty' as passed in from another page. My problem is (shown by using print_r to print out the contents of the arrays) each time I...
3
1423
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 store some information in some session variables. But this information is coming from an old dbase system. In the beginning of this project it was just a simple static array of information. Afterwards it turns out that this must be a dynamic array...
0
5414
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 new session is created. The original and new session id don't match. Any help would be appreciated!
6
3807
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 one day): \\FFDS24\ASP.NET Applications(_LM_W3SVC_1_Root_ATV2004)\Errors During Execution: 7 \\FFDS24\ASP.NET Apps v1.1.4322(_LM_W3SVC_1_Root_ATV2004)\Compilations
1
2068
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 query language, which forces me to do the join manually via arrays. Right now, I'm having trouble getting these query results (which are in
1
2449
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 of different lengths (e.g. usually between 25 and 45 rows. The columns in each file have the same length). The text files have been numbered sequentially e.g. cb0, cb1, cb2 and so on. I would like to read the data from each text file into...
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.