Jump to content

[SOLVED] Sorting/extracting associative array according to keys?


Chotor

Recommended Posts

I get this (example) array from a $_POST and need to prepare it for easy entry into a DB.

 

[title0] => Iron Man

[year1] => 2008

[country2] => US

[director3] => Jon Favreau

[genre4] => Sci-Fi

[genre5] => Action

[actor7] => Robert Downey Jr.

[actor9] => Gwyneth Paltrow

[actor11] => Terrence Howard

[runningtime12] => 126

[actor13] => Jeff Bridges

[genre15] => Adventure

(The structure is like this: User can enter info in any of these categories, so the sorting is arbitrary. For each item entered, the counter will increase by 1. User can also remove elements, hence the "missing numbers". I could probably make another algorithm for constructing the keys if anyone has a better idea)

 

Now I need a good/efficient way of extracting these values accoring to keys for entry into the DB. Same key elements go together.

The different elements will be entered into different tables.

 

Preferably I'd probably like one separate array or similar for each 'category'. Also I then need some tips on how to enter these arrays into a DB.

 

Link to comment
Share on other sites

I really don't think I'm following your question so I'm sorry for the simple answer that your probably not looking for.

 

$val = $arr['title0']; //$val == 'Iron Man'

$val = $arr['year1']; //$val == 2008

 

From what I'm looking at...people are entering more than one year, country, actor, etc...

 

Why not separate them all into their own arrays?

form could look something like:

Title 1: <input type='text' name='title[]' />

Title 2: <input type='text' name='title[]' />

 

Actor 1: <input type='text' name='actor[]' />

Actor 2: <input type='text' name='actor[]' />

 

etc...

 

then on the server side you could loop through each separate array like this:

 

//loop through actors

if (isset($_POST['actor'])) {

  //count actors

  $cnt = count($_POST['actors']);

  for ($i = 0; $i < $cnt; $i++) {

      $actors[] = mysql_real_escape_string(htmlentities(strip_tags($_POST['actor'][$i])));

  }

}

 

//loop through other arrays accordingly, then construct your query ;)

 

Hope that helps

Link to comment
Share on other sites

Thanks. Yes, you are correct. People can add more than one year, country, actor.. In fact, they can add as many as the want in the order they want.

I'm not sure if your suggestion can be done.

The input is dynamically created HIDDEN INPUT, and it's the same JS function for all types.

 

This is the code that generates the input:

var inputelement = document.createElement('input');
inputelement.setAttribute('type', 'hidden');
inputelement.setAttribute('name', elem+i);
inputelement.setAttribute('value' , getinput.value);
document.getElementById('content').appendChild(inputelement);

Maybe I could do something with my "elem+i" ?

Will that be the most efficient way to get ready data for MySQL?

 

And later on, when I have my $actors[], $countries[], $years[], etc., how can I easily add them to the DB? (read: looking for PHP functions to insert arrays, and also avoid inserting duplicate values)

Link to comment
Share on other sites

  • 2 weeks later...
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.