Display user profile data in select menu

11 posts by 2 authors in: Forums > CMS Builder
Last Post: February 17, 2016   (RSS)

Hello,

I would like to display a user's saved country option in a select menu on the user profile page.

I have created a table called "countries" and have created an individual record for each country (I have done this rather than just create a list of countries due to other requirements).

I have found code in the forum that displays the list of options in the select menu but I am not sure how to display the user's saved value. Can someone please help me out with this?

Below is the code I am using to display the options in the select menu:

<?php
                                // get field options
                                $tablename = 'countries';
                                $fieldname = 'country_name';
                                $selectedValue = @$_REQUEST['country_name'];
                                $valuesAndLabels = getListOptions($tablename, $fieldname);
                                $optionsHTML = getSelectOptions($selectedValue, array_keys($valuesAndLabels), array_values($valuesAndLabels));
                              ?>
                              <select name="country_name">
                                <option value="">Please select</option>
                              <?php echo $optionsHTML ?>
                              </select>

I imagine I will need to add a "where" clause that does something with the $CURRENT_USER 'num' value. Am I along the right lines?

Any help would be greatly appreciated!

Regards,

Greg

Hi there,

I have come across this code to display the selected option:

<option value="Verizon" <?php selectedIf(@$_REQUEST['cell_phone_carrier'], 'Verizon') ?>>Verizon</option>

This was taken from this post: http://www.interactivetools.com/forum/forum-posts.php?postNum=2204947#post2204947

However, this isn't really viable with my set up because there are so many countries and I am displaying the options using <?php echo $optionsHTML ?>

Just thought I would mention this...

Thanks,

Greg

Hi Greg,

If you're using the standard code that's generated by the website membership profile page code generator,and added the code in your first post, I can't see any obvious reason why this shouldn't work. The only thing I can think of is to make sure this code:

<?php
  // get field options
  $tablename = 'countries';
  $fieldname = 'country_name';
  $selectedValue = @$_REQUEST['country_name'];
  $valuesAndLabels = getListOptions($tablename, $fieldname);
  $optionsHTML = getSelectOptions($selectedValue, array_keys($valuesAndLabels), array_values($valuesAndLabels));
?>

Loads after the users values have been set to the request, which happens here:

  // prepopulate form with current user values
  foreach ($CURRENT_USER as $name => $value) {
    if (array_key_exists($name, $_REQUEST)) { continue; }
    $_REQUEST[$name] = $value;
  }

Did you use the website membership code generator to create your user profile page?

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Greg,

Thanks for the speedy response. Yes, I did use the Code Generator to create the User Profile page.

The select menus display the correct value for any field that is stored in the "accounts" table. It's just select menus from a different table that are not displaying.

I think I need to insert a where statement somewhere but not sure...

Thanks,

Greg

Hey Greg,

The select menus display the correct value for any field that is stored in the "accounts" table. It's just select menus from a different table that are not displaying.

So the users country select drop down system is displaying correctly, and the correct country is highlighted when the user loads the page? I'm not sure what the select menus from different tables are, is this another drop down field that's linked to another table? Could you give me a few more details on how those fields are set up?

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Greg,

Slight change of set up:

Let's say I have the 'accounts' table with a user with a num value of "1".

I created another table called "countries" and in this table I have a list field called, "country_name". I have pasted all of the country names into this field as I selected "Use options listed below".

In cmsAdmin > Section Editors > Accounts, I have created a list menu called "Country" and chosen from List Options > Get options from database (advanced)". I have then chosen the database table, "countries" and "country_name" for both option values and option labels.

When I set the value of the "Country" list for user (num="1") in cmsAdmin then it saves and displays fine. However, when I view the user's record via the User Profile page (which I created with the Code Generator) there is no option value selected in the Country field.

Does that make more sense?

Thanks again for your help!

Greg

Ok, will do. Thanks for your continued support, Greg!

Regards,

Greg

Hi Greg,

I have gone ahead and created the second level support request.

I think I need to use a join to display the data from the other table.

'joinTable' => 'tablename',// optional, add results from another table that are created by the same users

Not really sure how to use this yet though...

Look forward to hearing from you!

Regards,
Greg

Hi Greg,

I thought I'd post the example code I sent in the support ticket as it might be useful to other forum users:

$propNums      = explode("\t", trim($CURRENT_USER['apartment_number']));
$propNumString = implode(", ", $propNums);

// load records from 'accounts'
list($properties, $propertiesMetaData) = getRecords(array(
  'tableName' => 'properties',
  'loadUploads' => true,
  'allowSearch' => false,
  'where' => "`num` IN ($propNumString)",
));

showme($properties);
exit;

So the code above works by getting the users selected apartments nums from their current user record, then creating a string of the num values using the implode function. The final step is to add these to a where statement that will only return property records with those num values.

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com