PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1848
Number of posts: 5016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Checkbox selections put into a MySQL Database Posted by jandrews on 22 Jul 2005 at 9:46 PM
I want to create a page of checkboxes and have a user select a certain number of them. I then need those choices stored in a MySQL database. The html page is located at:


http://www.jamesa3.com/Profiles/profiles_start3.html


I will use those selections later to determine which resources are laid out on the teacher's webpage. My first step is getting the selections into a database.

Report
Re: Checkbox selections put into a MySQL Database Posted by tvienti on 25 Jul 2005 at 2:34 PM
Your link doesn't work. But your question is a little vague... when a checkbox is checked it comes through on $_GET or $_POST with a value of "on", I believe. All you have to do is in your script that handles the input, check to see if that checkbox is set to "on" or not. If it is, then you'll want to flag it as true in your DB, or 1, or "Y", or whatever format you're storing them in your DB. Make sense?

: I want to create a page of checkboxes and have a user select a certain number of them. I then need those choices stored in a MySQL database. The html page is located at:
:
:
: http://www.jamesa3.com/Profiles/profiles_start3.html
:
:
: I will use those selections later to determine which resources are laid out on the teacher's webpage. My first step is getting the selections into a database.
:
:

Report
Re: Checkbox selections put into a MySQL Database Posted by jandrews on 25 Jul 2005 at 5:09 PM
I'm sorry. I misspelled it. The link is:
http://www.jamesa3.com/Profiles/profile_start3.html

Out of more than 200 links, I need my database to remember which ones were selected. I need to recall this information later so that on that specific teacher's webpage, only those resources appear which had been checked. I understand that the answer to this problem may be much more of an undertaking than I have the right to ask of anyone.
Report
Re: Checkbox selections put into a MySQL Database Posted by tvienti on 26 Jul 2005 at 5:44 AM
This message was edited by tvienti at 2005-7-26 5:45:11

Well it may be a big question to ask, but I can try to point you in the right direction. If I were tackling this problem, this is how I might lay out my tables:

[teachers] - a table of teachers, each with a unique ID, name, any other info you want to store that has a 1-1 relationship with that teacher

[resources] - a table of resources, each with a unique ID, name, group, and subgroup. These fields will be used to render your checkboxes. Group would be "General Resources", "Resources for Social Science", etc. Subgroups would be "Economics", "Government", etc. With this information you could dynamically generate your page of checkboxes, and for the name of each checkbox just use the ID of that resource.

[teachers_resources_xref] - a cross-referencing table (needed since you have a many to many relationship between teachers and resources). This will just have two columns: teachers_id and resources_id. So if teach 1 subscribes to resources 2 3 and 4, there will be three rows: (1,2), (1,3), and (1,4).

When the teacher submits your page, for each checked resource you should get a $_GET[resource_id] = "on". So in your parsing script, just each key where the value = "on" and store that in an array, which will be your list of resources the teacher subscribed to:

$resources = array();
foreach ($_GET as $res_id => $val)
  if ($val == "on") $resources[] = $res_id;


At the end of that code you should have an array of $resources which are numbers corresponding to the id field of your resources table. Assuming you already have the teacher's ID somehow - through login or what have you - you just need to build a series of insert statements for each resource ID in the array, which is easy...

// Assumed teacher's ID is already stored in $tid
foreach ($resources as $rid)
{
  $query = "INSERT INTO `teachers_resources_xref` VALUES ($tid, $rid);"
  // Whatever code you use to execute the query
}


Hope this helps. I intentionally left the mechanics of connecting to and querying the DB because I'm not sure what you do or don't know. If you don't know how to handle databasing in PHP I'd take a look at PHP.net for some really easy to learn functions.

T

: I'm sorry. I misspelled it. The link is:
: http://www.jamesa3.com/Profiles/profile_start3.html
:
: Out of more than 200 links, I need my database to remember which ones were selected. I need to recall this information later so that on that specific teacher's webpage, only those resources appear which had been checked. I understand that the answer to this problem may be much more of an undertaking than I have the right to ask of anyone.
:



Report
Re: Checkbox selections put into a MySQL Database Posted by jandrews on 26 Jul 2005 at 7:22 PM
That's a lot to think about. I'm pretty sure I understand the concepts but the mechanics are going to take me a long time. I may have to put this one on the back burner until I have some other things done.
Report
Re: Checkbox selections put into a MySQL Database Posted by tvienti on 26 Jul 2005 at 8:06 PM
Yes, SQL is a whole topic in and of itself. If I may recommend a good starting point... it's online, free, interactive, and a great primer for SQL: http://www.sqlcourse.com/

