You can either put a flag indicating your first time around the field loop 
and add the comma before each field, when it is not the first time around, 
or build the fieldname and fieldvalue list on a string and then trim out the 
last character once the loop is finished.


"Rahul S. Johari" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

Ave,

Iıve written a code that is able to extract the Column names and Records
from a simple dbf (foxpro) file and create an INSERT INTO sql statement
which can be used to insert all those records with their corresponding field
names in an existing mySQL table. (A CREATE TABLE code I wrote is able to
create the table from the dbf file information).

Following is the code I wrote for creating the INSERT INTO sql:

<?php
            $db_path = "$DATABASEFILE";
            $dbh = dbase_open($db_path, 0) or die("Error! Could not open
dbase database file '$db_path'.");
            if ($dbh) {

            #Get the Information
            $column_info = dbase_get_header_info($dbh);
            $record_numbers = dbase_numrecords($dbh);

            #Run the loop for all the records in the Table
            for ($i = 1; $i <= $record_numbers; $i++) {
            $row = dbase_get_record_with_names($dbh, $i);

            echo "INSERT INTO ".substr($DATABASEFILE_name,0,-4)." (";

            #Run the loop for all the fields in the Table
            foreach ($column_info as $v1) {    echo "$v1[name],";    }

            echo ") VALUES (";

            #Run the loop for all the values corresponding to fields in the
Table
            foreach ($column_info as $v1) {    echo
"'".trim($row[$v1[name]])."',";    }

            echo "'); <br>";

            }
            }
            dbase_close($dbh);
?>

It works fine, except for one problem. Itıs able to create the INSERT INTO
sql statement, with all the fields and corresponding values, but as Iım
running a loop for both the fields names, and the values corresponding to
fields names, it leaves a comma after the records are over.

So instead of having this : INSERT INTO tblname (c1,c2,c3) VALUES
(Ov1ı,ıv2ı,ıv3ı);
I achieve this : INSERT INTO tblname (c1,c2,c3,) VALUES (Ov1ı,ıv2ı,ıv3ı,ı);

Notice an additional Comma after column names, and an additional ,ı after
the values. Iım not quite sure what to do to get rid of those. Iıve tried
some different combinations using different kind of logic with the echo
statements, but itıs not working out. Would love some help.

Thanks,

Rahul S. Johari
Coordinator, Internet & Administration
Informed Marketing Services Inc.
251 River Street
Troy, NY 12180

Tel: (518) 266-0909 x154
Fax: (518) 266-0909
Email: [EMAIL PROTECTED]
http://www.informed-sources.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to