Jump to content

[SOLVED] need help imploding a multidimensional array for insert into database


vynsane

Recommended Posts

i've got a form that i want to use to insert a multidimensional array into a database table. i just need to know the syntax of the implode function to get it to break the array down. this is the meat of my form:

 

       
       echo "\t\t\t\t\t<tr>\n";
       echo "\t\t\t\t\t\t<td class=\"textright\"><a href=\"http://".$baseURL."/database/issue.php?title=".$title."&vol=".$Vol."&issue=".$row['issueNum']."\" target=\"blank\"><strong>".$row['issueNum']."</strong></a><input type=\"hidden\" name=\"collection[]\" value=\"".$row['ID']."\" /></td>\n";
       echo "\t\t\t\t\t\t<td class=\"center\">";
       echo "\t\t\t\t\t\t\t<input type=\"text\" name=\"collection[]\" value=\"0\" style=\"width:20px;\" />\n";
       echo "</td>\n";
       echo "\t\t\t\t\t\t<td class=\"center\">";
       echo "\t\t\t\t\t\t\t<select name=\"collection[]\">\n";
       echo "\t\t\t\t\t\t\t\t<option selected=\"selected\" value=\"\">Choose...</option>\n";
       echo "\t\t\t\t\t\t\t\t<option value=\"M\">Mint</option>\n";
       echo "\t\t\t\t\t\t\t\t<option value=\"NM\">Near Mint</option>\n";
       echo "\t\t\t\t\t\t\t\t<option value=\"VF\">Very Fine</option>\n";
       echo "\t\t\t\t\t\t\t\t<option value=\"F\">Fine</option>\n";
       echo "\t\t\t\t\t\t\t\t<option value=\"P\">Poor</option>\n";
       echo "\t\t\t\t\t\t\t\t<option value=\"VP\">Very Poor</option>\n";
       echo "\t\t\t\t\t\t\t</select>\n";
       echo "</td>\n";
       echo "\t\t\t\t\t\t<td class=\"center\">";
       echo "\t\t\t\t\t\t\t$<input type=\"text\" name=\"collection[]\" style=\"width:50px;\" />\n";
       echo "</td>\n";
       echo "\t\t\t\t\t\t<td class=\"center\"><input type=\"text\" name=\"collection[]\" /></td>\n";
       echo "\t\t\t\t\t\t<td class=\"center\"><input type=\"text\" name=\"collection[]\" /></td>\n";
       echo "\t\t\t\t\t</tr>\n";

 

that table row is echoed while php runs an array of comic book issues. the different inputs are the quantity of copies of the issue, the condition of the copy, the purchase price, purchase location and purchase date. there is also a hidden field with the comic's ID number. i want to create a multidimensional array that splits those values off at each  repeat, so it looks something like this:

 

Array (
     [collection] => Array
        (
            [0] => Array
                      (
                         [0] => 300
                         [1] => 1
                         [2] => NM
                         [3] => 3.00
                         [4] => local comic book store
                         [5] => 2007-01-01
                       )
            [1] => Array
                      (
                         [0] => 301
                         [1] => 1
                         [2] => VF
                         [3] => 1.50
                         [4] => local comic book store
                         [5] => 2007-01-01
                       )
          )
    [submit] => Submit
)

 

i found code that will implode the values,

 


      $collectionArray = '*'.implode('*',$_POST['collection']).'*';

 

but i can't get it to be multidimensional... it turns every [collection] value into a node of the [collection] array, like this:

 

    [collection] => Array
        (
            [0] => 300
            [1] => 1
            [2] => NM
            [3] => 3.00
            [4] => local comic book store
            [5] => 2007-01-01
            [0] => 301
            [1] => 1
            [2] => VF
            [3] => 1.50
            [4] => local comic book store
            [5] => 2007-01-01
          )
    [submit] => Submit
)

 

how can i get this implode to create a second delimiter that separates each set of values to get the desired effect? the end result right now (as inserted into the DB) looks like this:

 


