Hi,
I have a table which stores country codes, normally they do not change however the sub-catagories I am putting into the database will and I’d like this functionality throughout the structure employing listboxes that are generated (which I have covered), then written to a file (which I did manually till I figure this problem out), and then included where a variable in the page stored this html needed for the job. Problem is, I am trying to get “select=‘selected’” inserted into the right field. The behaviour I have in the embedded iframe I have on the transparency page for my website isn’t quite right (as the correct item is not selected). The first thing I would like to do is use the country querystring to search for the correct 2 letter code and select that option in the listbox. The second one is if there is a group querystring assigned it will select the numerical value instead as a means of tagging the right spot.
I’m confused this used to be so simple to do in asp.
Your explanation may have made some sense to you, but, not us programmers.
First, what is your PHP question? Where’s your code with the error showing?
I am sure someone can help you, if we know what you need.
By the way, PHP is nearly the same as ASP in most ways. They both are server-side programming that are used to read databases. Therefore, they do work the same. If you have ASP code that works and now you need it to be written in PHP, show that code and we can help with the translations.
Please put your code inside the above PHP buttons. Also, we do not need all of your code, just the problem areas. Help us help you…
pretty straight forward. I cache the listbox just like the one demonstrated here: http://www.w3schools.com/tags/tag_select.asp
It goes into a file. When I recall the file I would like to insert SELECTED into the correct html option tag so that it is on the right selection within the pages it is used in my site.
I have no code whatever I’ve done I’ve deleted it over 100 times. The code is a listbox with 255 option tags in it, I don’t think you want 10kb of garbage posted here it is not really necessary. Very simple problem.
And before I get the idiots answer, yes the file is php and yes it is on a php server I’ve gotten everything else working BUT that.
So all that is stored in a regular file? is xml, delimited, etc? Sounds like your trying to do cascading drop boxes. but before we make suggestions, it would helpful. Just post what you have and we’ll go from there.
Once you put “idiots” into a post, you will not get respect from this site! Nobody is an idiot here.
We DO need to know what you want. If you simply want to know how to tell a select clause that one OPTION is selected, why didn’t you read all of links on the site you posted.
Read this: http://www.w3schools.com/tags/att_option_selected.asp
Hope that helps.
ok file has this in it:
[code]
My php just barfs it onto the page wherever I need the select box (this example is simplified).
It is meant to reference country codes that are inside of the database. I have this file read into a variable, and before it is displayed, I need to have it add “selected” into the correct option tag based on value or ID (since my site detects which country people are surfing in from) so that the box starts with the last selected option. Why must I convolute the issue further by posting the rest of the code irrelevant to the problem I am trying to solve?
How is this difficult to understand?
No need to be rude.
Showing us a few lines from a text file doesn’t tell us anything. We need to see the code that’s doing the selecting. Why you’re doing everything the long way i don’t know. You already have the country codes in a db table. Inside the option tag, do an if statement comparing what’s there against whatever else is there. If it matches, then do your select thing. I do that on all my client edit pages. as an example -
>United StatesFor you, it would be
>US100% agreement with Richei !!!
He gave you the instrtuctions. Same as I did by giving you:
http://www.w3schools.com/tags/att_option_selected.asp
We do not know how your code is laid out. Where the matching country code is. You showed us one little select. We would love to help you, but, you need to help us help you. If you can not figure this out by the link I gave you and by the code Richei gave you, (same thing) , then post more of your code showing where you are having trouble and we will help you fix it. Good luck!
ok that would work, but I’d have to do a case statement 255 times and it would extend my script run time with 255 statements. How about a way to do it with string searching and replacement?
I tried $var=Replace(“value=‘23’”,“value=‘23’ SELECTED”,$var);
although that doesn’t seem to work, not sure why.
Again, XT, without your code, we are having trouble giving you an answer. If your code “barf’s” out data which is a select group, it should be something like this:
(Again, not your code which you do not want to show us!)
$targetState = “VT”; //picked a state to check on…
echo ‘’;
$databaseID = mysql_connect( $hostname, $username, $password );
@mysql_select_db($dbname);
$query=“SELECT * FROM States”;
$results = mysql_query ( $query, $databaseID);
// loop through the results, creating the states option list
while( $row = mysql_fetch_row($results) ) {
$isSelected=""; //Empty the temporary variable…
if($row[1]==$targetState) //If matches, mark temporary variable as selected…
$isSelected=“selected='selected”;
print “<OPTION VALUE=’” . $row[0] . “’” . “>” . $row[1] . $isSelected . “\n”;
}
mysql_close($dbLinkID);
echo “”;
Now that I have given you one way to do it and Richei gave you another, we hope this helps.
Next question, please show us your code a little and we will be very happy to help you.
Hope this helps, good luck…
Ok, the database connection that generates the data is irrelevant, the code is cached in a text file and the script that generates the text file is not relevant. When something changes, the cache regenerates. The reason is because it is inefficient to get 255 items from a database everytime someone loads a page that includes this select box. Back in the day of ASP (before .NET) this was a standard strategy for cutting down excessive page load times. That is why I was wanting to do it with the replace, but as I said for some stupid reason that replace statement does not work. Nevermind how the file is read and how it gets put into a variable, let’s just accept the fact that it is in a variable now as a solid chunk of code, which means ALL options encased within SELECT tags. None of them are selected. That needs to be added based on a QUERYSTRING variable that uses the VALUE (number/property of the option tag) to target the right option. OR. A separate QUERYSTRING relaying a 2 letter country code which I’ve included as the ID property of the option.
Hope this clarifies some – the problem is not with anything other than string searching, replacement. I always have problems with regEXP, it annoys me to no end and I’d expect this is yet another one of those things.
This is why we were confused. You never mentioned that you are having a string parsing problem.
You mentioned databases, selects, files, etc. Never a string issue…
Okay, now we can figure it out for you…
You said you have a file with this in it:
Assuming you read it into a variable, you get something like:
$var=" /r/n/r/n/r/n/r/n"
**** NOTE: this was from testing I did where I used encoding to code the returns correctly…
**** Since we do not have your code for how this file variable was created and you did not post the actual value of the code stored into the variable, I am only guessing… Perhaps there are no returns in your version…
So, your code: $var=Replace(“value=‘23’”,“value=‘23’ SELECTED”,$var); would not work because you are searching for —>“value-‘23’”<— and the actual code contains -->value=“1”<-- Note the differences in the quotes around the numbers for the value.
I am guessing that your replace command is not replacing due to single-quote/double-quote issues.
To fix that, try this…
First, you must look at PHP’s version of the state’s select file. (Not the actual file)
Get your data from the file into your $var variable and just echo it and die:
echo $var;
die();
(this will show you exactly the format of your live data…)
Next, verify in your REPLACE statement that you have correctly “escaped” all quotes.
(I am very sure that is the issue with your REPLACE not working. Almost always is!)
If you can not figure it out, please post a few lines of the actual data in that file and your latest version of the REPLACE command. And, sorry for the confusion with not understanding what you needed help with!
Good luck…
SELECTED is also the wrong context for that too. It’s selected=“selected”
Yes, and when you replace it, make sure it get replaced with the correct quote or double-quotes. Important. I am sure you will figure it out now…
Myself I would store the cached listbox code with php code to have the comparison check.
<select>
<option value="1" id="AE" <?php if($selected="1") echo 'selected="selected"'; ?>></option>
<option value="2" id="DE" <?php if($selected="2") echo 'selected="selected"'; ?>></option>
<option value="3" id="CA" <?php if($selected="3") echo 'selected="selected"'; ?>></option>
</select>
this just removes the necessity of doing a string search and replace. and include the file where needed
Laffin, he is not using PHP to parse the SELECT. He is storing it into a file and playing with the strings CLIENT-SIDE. That is what got us all mixed up on what he was asking about. I am sure he will figure it out now…
Read again, an include file will work as needed, all you need to do is change the cache file handler to output the format I described, include it, and it will barf it out as needed.
actually it was server side, and I resolved it, a NULL value was sneaking through somehow so I just explicitly declared the variable types. Probably should have done that to begin with anyways. thanks.