Current location: Hot Scripts Forums » Programming Languages » PHP » Excel Spreadsheets and php

Excel Spreadsheets and php

Reply
  #1  
Old 02-24-06, 01:41 PM
pcinfoman pcinfoman is offline
Coding Addict
 
Join Date: Jan 2006
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Excel Spreadsheets and php

I have an extensive website with lots of prices hard-coded into the html. Is there an article I can read or instructions somewhere that tel me how to use php to pull thos same prices from an excel spreadsheet or csv file? this way I can make one change and have it affect the site globally.
Reply With Quote
  #2  
Old 02-24-06, 02:30 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Searching hotscripts.com and sourceforge for excel and csv does not return much! Here is one script that you can probably examine for ideas or make use of -
http://www.hotscripts.com/Detailed/28499.html
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #3  
Old 02-24-06, 03:03 PM
pcinfoman pcinfoman is offline
Coding Addict
 
Join Date: Jan 2006
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Hmm, this looks good if I wanted to pint the contents of a csv file to the web. I am looking for something I can use to call one part of a csv file and display it on a page.
Reply With Quote
  #4  
Old 02-24-06, 04:41 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Actually, I was playing with that code and see the following -
PHP Code:
<?php
include('onesqlcoma.php');

// define simple function to return value from "one.csv" given the value's name
function display_value($item_in){
 
$result nosql_selectAllFromWhere("one","name","=",$item_in);
 if (
$record=nosql_fetch_array($result)){
 return 
$record[value];
 } else {
 return 
"value not found";
 }
}

echo 
"Name: item2, Value: ".display_value("item2")."<br>";

?>
Produces this output - Name: item2, Value: 11.91

The above gets and displays values from a .csv file (called one.csv in my example) with this data -
name,value
item1,12.13
item2,11.91
item3,39.21

Note, the author of that script missed the meaning of "C" in csv and used ; as a separator. To get his script to work for reading a standard MS CSV file, you need to search and replace all the quoted ";" with "," - hence my modified include filename of onesqlcoma

P.S. His code and my example assumes unique names in the file. My code would only return the 1st occurrence should there be any duplicates.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???

Last edited by mab; 02-24-06 at 04:45 PM.
Reply With Quote
  #5  
Old 02-24-06, 05:01 PM
pcinfoman pcinfoman is offline
Coding Addict
 
Join Date: Jan 2006
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
OK, here is a question. My csv file looks like this:
Code:
item#,category,name,rental,price,sale
1840,amphibians,,Frogs,$500.00,
1841,amphibians,Frog,Call,$95.00,
1077,arachnids,Scorpion,Call,$950.00,
1081,arachnids,Tarantula,Call,$950.00,
1150,bears,"Bear, Kodiak","$7,300.00","$25,500.00",
1170,bears,Polar Bear Display,Call,"$2,500.00",
1195,bears,"Bear, Teddy",$275.00,$950.00,
1910,bears,Koala,"$2,750.00","$5,500.00",
1920,bears,Koala,$625.00,"$1,250.00",
2415,bears,Polar Bear Paws,"$1,500.00","$4,750.00",
3328,bears,"Bear Paws, Gloves",$550.00,"$2,250.00",
5575,bears,Panda,Call,"$7,750.00",
5656,bears,Dave's Bear,"$1,250.00","$9,500.00",
5665,bears,Momma Bear Suit,"$6,300.00","$18,500.00",
6225,bears,Polar Bear Head,Call,"$13,500.00",
7319,bears,Bear Character,"$3,750.00","$7,500.00"
7326,bears,Baby Bear Suit,"$5,300.00","$7,500.00"
7328,bears,Poppa Bear Suit,"$7,300.00","$25,500.00"
Now, what I want to do is take the rental value for item #5656 and place it one one spot of the page and the "sale" value for item #5656 and put it somewhere else. In addition, on the same page, I want to put in "name" somewhere else on the page.

I want to do this for each and every item# in my csv file.

How do I do this?
Reply With Quote
  #6  
Old 02-24-06, 05:14 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
The function is changed slightly to return an array with the contents of the row from the csv.
PHP Code:
<?php
include('onesqlcoma.php');

// define simple function to return row from "one.csv" given the item_no
function get_row($item_in){
 
$result nosql_selectAllFromWhere("one","item_no","=",$item_in);
 if (
$record=nosql_fetch_array($result)){
 return 
$record;
 } else {
 return 
"value not found";
 }
}
$row get_row("5656");
echo 
$row[rental];
echo 
$row[sale];
echo 
$row[name];

?>
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 04:29 AM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2 (Unregistered)