Jump to content

Loading a .txt with php


patrick87

Recommended Posts

Ok, im working on a website, and i have to do a grain prices page. I want this very simple as well 1. im not being paid much lol. and 2. I dont want it complicated.

So my uncle explained to me to use [code]<?php require("pricesffs.txt"); ?>[/code] however, the txt does not show up or load or what not. Is the code right?

btw, the site is [a href=\"http://www.falmouthfarmsupply.com\" target=\"_blank\"]http://www.falmouthfarmsupply.com[/a]

its a simple site and i really need to get the prices page up and loaded right. The actual file im working with is [a href=\"http://www.falmouthfarmsupply.com/prices1.htm\" target=\"_blank\"]http://www.falmouthfarmsupply.com/prices1.htm[/a] which is not linked to the navigation yet. Im not going to link it until i get the txt doc. to load in the page.

thanks for any help in advanced..


Patrick Best
Indiana
Link to comment
Share on other sites

Depends on the exact format of your text file, but something like this might get you closer:

[code]<?php
$prices = file("pricesffs.txt");// read file into array
for ($i=0;$i<count($prices)-1;$i++) {
    echo $prices[$i]. "<br/>"; // echo each element of array
}[/code]
Link to comment
Share on other sites

[!--quoteo(post=364657:date=Apr 13 2006, 10:14 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Apr 13 2006, 10:14 PM) [snapback]364657[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Depends on the exact format of your text file, but something like this might get you closer:

[code]<?php
$prices = file("pricesffs.txt");// read file into array
for ($i=0;$i<count($prices)-1;$i++) {
    echo $prices[$i]. "<br/>"; // echo each element of array
}[/code]
[/quote]
didnt work... but was i supposed to end that code with ?> by chanced.. i tried with and without, but with no results. I guess its the txt document. but i didnt know there was different versions :S.
Link to comment
Share on other sites

Can we see (some of) this text file so we know what we're trying to work with?

When you say something "didn't work", we're still completely in the dark. If you can explain what it did, what you saw, whether there were error messages, etc. we'll be able to help faster.
Link to comment
Share on other sites

ok....the text file doesnt show up..but the page loads everything else...and yes..the doc. is FROM my computer..but it is with the rest of the stuff via ftp to the site. I have it linked correctly and everything.

um.. here is all that is in this txt doc.


Falmouth Location


Month

April May June July August


Corn $0.00 $0.00 $0.00 $0.00 $0.00


Soybean $0.00 $0.00 $0.00 $0.00 $0.00


Wheat $0.00 $0.00 $0.00 $0.00 $0.00


This is how i have to have the prices of each thing laid out.. its for this site... www.falmouthfarmsupply.com

If you go to the site..and click on grain prices...thats how it should look.. but that is all html.. on one page.. it would be easy for someone like ME to edit this html and change prices daily..but not for the ppl that are going to take care of this site. They need sumthing like a txt doc. that they can go in.. change the file, upload it..and not have to worry about learning html, or anything. And none of them have frontpage or anything so thats another reason i was recommended to just use a txt file. I have also been told this is bad practice 2 do... i should stay away from a txt file. I dont know.. i may be going about making this prices page all wrong.


Link to comment
Share on other sites

Just my 2 cents.

perferred method:

create a Price Update form. they can access via their browser, verify the data and click ok. the script then takes the data and stores it in a database. when the main price page is accessed that script gets the data from the database and displays it properly.

It really really is NOT as hard as it might seem.

If you need further help ask.

Lite...

rather than a txt/doc file (which is very prone to creator errors) perhaps use an excel template in which they enter the data
Link to comment
Share on other sites

[!--quoteo(post=364657:date=Apr 13 2006, 09:14 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Apr 13 2006, 09:14 PM) [snapback]364657[/snapback][/div][div class=\'quotemain\'][!--quotec--] Depends on the exact format of your text file, but something like this might get you closer:

[code]<?php
$prices = file("pricesffs.txt");// read file into array
for ($i=0;$i<count($prices)-1;$i++) {
    echo $prices[$i]. "<br/>"; // echo each element of array
}[/code] [/quote]

This worked beautifully for me, except you need to change [b]count($prices)-1[/b] to [b]count($prices)[/b], or [b]<[/b] to [b]<=[/b]. Try this:

[code]<?php
$prices = file("pricesffs.txt");// read file into array
for ($i=0; $i<count($prices); $i++) {
    echo $prices[$i]. "<br/>"; // echo each element of array
}
?>[/code]

I've taken off the -1.

Make sure you have pricesffs.txt in the same folder as the PHP script that's being called, or it won't find it.
Link to comment
Share on other sites

[!--quoteo(post=364927:date=Apr 14 2006, 06:29 PM:name=litebearer)--][div class=\'quotetop\']QUOTE(litebearer @ Apr 14 2006, 06:29 PM) [snapback]364927[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Just my 2 cents.

perferred method:

create a Price Update form. they can access via their browser, verify the data and click ok. the script then takes the data and stores it in a database. when the main price page is accessed that script gets the data from the database and displays it properly.

It really really is NOT as hard as it might seem.

If you need further help ask.

Lite...

rather than a txt/doc file (which is very prone to creator errors) perhaps use an excel template in which they enter the data
[/quote]

If its not as hard as it seems.. then its still really hard LOL im very very new to PHP. I try to avoid it at all costs since i usually do simple sites, or photoshop sites..since im good with those. And i couldnt get the txt to show up still even with that updated code... um.. am i supposed to create a certain file with the PHP script in it??? cuz u said keep the txt in the same place as the php script.. the script is within the prices1.htm file..and the txt file is in the same folder as it.. so i thought i did it right.

Correct me if im wrong.

Link to comment
Share on other sites

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]the script is within the prices1.htm file [/quote]
AH! There's your problem. In order for a php script to get executed, the server needs to know ... and it knows that when the script extension is .php (or you trick the server into processing all types of files. Try renamimg prices1.htm as prices1.php and see what happens.
Link to comment
Share on other sites

Just in case (this old man just spent 30 mins doing this)...

Since your 'clients' are not extremely computer literate AND in an effort to make things clear (oh sure) here is a suggestion

1. Have the 'client' use an Excel template to enter their data (see picture)

2. Have 'client' SAVE AS price.csv (comma separated)

3. Allow 'client' to upload price.csv

4. The following php script will gather and display the data

(please note this is a very rough teaching script - it is NOT the most streamlined way to accomplish your objective BUT it works)

[code]<?PHP
############################################
# this gets the price.csv file and displays the data
#
#    IMPORTANT: Array elements begin with the number 0 (zero)
#
#    this entire example is predicated upon the use of a
#    predetermined excel template
#
#    as you become more proficient, you will be able to
#    modify this script to dynamically adjust
#############################################

###############################################################
# read the csv file into an array
#
#    this array has elements that represent each row of the excel file
#    each element will have data separated by commas
#    we will turn each element into a new array so that we can extract the data
###############################################################

$array_01 = file("price.csv");

###################################################
# count the number of elements in the first array
###################################################

$elements_01 = count($array_01);

#######################################################################
# get the location - based upon our excel template
# this information would be the 2nd piece of data in the first element
#    in our example the first element looks like this
#    location,London,,,,,

$array_02 = explode(",", $array_01[0]);
$location = $array_02[1];

############################################################################
# get the months for which we have prices - based upon our excel template
# this information would be the 2nd through 6th pieces of data in the 2nd element
#    in our example the second element looks like this
#    month,April,May,June,July,August

$array_03 = explode(",", $array_01[1]);
$month01 = $array_03[1];
$month02 = $array_03[2];
$month03 = $array_03[3];
$month04 = $array_03[4];
$month05 = $array_03[5];

####################################################################################
# get the crop names as well as the relevant prices - based upon our excel template
# this information would be in the elements 3 through 5
#    in our example those elements look like this
#    corn,12.5,9.75,8,13.37,6
#    soybean,3.25,4.68,5.23,4.5,2
#    wheat,7.5,4.75,2,10.37,23.98

$array_04 = explode(",", $array_01[2]);
$array_05 = explode(",", $array_01[3]);
$array_06 = explode(",", $array_01[4]);
$crop_01_name = $array_04[0];
$crop_01_price01 = "$".number_format($array_04[1], 2, '.', ',');
$crop_01_price02 = "$".number_format($array_04[2], 2, '.', ',');
$crop_01_price03 = "$".number_format($array_04[3], 2, '.', ',');
$crop_01_price04 = "$".number_format($array_04[4], 2, '.', ',');
$crop_01_price05 = "$".number_format($array_04[5], 2, '.', ',');

$crop_02_name = $array_05[0];
$crop_02_price01 = "$".number_format($array_05[1], 2, '.', ',');
$crop_02_price02 = "$".number_format($array_05[2], 2, '.', ',');
$crop_02_price03 = "$".number_format($array_05[3], 2, '.', ',');
$crop_02_price04 = "$".number_format($array_05[4], 2, '.', ',');
$crop_02_price05 = "$".number_format($array_05[5], 2, '.', ',');

$crop_03_name = $array_06[0];
$crop_03_price01 = "$".number_format($array_06[1], 2, '.', ',');
$crop_03_price02 = "$".number_format($array_06[2], 2, '.', ',');
$crop_03_price03 = "$".number_format($array_06[3], 2, '.', ',');
$crop_03_price04 = "$".number_format($array_06[4], 2, '.', ',');
$crop_03_price05 = "$".number_format($array_06[5], 2, '.', ',');

###################################################################
# display the data in a nicely formatted table

?>
<center>
<table width=50% Border = 1>
  <tr>
    <td colspan=6 align=center>
      Location: <?PHP echo $location; ?>
    </td>
  </tr>
  <tr>
    <td></td>
    <td align=center><?PHP echo $month01; ?></td>
    <td align=center><?PHP echo $month02; ?></td>
    <td align=center><?PHP echo $month03; ?></td>
    <td align=center><?PHP echo $month04; ?></td>
    <td align=center><?PHP echo $month05; ?></td>
  </tr>
  <tr>
    <td><?PHP echo $crop_01_name; ?></td>
    <td align=right><?PHP echo $crop_01_price01; ?></td>
    <td align=right><?PHP echo $crop_01_price02; ?></td>
    <td align=right><?PHP echo $crop_01_price03; ?></td>
    <td align=right><?PHP echo $crop_01_price04; ?></td>
    <td align=right><?PHP echo $crop_01_price05; ?></td>
  </tr>
  <tr>
    <td><?PHP echo $crop_02_name; ?></td>
    <td align=right><?PHP echo $crop_02_price01; ?></td>
    <td align=right><?PHP echo $crop_02_price02; ?></td>
    <td align=right><?PHP echo $crop_02_price03; ?></td>
    <td align=right><?PHP echo $crop_02_price04; ?></td>
    <td align=right><?PHP echo $crop_02_price05; ?></td>
  </tr>
  <tr>
    <td><?PHP echo $crop_03_name; ?></td>
    <td align=right><?PHP echo $crop_03_price01; ?></td>
    <td align=right><?PHP echo $crop_03_price02; ?></td>
    <td align=right><?PHP echo $crop_03_price03; ?></td>
    <td align=right><?PHP echo $crop_03_price04; ?></td>
    <td align=right><?PHP echo $crop_03_price05; ?></td>
  </tr>
</table>
</center>
<?PHP

?> [/code]

A sample of the display is here [a href=\"http://nstoia.com/pricetest/price.php\" target=\"_blank\"]http://nstoia.com/pricetest/price.php[/a]

An image of the excel template is here [a href=\"http://nstoia.com/pricetest/excel.jpg\" target=\"_blank\"]http://nstoia.com/pricetest/excel.jpg[/a]

Hope this is of some help.

Lite...
Link to comment
Share on other sites

great! The csv thing...that worked AWESOME... i found out the parse error.. that i kept getting..it was the beginning header thing sayin it was an xml file or sumthing. cuz i deleted line one..where it had the error..and it worked good. BUT.. LOL..

[a href=\"http://www.falmouthfarmsupply.com/prices.php\" target=\"_blank\"]http://www.falmouthfarmsupply.com/prices.php[/a]

i think its looking sorta wrong cuz i didnt lock anything in the excel doc. u said to lock everything but those one things in the image.. well.. i have no clue what ur talkin bout when u say that.


ull have to clarify. sry im not so smart guys.. obviously why im here..in the newbie section. Im goin to Wright State U. this fall to start on my Computer Engineering Degree..maybe ill learn sumthing there. :P

And id nvr heard of a csv file until today... kinda cool... and they are very small in size lol 357 bytes!


Thanks for all ur help... u had to have worked alot on this if u got that much coding done .. unless u had the script already and jus tweaked it... id hope thats the case..thats a LONG script.
Link to comment
Share on other sites

Glad to have helped. all coded from scratch by hand.

Excel has the ability to 'lock/protect' a spreadsheet so that other people are unable to make changes. It is a very helpful feature when you want to control what and where a user can input to a 'form'.

here are some links to explain how to do it.

[a href=\"http://www.meadinkent.co.uk/xlprotn.htm\" target=\"_blank\"]http://www.meadinkent.co.uk/xlprotn.htm[/a]

[a href=\"http://j-walk.com/ss/excel/usertips/tip022.htm\" target=\"_blank\"]http://j-walk.com/ss/excel/usertips/tip022.htm[/a]

[a href=\"http://www.mrexcel.com/archive/Formatting/14966.html\" target=\"_blank\"]http://www.mrexcel.com/archive/Formatting/14966.html[/a]

[a href=\"http://www.google.com/search?hl=en&q=excel+protecting+cells\" target=\"_blank\"]http://www.google.com/search?hl=en&q=excel+protecting+cells[/a]

As an aside, depending upon your 'clients' proficiency, there are FREE programs that will automatically upload the csv file to your site at scheduled times.

Also as you develop your knowledge and understanding of php and mysql, you will see that there are ways to really streamline the code and make it more effective.

Good Luck.

Lite...
Link to comment
Share on other sites

well... i got everything pretty much under control. It is showing up completely properly. The only thing i cant figure out.. and maybe it was sumthing i did in the csv file.. is that the title variables.. like corn, soybeans, wheat, location, month's, all those.. they have quotation marks around them.

I noticed u dont have them in yours. Simple resolution to that? Sry im being such a pain...

but u have helped a TON...


Link to comment
Share on other sites

Usually, Excel only adds quotes to cells that contain commas

ie if a cell contained an address -- Detroit, Michigan 48383 --
then the csv file would look like --- "Detroit, Michigan 48383"


or quotes themselves

ie -- he said ""Hi!"" would be -- "he said ""Hi!"""


Also (not really positive here) , there may be some Preference settings in Excel that automatically will add quotes to cells containing text.


Lite...

Link to comment
Share on other sites

  • 2 weeks later...
ok.. now the company wants me to basically have two of these prices things.. they love the first one...they update it daily..and everything.. can i copy that code and use a different document..put it in as..price1.csv? and do it that way? they thought maybe i could do it as one document..but i know thats gonna add some coding LOL.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.