[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP list box with field populated from database

Posted on 2014-03-26
8
Medium Priority
?
1,124 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

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 45

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 45

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

829 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