T

: That's a lot to think about. I'm pretty sure I understand the concepts but the mechanics are going to take me a long time. I may have to put this one on the back burner until I have some other things done.
:

Report
Re: Checkbox selections put into a MySQL Database Posted by jandrews on 31 Jul 2005 at 5:17 PM
Would perhaps this concept work.

<form action="test5.php" method="post">
<table border="0" cellpadding="0" cellspacing="0" width="765">
<tr>
<td><input type="checkbox" name="aw1">John's ESL Site</td>
<td><input type="checkbox" name="aw2">Dave's ESL Site</td>
</tr>
</table>
<input type="submit">
</form>

if aw1 = 'checked' {
resource<?php $num;?> = 'http://www.johnnsesl.com' and
rname<?php $num;?> = 'John&#146;s ESL Site' and
<?php $num = $num + 1;?>
}


Report
Re: Checkbox selections put into a MySQL Database Posted by tvienti on 1 Aug 2005 at 6:30 AM
There's a few flaws in that code... to get you started:

 if aw1 = 'checked' {
 	resource<?php $num;?> = 'http://www.johnnsesl.com' and 
 	rname<?php $num;?> = 'John&#146;s ESL Site' and 
 	<?php $num = $num + 1;?>
   }


You want to compare for the checkbox being set to "on", not "checked". You should double check that because I don't use checkboxes much but I believe all browsers will use the word "on". Also, you need to compare using ==, not =. Finally, aw1 would likely be interpreted as a string constant, ie "aw1". You need a $ in front of it for PHP to treat it as a variable. You should retrieve that value with $_POST['aw1'].

The structure of the if clause would then become

 if ($_POST['aw1'] == 'on') {...} 


Hope that helps.

T

: Would perhaps this concept work.
:
: <form action="test5.php" method="post">
: <table border="0" cellpadding="0" cellspacing="0" width="765">
: <tr>
: <td><input type="checkbox" name="aw1">John's ESL Site</td>
: <td><input type="checkbox" name="aw2">Dave's ESL Site</td>
: </tr>
: </table>
: <input type="submit">
: </form>
:
: if aw1 = 'checked' {
: resource<?php $num;?> = 'http://www.johnnsesl.com' and
: rname<?php $num;?> = 'John&#146;s ESL Site' and
: <?php $num = $num + 1;?>
: }
:
:
:

Report
Re: Checkbox selections put into a MySQL Database Posted by jandrews on 9 Aug 2005 at 8:26 AM
Ultimately, this is what I got to work for me. I tested whether or not it properly transmitted the value of the checkbox. It works fine, but my problem is that I now need the variable "irname" and "iraddress" to increment when an "IF" statement is true so that when the next "IF" statement is true, the "irname" and "iraddress" variables are now "irname2" and iraddress2".


if ($aw1 == 'on') {
$irname1 = "The History Net";
$iraddress1 = "http://www.about.com/homework/";
print "It was on.<br>&bull; ".$irname1."<br>&bull; ".$iraddress1;}
else {print "It was off.<br>&bull; ".$irname1."<br>&bull; ".$iraddress1;}

Report
Re: Checkbox selections put into a MySQL Database Posted by tvienti on 9 Aug 2005 at 9:52 AM
If you need to cycle through variables, your best bet is to use an array. Instead of $irname1, $irname2, etc... you can just use $irname[0], $irname[1]. Then it's pretty easy...

$count = 0;
if (/*condition*/)
 $count++;

echo "irname is " . $irname[$count];


T

: Ultimately, this is what I got to work for me. I tested whether or not it properly transmitted the value of the checkbox. It works fine, but my problem is that I now need the variable "irname" and "iraddress" to increment when an "IF" statement is true so that when the next "IF" statement is true, the "irname" and "iraddress" variables are now "irname2" and iraddress2".
:
:
: if ($aw1 == 'on') {
: $irname1 = "The History Net";
: $iraddress1 = "http://www.about.com/homework/";
: print "It was on.&bull; ".$irname1."&bull; ".$iraddress1;}
: else {print "It was off.&bull; ".$irname1."&bull; ".$iraddress1;}
:
:



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.