Jump to content

from text file to drop down list


Sharkadder

Recommended Posts

Hi there,Ok so let me begin by stating what i am trying to achieve and then i'm sure you'll come up with a better solution to help my aid.Basically i am creating a data input form for somebody, what i have is a text file in which i am reading in using PHP. The contents of the text file will then be put into a drop down list using the following code:

<select><option>Select Continent</option><?php$lines = file("world.txt");foreach ($lines as $line) {	if (substr_count($line,'#') == 2 and strlen($line) > 2)	{		#echo $line . "<br />\n";		?>			<option><?=$line;?></option>		<?php	}}?></select>

Ok so the code above checks a text file, if the current line within the file contains two hashes and is greater than two in length the line then gets put as an option within the drop down list...so this code is working fine.The problem i then have is, what if i'd like to narrow down a search e.g. i may only wish for the continent "africa" to be shown if the user clicks a button saying "africa". I have been told that in order to achieve such a thing then the page would have to be refreshed every time to narrow down search results. What if it'd like two drop down boxes, one gets activated once a value from the other one has been selected, e.g. the user selects "africa" and so the next drop down box would add the option "africa", would i need a refresh there too?I have looked up alternative ways to do this using javascript to avoid page refresh (as the person i am doing the website for may find this annoying) but they have all failed so far and scripts don't run at all, javascript doesn't seem to load the file whereas php will. So i guess my question is, what can i do to narrow down a search and display the results in a drop down list using php without page refresh, or will i have to have a refresh?I have also tried telling a javascript variable to equal my $line variable inside of the foreach loop in the code shown above but for some reason the code

var myvar = '<?php $line;?>';

doesn't execute at all.Can somebody give me some suggestions for this? I would put up with the refresh but it is a data input form and after about 2 entries you'll get sick of waiting for page to refresh everytime you change dropdown option.Thanks,Mark

Link to comment
Share on other sites

Hi there,Ok so let me begin by stating what i am trying to achieve and then i'm sure you'll come up with a better solution to help my aid.Basically i am creating a data input form for somebody, what i have is a text file in which i am reading in using PHP. The contents of the text file will then be put into a drop down list using the following code:
<select><option>Select Continent</option><?php$lines = file("world.txt");foreach ($lines as $line) {	if (substr_count($line,'#') == 2 and strlen($line) > 2)	{		#echo $line . "<br />\n";		?>			<option><?=$line;?></option>		<?php	}}?></select>

Ok so the code above checks a text file, if the current line within the file contains two hashes and is greater than two in length the line then gets put as an option within the drop down list...so this code is working fine.The problem i then have is, what if i'd like to narrow down a search e.g. i may only wish for the continent "africa" to be shown if the user clicks a button saying "africa". I have been told that in order to achieve such a thing then the page would have to be refreshed every time to narrow down search results. What if it'd like two drop down boxes, one gets activated once a value from the other one has been selected, e.g. the user selects "africa" and so the next drop down box would add the option "africa", would i need a refresh there too?I have looked up alternative ways to do this using javascript to avoid page refresh (as the person i am doing the website for may find this annoying) but they have all failed so far and scripts don't run at all, javascript doesn't seem to load the file whereas php will. So i guess my question is, what can i do to narrow down a search and display the results in a drop down list using php without page refresh, or will i have to have a refresh?I have also tried telling a javascript variable to equal my $line variable inside of the foreach loop in the code shown above but for some reason the code

var myvar = '<?php $line;?>';

doesn't execute at all.Can somebody give me some suggestions for this? I would put up with the refresh but it is a data input form and after about 2 entries you'll get sick of waiting for page to refresh everytime you change dropdown option.Thanks,Mark

Link to comment
Share on other sites

ok i had tried echoing before but nothing seems to happen, here is my updated code after i try writing the contents of $line in javascript.

<select><option>Select Continent</option><?php$lines = file("world.txt");foreach ($lines as $line) {	if (substr_count($line,'#') == 2 and strlen($line) > 2)	{		#echo $line . "<br />\n";		?>			<option><?=$line;?></option>		<?php	}}?></select><script>var myvar = new array()myvar = '<?php echo $line;?>';document.write(myvar.length);</script>

for some reason myvar just doesn;t appear to alert, i try using write and document.writeln and write as shown above also and nothing gets written when i tell it to write length of myvar, so i wonder if the code to create myvar ever actually gets called. Any idea why myvar is not equaling the array $line so that i can go through it a line at a time like i did in php?I'm thinking maybe it is because i'm trying to do the operating but server side code is going through and client side has gone through after it finished possibly so no variable $lune exists or something?thanks,Mark

Link to comment
Share on other sites

Does this part work:

<option><?=$line;?></option>

?I'm suddenly noticing that in this part:

myvar = '<?php echo $line;?>';

your loop has completed, so the value of $line is the last line in your file. If your file ends with a newline character, then the last element of your array will be empty. I suspect that if values are being printed into your options, the final option is also empty.

Link to comment
Share on other sites

The part in the option works yes, $line does get added as an option to the drop down menu. If i tell myvar to equal echo $line during the loop and then show a messagebox, nothing appears to happen. My code below shows i am trying to write the length of each line from the file onto the document but it doesn't show anything, it doesn't show anything if i just try and show myvar without .length too, any ideas why?

<select><option>Select Continent</option><?php$lines = file("world.txt");foreach ($lines as $line) {	if (substr_count($line,'#') == 2 and strlen($line) > 2)	{		#echo $line . "<br />\n";		?>			<option><?=$line;?></option>		<script>		var myvar = '<?php echo $line;?>';		document.write(myvar.length);		</script>				<?php	}}?></select>

Ok that is my current code, still javascript doesn't do/show anything. What more can i try? I'd preferably like the contents from $lines (the whole file) into a javascript array where each line is an array member but as i say javascript isn't doing anything at all in the loop.

Link to comment
Share on other sites

Okay. This version won't work at all. View the generated source code on your browser. You have a bunch of script elements inside your select element. Only option elements are allowed in a select element. There is no place in the DOM for the document.write() data to go, so it is pretty much ignored.I noticed some things wrong in the original code that I should have noticed before. :) They became very clear when I broke down and actually ran your code (using my own data file).When you write new Array() , Array needs an initial capital. JavaScript is case sensitive. The error causes the script (the JavaScript) to terminate, so it never gets to the document.write() statement.When you write myvar = '<?php echo $line;?>'; , the assignment won't work the way you think it should because you're assigning a string to an array. Either of the following will work:myvar.push('<?php echo $line;?>');ormyvar[0] = '<?php echo $line;?>';Remember, I'm working here with the code you posted in Post #4.BTW, your script tag should look like this: <script type="text/javascript">

Link to comment
Share on other sites

ok so i modified the script and it is still not working, i removed the select and option tag as requested but still i get no results displaying, here is the amended code from post 4:

<label>Select Continent:<script type="text/javascript">var myvar = new Array()</script><?php$lines = file("world.txt");foreach ($lines as $line) {	if (substr_count($line,'#') == 2 and strlen($line) > 2)	{		#echo $line . "<br />\n";		?>			<script type="text/javascript">		myvar[0] = '<?php echo $line;?>';		document.write(myvar[0]);		alert(myvar[0]);		</script>		<?php	}}?>

As you can see, i initialise the array before the php statement, i also use a captial letter here for the Array.I then tell element zero of the array to equal the line from the text file, i then write this and alert it but nothing gets written or alerted. Do you have any idea why it may now not be working as i think that code should run.Thanks

Link to comment
Share on other sites

One more :) to add to the list. If your generated source looks like mine, then the line break character(s) are being included at the end of every iteration of $line . When they print, that causes the closing quotation marks to be on the following line. What I mean is, your lines might be getting printed like this:

myvar[0] = 'apples';

JavaScript now considers your strings unterminated, and you should be getting an error like that in your Firefox error console. (Are you using that, BTW? It's a simple built-in tool that helps debugging. I use it extensively.) Anyway, we need to get that line break character out of there. You have 2 options. The first is to add this line to the top of the loop:

$line = trim($line);

Better yet, change your call to file():

file("world.txt", FILE_IGNORE_NEW_LINES);

which has the same effect, but saves a line of code.Just to be on the safe side, do you know for a fact that you are entering the loop, i.e. that your conditions are being met? You are using "view source" to check out the generated code, yes?

Link to comment
Share on other sites

DUDEEEEEEEEEEEEEEEEEEEEEE!!!!!!!!!!!!I tried your amendments and it works like a charm. I added in the new file open operation code and it did nothing, the file still loaded but javascript never printed anything out. I then checked my error console and it was exactly as you said, unterminated strings were occurring, variable myvar[0] was equaling php code but it wasn't being terminated with the last single quote and semi-colon. I then added in the trim code at the start of the foreach loop and to my amazement the variable now started to print. I can now add in a variable i to count up every time i go around the loop and so all of my values will be stored inside of an array. I should now be able to do the rest from here with a few google searches on getting items into drop down menu's using javascript.Many thanks for the help and just in case anybody else gets stuck on such a thing, my final code now looks like this:

<label>Select Continent:<script type="text/javascript">var i = 0var myvar = new Array()</script><?php$lines = file("world.txt", FILE_IGNORE_NEW_LINES);foreach ($lines as $line) {	$line = trim($line);	if (substr_count($line,'#') == 2 and strlen($line) > 2)	{		#echo $line . "<br />\n";		?>			<script type="text/javascript">		myvar[i] = '<?php echo $line;?>';		document.write(myvar[i]);		alert(myvar[i]);		i+= 1;		</script>		<?php	}}?>

Many thanks for all of the help dude, i'm sure we've both learnt something from this experience lol.Glad it's sorted, thanks once again.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...