I'll try to be short with this one. Most probably I'm missing something obvious so any input would be greatly appreciated.
SQL structure/columns:
id_user
(primary key)income_input
income_select
PHP Code:
<?php
$con = mysql_connect("localhost","dbname","pwd");
if (!$con)
{
die('Could not connect to the database: ' . mysql_error());
}
mysql_select_db("dbname", $con);
global $user; // Drupal 7 global
$userid = $user->uid; // Drupal 7 currently logged-in user id
$result = mysql_query("SELECT * FROM my_table WHERE id_user='$userid'");
if($result === FALSE) {
die(mysql_error());
}
if ($user->uid) {
?>
<form>
<label for="edit-1">Label</label>
<input id="edit-1" maxlength="60" size="60" type="text" value="<?php while($row = mysql_fetch_array($result)){ echo $row['income_input']; } ?>">
<select id="select-edit-1">
<option value="daily" <?php while($row = mysql_fetch_array($result)){ $income_select = $row['income_select']; if ($income_select == 'daily') echo 'selected="selected"'; }?>>Daily</option>
<option value="weekly" <?php while($row = mysql_fetch_array($result)){ $income_select = $row['income_select']; if ($income_select == 'weekly') echo 'selected="selected"'; }?>>Weekly</option>
</select>
</form>
<?php
}
mysql_close($con);
?>
The input value (income_select
) is fetched properly from the database, but the dropdown (income_select
) won't return the selected state. If I include the entire form into the WHILE statement it's working fine and I can echo quickly the DB values and the select dropdown "selected" state is working fine too. I would opt for this method but I have to keep the form as it is and only populate it with data from the DB if there is any entry for the currently logged-in user.
I hope the above one makes sense and finally thank you in advance for your help!
You do not need a while if you want to fetch just one row.
First, fetch the row and get what option should be selected.
<?php
// better use 'fetch_assoc' here
$row = mysql_fetch_assoc($result) or die(mysql_error());
$income_select = $row['income_select'];
$daily = ($income_select=='daily');
$weekly = ($income_select=='weekly');
?>
And in form, use it like this:
<option value="daily" <?php if($daily) echo 'selected'; ?>>Daily</option>
<option value="weekly" <?php if($weekly) echo 'selected'; ?>>Weekly</option>
If your PHP settings and version allow it, you can even use this:
<option value="daily" <?= $daily ? 'selected' : '' ?>>Daily</option>
<option value="weekly" <?= $weekly ? 'selected' : '' ?>>Weekly</option>
Change the code like this so it is readable and more efficient.
<?php
$income_select = "weekly"; //default value
$row = mysql_fetch_array($result);
$income_select = $row['income_select'];
echo $income_select; // echo so you can see the value and debug why it doesnt work because the code looks ok at first sight
?>
<select id="select-edit-1">
<option value="daily" <?php if ($income_select == 'daily') echo 'selected="selected"'; }?>>Daily</option>
<option value="weekly" <?php if ($income_select == 'weekly') echo 'selected="selected"'; }?>>Weekly</option>
You should try it like,
<select id="select-edit-1">
<?php
$row = mysql_fetch_array($result);
$income_select = $row['income_select'];// will return daily or weekly
$dailySel='';$weeklySel='';
if ($income_select == 'daily')
{
$dailySel='selected="selected"';
}
else
$weeklySel='selected="selected"';
?>
<option value="daily" <?php echo $dailySel;?>Daily</option>
<option value="weekly" <?php echo $weeklySel; ?>Weekly</option>
</select>