Solved

PHP list box with field populated from database

Posted on 2014-03-26
8
1,111 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 43

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 43

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 43

Expert Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 look for a specific file type in a local or remote server directory using PHP.

695 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