如果值等于1,则尝试显示已签入复选框

时间:2014-04-04 15:02:21

标签: php checked

基本上我试图显示一个复选标记,如果数据库中的值等于1我想要选中复选框,如果它等于0则显示一个空复选框!

$sql_works="SELECT task.taskId, task.adminId, task.taskName, task.day, task.month, task.year, task.taskText,task.checkMark FROM task INNER JOIN administrator ON administrator.id = task.adminId Where taskId =" . $_GET['id'];         

                $result_works = mysql_query( $sql_works )or die("error");


                if ( !$result_works  ) 
                {
                    die ('Query Error, Please try again later ...');
                }

                $nb_works = mysql_num_rows ( $result_works );

                if ($nb_works > 0 ){
                    $row_works = mysql_fetch_assoc ($result_works);
    $checkmark= "$row_works['checkMark']";

    <input type="checkbox" name="checkbox" checked = "<?php echo $row_works['checkMark'];?>" /> <label for="custom">Task done!</label>

继承我的数据库:

    -- Dumping database structure for workspacedb
    CREATE DATABASE IF NOT EXISTS `workspacedb` /*!40100 DEFAULT CHARACTER SET latin1 */;
    USE `workspacedb`;


    -- Dumping structure for table workspacedb.task
    CREATE TABLE IF NOT EXISTS `task` (
      `taskId` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `adminId` int(10) unsigned NOT NULL,
      `taskName` varchar(50) DEFAULT NULL,
      `day` tinyint(4) NOT NULL,
      `month` varchar(50) NOT NULL,
      `year` smallint(6) NOT NULL,
      `taskText` text,
      `checkmark` tinyint(1) DEFAULT NULL,
      PRIMARY KEY (`taskId`),
      KEY `FK_task_administrator` (`adminId`),
      CONSTRAINT `FK_task_administrator` FOREIGN KEY (`adminId`) REFERENCES `administrator` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
    ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;

    -- Dumping data for table workspacedb.task: ~7 rows (approximately)
    /*!40000 ALTER TABLE `task` DISABLE KEYS */;
    INSERT INTO `task` (`taskId`, `adminId`, `taskName`, `day`, `month`, `year`, `taskText`, `checkmark`) VALUES
        (3, 21, 'task1', 6, '1', 2015, 'this is a test task 1', 0),
        (5, 21, 'comptask', 5, '4', 2014, 'this is a completed task', 1),
        (7, 21, 'tasss', 4, '3', 2015, '', 0),
        (8, 20, 'taaa', 2, '1', 2014, 'jjjjjj', 0),
        (9, 22, 'task1212', 1, '2', 2015, 'jjjjj', 0),
        (10, 21, 'task4', 1, '12', 2015, 'skksksmnd, cs cdnms cdn ms cds dcm sdc', 1),
        (11, 22, 'task22', 4, '1', 2014, 'testttstststststtstZL  awsidpoiawjde  awed i0owde r0ju qewdr 0qweurd woqej fdoqe fdoqe oijqeddij qe', 0);
    /*!40000 ALTER TABLE `task` ENABLE KEYS */;
    /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
    /*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

3 个答案:

答案 0 :(得分:1)

<?php $isChecked = $row_works['checkMark'] == 1 ? 'checked="checked"' : null; ?>
<input type="checkbox" name="checkbox" <?= $isChecked;?> /><label for="custom">Task done!</label>

答案 1 :(得分:0)

检查输入的正确语法是

<input type="checkbox" checked="checked" name="myCheckbox" />

所以php代码将是

<input type="checkbox" <?php if ($row_works['checkMark'] == 1) { 'checked="checked"'; } ?>  name="myCheckbox" />

答案 2 :(得分:0)

右。我发现这更容易阅读:

<?php
$sChecked = "";
if ($row_works['checkMark'] == 1) {
    $sChecked = ' checked="checked"';
}
?>
<input type="checkbox" <?=$sChecked?>  name="myCheckbox" />