Building a page to edit a record in a custom record.
I have managed to build a snippet to retrieve and populate the form with a record from the database.
However, for some fields instead of just showing the current value in a text field, i need to have a dropdown.
What I am trying to do is get the values from a table and then to set the correct 'selected' for the value in the database.
I have a snippet to retrieve the record
Then I have added another snippet to get the values for the dropdown.
This works, but I can't work out the last setp - how to set the 'selected' property correctly.
Based on various tutorials below is the point I have got to - but this fails because the [[+item]] placeholder produces an parse error, so seems like you can't use a placeholder in this context.
Hope this makes sense to someone who could point out the correct to try way to do this.
This is the end of the snippet:
$list = array();
foreach ($rows as $row) {
// from object to array you can also do $row->get('name');
$row_array = $row->toArray();
if([[+item]] == $row_array['item']){
$selectedType = 'selected="selected"';
$output .= '<option value="'.$row_array['item'].'" ' . $selectedType . '>'.$row_array['item'].'</option >';}
else{
$output .= '<option value="'.$row_array['item'].'">'.$row_array['item'].'</option >';
}
}
return $output;
-
☆ A M B ☆
- 222 Posts
If I read this write you are trying to use the chunk placeholder syntax of [[+placeholder]] in a php snippet which won't fly.
If you are passing placeholders to a snippet, you will need to either set them as a $variable, or call $modx->getPlaceholder('placeholder_name') each time you want the placeholder.
Where are you getting the value of [[+item]]? From earlier in your snippet (from the database), from a $_POST variable on a form submit, or from a parameter in your snippet call ([[snippet?&item=`something`]])?
Jason, thanks SO much, just what I needed
the placeholder was coming from another snippet
I've added
$currentvalue = $modx->getPlaceholder('item');
and then changed
if([[+item]] == $row_array['item']){
to
if($currentvalue == $row_array['item']){
and it is working now
Thank you, that's helped me a lot
-
☆ A M B ☆
- 222 Posts
many times, it's the little things that cause insanity.