Click to See Complete Forum and Search --> : how to manage a text file with php


luvv2rock
03-28-2007, 04:24 PM
I posted in the sql forum and now realize I don't know at ALL what I'm doing. I work for a car dealership and am trying to build a page for our used car inventory. Our sticker company (people who take pics of vehicles) will be uploading a pipe delimited text file with all of the vehicles and information onto my server. I need to know how to connect to the text file and then manipulate the data to form a sortable/searchable table of vehicles. Then I need that to build a page that has the vehicle information so that when you click a car on the table it takes you to the info page. Examples can be found at autotrader.com cars.com or any car dealership site almost heh. Thank you guys.

DARTHTAMPON
03-28-2007, 04:34 PM
that depends are all the rows in the text file the same?

make|model|year|capicity
make1|model1|1993|8
make2||1994|2

if a coulumn does not exist how is it handled?

If everthing is the same then you could just $carData = explode("|", $filerow);

insert into tablename values($carData[0],$carData[1],$carData[2],$carData[3])

luvv2rock
03-28-2007, 04:48 PM
everything is the same i still don't understand really how to make a connection to the file

DARTHTAMPON
03-28-2007, 04:56 PM
$db_host = 'localhost'; #set up db info
$db_name = 'your_site';
$db_user = 'username';
$db_pass = 'password';

mysql_connect($db_host, $db_user, $db_pass); #connect to db
mysql_select_db($db_name); #select what db you want to use



$fh = fopen("cars.txt", 'r') or die("Can't open file"); #open file
while(!feof($fh)) #go through file until the end
{
$fileRow = fgets($fh); #get whatever row you are on
$carData = explode("|", $filerow); #put data into an array
$sqlStatement = "insert into table values ($carData[0],$carData[1],$carData[2],$carData[3])"; #create sql statement
mysql_query($query); #run query
}
mysql_close(); #close db connection
fclose($fh); # close file

NightShift58
03-29-2007, 04:17 AM
<?php
$db_host = 'localhost'; #set up db info
$db_name = 'your_site';
$db_user = 'username';
$db_pass = 'password';

mysql_connect($db_host, $db_user, $db_pass); #connect to db
mysql_select_db($db_name); #select what db you want to use

IF ($arrFILE = file("cars.txt")) : #load file into array
FOREACH ($arrFILE as $key => $carData) :
$sql = "insert into table values ('" . str_replace("|", "','", $carData) . "')";
mysql_query($query) or die("SQL Error: $sql<br>" . myql_error());
ENDFOREACH;
ENDIF;
?>