Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
first of all, WHERE plugin = `$check` ` should be changed to single quota ' like this WHERE plugin = '$check' –  user1646111 Jul 20 '13 at 1:09
    
You my friend are the best! Thanks! –  Tomas Cordero Jul 20 '13 at 1:12
    
so your problem solved? –  user1646111 Jul 20 '13 at 1:13
    
Yes it did. Thank you very much =) –  Tomas Cordero Jul 20 '13 at 1:14
    
Ok I must ask now... Why did I get a down vote? It is a very valid question. I am in need of legitimate help and help was provided. I accepted the help and thanked the person. It worked. I was very nice and I tried my best to explain the problem. Why did I get down voted?! –  Tomas Cordero Jul 20 '13 at 1:28

1 Answer 1

up vote 0 down vote accepted

You need to take the include out of the foreach loop. Try this:

include "functions.php";
foreach($check1 as $check) {
    checkboxes($check);
}
share|improve this answer
    
That actually fixed one of my other problems =) Thank you! All is working well now. –  Tomas Cordero Jul 20 '13 at 2:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.