0

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:

  1. id_user (primary key)
  2. income_input
  3. 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!

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

This worked perfectly. One thing tho, I tried to use 'fetch_assoc' but if the "current logged-in user" had no previous data, the form wasn't displaying at all. I used 'mysql_fetch_array' ($row = mysql_fetch_array($result);) and now it's working properly. Still running some test but this was a real help. Thanks again!
Since you want an associative array, assoc is the right choice - but I guess you should first use mysql_num_rows to check if anything was selected at all, and only if it was, fetch it.
Ohh alright, that makes sense now. Thank you for the clarification.
0

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>

1 Comment

Your while loop is pretty useless here.
0

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> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.