Need some PHP Code, Trying to do much on one page?

Posted on 2004-10-09
Last Modified: 2008-03-06

I am developing a web based "progress report" system for a school.  So on one page, I say "enter Grade, teacher name, subject, and Term".  once the teacher enters that (and presses submit), I query the DB (MySqL) for the # of kids, and then we move to the next page.

On the next page, I create the array of student IDs and names.  I am able to say "Enter progerss report for " and I get the first students name (using current function in PHP).  Under this, there are numerous radio buttons for the teachers (Conduct - Good, bad, ugly, Effort - Good Bad Ugly, Homework -Same, etc.) and then a comment box.  

I want two hyperlinks at the bottom (submit, which would write all the choices to the DB), and Next, which would skip this student (since not all teachers have all the kids).  of course, once eitehr hyperlink is chosen, we need to refresh the page, show the next kids name, etc.

How do I do that part?  Since I create the array on this page, if I just reload thepage, wouldn't I be recreating the array? How do I move to the next student?

Sorry if this is lengthy, I am trying to be explicit!
Any help would be great!
Question by:dougp23
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2

Expert Comment

ID: 12268284
you can have a session variable which is an integer and increase it by 1 everytime a page is loaded, and select that row from the database.

Expert Comment

ID: 12268851
That would be assuming the student IDs and names are stored in a database at first.
LVL 49

Expert Comment

ID: 12269494
Why wouldn't you use a kind off big table with a row for each user and first col name, other colls radio buttons, like they use in online questionaires?

But as to your question about passing students array. You could use a session. You could also output something like:
<input type="hidden" name="nextstudents[]" value="s0002" />
<input type="hidden" name="nextstudents[]" value="s0003" />
<input type="hidden" name="nextstudents[]" value="s0004" />
<input type="hidden" name="nextstudents[]" value="s0005" />

And when clicked next, the first one would be popped off offcourse and things would look like
<input type="hidden" name="nextstudents[]" value="s0003" />
<input type="hidden" name="nextstudents[]" value="s0004" />
<input type="hidden" name="nextstudents[]" value="s0005" />

Quite obviously you could use
<input type="submit" name="submit_skip" value="Next/">
<input type="submit" name="submit_save" value="Save/">

And check with if(isset($_POST['submit_skip'])) in php.



Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users


Author Comment

ID: 12271250
Wow, thanks all.

But, here's my problem ( I think).  The script to load the page goes like this (It's on my other system, so I'm gonna do it from memory!)

1.  session_Start()   (to preserve and get anything from last script, i.e. teacher name, grade, etc.)
2.  connect to DB, create the array.
3.  create the html with radio buttons and comment box.
4.  Display the first kids name.
5.  have user enter data, then click appropriate button/hyperlink.

So, my feeling is, If I write to the db, and reload this script, don't I recreate the array all over again?  I want to make sure I don't recreate the array (I'm new top PHP, so this may not even be an issue!)  Should I offload the creation of the array to another script and use sessions to read what's next in the array?  Could I do a While -> For loop in this script on the array, and after the user chooses a button, get the next record in the array and just stay in the same script?


Author Comment

ID: 12271305
Let me clarify, there are 2 tables here!
Teh first one (the one I create the array with) will be static, I am just pulling info from it (student ID, which may NOT be sequential,) and then the second table will store what the user inputs.

I hope that helps!

Accepted Solution

cachedVB earned 125 total points
ID: 12271497
right after session_start();
add the line $_SESSION[studentid]++;
and only use row ($_SESSION[studentid] - 1) from the database each time

Author Comment

ID: 12272127
It doesn't matter that studentid's may NOT be sequential?? (in most cases, they are, but if a student moves or drops out, we do not reassign the id, the next student gets an all new id.  So we could have 400111, 400112, 400113, then 400116 because 2 kids have moved).

Does it matter that I build the array in this script?


Expert Comment

ID: 12274486
You can use the LIMIT keyword in your SQL and increment that every time the page is loaded  (That's assuming the contents of the table are not changed.


Expert Comment

ID: 12277764
I (think I) don't see the problem...

Since you're already using sessions, why not just test for the array's existence and if it doesn't yet exist (1st execution of script) then create it and add it to the 'storage' (with other session vars). If it does exists, then just use it with whatever you fancy as an increment counter... (?)

(well, that only take care of the 'not recreating array'..  Which actually wouldn't be that important : unless you have thousands of teachers, the overhead wouldn't exactly be 'that bad'. -- Maybe I missed some more important issue?)

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question