Save checkbox data in a database in PHP (2) _php tutorial

Source: Internet
Author: User
This code is very simple, you have to read it quickly. The main work has two functions complete: "Get_checkbox_labels" and "make_checkbox_html". Where the "get_checkbox_labels" query table Const_skills and returns an array of objects, each object has an ID value and the corresponding skill name. We transmit this array and some other parameters to "make_checkbox_html", which will return a string to generate the HTML code for the checkbox. Now let's insert this string into the HTML file to generate the form we need to include various skill choices. Note that I did not transfer the variable $checked to "make_checkbox_html", which is an array of checked objects we want to display. If a user learns a new skill, we can provide an "edit skills" page that shows the user's skill entry saved in the CheckBox box should be pre-checked.

What is the benefit of creating a form dynamically with this method relative to the ability to generate a skill checkbox with a fixed HTML code? Well, maybe we allow job seekers to choose a project in our table const_skills, such as DHTML, so that we can insert it into the table const_skills, and then, the candidate to visit our site, we will find a more DHTML option. No need to adjust the HTML file.

Insert Lookup_skills

Now that we have created this form, we need to save the user's chosen skills. In the make_checkbox_html function, we invoke each of the selection elements with skill[], meaning that we can access each selection as an array element. This allows us to insert this option into the table Lookup_skill. If the user selects 5 options, we insert 5 records in the Lookup_skill. Remember that each record in table Lookup_skills has only two field User ID and skill ID. In my example site, users can register and then create/edit their profiles. You may want to use the session to save the UserID when they log in. But how to manage the UserID is beyond the scope of this article.

The following code, we assume we might access this userid with this variable name $uid, here is the function code that inserts the record:


/* The function we call to insert.
The $skills argument is the skills array that
was sent to the script when the user hits the Submit button
*/
function Insert_skills ($uid, $skills) {

/* First, we ' ll delete any entries this user already have
In the table */
Purge_lookup ("Lookup_skills", $uid);

/* Now create the SQL Insert query */
$query = Create_checkbox_query ($skills, "Lookup_skills", $uid);

/* Execute the query */
mysql_query ($query);
}

/* Helper function for Insert_skills ().
Removes all rows in $table with $uid */
function Purge_lookup ($table, $uid) {
$q = "DELETE from $table, where uid = ' $uid '";
mysql_query ($q);
}

/* Helper function for Insert_skills ().
Generates the Sctual SQL query */
function Create_checkbox_query ($arr, $table, $uid) {
$q = "INSERT into $table (UID, skill_id) VALUES";

foreach ($arr as $check) {
$q. = "($uid, $check)". ",";
}

/* Remove the last comma and return */
Return substr ($q, 0,-1);
}

?>

It's simple. Now you know how to create a form dynamically by reading records from a table Const_skill, and also know how to save the user's chosen skills to table Lookup_skills. What are we going to do next? Let's take a look at the search.
Search

When an employer comes to a web developer, he comes to your search page, and you can show the same form and allow him to choose the skills he wants to be an employee. You get an array of his selected skills, and then you can iterate over the array and use an SQL statement to find the candidate who has the skill, you can display the list or result, and allow the searchers to point to an item to display its details. The following function describes how to create this query statement:


/* Builds a query to search for the skills
Checked off in the $skills array */

function Skill_search ($skills) {
if (!empty ($skills)) {
$query = "Select DISTINCT user.username
From user, Const_skills, lookup_skills
WHERE Lookup_skills.uid = user.id
and lookup_skills.skill_id = Const_skills.id ";

$query. = "and (";
foreach ($skills as $check) {
$query. = "Const_skills.id = $check or";
}

/* Remove the final OR * *
$query = substr ($query, 0,-2);
$query. = ")";

$count = count ($skills);
$query. = "GROUP by User.username have count (user.username) >= $count";

$query. = ";";
return $query;
}
}

?>

If you perform a search for PHP and Javascript, this function returns this statement:

SELECT DISTINCT user.username from user, const_skills, lookup_skills WHERE lookup_skills.uid = user.id and LOOKUP_SKILLS.S kill_id = Const_skills.id and (const_skills.id = 3 OR const_skills.id = 5) GROUP by User.username have count (user.user Name) >= 2;

This function will return the logic of the project you have chosen, that is, if we choose PHP and JavaScript Two, we will only return * username of candidates with PHP and JavaScript two skills. If you want to find a candidate who has any of these skills, you can use PHP *or* Javascript, and if you want to show the same record, you can remove the last "GROUP by ..." Clause.


Summarize

All right, that's it. Checkboxes is an excellent form element, as discussed in this article. I hope this helps you to work with them and create a data-driven website.

http://www.bkjia.com/PHPjc/314272.html www.bkjia.com true http://www.bkjia.com/PHPjc/314272.html techarticle This code is very simple, you have to read it quickly. The main work has two functions complete: "Get_checkbox_labels" and "make_checkbox_html". where "get_checkbox_labels" Query ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.