Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open a csv and read into an array

Status
Not open for further replies.

someoneneedshelps

Technical User
Mar 3, 2016
75
0
0
GB
Hi Guys,

Im not that bright with php all those hidden functions, I want to open a csv file, get the tope line which is the titles and the rest is data

Code:
user,title,subtitle,starts,description,pict_url,category,secondcat,minimum_bid,shipping_cost,shipping_cost_additional,reserve_price,buy_now,auction_type,duration,increment,shipping,payment,international,ends,current_bid,closed,photo_uploaded,quantity,suspended,relist,relisted,num_bids,sold,shipping_terms,bn_only,bold,highlighted,featured,current_fee,tax,taxinc,asking,item_condition,item_manufacturer,item_model,item_colour,item_year
4, 1999 Vauxhall Astra Esate Diesel, Car, 1459934546, <p>\r\n MOT Status Ends : 09 Nov 2016</p>\r\n<p>\r\n Mileage 164409 miles</p>\r\n<p>\r\n Diesel Run for ever isuzu engine astra estate, needs cleaning inside, does not smell but needs a clean hence price. Long mot nov 2016 V5 with me</p>\r\n, test2.jpg, 16, 0, 0.99, 0.00, 0.00, 250.00, 0.00, 1, 21, 0.00, 3, paypal,0, 1461748946, 0.00, 0, 0, 1, 0, 2, 0, 0, n, , n, n, n, n, 0.00, n, y, NULL, Used, vauxhall, astra, blue, 1999

Do I want two arrays or a two dimensional array, I would then like to compare the titles to the database before creating an insert statement

Code:
$types = array(
	'user' => 'int',
	'title' => 'text',
	'subtitle' => 'text',
	'starts' => 'time',
	'description' => 'text',
	'pict_url' => 'image',
	'category' => 'int',
	'secondcat' => 'int',
	'minimum_bid' => 'float',
	'shipping_cost' => 'float',
	'shipping_cost_additional' => 'float',
	'reserve_price' => 'float',
	'buy_now' => 'float',
	'auction_type' => 'digitnory',
	'duration' => 'int',
	'increment' => 'float',
	'shipping' => 'yorn',
	'payment' => 'pay',
	'international' => 'yorn',
	'ends' => 'int',
	'current_bid' => 'float',
	'closed' => 'digityorn',
	'photo_uploaded' => 'digityorn',
	'quantity' => 'int',
	'suspended' => 'int',
	'relist' => 'int',
	'relisted' => 'int',
	'num_bids' => 'int',
	'sold' => 'yorn',
	'shipping_terms' => 'text',
	'bn_only' => 'yorn',
	'bold' => 'yorn',
	'highlighted' => 'yorn',
	'featured' => 'yorn',
	'current_fee' => 'float',
	'tax' => 'yorn',
	'taxinc' => 'yorn',
	'asking' => 'float',
	'item_condition' => 'text',
	'item_manufacturer' => 'text',
	'item_model' => 'text',
	'item_colour' => 'text',
	'item_year' => 'text'	
);

Code:
function mysql_insert_array($table, $data) {
	
	foreach ($data as $field=>$value) {
		$fields[] = '`' . $field . '`';
		if (is_array($value)) {
		$arr = '';
		foreach ($value as $key1=>$value1){
		//$arr .= $key1 .'-'. $value1 .'<br>';
		}
		$values[] = "'" . mysql_real_escape_string($arr) . "'";
		
		} else
		$values[] = "'" . mysql_real_escape_string($value) . "'";
		
	}
	$field_list = join(',', $fields);
	$value_list = join(',', $values);
	
	$query = "INSERT INTO " . $table . " (" . $field_list . ") VALUES (" . $value_list . ")";
	
	return $query;
}

I then ties together with the above and place a switch inside this function to find the types in order to wrap in quotes or not, thxs in advance
 
I would suggest that it would be simpler and less risky to create the table and specify all the columns first, then import the CSV using the MySQL LOAD DATA INFILE query, that way MySQL or PHP do not have to 'guess' the column data types


And a "yorn" datatype in MySQL is a ENUM type

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Sounds complicated, I can get away from titles out of csv but the data has to be processed in a csv. thxs
 
but the data has to be processed in a csv. thxs


LOAD DATA INFILE will take the data from a CSV file, you are simply cutting out the PHP "middle man", which by your own admission are "not that bright at".


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
There's no hidden functions. All functions are perfectly documented in the PHP online manual. It can be found at:
You can read a file, into PHP using the file() function.

From there, since the first row is going to contain the titles, you could do something like:

Code:
$file_contents = file("path/to/file.csv");

$titles = $file_contents[0];

$title_array =  explode(",", $titles);

That will give you an array of titles.

From there you can unset the title row in the $file_contents variable, or pop it out of the array, so you are left with only data, or even just ignore it.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
So $title_array will have the first line of the csv? how is that then printed to screen in a foreach loop? sorry, still learning
 
learning!!!

so from this

Code:
	if($data_insert)
	{
		$file_contents = file("auctions.csv");
		$titles = $file_contents[0];
		$title_array =  explode(";", $titles);
		echo '<br>----   title start ------<br>';
		foreach($title_array as $key => $val)
		{
			echo "value is " . $val . "<br>";
		}		
		echo '<br>----   title end ------<br>';		
	}

how do I get the rest of the file which is data... thxs in advance
 
The rest of the file is still in $file_contents.

You can loop through it line by line just the same, to get each row of data.

Code:
foreach($file_contents as $key => $line)
{
$items = explode(...);
[indent]foreach($item as $value)[/indent]
[indent]{[/indent]
[indent][/indent][indent]echo " | " . $value . "|";[/indent]
[indent]}[/indent]
}



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Have you replaced ... in this code "explode(...);" with the appropriate values?

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
That information is in THREE earlier posts AND, can also be found HERE.





Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top