Jump to content


Photo

Combining variables into an array


  • Please log in to reply
4 replies to this topic

#1 steve m

steve m
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 23 October 2006 - 03:54 PM

Hi All,

I have this problem, I'm not even sure if this can be done.  I have this form where I have a drop down list for a quantity of serial numbers.  When a user selects a numbers from that list I have some JavaScript to dynamically create the number of Text Boxes selected.  In the JavaScript, the name of the text box is serial_num and a number is added at the end of the name ex. serial_num0, serial_num1, etc...  Anyways, what I want to do is to take the serial numbers from the form and create a separate record for each in a MySQL database.

The problem I am having is that since the serial_num fields have numbers at the end of the field names I can't seem to come up with some kind of loop to separate those different serial numbers.  Is there a way I can combine all of those serial number fields into an array to try to get the results that I want?  If that can be done, I'm lost on how to do that coming from a form submission.


#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 23 October 2006 - 04:03 PM

Sure, rather than using javascript to call the fields serial_num0 serial_num1 etc, get it to call all the fields the same thing.  Get it to call them all serial_num[].

Then php can access them by using the following:

<?php
foreach ($_REQUEST['serial_num'] as $serial){
   echo "$serial<br>\n";
}
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 important

important
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 23 October 2006 - 04:08 PM

Create a text-field as "fields_count" and when fields are added/removed using javascript, this field value should be updated accordingly. Then you can run a loop such as this:

$fields_count = intval($_POST['fields_count']);
for ($i = 0; $i <= count($fields_count); $i++)
{
      if (empty($_POST['serial_num'.$i])) {
            continue;
      }

      // or here, do anything you like with the data now... it's in $_POST['serial_num' . $i] ....
}


#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 23 October 2006 - 04:11 PM

Huggie's suggestion, IMHO, is going to be your best bet here since it won't matter how many additional fields they add. You just need to run a count() on the $_POST['serial_num'] field to see how many they submitted.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 steve m

steve m
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 23 October 2006 - 04:19 PM

Great! Thanks HuggieBear that did the trick. 

Thanks a lot to everyone’s comments.  That helped me out a lot!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users