Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,491 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,710 people online right now. Registration is fast and FREE... Join Now!




How to dynamically populate HTML SELECT list from LST file?

 
Reply to this topicStart new topic

How to dynamically populate HTML SELECT list from LST file?

-Monkey-
2 Mar, 2008 - 02:42 PM
Post #1

New D.I.C Head
*

Joined: 2 Mar, 2008
Posts: 2

Hi,

I have some websites with database search forms which pass the form user inputs to a CGI script, which in turn queries an MS Access database on the server (W2K, IIS 5), based on the corresponding database fields the user has searched on. The search rsults are then returned to the user.

The back end database is a photographic image library, which administrators update using the database PC client app. The websites hook into this database, to publish the content to web users.

I have absolutely no control of either the Access database or the CGI script code. I can therefore only edit the HTML for the website.

The question is: Can I populate the web form SELECT lists dynamically from an .LST file that resides on the web server, rather than hard code the list options in the HTML SELECT options?

For example, there is a field called LOCATION in the Access database. On the back end, the Access ListBox values (countries) for the database admin's field dropdown list are stored in a file called LIST1.LST (not sure whether .LST file is native to Access, or whether it's related to VB code that's used in the CGI search engine script - anyway, I know that's where the list values for the database's forms are stored!).

Currently, I have to frequently look at the back end database (using the library administrator's client app) to see what countries are defined in the field list, then manualyy update the website SELECT list options to match. Users can frequently add/remove a list item in the backend database, so this can be a real pain, to users and myself!

I know virtually nothing about PHP (though already using a PHP script to force a Save As dialog when linking to jpeg files, so not completely new). Unfortunately I don't have time to crash learn it right now, so would appreciate if someone could please give me a Lego-like solution here!

So, currently my website HTML code for the form input of the LOCATION field is something like:

<TD>
<SELECT NAME="LOCATION">
<OPTION VALUE="">
<OPTION VALUE="Austria">Austria
<OPTION VALUE="Denmark">Denmark
<OPTION VALUE="Germany">Germany
...

</SELECT>
</TD>


Instead of using hard-coded OPTION values for the dropdown list like this, I want to pull them straight in from the LIST_1.LST file that lives in the database folder on the server.

I opened the LST file in Notepad and can see the country names followed by white space. Each country appears to take up 80 bytes in the file, even though the LOCATION field is only 64 characters long in the Access database, so I guess there are some kind of invisible delimiter characters in there, or hex codes used by Access, not sure. The country names themselves appear fine.

I'd be over the moon if anyone can help thanks. I've spent ages looking already. Ideally, a block of code I can just paste straight into the HTML. Already running PHP 5.

Thanks!

This post has been edited by -Monkey-: 2 Mar, 2008 - 02:46 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: How To Dynamically Populate HTML SELECT List From LST File?
2 Mar, 2008 - 06:10 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,212



Thanked: 216 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well you are going to have to do a bit of PHP learning to actually accomplish this... so get ready. The functions you are going to want to look at first are for actually opening the LST file and reading it. PHP functions to look at include

1) fopen(), fread(), and fclose(). These open, read and close files respectively. Or if the lines are nice and crisp you could use file() to read in the entire file into an array (each line being part of the array).

2) Create a while loop which can read each line of the array or of the file itself (depending on which method you chose in item 1 above) and print out an HTML option using echo.

Here is an example I have put together...

php

<html>
<head>
<title>Read File</title>
</head>

<body>

<?php

// Read in the contents to an array
$myarray = file("ourlstfile.txt");

// Print the beginning of our select item.
echo "<select name='selectname'>";

// Loop through each value of the array and print it as an option element.
foreach ($myarray as $value) {
echo "<option value='$value'>$value</option>";
}

// Finish off the select control.
echo "</select>";

?>

</body>
</html>


It would be wise for you to actually look and learn from this example rather than just copying and pasting because this is something you will find yourself needing to do a lot in web development and especially in PHP. Whether it is printing rows of a table to drop downs to calling functions etc.

Enjoy!

"At DIC we be select control option printing code ninjas!" decap.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:39PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month