Jump to content

Re-populating a DropDown Menu and selecting the correct value


acctman

Recommended Posts

I hope I can explain this correctly. I have a user edit page that will retrieve info such ass Month, Day, Year, etc... for a user. My problem is how do I reinsert the value into the Select drop down menu.

 

I set <%m_month%> as a variable it holds the users Month from the database, the value will be a digit 1-12. As you can see my select drop down menu has user digits for the value and text for the months. My question is how do I write a short fast code to take the value of <%m_month%> and associate it with the correct month and have it marked as Selected?

 

code would be something like this I guess, this is just to show you what i'm trying to do

 

if <%m_month%> == "" {

echo '<OPTION value="" selected>Month</option>';

 

} else {

 

echo '<OPTION value="<%m_month%>">FILL IN MONTH NAME</option>';

 

SHOW THE REST OF THE OPTIONS

 

}

 

<select name=add[month]>

<OPTION value="" selected>Month</option>

<OPTION value="1">January</option>

<OPTION value="2">Feburary</option>

<OPTION value="3">March</option>

<OPTION value="4">April</option>

<OPTION value="5">May</option>

<OPTION value="6">June</option>

<OPTION value="7">July</option>

<OPTION value="8">August</option>

<OPTION value="9">September</option>

<OPTION value="10">October</option>

<OPTION value="11">November</option>

<OPTION value="12">December</option>

</select>

Link to comment
Share on other sites

$months = array(
  1 => 'January',
  2 => 'February',
  3 => 'march',
  4 => 'April',
  5 => 'May',
  6 => 'June',
  7 => 'July',
  8 => 'August',
  9 => 'September',
  10 => 'Octobner',
  11 => 'November',
  12 => 'December' );

echo "<select name=\"add[month]\">\n";
foreach ($months as $month_id => $month_name)
{
    $selected = ($m_month == $month_id) ?' selected="selected"':'';
    echo "<optiojn value=\"$month_id\">$month_name</option>\n";
}
echo "</select>\n";

Link to comment
Share on other sites

$months = array(
  1 => 'January',
  2 => 'February',
  3 => 'march',
  4 => 'April',
  5 => 'May',
  6 => 'June',
  7 => 'July',
  8 => 'August',
  9 => 'September',
  10 => 'Octobner',
  11 => 'November',
  12 => 'December' );

echo "<select name=\"add[month]\">\n";
foreach ($months as $month_id => $month_name)
{
    $selected = ($m_month == $month_id) ?' selected="selected"':'';
    echo "<optiojn value=\"$month_id\">$month_name</option>\n";
}
echo "</select>\n";

 

how would I apply <%m_mouth%> to the coding above?

Link to comment
Share on other sites

What is <%m_month%> exactly? You stated it was a variable, so I thought you had incorrecty typed that and you really meant that <%m_month%> was a variable such as $m_mouth (which is what I included in the code above).

    $selected = ($m_month == $month_id) ?' selected="selected"':'';

 

If it's a constant then use it as such

    $selected = (m_month == $month_id) ?' selected="selected"':'';

Link to comment
Share on other sites

What is <%m_month%> exactly? You stated it was a variable, so I thought you had incorrecty typed that and you really meant that <%m_month%> was a variable such as $m_mouth (which is what I included in the code above).

    $selected = ($m_month == $month_id) ?' selected="selected"':'';

 

If it's a constant then use it as such

    $selected = (m_month == $month_id) ?' selected="selected"':'';

 

its a variable it holds the number value from the database for the users month. if I echo <%m_month%> for the testuser I'll receive a value of 2 (i.e February).

Link to comment
Share on other sites

Okay, this <%m_month%> has no meaning to me. I would assume it would either be a variable such as $m_month or a constant such as m_month. What are the tags <% and %> supposed to represent? I've never seen that before.

 

Show me how you would echo that in your code.

Link to comment
Share on other sites

Okay, this <%m_month%> has no meaning to me. I would assume it would either be a variable such as $m_month or a constant such as m_month. What are the tags <% and %> supposed to represent? I've never seen that before.

 

Show me how you would echo that in your code.

 

a .tpl template file holds both HTML/CSS and PHP in order for the site to process global variables and constants, <% %> is used to distinguse it within php code or html code. so pretty much <%m_month%> can be echoed / insert anywhere on the site and it would return the value for that user. a parser.php file handles the processing of the var and const. m_user, m_age, m_city, m_day, m_year, etc... its OO but not standard why. Every user value from the database can easily be retrieved by inserting the db field name from the member table in between <% %>

 

 

to echo the value within php you would do the following below. if you're in a .tpl file with html coding you'd just type <%m_month%>. it does all the sql lookup for you.

echo <%m_month%>; // testuser value would be "2"

Link to comment
Share on other sites

I take it you're using something like Mambo that uses a template system like that?

 

Yes, its like Mambo. Its actually an old image rating community script that i've pretty much recode with the help of other programmers on forums. all thats left is to re-populate the drop down menu with the correct selected value for editing and getting tinymce to work correctly

Link to comment
Share on other sites

ok I think I figured it out, $selected was never added to the echo and I just inserted <%m_month%> so far everything looks good

 

<?php
$months = array(
  1 => 'January',
  2 => 'February',
  3 => 'march',
  4 => 'April',
  5 => 'May',
  6 => 'June',
  7 => 'July',
  8 => 'August',
  9 => 'September',
  10 => 'Octobner',
  11 => 'November',
  12 => 'December' );

echo "<select name=\"add[month]\">\n";
foreach ($months as $month_id => $month_name)
{
$selected = (<%m_month%> == $month_id) ?' selected="selected"':'';
echo "<option value=\"<%m_month%>\" $selected>$month_name</option>\n";

}
echo "</select>\n";
?>	

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.