在foreach循环中运行的函数

时间:2013-07-20 01:05:54

标签: php function checkbox foreach mysqli

谢谢你花时间看看这个。我一直在处理这个烦人的foreach循环。这是我想要做的。

我有第一页“form.php”。 在这个页面上我有复选框。每个复选框都是从数据库表生成的。这是代码:

<?php
include("config.php");
$mysqli = new mysqli($host, $db_uname, $db_pass, $db);
$query = "SELECT * FROM `plugins` WHERE 1";

if ($result = $mysqli->query($query)) {

    echo '<form action="test.php" method="post">
            <input name="gname" placeholder="Group Name..."/>
                <table width="200">
                    ';

    while ($row = $result->fetch_assoc()) {

echo '<tr><td>
        <label>
            <input type="checkbox" value="'.$row["plugin"].'" name="checkbox[]">
            '.$row["plugin"].'
        </label>
        </td></tr>';

    }
    echo '
            </table>
            <select name="permplugin">
            <option>Select One...</option>';

$query2 = "SELECT * FROM `permission_types` WHERE 1";

if ($result2 = $mysqli->query($query2)) {
echo '<h3>Select Permission format below</h3><hr />';
while ($row2 = $result2->fetch_assoc()) {
echo '
<option value="'.$row2["plugin_name"].'">'.$row2["plugin_name"].'</option>';
}
echo '
</select>
<br />
<input name="" type="reset"><input name="" type="submit">
</form>';
}
}
?>

之后,它将复选框发送到“test.php” 这是代码:

<?php
if(!empty($_POST['checkbox']) || !empty($_POST['select']) || !empty($_POST['gname'])) {
    echo '<h1>'.$_POST['gname'].'</h1>';

    $check1 = $_POST['checkbox'];
    foreach($check1 as $check) {
        include "functions.php";
            checkboxes($check);

    }
    echo '<h3>Selected Permission format below</h3><hr />';
    echo $_POST['permplugin'];

} else {

    echo "please select atleast one plugin.";
}
?>

函数页面代码如下所示:

<?php
//all functions are here.
function checkboxes($check){
$mysqli_perm = new mysqli("localhost", "uname", "pword", "tcordero_permnodes");
$query_perm = "SELECT * FROM permission_nodes WHERE plugin = `$check`";
if ($result_perm = $mysqli_perm->query($query_perm)) {
    echo $check;
    /* fetch associative array */
    while ($row_perm = $result_perm->fetch_assoc()) {
        echo $row_perm['node'].'<br />';

    }
    unset($check);
}
}

当我运行test.php时出现此错误: 致命错误:无法在第15行的C:\ xampp \ htdocs \ TPYC \ functions.php中重新声明复选框()(之前在C:\ xampp \ htdocs \ TPYC \ functions.php:3中声明)

我做错了什么?

1 个答案:

答案 0 :(得分:1)

你需要从foreach循环中取出包含。试试这个:

include "functions.php";
foreach($check1 as $check) {
    checkboxes($check);
}