*300*1*NM*3.00*local comic book store*2007-01-01*301*1*VF*1.50*local comic book store*2007-01-01*

 

i would like to be able to delimit it another way, like this:

 


*300~1~NM~3.00~local comic book store~2007-01-01*301~1~VF~1.50~local comic book store~2007-01-01*

 

so that when i try to explode the values when retrieving it from the database, i will be able to break it up into an array based on those delimiters... any help would be awesome.

Why bother with a database if you're storing it like

 

*300~1~NM~3.00~local comic book store~2007-01-01*301~1~VF~1.50~local comic book store~2007-01-01*

 

May as well just use a text file.

try

<?php
$collection = array
        (
            array
                      (
                         0 => 300,
                         1 => 1,
                         2 => 'NM',
                         3 => 3.00,
                         4 => 'local comic book store',
                         5 => '2007-01-01'
                       ),
            array
                      (
                         0 => 301,
                         1 => 1,
                         2 => 'VF',
                         3 => 1.50,
                         4 => 'local comic book store',
                         5 => '2007-01-01'
                       )
          );

echo '<pre>';
foreach ($collection as $ar) {
    echo '*' . join ('~', $ar) . "*\n";
}
echo '</pre>';
?>

 

-->[pre]

*300~1~NM~3~local comic book store~2007-01-01*

*301~1~VF~1.5~local comic book store~2007-01-01*

 

 

[/pre]

okay, maybe i'm not making myself totally clear, but first i need to get my array to look like that... right now it isn't multidimensional... it looks like this:

 

   [collection] => Array
        (
            [0] => 300
            [1] => 1
            [2] => NM
            [3] => 3.00
            [4] => local comic book store
            [5] => 2007-01-01
            [0] => 301
            [1] => 1
            [2] => VF
            [3] => 1.50
            [4] => local comic book store
            [5] => 2007-01-01
            ...
          
            ...
            [243] => 2007-01-01
          )
    [submit] => Submit
)

 

i have a feeling that my form isn't setup correctly to achieve the dimensions i need in the array. i've been looking everywhere to find a suitable tutorial to no avail.

 

i HAVE, however, found the "serialize()" function, but i don't know to much about it and it's not intuitive to look at to figure out whether or not it's the answer.

 

anyway, the original place i found anything about using the "implode()" function with inserting an array to the DB was here:

 

http://evolt.org/node/60222#comment-61027

 

<?php
$ColorsInsert = '*'.implode('*',$_POST['colors']).'*';
$Query = "INSERT INTO colors VALUES ($ColorsInsert)";
?> 

 

but since it's only dealing with this one-dimensional array ("colors"), it's not the exact answer. i THINK the only thing i need to do is add another delimiter around each subset to achieve this:

 

*300~1~NM~3.00~local comic book store~2007-01-01*301~1~VF~1.50~local comic book store~2007-01-01*

 

instead of this:

 

*300*1*NM*3.00*local comic book store*2007-01-01*301*1*VF*1.50*local comic book store*2007-01-01*

 

so that i can explode the first example into the subsets

 

300~1~NM~3.00~local comic book store~2007-01-01

 

and

 

301~1~VF~1.50~local comic book store~2007-01-01

 

and then explode that into

 

300
1
NM
3.00
local comic book store
2007-01-01

 

but at this point i don't even know if this is the answer. even if it works i don't now how malleable the data output will be, say to populate another page.

 

do you mean this?

 

<?php
$collection = array
        (
            300,
            1,
            'NM',
            3.00,
            'local comic book store',
            '2007-01-01',
            301,
            1,
            'VF',
            1.50,
            'local comic book store',
            '2007-01-01'
          );
          
$k = count($collection);

echo '<pre>';
for($i=0; $i<$k; $i+=6) {
    $ar = array_slice($collection,$i,6);
    echo '*' . join ('~', $ar) . "*\n"; 
}
echo '</pre>';
?>

on second thought i'm going to go a different route and put individual rows in the table for each comic issue with different columns for the different values. it will just be easier to edit later on. thanks for you help nonetheless.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.