Solved

PHP list box with field populated from database

Posted on 2014-03-26
8
1,083 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
 
LVL 42

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 42

Accepted Solution

by:
Chris Stanyon earned 500 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 42

Expert Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Php Array Diff 3 26
How to remove decimals from calculation amount 8 22
Time difference 10 35
XML Data Missing in PHP SimpleXML 8 21
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now