Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 20th, 2004, 04:18 AM
Registered User
 
Join Date: Jul 2004
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;
 :



 
Old July 20th, 2004, 04:43 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 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
::::::::::::::::::::::::::::::::::::::::::
 
Old July 20th, 2004, 05:01 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 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
::::::::::::::::::::::::::::::::::::::::::
 
Old July 20th, 2004, 08:37 PM
Registered User
 
Join Date: Jul 2004
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>';
?>
 
Old July 20th, 2004, 08:39 PM
Registered User
 
Join Date: Jul 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


 
Old July 20th, 2004, 09:49 PM
Registered User
 
Join Date: Jul 2004
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.

?>







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 06: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 09:46 AM
Changing between bold and plain text in a text box funkybuddha Access 2 January 3rd, 2006 10:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.