MSQL, PHP & Tick Boxes
Have had a busy few weeks if you’ll forgive the lack of blogging of late. I’ve got a number of projects on the go at work. The main achievement of the week has been adding tick boxes (thats “checkboxes” for our US listeners) to a dynamic page written in php, pulled from a MySql database. I’ve done loads of reading and searching and found pieces of the puzzle all over the net. This video however was most useful, although it isnt quite what i’m after it helped get my head round a few things i was trying to achieve.
The scenario is such:
A page is generated giving a list of member in a particular group, with over a thousand members each being part of several groups. I want to add to be able to select a subset of member from each group (or even the whole group) and add a number of points to each member. This is effectively a point scoring system keeping only running totals..
How i achieved it:
First the page was written in php with a MySql query which displays the relevant list of members. Simply a query to read the list of members of the appropriate group then a couple of lines of PHP to write out the contents of the page. I used a For loop to cycle through each of the members found.
In the write out i wanted to add a tick box alongside each name, so as to select multiple members or use a floodfill. This was more difficult than i first thought. I easily added a tick box to each line, just a loop to write one after each entry, however i failed to make these do anything useful.
I did some searching on the net and found a script that was written a few years ago that makes the use of tick boxes much easier. The script seems that old that the URL of the author is no longer valid.
The script goes a lot of the way to achieving what i need. It allows multiple tick boxes to be linked together to make an array of values. This array is then sent as a POST variable to the next page. After implementing the script i was then in a position to use a “flood fill” option, which is most useful to me as group sizes are sometimes 35+.
Once the array is sent to the next page it needs to be interpreted and the selected records acted upon. On this page i used the PHP POST function to retrieve the array sent as a variable.
<?php print_r($_POST) ?>
This is a useful function i found which can be used to display the variables being passed on. A very handy troubleshooter.
I then used the PHP IMPLODE to change the array to a string a variable, this is a most handy function as it allows to be searched. The final step was to search my database looking for members that matched the selection, each member has unique identifier which i assigned as the value of the tick box, and add points to their running total. This was achieved with a simple SQL query which reads the current value and incremements it as necessary. Here is the relevant bit of code.
<?php
include '../config.php'; // includes the information to connect to the mysql database
$credit = $_POST[credit]; // grabs a number of credit to add to the total
$selection = implode(’,', $_POST['subset']); //grabs the id of the subset of members
and converts to a string
//the query updates the records of the selected members by adding the new credit.
$updatequery = mysql_query(”UPDATE credits SET
total = total +”. $credit. ” WHERE `id` IN ($selection)”)
or die(mysql_error());
mysql_query($updatequery);
header(’Location:submitted.php’); //page redirect
?>
Posted in Today's Achievements