Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 20th, 2004, 04:18 AM
Registered User
 
Join Date: Jul 2004
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Extract text from text file & put in dropdown box

Hi,

 Tried a lot of times didn't work,so asking.
 I want to extract the text inside and create 2 dropdown boxes. So, one dropdown box will contain racket,shuttlecock,tennis ball etc. The other dropbox will appear after user choose the option from first dropbox.

 Eg: if user borrow racket, the 2nd dropbox will show how many left, eg. 3 left. However, after pressing a button to borrow 1 racket, the text file need to be updated.

 I have a text file(records.txt) like this:
 racket;3 available;
 shuttlecock;6 available;
 tennis ball;4 available;
 football;3 available;
 :



Reply With Quote
  #2 (permalink)  
Old July 20th, 2004, 04:43 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 Posts
Default

For the first part of question, dynamically populating the second drop down menu based on what is selected with the first is a JavaScript question.

For the second part of the question, PHP has a number of built-in functions for handling text files.

Here's a simple example of getting the contents of a text file.
This approach uses the fgetcsv function, since the text file is structured like a csv file. (comma separated values)
http://www.php.net/fgetcsv

Code:
// Open the products.txt file read only.
$handle = fopen('products.txt', 'r');

// iterate through each line of the file.
// read up to 1000 characters per line of data, use a semi-colon instead of a comma as the data separator.

while (FALSE !== ($data = fgetcsv($handle, 1000, ';')))
{
   // $data is an array that contains all of the information from that line.
   $product[]    = $data[0];
   $inventory[]  = $data[1];
}

fclose($handle);
This should produce output like this:
Code:
$product[0]   = 'racket';
$inventory[0] = '3 available';
$product[1]   = 'shuttlecock';
$inventory[1] = '6 available';

//.. etc.
Then at this point you can populate a <select> element with the data. If I have time I'll demonstrate how to do the javascript part of it later (not-with-standing that someone else on the list doesn't do it in the meantime:)).

HTH!

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
Reply With Quote
  #3 (permalink)  
Old July 20th, 2004, 05:01 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Of course, it goes without saying, that an RDBMS would be much better suited for the storage of the product data. Opening files can be costly in terms of resource consumption.

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
Reply With Quote
  #4 (permalink)  
Old July 20th, 2004, 08:37 PM
Registered User
 
Join Date: Jul 2004
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<?php
$handle = fopen('products.txt','r'); //open products.txt,read only

// iterate through each line of the file.
// read up to 1000 characters per line of data, use a semi-colon instead
//of a comma as the data separator.

while (FALSE !== ($data = fgetcsv($handle, 1000, ';')))
{
   // $data is an array that contains all of the information from
   // line.
   $product[] = $data[0];
   $inventory[] = $data[1];
}

fclose($handle);

   echo '<select name="product" onChange="JumpMenu(this)">';
         echo '<option>------------------------</option>';
         echo '<option>$product[]</option>';

   echo '</select>';
?>
Reply With Quote
  #5 (permalink)  
Old July 20th, 2004, 08:39 PM
Registered User
 
Join Date: Jul 2004
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I did the code like this. but don't seem to look right..


Reply With Quote
  #6 (permalink)  
Old July 20th, 2004, 09:49 PM
Registered User
 
Join Date: Jul 2004
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, Sorry for earlier code..
I managed to get the text into dropdown box.
But uhmm, I got a problem now.
Instead of having a 2nd dropdown box, I think I will just have a input there like this:

Available : 2

This appears when user choose the respective item.

But how to code that?

COdes:

<?php
$handle = fopen('poroducts.txt','r'); //open products.txt,read only

while (FALSE !== ($data = fgetcsv($handle, 1000, ';')))
{
   // $data is an array that contains all of the information from that
   // line.
   $product[] = $data[0];
   $inventory[] = $data[1];
}
fclose($handle);
   echo "Select product:";
   echo "<select name='product'>";
         echo "<option>------------------------</option>";
         foreach ($product as $prod){
         echo "<option>$prod</option>";
         }
   echo "</select><p>";

   echo"Availability:";

   echo "$inventory[0]"; //the part I think need to edit.

?>


Reply With Quote
Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to put a value into another form's text box? alastair Access VBA 3 December 20th, 2006 05:19 PM
Text Box .text Property Displays conflicting Valus JPOhlman Visual Basic 2005 Basics 3 August 23rd, 2006 02:39 PM
extract text and image content from .ppt file pratik28 C# 0 May 11th, 2006 06:20 AM
insert text into a text box by selecting an link gavmc Other Programming Languages 0 February 15th, 2006 08:46 AM
Changing between bold and plain text in a text box funkybuddha Access 2 January 3rd, 2006 09:15 AM



All times are GMT -4. The time now is 03:44 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.