Passing an Array to mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kanshou
Forum Commoner
Posts: 47
Joined: Sun Aug 03, 2003 1:57 pm
Location: San Diego
Contact:

Passing an Array to mysql

Post by kanshou »

Ok, I have a few check boxes. I want them to be stored in a table, and I am using the following code to do it. The only problem is - all the table ever stores is "Array". If you can tell me how to store it as ANYTHING other than "Array", that would just make my day!

Code: Select all

<?php
// -- setup of the custom values -- 
$site_extras = array (
   array (
     "ServerName",       // unique name of this extra field (used in db)
     "Server Name",           // how this field will be described to users
     $EXTRA_SELECTLIST,    // type of field
                           // List of options (first will be default)
     //array ( "test1", "test2" ),  (Unused)
     0                     // arg 2 (unused)
   )
 );


// --- snippet: Input checkboxes ---
 if ( $extra_type == $EXTRA_SELECTLIST ) {
    // show custom select list.
    echo "<input type="checkbox" name="" . $extra_name . "[]" value="test1">Test 1";
     echo "<input type="checkbox" name="" . $extra_name . "[]" value="test2">Test 2";

//--- mySQL handling ---
for ( $i = 0; $i < count ( $site_extras ) && empty ( $error ); $i++ ) 
{
    $sql = "";
    $extra_name = $site_extras[$i][0];
    $extra_type = $site_extras[$i][2];
    $extra_arg1 = $site_extras[$i][3];
    $extra_arg2 = $site_extras[$i][4];
    $value = $$extra_name;
     if ( strlen ( $$extra_name ) || $extra_type == $EXTRA_DATE ) 
         {
         if ( $extra_type == $EXTRA_SELECTLIST  ) 
           {
$sql = "INSERT INTO webcal_site_extras " .
          "( cal_id, cal_name, cal_type, cal_data ) VALUES ( " .
          "$id, '$extra_name', $extra_type, '$value' )";
           }
         }
}
?>
Thanks in advance for any help!! This thing is gonna put me in an ol' folks home before too much longer.
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

You can implode() that array into a string separated by commas. Then insert the string.

http://ca3.php.net/manual/en/function.implode.php

Code: Select all

<?php

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

?>
User avatar
kanshou
Forum Commoner
Posts: 47
Joined: Sun Aug 03, 2003 1:57 pm
Location: San Diego
Contact:

Post by kanshou »

I'm not really sure how you mean. from the example, it looks like it would be laid out like this.

Code: Select all

<?php
 $extra_name = array(/*? I don't know what goes here*/);
$comma_sep = implode(",", $extra_name);

$sql = "INSERT INTO webcal_site_extras " . 
          "( cal_id, cal_name, cal_type, cal_data ) VALUES ( " . 
          "$id, '$extra_name', $extra_type, '$comma_sep')"; 

?>
It seems to me like that would work fine for something like static fields, but maybe I am not understanding something.

I don't know what goes into the array area for ----

Code: Select all

$extra_name = array(/*? I don't know what goes here*/);
Thanks in advance for the help!
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

I believe from your code $extra_name is already an array so that line regarding array() you can ignore.

That was just an example to show that it's input is an array.
User avatar
kanshou
Forum Commoner
Posts: 47
Joined: Sun Aug 03, 2003 1:57 pm
Location: San Diego
Contact:

Post by kanshou »

after an hour of missing a FRIGGIN TYPO.. the implode worked just fine. Thanks for all your help.
Post Reply