On Wed, 2003-06-11 at 15:55, jtjohnston wrote:
> Can someone see clearly through what I'm trying to do please?
> I've been at this for 4 hours trying to see where I have gone wrong.

[snipped]

Might something like this work? 


<?php
error_reporting(E_ALL);

$var = array (
 'TI' => array (
 'Description' => 'Title: (TI)',
 'Option'      => 'ST',
 ),
 'AU' => array (
 'Description' => 'Author(s): (AU)',
 'Option'      => 'AU',
 ),
 'PB' => array (
 'Description' => 'Publication Information: (PB)',
 'Option'      => 'BT',
 ),
 'SOM' => array (
 'Description' => 'Source (Bibliographic Citation): (SO)',
 'Option'      => 'JR',
 )
);

$filename = 'test_enreg.txt';
$fh = fopen($filename, 'r');
$fileContents = fread($fh, filesize($filename));

$lines = explode("Enregistrement", $fileContents);
$dbtable = 'test_table';

foreach($lines as $line) 
{
    $line = stripslashes($line);
    $line = str_replace("\r", "", $line);
    $newlines = explode("\n", $line);

    // Init to keep clean--if you really need them as separate scalars,
    // try extract()ing $record, although they'll get overwritten on
    // each iteration.
    $record = array('ST' => '', 'AU' => '', 'BT' => '', 'JR' => '');
    $found = false; // Just a flag to avoid empty SQL statements.
    foreach($newlines as $newline)
    {
        if (empty($newline))
        {
            continue;
        }
        list($key, $val) = explode(':', $newline);
        if (isset($var[$key]['Option']))
        {
            // Flag that we got one this time round.
            $found = true;
            // Get the translated name from $var.
            $record[$var[$key]['Option']] = trim($val);
        }
    }#end of foreach($newlines as $newline)

    if ($found) 
    {
        $sql = "INSERT INTO $dbtable (ST, AU, BT, JR) VALUES
('{$record['ST']}', '{$record['AU']}', '{$record['BT']}',
'{$record['JR']}')";
        echo "$sql\n";
    }
}#end of foreach($lines as $line)

?>

-- 
 Torben Wilson <[EMAIL PROTECTED]>                        +1.604.709.0506
 http://www.thebuttlesschaps.com          http://www.inflatableeye.com
 http://www.hybrid17.com                  http://www.themainonmain.com
 -----==== Boycott Starbucks!  http://www.haidabuckscafe.com ====-----




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

Reply via email to