函数在foreach循环中运行 [英] function running in foreach loop
问题描述
我有第一页form.php。
在这个页面上,我有复选框。每个复选框都是从数据库表中生成的。这里是代码:
<?php
include(config.php);
$ mysqli = new mysqli($ host,$ db_uname,$ db_pass,$ db);
$ query =SELECT * FROM`plugins` WHERE 1;
$ b $ if($ result = $ mysqli-> query($ query)){
echo'< form action =test.phpmethod =post >
< input name =gnameplaceholder =Group Name .../>
< table width =200>
';
while($ row = $ result-> fetch_assoc()){
echo'< tr>< td>
< label>
< input type =checkboxvalue ='。$ row [plugin]。'name =checkbox []>
'。$ row [plugin]。'
< / label>
< / td>< / tr>';
}
echo'
< / table>
< select name =permplugin>
< option>选择一个...< / option>';
$ query2 =SELECT * FROM`permission_types` WHERE 1;
if($ result2 = $ mysqli-> query($ query2)){
echo'< h3>选择下面的权限格式< / h3>< hr />' ;
while($ row2 = $ result2-> fetch_assoc()){
echo'
< option value ='。$ row2 [plugin_name]。'>' 。$ ROW2 [ PLUGIN_NAME]。 '< /选项>';
}
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){
includefunctions.php;
复选框($ check);
$ b echo'< h3>下面选定的权限格式< / h3>< hr />';
echo $ _POST ['permplugin'];
} else {
echo请选择至少一个插件。
}
?>
函数页面代码如下所示:
<?php
//所有函数都在这里。
函数复选框($ 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;
/ *获取关联数组* /
while($ row_perm = $ result_perm-> fetch_assoc()){
echo $ row_perm ['node']。'< br /> ;
unset($ check);
$ / code $ / pre
当我运行test.php时,出现这个错误:
致命错误:无法在C:\xampp\htdocs\TPYC\中重新声明复选框()(以前在C:\xampp\htdocs\TPYC\functions.php:3中声明)第15行的functions.php
我在做什么错?
解决方案 <你需要把包括foreach循环。试试这个:
includefunctions.php;
foreach($ check1 as $ check){
checkboxes($ check);
}
thank you for taking time to look at this. I have been dealing with this annoying foreach loop. Here is what I am trying to do.
I have the first page "form.php".
On this page I have check boxes. Each check box is generated from a database table. Here is the code:
<?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>';
}
}
?>
Now after that it sends the checked boxes to "test.php"
here is the code for that:
<?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.";
}
?>
The functions page code looks like this:
<?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);
}
}
When I run the test.php I get this error:
Fatal error: Cannot redeclare checkboxes() (previously declared in C:\xampp\htdocs\TPYC\functions.php:3) in C:\xampp\htdocs\TPYC\functions.php on line 15
What am I doing wrong?
解决方案 You need to take the include out of the foreach loop. Try this:
include "functions.php";
foreach($check1 as $check) {
checkboxes($check);
}
这篇关于函数在foreach循环中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!