[SOLVED] Displaying list from mysql with dropdown

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
brendonknoetze
Forum Newbie
Posts: 5
Joined: Wed Mar 16, 2005 4:50 pm

[SOLVED] Displaying list from mysql with dropdown

Post by brendonknoetze »

Hi,

I am trying to populate a table on a webpage with the list from my industry_list table. This list is one of many that will display the "active" list that I have in the table. I am trying to do this in dreamweaver.

When the list is retrieved, it needs to add a dropdown list (3 month, 6 months etc) and a checkbox. I need to update my industry table with the values that the user selects.

I know what it needs to look like, but don't know how to get there. If there is anyone out there that might help, or point me in the right directions i would be grateful.

Here is a link to a screenshot of what I am trying to do. http://www.knoetze.plus.com/university/ ... endump.JPG

Many thanks,
Brendon
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Dreamweaver .. :lol: :P

the "list" retrival and drop down is a simple selection loop similar to the following:

Code: Select all

// ...

$query = mysql_query('SELECT * FROM `some_table`') or die(mysql_error());

if(!$query || mysql_num_rows($query) <= 0)
{
  echo 'no data found.';
}
else
{

  $check = 'hi'; // pre-checked value (may come from a database)
  $out = '<select name=&quote;mydropdown&quote;>';

  while($row = mysql_fetch_assoc($query))
  {
    $out .= '<option value=&quote;' . $rowї'fieldval'] . '&quote;' . ($check == $rowї'fieldval'] ? ' checked=&quote;checked&quote;' : '') . '>' . $rowї'fieldname'] . '</option>' . &quote;\n&quote;;
  }
  echo $out . '</select>';
}
handling the submission is as simple as $_POST['mydropdown'] if your form used the post method.
atypk
Forum Newbie
Posts: 4
Joined: Sat Nov 22, 2003 4:06 am

selection is not made by this code

Post by atypk »

Code: Select all

echo " <TABLE class = sort-table width = 100% border = 1>";
		
		echo "<TR>";
		echo"<thead>";
		echo "<TD align = center>Appointee</TD>";
		echo "<TD align = center>Organization</TD>";
		echo "<TD align = center>Person</TD>";
		echo "<TD align = center>Purpose</TD>";
		echo "<TD align = center>Date</TD>";
		echo "<TD align = center>Status</TD>";	
	 
		echo "</thead>";
		echo "</TR>";


		while($row = mysql_fetch_object($resultappoint)) 
		{ 
		echo "<tr>";
	
		echo "<td width= 15% p align = center>" ."<a href='appointmentedit.php?$row->appointment_id'> $row->user_id</a>"."</td>";
		echo "<td align = center>". $row->org_id."</td>";
		echo "<td align = center>". $row->contact_person."</td>";
		echo "<td align = center>". $row->purpose."</td>";
		echo "<td  p align =center>". $row->appoint_date. "</td>";
		echo "<td p align = center>";
		//echo "<select>";
		$querystatus = "SELECT * FROM appointmentstatus";
		$resultstatus = mysql_query($querystatus) or die('Error, query failed dfsdf'); 
		$check = 'Pending';
		$out = '<select name="mydropdown">';   
		while($row2 = mysql_fetch_assoc($resultstatus))  
		{    
		$out .= '<option value="' . $row2['status_name'] . '"' . ($check == $row2['status_name'] ? ' checked="checked"' : '') . '>' . $row2['status_name'] . '</option>' . "\n";  
		}  
		echo $out . '</select>';
		
		echo "</tr>";

		} 

		?>
Please check that why selected list in not selecting
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

provided the outer loop is running correctly, the <select> tags themselves should display at least. If $resultstatus contains at least 1 record, an option should also exist.

I do have to apologize though. I wrote a slight error in my code previously.. it should be selected="selected" not checked="checked".

When I have a select to create from a table that doesn't change throughout the page, or loop it's used in, I will cache off the results into a variable so I don't redundantly query the database..
atypk
Forum Newbie
Posts: 4
Joined: Sat Nov 22, 2003 4:06 am

Problem Solve

Post by atypk »

Code: Select all

while($row = mysql_fetch_object($resultappoint)) 
		{ 
		echo "<tr>";
	
		echo "<td width= 15% p align = center>" ."<a href='appointmentedit.php?$row->appointment_id'> $row->user_id</a>"."</td>";
		echo "<td align = center>". $row->org_id."</td>";
		echo "<td align = center>". $row->contact_person."</td>";
		echo "<td align = center>". $row->purpose."</td>";
		echo "<td  p align =center>". $row->appoint_date. "</td>";
		echo "<td p align = center>";
		echo "<select>";
		$querystatus = "SELECT * FROM appointmentstatus";
		$resultstatus = mysql_query($querystatus) or die('Error, query failed dfsdf');
		while($row2 = mysql_fetch_object($resultstatus)) 
		{ 
			echo "<option value=$row2->status_name "; 
			if ( $row->status == $row2->status_name){ 
				echo  " selected " ; 
				echo  ">$row2->status_name</option>" ;
			}
			else
			{
				echo  ">$row2->status_name</option>" ;
			
			}
		}
		echo "</select>";
This is simple way to get selected row in dynamic table and records.Beware of Html tages closing and spacing always check your html source for better results in removing simple mistakes. Thanks a lot for give time and help :D
brendonknoetze
Forum Newbie
Posts: 5
Joined: Wed Mar 16, 2005 4:50 pm

Thanks for your help - my solution

Post by brendonknoetze »

Thanks for all of you suggestions. Here is my solution.

Code: Select all

<?php 
	  $linkID = mysql_connect("localhost", "root", "durban12");
	  mysql_select_db("talent", $linkID);
	  
	  $resultID = mysql_query("SELECT fun_list_id, fun_list_name, fun_checkbox
	  							FROM function_list where fun_list_active = '1'", $linkID);
	  
	  		$row = mysql_fetch_assoc($resultID);
		
		print "<font face='Arial, Helvetica, sans-serif'> <table border=\"1\"><tr><th><font size='1' >Function ID</th>";
		print "<th><font size='1'>Functional Experience</th>";
		print "<th><font size='1'>Year Exp.</th>";
		print "<th><font size='1'>Core</th></tr>";

		for ($x = 0; $x <mysql_num_rows($resultID); $x++)
		{
			$row = mysql_fetch_assoc($resultID);
			
			print "<tr>";
			print "<td><font size='1'>" . $row[fun_list_id] ."</td>";
			print "<td><font size='1'>" . $row[fun_list_name] ."</td>";
			print "<td><font size='1'> <select name='select".$row[fun_list_id]."'>
        					<option value='0'>None</option>
							<option value='1'><6 months</option>
       						<option value='2'>6-12 months</option>
        					<option value='3'>18 months</option>
        					<option value='4'>2-3 years</option>
        					<option value='5'>3-4 years</option>
        					<option value='6'>>5 years</option>
      					</select>";
			print "<td> <input type='checkbox' name='checkbox".$row[fun_list_id]."'/></td>";						
			print "</tr>";
		} //close for loop
		print "</font></table>";
	  mysql_close($linkID);
	  ?>
Now - Here is my next delema, How do I get this data in to a table. What will the update sql query look like, as the select name and checkbox name are all dianamic?

Any help would be GREAT. :-)


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your form processor sucks in the submitted data, validates and verifies the submission for integrity, then creates an UPDATE/INSERT query (we've only talked about this particular thing many times..) and does the database change.
Post Reply