Forum Moderators: coopster

Message Too Old, No Replies

How do I write a simple PHP program with a flatfile?

Trying to convert a CSV to a .txt file

         

ScottM

4:05 pm on Apr 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have several spreadsheets (.xls files) that have prices and descriptions in them.

My desire is to convert those spreadsheets into options that go on a drop-down form.

I've asked a friend to write a simple utility for me, and it works great, but I would like to customize it a little more. Since the program was written in a language I know nothing about, I can't change anything.

The program he wrote is this:

use cart
zap
append from cartcsv.csv delimited
mcomplete=space(256)
goto top
do while .not. eof()
mcomplete='<option value="' + trim(a) + "^" + trim(c) + "^" + trim(b) + "<br>" + trim(e) + " " + trim(f) + "^" + trim(g) + '^1">' + trim(g) + " - " + trim(e) + " " + "(" + trim(f) + ")" + "</option>"
replace complete with mcomplete
mcomplete = space(250)
skip
enddo
goto top
do while .not. eof()
copy to scott.txt fields complete delimited with blank
enddo

Basically, this utility goes through my spreadsheet and turns the spreadsheet into some .txt (options) that I cut and paste into a form.

I would like to learn how to write the same type of program with PHP (or perhaps Javascript?)

So, I'll describe the xls sheet I have and hopefully I can get some help to get a working program...

The .xls file is like this:

widgetA ¦ descriptionA ¦ partnumberA ¦ colorA ¦ priceA
widgetB ¦ descriptionB ¦ partnumberB ¦ colorB ¦ priceB
widgetC ¦ descriptionC ¦ partnumberC ¦ colorC ¦ priceC
and so on....to
widgetZ ¦ descriptionZ ¦ partnumberZ ¦ colorZ ¦ priceZ

I would like the program to output a .txt file that looks something like this:

<option value="partnumberA^descriptionA<br>WidgetA colorA^priceA^1">priceA - widgetA colorA</option>
<option value="partnumberB^descriptionB<br>WidgetB colorB^priceB^1">priceB - widgetB colorB</option>
and so on...to
<option value="partnumberZ^descriptionZ<br>WidgetZ colorZ^priceZ^1">priceZ - widgetZ colorZ</option>

I have been trying to figure out how to do this, with a simple program for a while, but each time I look around for some examples I find I am bogged down trying to learn about installing a database and a bunch of other stuff that isn't relevant.

I'm trying to learn how to write code better, but the missing link is learning how to access the .xls file, output a line and then going to the next line and do the same.

Perhaps there is even a free script someplace that can be modified, but I don't even know the name of what I'm looking for.

Any ideas?

jatar_k

4:24 pm on Apr 19, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



as opposed to an .xls file I would just save it as .csv with commas between.

Then you don't need to change it to a text file you can just open/parse it and output it as is.

Your script might look something like this

<select name="products">
<?
$fp = fopen [php.net]("pathtofile.csv","r");
while ($row = fgetcsv [php.net]($fp,10000)) {
echo "<option value=\"$row[2]^$row[1]<br>$row[0] $row[3]^$row[4]^1\">$row[4] - $row[0] $row[3]</option>";
}
?>
</select>

not sure about the br in the value and the other symbols but I left it as is, just popped the values in there. That should work just fine.

ScottM

4:33 pm on Apr 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Jatar!

I'll be playing with this all weekend:>)
Hopefully others will find it useful, also!

ScottM

11:02 pm on Apr 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I actually got this working!

<select name="item">

<?php
$fp = fopen("http://www.gizmoworld.com/biggizmos.csv","r");
while ($row = fgetcsv($fp,10000))
{
echo "<option value=\"gizmo^$row[2]^$row[0]&#60;br&#62;$row[3]&#60;br&#62;$row[1]^$row[4]^1\">$row[4] - $row[3] $row[1]</option>";
}
?>
</select>

Thanks for the script Jatar!
Please note the ASCII code was used when I needed the actual character used...