Script Assistance Requested (if-arguement)

In the following code, I am trying to import a complete TLD list file, then display it in a select box, with the .COM being the default selection.

It works flawlessly except for the .COM being the default selection. Instead, its gets outputted like every other line instead of having the selected=“selected” inclusion.

I have been banging my head against a wall on this one. … i know it must be something simple but I just can not put my finger on it.

thanks in advance for any assistance.

<?php $fp = fopen('http://path/to/file.txt','r'); if (!$fp) { $fp = fopen('\PATH\TO\LOCAL\FILE.TXT','r'); if (!$fp) { echo "Unavailable</select></th></td></tr></table></div></font>"; echo "<script type='text/javascript'><!--"; echo "alert('this page is temorarily offline.nnPlease try a little later.');"; echo "document.location.replace(../);"; echo '//--></script></body></html>'; exit; } } while (!feof($fp)) { $tldname = fgets($fp, 1024); $str = $tldname; if ($str[0] <> "#") { echo "<option value='.$tldname' "; if ($tldname=="COM") { echo "selected='selected'"; } echo ">.$tldname</option>n"; $fp++; } } fclose($fp); ?>

This is actually more of a HTML thing, but it isn’t selected = selected.

Its just

Option 1 Option 2</option

Option 2 would be selected by default in the example above.

Also, Its easy to read code and easier on the paser if you hope in and out of PHP.

And I personally format this bit of code like the following…
[php]<?php
if ($str[0] <> “#”)
{
if ($tldname==“COM”)
{
$selected = “selected”;
}
else
{
$selected = “”;
}
?>
<option value="<?php echo $tldname;?>" <?php echo $selected;?>><?php echo $tldname;

<?php $fp++; } ?>[/php]

Its just a personal preferance, but just thought I would throw that out there.

In X(HT)ML, attributes are required to have a value, and HTML apparently supports this as well :wink:

is definately allowed.

What I’d like to know, is why you’re using fopen(), rather than file(). For your application, it is much more convenient.

thanks … i am going to try this now after i integrate it all together with the parser script.

One question, why is it easier on the parser ?

do you mean less resource intensive on the server itself ?

I am all for efficient coding practices and like to learn as much as possible.

While my scripts do work most of the time (after much trial and error), the path to get there can always be more efficient, and I am all for that !

I’m not sure how it affects the resources of your host machine, but it’ll definately make the programmer’s life easier :slight_smile: Using file() (or file_get_contents()), you won’t have to worry about fgets(), fclose(), etc. Since you’re only reading from the file, it would be an improvement.

@Zyppora:

Thanks for the info!

In X(HT)ML, attributes are required to have a value, and HTML apparently supports this as well ;)

I didn’t realize this meant things like the selected part as well. As you can see I haven’t handled XHTML much.

@Bind
Sorry for misinforming you!

Sponsor our Newsletter | Privacy Policy | Terms of Service