当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Put a checkbox in each table cell

问题描述:

I'm trying to add checkboxes to each cell in my table.

If $columns['mything$record'] is equal to 1, I want a checkbox that's checked to show up.

If $columns['mything$record'] is equal to 0, I want a blank checkbox to show up. I've created if statements which one can view below, but they don't put a checkbox in each table cell with the respective value, only one blank checkbox appears at the top of the page.

How can I put checkboxes in each cell that are checked and unchecked based on the cell value?

[![screenshot][1]][1]

 $stmt = $sql->prepare("SELECT record,shortName FROM dms

where dms.isInactive=0

ORDER BY lastName asc, firstName asc");

$stmt->execute();

$shortNameArray=array();

while ($row = $stmt->fetch()) {

// print_r($row);

$shortNameArray[$row['record']]=$row['shortName'];

}

//print_r($shortNameArray);

$columns = array(

1 => array(

"selectFieldName" => "nrs.record nrRecord",

"resultFieldName" => "nrRecord",

"headerName" => "NT#",

"defaultSortOrder" => -1,

),

array(

"selectFieldName" => "nrs.title",

"resultFieldName" => "title",

"headerName" => "Title",

"defaultSortOrder" => 1,

),

);

$selectedColumnNumbers = array(1,2,3);

$counter=4;

foreach($shortNameArray as $record=>$name){

if($record>0){

$columns[]=array(

"selectFieldName" => "CASE WHEN $record IN (GROUP_CONCAT(dm_nr_links.dmRecord)) THEN 1 ELSE 0 END mything$record",

"resultFieldName" => "mything$record",

"headerName" => "$name",

"defaultSortOrder" => 1,

);

$selectedColumnNumbers[]=$counter;

$counter++;

if ($columns['mything$record'] == 1) {

echo "<input type='checkbox' name='PLJan' checked='checked' />";

}

if ($columns['mything$record'] == 0) {

echo "<input type='checkbox' name='JLFeb' />";

}

}

}

$selectedColumns = array();

$selectedFields = array(); // contains all the fields to be selected

foreach ($selectedColumnNumbers as $columnNumber){

$selectedColumns[$columnNumber] = $columns[$columnNumber];

if (strlen($columns[$columnNumber]["selectFieldName"]) > 0)

$selectedFields[] = $columns[$columnNumber]["selectFieldName"];

}

$selectedFields = array_unique($selectedFields); // Remove all duplicate fields for selection

///// Process Filter Cookies

$filterWhereString = "";

$filterBindValues = array();

for ($c=1; $c<=5; $c++) {

if (isset($_COOKIE["nrsfl$c"])

&& isset($_COOKIE["nrsfl$c"."Op"])

&& isset($_COOKIE["nrsfl$c"."Val"])

&& strlen($_COOKIE["nrsfl$c"."Val"]) > 0

) {

$columnNumber = $_COOKIE["nrsfl$c"];

$filterOperator = $_COOKIE["nrsfl$c"."Op"];

$filterValue = $_COOKIE["nrsfl$c"."Val"];

if (isset($columns[$columnNumber])){

if ($c >= 2) {

$lastC = ($c-1);

if (strlen($filterWhereString) > 0) // If string exists, add conjunction

{

if (isset($_COOKIE["nrsfl$lastC"."C"])){

$conSelect = $_COOKIE["nrsfl$lastC"."C"];

} else $conSelect = 1;

if ($conSelect == 1) $conjunction = "AND";

else $conjunction = "OR";

$filterWhereString .= "$conjunction ";

}

}

$column = $columns[$columnNumber];

$fieldName = $column['resultFieldName'];

$filterWhereString .= "$fieldName ";

switch ($filterOperator) {

case 1:

$operator = "=";

break;

case 2:

$operator = "LIKE";

break;

case 3:

$operator = ">=";

break;

case 4:

$operator = "<=";

break;

case 5:

$operator = ">";

break;

case 6:

$operator = "<";

break;

case 8:

$operator = "NOT LIKE";

break;

default:

$operator = "!=";

}

if ((strcmp($operator, "NOT LIKE") == 0) ||(strcmp($operator, "LIKE") == 0)) $filterField = "CONCAT('%',:filter".$c.",'%')";

else $filterField = ":filter$c";

$filterWhereString .= "$operator $filterField ";

if (substr_compare($fieldName, "date", 0, 4) == 0) { // Convert dates

$filterValue = strtotime($filterValue);

$filterValue = date("Y-m-d",$filterValue);

}

if ($fieldName=="isRestricted") { // Convert binaries

if(strtolower($filterValue)=='y')$filterValue="1";

if(strtolower($filterValue)=='n')$filterValue="0";

}

$filterBindValues[":filter$c"] = $filterValue;

}

}

}

/////

?>

网友答案:

Replace your code line

echo "<input type='checkbox' name='PLJan' ";
    echo  "checked='checked'";

with

echo "<input type='checkbox' name='PLJan' checked='checked' />";

and

echo "<input type='checkbox' name='JLFeb' ";

with

echo "<input type='checkbox' name='JLFeb' />";
分享给朋友:
您可能感兴趣的文章:
随机阅读: