在php中创建与MySQL数据对应的复选框

时间:2013-12-31 01:57:57

标签: php mysql database checkbox

我有一个PHP脚本,可以从我的数据库中提取信息。

$result = mysqli_query($con,"SELECT * FROM menuitem WHERE id='$id' LIMIT 1"); 
    if (!$result) {
        printf("Error: %s\n", mysqli_error($con));// Displays the error that mysql will generate if syntax is not correct.
        exit();
    }

//DYNAMIC PHP PULLING IN THE DATA AND SPITTING OUT THE RESULTS
while($row = mysqli_fetch_array($result))
{
    $id = $row['id'];
    $description = $row['description'];
    $picturepath = $row['picturepath'];
    $name = $row['name'];
    $price = $row['price'];
    $keywords = $row['keywords'];

    $dynamiclist = '<table align="center" width="60%" border="0" cellspacing="5" cellpadding="8">
                        <tr height="150"></tr>
                        <tr>
                            <td width="30% valign="top" align="center"><img style="border: #66cc33 5px solid;" src=" ' .$picturepath . '" height="200" width="200" border="1"/></td>
                            <td width="70%" valign="top" align="left"> <br />' . $name . ' <br /><br />$' . $price . '<br /><br /><font>Description:</font><br /> ' . $description . ' <br /><br /><br />
                                <form id="form1" name="form1" method="post" action="cart.php">
                                    <input type="hidden" name="keywords" id="keywords" value=" '. $keywords .'"/>
                                    <input type="hidden" name="pid" id="pid" value=" ' . $name . ' &nbsp;&nbsp; - &nbsp;&nbsp; $' . $price . ' "/>
                                    <input type="submit" name="button" id="button" value="Add to Order"/> 
                                </form>
                            </td>
                        </tr>
                        <tr align="center"><td><a href="cart.php">View Your Order</a></td></tr>
                    </table>';
    echo $dynamiclist;

}

mysqli_close($con); //close the db connection

?>

然后我有另一个页面,我希望为列出的每个关键字创建复选框。我不确定我的db列是否设置正确,但我设置了一个包含多个关键字的列:firstsecondfiftynone,等等。

我已尝试创建此功能,但它只显示实际的“复选框”,其中任何一个旁边都没有文字。

<?php

require("database.php"); //connect to the database


if(isset($_POST['keywords'])){
    $pid = $_POST['keywords'];
}

$result = mysqli_query($con,"SELECT * FROM menuitem "); 
    if (!$result) {
        printf("Error: %s\n", mysqli_error($con));// Displays the error that mysql will generate if syntax is not correct.
        exit();
    }

//DYNAMIC PHP PULLING IN THE DATA AND SPITTING OUT THE RESULTS
$option = '';
while($row = mysqli_fetch_array($result))
{
    $id = $row['id'];
    $description = $row['description'];
    $picturepath = $row['picturepath'];
    $name = $row['name'];
    $price = $row['price'];
    $keywords = $row['keywords'];

    $dynamiclist ="<input type='checkbox' name='checkbox' value='". $keywords ."'>";

    echo $dynamiclist;

}

目标是让用户从第一页中选择一个项目,在第二页上,列出该特定项目的几个复选框选项。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

如果您希望复选框旁边有文字,则必须将其放在HTML中。如果列包含以逗号分隔的列表,则需要将其拆分。

foreach(explode(',', $keywords) as $keyword) {
    $keyword = trim($keyword);
    echo "<input type='checkbox' name='checkbox' value='$keyword'>$keyword ";
}

答案 1 :(得分:0)

试试这个:

<?php

$keywords = explode(',', $keywords);

if(!empty($keywords))
{
     foreach($keywords as $keyword) {
    $keyword = trim($keyword);
    echo "<input type='checkbox' name='checkbox' value='$keyword'>$keyword ";
   }
}
?>

谢谢!