×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Open a csv and read into an array

Open a csv and read into an array

Open a csv and read into an array

(OP)
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

RE: Open a csv and read into an array

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

RE: Open a csv and read into an array

(OP)
Sounds complicated, I can get away from titles out of csv but the data has to be processed in a csv. thxs

RE: Open a csv and read into an array

Quote:

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

RE: Open a csv and read into an array

There's no hidden functions. All functions are perfectly documented in the PHP online manual. It can be found at: http://php.net

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

RE: Open a csv and read into an array

(OP)
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

RE: Open a csv and read into an array

(OP)
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

RE: Open a csv and read into an array

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(...);
foreach($item as $value)
{
echo " | " . $value . "|";
}
}


----------------------------------
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

RE: Open a csv and read into an array

(OP)
That bottom example doesn't work

RE: Open a csv and read into an array

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

RE: Open a csv and read into an array

(OP)
No what are those please?

RE: Open a csv and read into an array

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close