PHP Gurus...
Can anyone give me a helping hand with this.
I'm more ASP, but trying to move to PHP and sturggling a bit.
I have a few products stored in a csv file created in Excel. Column 1
is the part #, column 2 is title, column 3 is color, column 4 is width
and 5 is price.
I need to read these into arrays, with a seperate array for each
column.
Can anyone suggest anything?
If someone could point me in the right direction it would be much
appreciated!
Tom 7 20640
Tom wrote: PHP Gurus...
Can anyone give me a helping hand with this.
I'm more ASP, but trying to move to PHP and sturggling a bit.
I have a few products stored in a csv file created in Excel. Column 1 is the part #, column 2 is title, column 3 is color, column 4 is width and 5 is price.
I need to read these into arrays, with a seperate array for each column.
Can anyone suggest anything?
If someone could point me in the right direction it would be much appreciated!
Tom
Hi Tom,
Try something like this:
1) find out what the end-of-line is. Probably \n
2) read the csv into an array using file().
Now each arrayelement is a row from the csv.
check www.php.net for details.
3) Explode each line, using the seperator for columns. (can be tab \t or ,
or whatever you decided.)
Good luck.
Regards,
Erwin Moller
> > I have a few products stored in a csv file created in Excel. Column 1 is the part #, column 2 is title, column 3 is color, column 4 is width and 5 is price.
I need to read these into arrays, with a seperate array for each column.
Try something like this: 1) find out what the end-of-line is. Probably \n 2) read the csv into an array using file(). Now each arrayelement is a row from the csv. check www.php.net for details.
3) Explode each line, using the seperator for columns. (can be tab \t or , or whatever you decided.)
Hello
have a look at this function, and see if it helps you http://uk2.php.net/manual/en/function.fgetcsv.php
Cheers
Mark
Tom wrote: I have a few products stored in a csv file created in Excel. Column 1 is the part #, column 2 is title, column 3 is color, column 4 is width and 5 is price.
I need to read these into arrays, with a seperate array for each column.
Try something like:
<?php
$lines =file('whatever.csv');
foreach($lines as $line)
{
list($partno[],$title[],$color[], ... )
= explode(',',$line);
}
?>
Keep in mind that all CSV files are not equal: some use other delimiter than
comma for separating fields, some encapsulate fields within double quotes,
etc. The above code is for a trivial CSV file.
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-arroba-escomposlinux-punto-org
TAG no encontrado... Insert disk #2
Great, thanks for the tips. I've managed to overcome the hard bit now
:)
I've encountered a small problem that I can't figure out though....
My 1st page is a table with each of the products listed, with a
quantity form field for each product. The name of this field is qty1,
qty2 etc depending on which product it is. With me so far I hope....
The next page calls up the CSV again and loops through each line. I now
want to request the quantity form field for each product.
So I'm trying this
function convertCSVtoAssocMArray($file, $delimiter)
{
$result = Array();
$size = filesize($file) +1;
$file = fopen($file, 'r');
$keys = fgetcsv($file, $size, $delimiter);
while ($row = fgetcsv($file, $size, $delimiter))
{
for($i = 0; $i < count($row); $i++)
{
if(array_key_exists($i, $keys))
{
$row[$keys[$i]] = $row[$i];
}
}
$result[] = $row;
}
fclose($file);
return $result;
}
$myarray = convertCSVtoAssocMArray("sscreen.csv", ",");
$numElements = count($myarray);
for($counter=0; $counter < $numElements; $counter++)
{
$unitprice = $qty * $myarray[$counter][4];
echo $qty[$counter];
echo $counter;
}
But nothing is outputting! Any ideas? I basically need to construct the
$qty1 variable by adding the record count onto the end.
Again, thanks for all your help!
Tom I've encountered a small problem that I can't figure out though....
$myarray = convertCSVtoAssocMArray("sscreen.csv", ",");
Try adding this line after the one quoted above:
print_r( $myarray );
and see if what you get in the array matches your expectations.
---
Steve
On Wed, 12 Oct 2005 01:18:07 -0700, Tom wrote: I have a few products stored in a csv file created in Excel. Column 1 is the part #, column 2 is title, column 3 is color, column 4 is width and 5 is price.
example of how I woud do it: (WARNING: Untested code!)
<?php
$file = file("theFile.csv");
$r=0;
foreach ($file as $line){
list($row[$r]['partno'], $row[$r]['title'], $row[$r]['color'],
$row[$r]['width'], $row[$r]['price']) = split(",", $line);
}
?>
That puts the whole file into a 2-dimensional arra ($row) that simulates
the layout of the Excel sheet. Do whatever you wnat with it after that.
--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/
Tom wrote: I have a few products stored in a csv file created in Excel. Column 1 is the part #, column 2 is title, column 3 is color, column 4 is width and 5 is price.
I need to read these into arrays, with a seperate array for each column.
$maxlinelength = 1000;
$fh = fopen('inventory.csv', 'r');
$firstline = fgetcsv($fh, $maxlinelength);
$cols = count($firstline);
$row = 0;
$inventory = array();
while ( ($nextline = fgetcsv($fh, $maxlinelength)) !== FALSE )
{
for ( $i = 0; $i < $cols; ++$i )
{
$inventory[$firstline[$i]][$row] = $nextline[$i];
}
++$row;
}
fclose($fh);
That puts the data in the array $inventory where $inventory['partno'][10]
is the 11th item in the 'partno' column. Column headings from the csv file
are used as keys for the associative array $inventory. You may address the
columns as "separate arrays" like so: $inventory['partno'] which is in
itself a numerically indexed array with 0 <= index < $row.
--
E. Dronkert This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brian |
last post by:
I'm diddlying with a script, and found some behavior I don't understand.
Take this snippet:
for ($i = 0; $i <= count($m); $i++) {
array_shift($m);
reset($m);
}
|
by: Stormkid |
last post by:
Hi Group I'm trying to figure out a way that I can take two (two
dimensional) arrays and avShed and shed, and subtract the matching elements
in shed from avShed I've pasted the arrays blow from a...
|
by: lawrence |
last post by:
I wanted to test xml_parse_into_struct() so I took the example off of
www.php.net and put this code up on a site:
<?php
$simple = <<<END
<item>
|
by: vcardillo |
last post by:
Hello all,
Okay, I am having some troubles. What I am doing here is dealing with
an employee hierarchy that is stored in an array. It looks like this:
$employees = array( "user_id" => array(...
|
by: Sam Collett |
last post by:
How do I remove an item with a specified value from an array?
i.e. array values 1,2,2,5,7,12,15,21
remove 2 from array would return
1,5,7,12,15,21
(12 and 21 are NOT removed, duplicates are...
|
by: Mike S. Nowostawsky |
last post by:
I tried using the "toUpperCase()" property to change the value of an array
entity to uppercase BUT it tells me that the property is invalid. It seems
that an array is not considered an object when...
|
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: Leszek |
last post by:
Hi.
Is it possible in javascript to operate on an array without knowing how mamy
elements it has?
What i want to do is sending an array to a script, and this script should
add all values from...
|
by: Jim Carlock |
last post by:
Looking for suggestions on how to handle bad words that might
get passed in through $_GET variables.
My first thoughts included using str_replace() to strip out such
content, but then one ends...
|
by: =?Utf-8?B?U2hhcm9u?= |
last post by:
Hi Gurus,
I need to transfer a jagged array of byte by reference to unmanaged
function, The unmanaged code should changed the values of the array, and when
the unmanaged function returns I need...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |