Reputation: 1949
I have a parser which extracts information into array $item. From my database, I am pulling all results where I store "Types" into array $getFormats.
I am then displaying $item into a big form which the user can modify (to fix any errors from the parser). In partiular, I am forcusing on $item[3] which contains "types" extracted by the parser.
My form displays $item[3] next to my dropdown list containing possible "types" contained in the database. The goal here is to allow the user to match or change the "type" the parser finds with a "type" contained in the database.
Obviously, I want the default dropdown value to be a value which matches one already in the database. If there is no match, then the default would be blank for the user to choose an appropriate type himself.
The code below demonstrates what I have. Obviously, I am going about it wrong as it doesn't work.
<form>
<?php
foreach($articles as $item) {
?>
Type: <input type="text" name="typeparsed" value="<?php echo $item[3]; ?>">
<select name="typechoose" value="<?php echo $value; ?>">
<?php
foreach ($getFormats as $format) {
$selected = '';
if($item[3] == 'entry form' && $format->name == 'Entry Form'){
$selected = 'selected';
}
echo "<option value='".$format->name."' ".$selected.">".$format->name."</option>";
}
?>
</select>
<?php }?>
</form>
$item[3] is the array containing "type" extracted by the parser.
$format->name is the array containing the dropdown options pulled from the database.
The above code works but my problem is that $format->name contains two instances of the same value.
My database has the following "types":
Entry Form
Facebook
Entry Form
This is because of the way wordpress stores hierarchical options. On my site, these values are actually as follows:
Entry Form
Facebook
- Entry Form
As you can see, it is because Entry Form is a top menu item AND a submenu of Facebook.
So, as I am trying to match a type of entry form with Entry Form, I run into a problem when trying to determine WHICH Entry Form is the right one to select.
As my code is presently, the second instance of Entry Form is selected.
How can I adapt the code so that if $item[3] == 'entry form' (as it is currently), then the FIRST Entry Form will be selected?
Upvotes: 0
Views: 1969
Reputation: 1844
Becous You have change Your question I will put another answer witch is simple: You will not determine that as long as both "Entry Form's" have the same name. If You can check them for they database ID's please try to use it, if not then You should change their names.
<?php // $value is actualy ID of the element; ?>
Type: <input type="text" name="typeparsed" value="<?php echo $item[3]; ?>">
<select name="typechoose" value="<?php echo $value; ?>">
<?php
foreach ($getFormats as $format) {
$selected = '';
if($value == $format->id){ $selected = ' selected="selected" '; }
echo "<option value='".$format->id."' ."$selected".>".$format->name."</option>";
}
?>
</select>
And as value pass ID of the selected element.
Upvotes: 0
Reputation: 1844
First thing is I don't understend why You use this:
<?php
foreach($articles as $item) {
if ($item[3] == 'entry form') {
$value = 'entry form';
} else {
$value = $item[3];
}
?>
You can simply use the code brlowe and it will do exacly the same:
<?php
foreach($articles as $item) {
$value = $item[3];
?>
To match selected element do that:
Type: <input type="text" name="typeparsed" value="<?php echo $item[3]; ?>">
<select name="typechoose" value="<?php echo $value; ?>">
<?php
foreach ($getFormats as $format) {
$selected = '';
if($value == $format->name){ $selected = ' selected="selected" '; }
echo "<option value='".$format->name."' ."$selected".>".$format->name."</option>";
}
?>
</select>
<?php }?>
Upvotes: 1