RE: Adding Function to Code

I would like to ad this function to the following php code but, not sure how to do it so. THe code converts csv to MySql on export from OpenOffice Calc.

Code wanting included to strip out apostrophes

[php]function clean_up( $text ) {
$unwanted = array("’"); // add any unwanted char to this array
return str_ireplace( $unwanted, ‘’, $text );
}[/php]

[php]<?php
echo ‘’;
echo ‘’;
echo ‘CSV to SQL convertor’;
echo ‘’;
echo ‘’;

// Parse incoming information if above form was posted
if($_POST[ref] == “csv2sql”)
{
echo “

SQL Query Output:

”;
// Get information from form & prepare it for parsing
$table_name = $_POST[table_name];
$csv_data = $_POST[csv_data];
$csv_array = explode("\n",$csv_data);
$column_names = explode(",",$csv_array[0]);
// Generate base query
$base_query = “INSERT INTO $table_name (”;
$first = true;
foreach($column_names as $column_name)
{
    if(!$first)
    $base_query .= ", ";
    $column_name = trim($column_name);
    $base_query .= "`$column_name`";
    $first = false;
}

$base_query .= ") ";
// Loop through all CSV data rows and generate separate
// INSERT queries based on base_query + the row information
$last_data_row = count($csv_array) - 1;

for($counter = 1; $counter < $last_data_row; $counter++)
{
    $value_query = "VALUES (";
    $first = true;
    $data_row = explode(",", $csv_array[$counter]);
    $value_counter = 0;

    foreach($data_row as $data_value)
    {
        if(!$first)
        $value_query .= ", ";
        $data_value = trim($data_value);
        $value_query .= "'$data_value'";
        $first = false;
    }

    $value_query .= ")";
    // Combine generated queries to generate final query
    $query = $base_query .$value_query .";";
    echo "$query <br />";
}

}

echo ‘

CSV to SQL convertor

’;

// Input form begin
echo ‘’;
echo ‘’;
echo 'Name of table:
';
echo ‘’;
echo ‘
’;
echo ‘
’;
echo ‘CSV data:
’;
echo ‘’;
echo ‘
’;
echo ‘
’;
echo ‘’;
echo ‘’;

// Input form end
echo ‘’;
echo ‘’;
?>[/php]

Try this:

[php]<?php
echo ‘’;
echo ‘’;
echo ‘CSV to SQL convertor’;
echo ‘’;
echo ‘’;

// Parse incoming information if above form was posted
if($_POST[ref] == “csv2sql”)
{
echo “

SQL Query Output:

”;
// Get information from form & prepare it for parsing
$table_name = clean_up($_POST[table_name]);
$csv_data = clean_up($_POST[csv_data]);
$csv_array = explode("\n",$csv_data);
$column_names = explode(",",$csv_array[0]);
// Generate base query
$base_query = “INSERT INTO $table_name (”;
$first = true;

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service