Page 1 of 1

Passing an Array to mysql

Posted: Thu Jan 29, 2004 9:38 pm
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.

Posted: Thu Jan 29, 2004 9:55 pm
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

?>

Posted: Thu Jan 29, 2004 10:32 pm
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!

Posted: Thu Jan 29, 2004 10:34 pm
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.

Posted: Fri Jan 30, 2004 12:23 am
by kanshou
after an hour of missing a FRIGGIN TYPO.. the implode worked just fine. Thanks for all your help.