Ryflex Posted July 5, 2012 Share Posted July 5, 2012 Hi all, I'm trying to build a menu which can be edited in the browser and will be loaded from the server. I have created a form which loads al the menu items from the DB and gives the field an array based name. I have tried several ways to get the info out of the database but not one of them works. Also when I use print_r($_POST); it gives the information like this: Array ( [name] => Array ( [0] => Home [1] => Buildings [2] => Log-out ) [link] => Array ( [0] => home.php [1] => buildings.php [2] => ../process.php ) [n] => 4 [submit] => Adjust ) Below is the code of the form: <form name="menu-edit" action="include/process.php" method="POST"> <ul id="level3"> <?php //set $n at 1 $n = 1; //query database for all level 3 parent items $sql = $database->connection->prepare("SELECT * FROM ".TBL_MENU." WHERE `userlevel` = '3' AND `parent` = '0' ORDER by `order` ASC"); $sql->execute(); //while loop to iterate the menu items while($row = $sql->fetch()) { echo "<li id='listItem_".$row['id']."'><img src='images/arrow.png' alt='move' width='16' height='16' class='handle' /><input type='text' name='name[]' value='".$row['name']."' /><input type='text' name='link[]' value='".$row['link']."' /></li>"; $n++; } ?> <input type="hidden" name="n" value="<?php echo $n; ?>" /> </ul> <input type="submit" name="submit" value="Adjust" /> </form> Below is what I have now as process.php: <?php //check if user has admin priveleges if(!$session->isUserlevel(9)) { header("Location:../main.php"); } if(!$_POST['submit']) { header("Location:..menu.php"); } print_r($_POST); $n = $_POST['n']; $s = 1; echo "<br>".$_POST['name'.$s]; $name[] = $_POST['name']; foreach($name[] as $key => $value) { echo $key."<br>".$value."<br><br>"; } ?> Hope you can help Quote Link to comment https://forums.phpfreaks.com/topic/265249-form-with-multiple-fields-with-the-same-names/ Share on other sites More sharing options...
PeoMachine Posted July 6, 2012 Share Posted July 6, 2012 I couldnt understand what you need... In your form you will create 2 arrays with theirs keys numerics ($name and $link). You want to do one array with the two informations? Something like that: $menu = array( 0 => array( 'name' => 'Menu Name', 'link' => 'Menu link', ) ); If yes, you can do something like that: <?php // your other stuff here while($row = $sql->fetch()) { echo "<li id='listItem_".$row['id']."'> <img src='images/arrow.png' alt='move' width='16' height='16' class='handle' /> <input type='text' name='menu[".$row['id']."][name]' value='".$row['name']."' /> <input type='text' name='menu[".$row['id']."][link]' value='".$row['link']."' /> </li>"; $n++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265249-form-with-multiple-fields-with-the-same-names/#findComment-1359512 Share on other sites More sharing options...
Ryflex Posted July 6, 2012 Author Share Posted July 6, 2012 Ok that sounds logical but how to extract the data then? Quote Link to comment https://forums.phpfreaks.com/topic/265249-form-with-multiple-fields-with-the-same-names/#findComment-1359597 Share on other sites More sharing options...
Ryflex Posted July 6, 2012 Author Share Posted July 6, 2012 I checked on the internet and tried it with the below. It seems to work. Now I only have to make a loop to iterate trough it echo "<br>".$_POST['menu'][$s]['name']; Quote Link to comment https://forums.phpfreaks.com/topic/265249-form-with-multiple-fields-with-the-same-names/#findComment-1359599 Share on other sites More sharing options...
Ryflex Posted July 6, 2012 Author Share Posted July 6, 2012 For others to use with the same problems in the future. The script that creates the form: <form name="menu-edit" action="include/process.php" method="POST"> <ul id="level3"> <?php //set $n at 1 $n = 1; //query database for all level 3 parent items $sql = $database->connection->prepare("SELECT * FROM ".TBL_MENU." WHERE `userlevel` = '3' AND `parent` = '0' ORDER by `order` ASC"); $sql->execute(); //while loop to iterate the menu items while($row = $sql->fetch()) { echo "<li id='listItem_".$row['id']."'> <img src='images/arrow.png' alt='move' width='16' height='16' class='handle' /> <input type='text' name='menu[".$n."][name]' value='".$row['name']."' /> <input type='text' name='menu[".$n."][link]' value='".$row['link']."' /> <input type='hidden' name='menu[".$n."][id]' value='".$row['id']."' /></li>"; $n++; } ?> <input type="hidden" name="n" value="<?php echo $n; ?>" /> </ul> <input type="submit" name="submit" value="Adjust" /> </form> The script that extracts the information to insert into the database: <?php if(!$_POST['submit']) { header("Location:..menu.php"); } //get the value of $n represents the number of rows $n = $_POST['n']; //loop through every row to update for($s = 1; $s < $n; $s++) { $name = $_POST['menu'][$s]['name']; $link = $_POST['menu'][$s]['link']; $id = $_POST['menu'][$s]['id']; echo "UPDATE ".TBL_MENU." SET `name` = '$name', `link` = '$link' WHERE `id` = '$id'"."<br>"; } ?> Have a good weekend! Quote Link to comment https://forums.phpfreaks.com/topic/265249-form-with-multiple-fields-with-the-same-names/#findComment-1359606 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.