Jump to content

Recommended Posts

Hi, all...

 

I have a form that is dynamically generated by the fields I have in a table. This table generates the navigation for my site, and I want to create a page that will allow me to reorder and hide/show and activate/deactivate the links. The form is working and I have the form generating an array based on the data collected on submit. I need to know how to break this data apart so I can update my site navigation table with the correct values. The table looks like this:

 

| ID | orderNum | sectionName | link | visible | active |

 

the ID is just a primary auto increment. The orderNum is one of the cells that needs to be updated by the form, as are "visible" and "active". orderNum has a value anywhere between "1" and "12" right now (but could be more depending on sections added...) and visible and active only have a value of either "Y" or "N" (for yes or no, natch...)

 

I have a form that just outputs the array onscreen after submit, and the array looks like this:

 

Array
(
    [orderNum] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
            [8] => 9
            [9] => 10
            [10] => 11
            [11] => 12
        )

    [visible] => Array
        (
            [0] => Y
            [1] => Y
            [2] => Y
            [3] => Y
            [4] => N
            [5] => Y
            [6] => Y
            [7] => Y
            [8] => Y
            [9] => N
            [10] => N
            [11] => N
        )

    [acitve] => Array
        (
            [0] => Y
            [1] => Y
            [2] => Y
            [3] => Y
            [4] => Y
            [5] => Y
            [6] => Y
            [7] => Y
            [8] => Y
            [9] => Y
            [10] => Y
            [11] => Y
        )

    [submit] => Update Navigation
)

 

obviously, the "0's" go together (1, Y, Y), the "1's" (2, Y, Y), the "5's" (4, N, Y) and so on... how can I break this data apart so as to insert/update them into the correct record?

<?php
foreach ($data['orderNum'] as $k = $ono) {

          $visible = $data['visible'][$k];
          $active = $data['active'][$k];

          // update using $ono, $visible, $active
}
?>

Okay, cool. Looks pretty straight-forward. Should I use a hidden "ID" input to WHERE against in my UPDATE?

 

EDIT: Hrm... getting an error:

 

Parse error: syntax error, unexpected '=', expecting ')' in path/to/sitenav.php on line 31

 

that's this line:

 

      foreach ($data['orderNum'] as $k = $orderNum) {

 

(I changed $ono to $orderNum just so as to keep things straight in my head...)

Ah, yes, that did the trick. What about adding the ID as a WHERE in the UPDATE? Like this...

 

      foreach ($data['orderNum'] as $k => $orderNum) {
          $visible = $data['visible'][$k];
          $active = $data['active'][$k];
          $ID = $data['ID'][$k];

      $insert = "UPDATE (table name) SET
          orderNum='$orderNum',
          visible='$visible',
          active='$active'
          WHERE ID = $ID";

 

I'm getting an error on submission of the form:

 

Warning: Invalid argument supplied for foreach() in /path/to/sitenav.php on line 31

 

Should I be using "$_POST" instead of "$data"?

It's not updating the table... my "if (mysql_query($insert)) is telling me that the insert is working, but nothing is getting changed. I have the entire thing set up like this at the moment:

 

   if (isset($_POST['submit'])){
      print_r($_POST);
      foreach ($_POST['orderNum'] as $k => $orderNum) {
          $visible = $_POST['visible'][$k];
          $active = $_POST['active'][$k];
          $ID = $_POST['ID'][$k];

          // update using $orderNum, $visible, $active
        $insert = "UPDATE (table name) SET
          orderNum='$orderNum',
          visible='$visible',
          active='$active'
          WHERE ID = $ID";
      }
}

 

It's just not updating anything. I no longer got the error above

 

Warning: Invalid argument supplied for foreach() in /path/to/sitenav.php on line 31

 

using "$_POST" instead of "$data", but should I go back to "$data"?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.