bigD21
Member
Registered: 22nd May 07
User status: Offline
|
Im fairly new to php and have been reading some material. I have been setting myself a couple of small projects to complete and then implement there methods to create larger projects. This example project is purely for my own purposes, not for any form of gain whatsoever.
I’ve got a mysql database that contains this;
ID COURSE
1 ENGLISH
2 ENGLISH
3 ENGLISH
4 MATHS
5 MATHS
What I’m wanting to do is create a simple form in php that contains a set of check boxes, for course (English, maths) I want the form to also contain a submit button which will pass the submitted info to results.php
Please could someone tell/show me how I’d go about displaying checkboxes within a form based upon mysql data?
What I want to do is be able to select a course, or both, and then submit the form. The following page would display the number of rows that I have selected. So based on the mysql table, if I’d just checked the “English” checkbox then it query SELECT * from database WHERE course=’english’ and in this case I would want it to display that “You have selected 3 rows”.
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
What you have written makes very little sense
|
bigD21
Member
Registered: 22nd May 07
User status: Offline
|
What do you mean - it simply says that I have a mysql table which is;
ID COURSE
1 ENGLISH
2 ENGLISH
3 ENGLISH
4 MATHS
5 MATHS
And that Im wanting to create a web form. I want to be able to have two checkboxes in the form that are called from the mysql table.
If they are checked and submitted then it passes the info to a new page called results.php which by then would have processed the mysql query for the checkboxes.
So basically, if ive got two options in the database then I only want to have two checkboxes displayed. If a checkbox is checked (e.g english) then the form s submitted it would process the appropriate sql query and would return the amount of rows selected
|
Ian
Site Administrator
Registered: 28th Aug 99
Location: Liverpool
User status: Offline
|
Try a
SELECT DISTINCT course FROM table
And put that loop inside <select> tags with an <option></option> on each iteration.
On second thoughts, you should be passing an integer around rather than the subject name. Ideally your example should be normalised in to two tables.
id course
1 1
2 1
3 1
4 2
5 1
id coursename
1 ENGLISH
2 MATHS
And you populate the list box from the second table. That way you can pass around the id from that table and it won't matter if you introduce special characters in to the subject name.
You can then extend the HTML syntax a bit and use
<option value="$id">$name</option>
ie. the HTML form details with id which will be your integer, but the user sees the course name.
When that gets to the other script you can just do a SELECT COUNT(*) FROM tablename WHERE id = $id and you'll get the number of rows that match.
Do also bear in mind that you could use an aggregate function to tell the user in the first place how many rows are in the table for each subject, ie.
SELECT course, COUNT(*) AS coursecount FROM table GROUP BY course
Should return
ENGLISH 3
MATHS 2
Which you can then format nice using some string concatenation and put that in your drop down box. Bit more difficult to pass around but it's better information for the user. Depends if you actually really want a drop down box or not. I generally use the aggregate stuff when I'm just displaying the values. Post Distribution in profiles is written in this way except that there is some arithmetic in the query as well.
|
bigD21
Member
Registered: 22nd May 07
User status: Offline
|
Would you be able to give me an example of some code as to how you would do it from scratch please?
|