Chotor Posted May 9, 2008 Share Posted May 9, 2008 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. Quote Link to comment Share on other sites More sharing options...
mlin Posted May 9, 2008 Share Posted May 9, 2008 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 Quote Link to comment Share on other sites More sharing options...
Chotor Posted May 9, 2008 Author Share Posted May 9, 2008 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) Quote Link to comment Share on other sites More sharing options...
mlin Posted May 10, 2008 Share Posted May 10, 2008 that's the most effiecient solution I can think of. Not sure if it would work, but you might wanna try: inputelement.setAttribute('name', elem+'[]'); hopefully that does the trick for you Quote Link to comment Share on other sites More sharing options...
Chotor Posted May 22, 2008 Author Share Posted May 22, 2008 Thanks! That was exactly the trick I needed! Works perfectly. Each and every category is saved in it's own array. Thank you. Quote Link to comment 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.