32393

function running in foreach loop

Question:

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 '

'.$_POST['gname'].'

'; $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?

Answer1:

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

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

Recommend

  • How to add multiple items to the DefaultComboBoxModel
  • How to insert integer value into SQLite table at Delphi
  • SELECT MAX query returns only 1 variable + codeigniter
  • Between statement missing indexes in certain cases
  • Yii2: Using Kartik Depdrop Widget?
  • MAVEN : Run Multiple Maven Project using Maven Test
  • How to Add Polymorphic Comments to Feed?
  • TFS - how do I sum child task hours to parent
  • Xaml, wpf image position and crop issue
  • jquery validation - waiting for remote check to complete
  • Getting different value with placeholder over CPU/GPU
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • Spring security and special characters
  • Read text file and split every line in MSBuild
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • JSON with duplicate key names losing information when parsed
  • Trying to switch camera back to front but getting exception
  • what is the difference between the asp.net mvc application and asp.net web application
  • jQuery tmpl and DataLink beta
  • Jquery - Jquery Wysiwyg return html as a string
  • php design question - will a Helper help here?
  • Matrix multiplication with MKL
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • AngularJs get employee from factory
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Memory offsets in inline assembly
  • embed rChart in Markdown
  • Turn off referential integrity in Derby? is it possible?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Programmatically clearing map cache
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize