Reputation: 301
I'm trying to generate a table with checkboxes on each row. I have found a working code below for generating a table based on query results. Is it possible to insert a code in here which will provide an extra column which will contain checkboxes in each row?
<?php
function SQLResultTable($Query)
{
$link = mysql_connect("localhost","root" , "") or die('Could not connect: ' . mysql_error()); //build MySQL Link
mysql_select_db("dbName") or die('Could not select database'); //select database
$Table = ""; //initialize table variable
$Table.= "<table border='1' style=\"border-collapse: collapse;\">"; //Open HTML Table
$Result = mysql_query($Query); //Execute the query
if(mysql_error())
{
$Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
}
else
{
//Header Row with Field Names
$NumFields = mysql_num_fields($Result);
$Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\">";
for ($i=0; $i < $NumFields; $i++)
{
$Table.= "<th>" . mysql_field_name($Result, $i) . "</th>";
}
$Table.= "</tr>";
//Loop thru results
$RowCt = 0; //Row Counter
while($Row = mysql_fetch_assoc($Result))
{
//Alternate colors for rows
if($RowCt++ % 2 == 0) $Style = "background-color: #FFCCCC;";
else $Style = "background-color: #FFFFFF;";
$Table.= "<tr style=\"$Style\">";
//Loop thru each field
foreach($Row as $field => $value)
{
$Table.= "<td>      $value      </td>";
}
$Table.= "</tr>";
}
}
$Table.= "</table>";
return $Table;
}
?>
Upvotes: 0
Views: 2434
Reputation: 1
its a very simply logic, excuse me i don't use your complete code:
index.php:
<html>
<head>
<title>your page</title>
</head>
<body>
<form method="post">
<table>
<caption>myTable</caption>
<thead>
<tr>
<th>column with checkbox</th>
<th>column with text</th>
</tr>
</thead>
<tbody>
<?php
// get your databaseresult to an array called $result
$connection = mysql_connect("server", "user", "password");
mysql_select_database("databasename");
$resultHash = mysql_query("SELECT * FROM mytable");
while($row = mysql_fetch_array($resultHash)){
$result[] = $row;
}
mysql_close($connection); // never forget to close the connection if not longer needed
foreach($result as $key => $value)
{
echo "<tr>\r\n";
echo " <td><input type=\"checkbox\" name=\"yourCheckboxName".$key."\" /></td>\r\m";
// $key is the index of your numeric $result array
echo " <td>".$value[0]."</td>\r\n";
echo "</tr>\r\n";
}
?>
<tbody>
</table>
</form>
</body>
<html>
That is simply all you need to do I think, and please work with mysql_fetch_array()
http://php.net/manual/en/function.mysql-fetch-array.php
it's even faster
I hope the code is correct, if not look what the arrays $result
and $value
are like by using var_dump()
. Didn't test it and didn't write php 4 weeks or something like this
Edit: I did a mistake last night, sorry, here is a little correction.
I think your database table design is like.
table(id int auto_increment, something varchar(255) not null, primary key(id))
<?php
foreach($result as $index => $row){
echo "<tr>";
echo "<td>";
echo "<input type='checkbox' name='yourname".$index."' />"; // now every checkbox has an unique identifier
echo "</td>";
foreach($row as $column => $value){
echo "<td>";
echo "column = ".$column;
echo "\r\n";
echo "value = ".$value;
echo "<td>";
}
echo "</tr>";
}
?>
Upvotes: 0
Reputation: 3711
If you want it as the last column in each row:
<?php
function SQLResultTable($Query)
{
$link = mysql_connect("localhost","root" , "") or die('Could not connect: ' . mysql_error()); //build MySQL Link
mysql_select_db("dbName") or die('Could not select database'); //select database
$Table = ""; //initialize table variable
$Table.= "<table border='1' style=\"border-collapse: collapse;\">"; //Open HTML Table
$Result = mysql_query($Query); //Execute the query
if(mysql_error())
{
$Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
}
else
{
//Header Row with Field Names
$NumFields = mysql_num_fields($Result);
$Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\">";
for ($i=0; $i < $NumFields; $i++)
{
$Table.= "<th>" . mysql_field_name($Result, $i) . "</th>";
}
$Table.= "<th>Checkbox column</th>";
$Table.= "</tr>";
//Loop thru results
$RowCt = 0; //Row Counter
while($Row = mysql_fetch_assoc($Result))
{
//Alternate colors for rows
if($RowCt++ % 2 == 0) $Style = "background-color: #FFCCCC;";
else $Style = "background-color: #FFFFFF;";
$Table.= "<tr style=\"$Style\">";
//Loop thru each field
foreach($Row as $field => $value)
{
$Table.= "<td>      $value      </td>";
$Table.= "<td><input type="checkbox" name="nameHere" value="valueHere" ></td>";
}
$Table.= "</tr>";
}
}
$Table.= "</table>";
return $Table;
}
?>
Though the code's that's generated is really ugly - inline styles, ugh. -_-
Upvotes: 1