Link to home
Start Free TrialLog in
Avatar of stkoontz
stkoontzFlag for United States of America

asked on

PHP list box with field populated from database

I have an edit page where a user can choose which church they are from.  (id_church)

The page needs to pull in the current id_church from the database, but it also needs to present the list of possible church ids to choose from in a dropdown list.

The code (below) works fine to present the list of church ids, but I don't know the logic to have the dropdown populated with the church id that's already in the database.

Any help is greatly appreciated.

Steve

<select name="id_church" id="id_church">
			<?php try {
				$qry_church = new PDO("connection information);
			    foreach($qry_church->query("SELECT id_church FROM church") as $row_church) {?>
          <option value="<?php echo $row_church['id_church']; ?>"><?php echo $row_church['id_church']; ?></option>
		<?php
		    }
			    $qry_church = null;
			} catch (PDOException $e) {
		    	print "Error!";
			    die();
			}
		?>
        </select>

Open in new window

Avatar of soupBoy
soupBoy
Flag of United States of America image

I see you are populating your <select> with the id_chruch from the church table...where are the 'church id that's already in the database'?  What are they in a different table?
Avatar of stkoontz

ASKER

Yes, they are in 2 different tables.  I have a church table that lists the church information.  There's also a registrants table where the registrants church ID is stored.
I'm still a little confused by the structure, but we can get past that :) (I'm not asking the right questions)

It sounds to me you will need to do a join (or possible a union) on a couple of tables.

You already have this statement:
SELECT id_church from church, which I imagine returns not all of the results you are looking for.

What would the SQL statement be that would return the missing results?
SELECT id_church from registrants ?
Avatar of Chris Stanyon
Normally what you do here is use 2 queries - one to pull the user details from the db, which includes the current_church, and another to pull all the available churches from your church table. Then when you loop through the available churches to populate your dropdown, you check each value against the current church and set the 'selected' attribute.

You haven't shown any code where you pull the current church details, but the principle is something like this:

<select name="id_church" id="id_church">
<?php
   //you'll normally already have this value from a previous query
   //$current_church = $user->current_church;
   $current_church = 3;
   $qry_church = new PDO("connection information);
   foreach($qry_church->query("SELECT id_church FROM church") as $row_church):?>
      <option value="<?php echo $row_church['id_church']; ?>" <?php echo ($row_church['id_church'] == $current_church) ? 'selected' : null">><?php echo $row_church['id_church']; ?></option>
   <?php endforeach; ?>
</select>

Open in new window

Chris:  I understand the concept now.  There's something not right in your code, though, that I can't figure out.  

  <?php echo ($row_church['id_church'] == $current_church) ? 'selected' : null">>
  <?php echo $row_church['id_church']; ?></option>

There are 2 "<?php" tags without a "?>" tag in between.

I can easily pull in the current church from the table, so I didn't include that part of the code.  Thanks for the help.

Steve
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Worked great!  Thanks!  Just one small typo in case someone stumbles on this.

<option value="<?php echo $row_church['id_church']; ?>" <?php echo ($row_church['id_church'] == $current_church) ? 'selected' : null"; ?>><?php echo $row_church['id_church']; ?></option>

The quote after null should be deleted.

<option value="<?php echo $row_church['id_church']; ?>" <?php echo ($row_church['id_church'] == $current_church) ? 'selected' : null; ?>><?php echo $row_church['id_church']; ?></option>
Doh! Sorry about the typos. Glad you got it working :)