Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing a list into a form ??

Status
Not open for further replies.

JohnnyT

Programmer
Jul 18, 2001
167
GB
I'm struggling a little bit here... I have a list like this :
$list = "item1
item2
item3
item4";

And I have a web form where I want to create a <select><option> box for each of the items in the list.

Does anyone know how I would get the length of the list and then parse the items into the web form??

Would it be better to use a | as a delimiter, like :
$list = &quot;item1|item2|item3|item4&quot;;

I'm really stuck with this problem, any help will be most appreciated.

Cheers

JT I don't make mistakes, I'm merely beta-testing life.
 
Well, to me it's obvious - if I understand you correctly.

First of all: Yes, I would use &quot;|&quot; or similar as a delimiter.
Other spaces might turn up within the items.

Q: From where does this list come from? Why does it look that way?

Anyway: Loop through the list. Use a string-variable for the long list and another for the current item.

When the loop reaches the &quot;|&quot; you chop the list off by teminating the long string with a &quot;\0&quot; at the position of &quot;|&quot; which leaves you with two strings: the current item and the rest of the list. Then you send the current item to the OPTION-tag in te SELECT.
Keep looping until the list is empty.
 
iguela

Thanks for your reply mate. The list comes from a list of possible postings for a website that allows you to find your ex-forces mates.

If I use the '|' as a delimiter, how would I loop through the list. I'm new to php (more of a Perl man).

Wondered if you could post the code I would use?

A big thanks for your help so far.

Cheers

JT I don't make mistakes, I'm merely beta-testing life.
 
You might also look into explode()

--
How can you be in two places at once when you're not anywhere at all?
 
mweinstock is perfectly right
I forgot explode...
Here's the code:

<HTML><BODY>

<?php

$list=&quot;Jack|Jones|Molly|Hugh|Peter|Charles|Brandy&quot;;

// splitting the $list into $L with | as a delimiter
$L = explode( '|' , $list );

print(&quot;<SELECT NAME='mates'>&quot;);

// now loop through $L and display options
for($opt = 0; $opt < count($L); $opt++)
printf(&quot;<OPTION VALUE='%s'>%s</OPTION>&quot;, $L[$opt], $L[$opt]);

print(&quot;</SELECT>&quot;);

?>

</BODY></HTML>
 
Brilliant that mate !!

I can't thank you enough. A big thanks to mweinstock as well.

Cheers both of you, You've made my weekend !

;-))

Thanks for all your help

JT I don't make mistakes, I'm merely beta-testing life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top