Jump to content

PHP solution for this?


webguync

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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       

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.