Jump to content

Extracting data from a multidimensional array and populating db with the values


conork

Recommended Posts

Hi all,

 

I hope someone can help me out here as I think I am close but am not sure where to go from here.  The problem is:

I have a cms where a user can see a list of photos in the gallery.

I provide them the ability to select what photos they want to appear on a particular webpage.

The problem I have is that the user wants to be able to put the photos in whatever order they choose.

 

So, I have a form (see attached), the html code is:

 

          <table width="566px" cellspacing="0" border="0">              <tr>

                <td width="33%">

                  <div class="serviceCell">

<input type='checkbox' checked='checked' name='photos[][0]' value='119' /> Bathroom Sin...

<img src="./images/help.gif" title="Enter an integer to signify the order in which this image will appear on the webpage." alt="Enter an integer to signify the order in which this image will appear on the webpage."/> <input type="text" size="2"  name="photos[][1]" value="0"/>                  </div>

                </td>

                <td width="33%">

                  <div class="serviceCell">

<input type='checkbox' checked='checked' name='photos[][0]' value='113' /> Courtyard 1

<img src="./images/help.gif" title="Enter an integer to signify the order in which this image will appear on the webpage." alt="Enter an integer to signify the order in which this image will appear on the webpage."/> <input type="text" size="2"  name="photos[][1]" value="0"/>                  </div>

                </td>

                <td width="33%">

                  <div class="serviceCell">

<input type='checkbox' name='photos[][0]' value='114' /> Courtyard 2

<img src="./images/help.gif" title="Enter an integer to signify the order in which this image will appear on the webpage." alt="Enter an integer to signify the order in which this image will appear on the webpage."/> <input type="text" size="2"  name="photos[][1]" value=""/>                  </div>

                </td>

              </tr>...

 

Now, lets assume that the user ticked the box next to 'Courtyard 2' and entered an integer '1' in the input box to signify that this image should be displayed 1st on the chosen webpage.  These 2 values go into the multidimensional array (photoid, ranking) and are submitted in the form to php in the photos[][] array.

 

But here is where I have the problem.  I am trying to extract the values from the array and then do an insert to a table in mysql.  My syntax must be wrong somewhere so I am asking anyone to please offer any advice:

 

  if (isset($_REQUEST['submitted'])){ 

      $photos = $_REQUEST['photos'];

...

 

    if(!empty($photos)){

      foreach($photos as $photoID){

        $sql = "INSERT IGNORE INTO menuItemPhotos

                SET photoid='$photoID[0]', menuItemid='$id', ranking='$photoID[1]'";

        $ok = @mysql_query($sql); // execute the query

      } // End of foreach($photos as $photoID){

    } // End of if(!empty($photos)){

 

Thanks

Conor

 

[attachment deleted by admin]

Link to comment
Share on other sites

You're suppressing errors and not making sure mysql_query() didn't return false. Are you surprised you aren't seeing errors?

 

Echo your queries. Are they populated and formatted as you'd expect? If so, verify that mysql_query() returned true. If it didn't, you should echo mysql_error().

Link to comment
Share on other sites

The names of your checkboxes and input fields are all messed up. If you view your POST data you will see.

For a better way to name them I would use this..

<div class="serviceCell">
<input type='checkbox' checked='checked' name='photos[119][]' value='1' /> Bathroom Sin...
<img src="./images/help.gif" title="Enter an integer to signify the order in which this image will appear on the webpage." alt="Enter an integer to signify the order in which this image will appear on the webpage."/> 
<input type="text" size="2"  name="photos[119][]" value="0"/>
</div>

Then in PHP

foreach($photos as $photoID=>$values){
// $values[0] can be ignored as the ID is the array key //
$sql = "INSERT IGNORE INTO menuItemPhotos
                SET photoid='$photoID', menuItemid='$id', ranking='$value[1]'";
$ok = mysql_query($sql);
}

Link to comment
Share on other sites

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.