Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP list box with field populated from database

Posted on 2014-03-26
8
Medium Priority
?
1,122 Views
Last Modified: 2014-03-27
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

0
Comment
Question by:stkoontz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 8

Expert Comment

by:soupBoy
ID: 39957531
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?
0
 
LVL 2

Author Comment

by:stkoontz
ID: 39957618
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.
0
 
LVL 8

Expert Comment

by:soupBoy
ID: 39957635
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 ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39958597
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

0
 
LVL 2

Author Comment

by:stkoontz
ID: 39958794
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
0
 
LVL 44

Accepted Solution

by:
Chris Stanyon earned 2000 total points
ID: 39958843
Hey Steve,

Sorry. Slight typo in my code, although there's only one missing ? after 'selected' : null

<select name="id_church" id="id_church">
<?php
   $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

0
 
LVL 2

Author Comment

by:stkoontz
ID: 39959289
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>
0
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39959312
Doh! Sorry about the typos. Glad you got it working :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question