webguync Posted May 27, 2008 Share Posted May 27, 2008 I need to create a grid matrix, which I can easily do in HTML as a table. I also then need a solution to be able to hi-lite a cell by selecting an individual cell. There needs to be the ability to make multiple selections, and the data inside the cells will need to be able to make it's way into a MySQL database. So basically it will be like a form with radio buttons except this will be a table based grid. Can this be done w/ PHP or maybe a combination of PHP and JavaScript? Quote Link to comment Share on other sites More sharing options...
prcollin Posted May 27, 2008 Share Posted May 27, 2008 I need to create a grid matrix, which I can easily do in HTML as a table. I also then need a solution to be able to hi-lite a cell by selecting an individual cell. There needs to be the ability to make multiple selections, and the data inside the cells will need to be able to make it's way into a MySQL database. So basically it will be like a form with radio buttons except this will be a table based grid. Can this be done w/ PHP or maybe a combination of PHP and JavaScript? are you trying to do a pixel space type of thing? Quote Link to comment Share on other sites More sharing options...
webguync Posted May 27, 2008 Author Share Posted May 27, 2008 there needs to be pixel space with the cells, yes. Is that what you are asking? Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 27, 2008 Share Posted May 27, 2008 I "think" I undersand what you want. What you are asking for is pretty much a JavaScript solution. I would suggest making each cell a form input field. When unselected make the field disabled. When selected make it enabled. Simply add an onclick trigger to change the disabled state. When a form is submitted disabled fields are not inlcuded in the submission. You can also use style properties so they doesn't "look" like input fields. Here's a quick example. You would also want to create an onsubmit trigger to diable any fields that do not have the class of "active" <html> <head> <style> input { border:0px; } .active { background-color:yellow; } .inactive { background-color:#cecece; } </style> <script type="text/javascript"> function changeState(fieldObj) { if (fieldObj.className=='active') { newClass = 'inactive'; } else { newClass = 'active'; } fieldObj.className = newClass; return; } </script> </head> <body> <table> <tr> <td><input type="text" class="inactive" value="Cell Data" onclick="changeState(this);"></td> <td><input type="text" class="inactive" value="Cell Data" onclick="changeState(this);"></td> </tr> <tr> <td><input type="text" class="inactive" value="Cell Data" onclick="changeState(this);"></td> <td><input type="text" class="inactive" value="Cell Data" onclick="changeState(this);"></td> </tr> </table> </body> </html> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted May 27, 2008 Share Posted May 27, 2008 You could use checkboxes as part of a form, name them all something like "grid[]" and give them values ranging from 1 to 100 (if you have a 10x10 matrix). Build the table and the form with PHP, since you don't want to write all that repetitive code. On your processing page, you can store the numbers of the selected fields in a DB: <?php $numbers = $_POST['grid']; //check if any fields were selected if (is_array($numbers)) { $data = implode(',', $numbers); //the $data variable now holds something like "4,5,6,50,51" depending on which fields was selected } //now store the $data in a DB ?> You can then rebuild the stored grid with PHP, using checked="checked" inside the input tags, every time the name equals one of the stored numbers. Quote Link to comment Share on other sites More sharing options...
webguync Posted May 27, 2008 Author Share Posted May 27, 2008 thanks mjdamato, that *MIGHT* be what I am looking for, or possibly a combination of your solution and that mentioned by thebadbad. So I could set up the values in a database table named grid and associate an ID with the DB info? Can you provide an example of PHP code which creates the HTML table? thanks Quote Link to comment Share on other sites More sharing options...
thebadbad Posted May 27, 2008 Share Posted May 27, 2008 Rough example: <?php $fields = 100; $fields_per_row = 10; echo '<form action="" method="post"><table><tr>'; for ($i = 1; $i <= $fields; $i++) { echo '<td><input type="checkbox" name="grid[]" value="', $i, '" /></td>'; if (!($i % $fields_per_row) && $i != $fields) { echo '</tr><tr>'; } } echo '</tr></table><input type="submit" /></form>'; ?> Quote Link to comment Share on other sites More sharing options...
webguync Posted May 27, 2008 Author Share Posted May 27, 2008 ok, so for the database I would basically just need two fields, one as a unique identifier and one with the table cell data? eg: ID Data 1 blah blah blah blah blah Quote Link to comment Share on other sites More sharing options...
webguync Posted May 28, 2008 Author Share Posted May 28, 2008 Trying to put everything together and I am not quite sure I am on the right path. Here is what I have so far: <?php /*--------- DATABASE CONNECTION INFO---------*/ $hostname="localhost"; $mysql_login="login"; $mysql_password="password"; $database="NN_FollowUp"; // connect to the database server if (!($db = mysql_pconnect($hostname, $mysql_login , $mysql_password))){ die("Can't connect to database server."); }else{ // select a database if (!(mysql_select_db("$database",$db))){ die("Can't connect to database."); } } $numbers = $_POST['grid']; //check if any fields were selected if (is_array($numbers)) { $data = implode(',', $numbers); //the $data variable now holds something like "4,5,6,50,51" depending on which fields was selected } //now store the $data in a DB $fields = 24; $fields_per_row = 5; echo '<form action="" method="post"><table id='matrix'><tr>'; for ($i = 1; $i <= $fields; $i++) { echo '<td><input type="checkbox" name="grid[]" value="', $i, '" class="inactive" onclick="changeState(this);">/></td>'; if (!($i % $fields_per_row) && $i != $fields) { echo '</tr><tr>'; } } echo '</tr></table><input type="submit" /></form>'; ?> I created a database called 'NN_FollowUp' and the table is called 'grid'. right now I get a blank white page, so there is a syntax error somewhere. Quote Link to comment Share on other sites More sharing options...
webguync Posted June 2, 2008 Author Share Posted June 2, 2008 can anyone see where my error is? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.