Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

drop down list

Status
Not open for further replies.

mikeba1

Programmer
Jan 2, 2005
235
GB
Hi people

I am new to php, attempting to take an access db to the web ???
I seem to be unable to create a drop down list that has two columns 'membershipid,surname'.
The first column is what I need to pass to the next procedure.

I apologise if I am being stupid but I don't seem to find any obvious tutorials on the web about this.

Thanks
 

If by dropdown you mean a <select> element in HTML, by default those only have one column. However usually what you do is assign the value you need to pass to the next page to the actual value property of the <option> elements of the select. When an option is chosen, its value is taken, and sent when you submit the form it in.

For instance:
Code:
<select..>
<option value="val1">Text to Show</option>
<option value="val2">Text to Show again</option>
...
</select>

This however, has really nothing to do with PHP, its just HTML.

You can use PHP to create the dropdown from results from a DB, but the actual limitations of the elements is strictly HTML. PHP cannot really do anything to modify them.

forum215 for HTML questions.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Mike,

While I could not even begin to tell you anything about Access, may I suggest this method for "universal" selection sets

Code:
function reftableselect($table,$value,$orderby,$keyfield,$matching='',$matchcol='') {
	$link=OpenDB('');
	$sql = 'SELECT * FROM '.$table.' WHERE 1 ORDER BY `'.$orderby.'`;';
	if ($matching) { $sql = 'SELECT * FROM '.$table.' WHERE '.mysqli_real_escape_string($link, $matchcol).' = "'.mysqli_real_escape_string($link, $matching).'";'; }
	$doquery = DoQuery($sql,$link);
        $query=$doquery[0];
	$string = '<option value="">&nbsp;</option>';
	if($query != false) {
	 while ($row = mysqli_fetch_assoc($query)) {
	 $selected='';
	 if ($row[$keyfield] === $value) $selected = ' selected ';
	 $string .= '<option value="'.$row[$keyfield].'" '.$selected.'>'.$row[$orderby].'</option>';
	 }
	}
	return ($string);
function OpenDB($db='') {
	if(!isset($db) || $db === '') { $db = DBNAME; }
	$link = mysqli_connect(DBHOST,DBUSER,DBPWD,DBNAME);
	return($link);
}
function DoQuery($sql,$conn) 
{
	$query = mysqli_query($conn, $sql);	
	if (mysqli_connect_errno()) {
		printf("Connect failed: %s\n", mysqli_connect_error());
		exit();
	}
	$return=array($query,mysqli_insert_id($conn));
	return $return;		// Return Query Results
}

Modify above to suit Access "syntax" if you will (or swap to MySQL - lol)

Please notice "no error trapping" using try() | catch() ...

The use is very simple, within your HTML template or page, add this
Code:
<select..>
<?php echo reftableselect('[tablename]','[currently_selected_value]','[sort_order]','[keyfield]',['optional_match_column_value]','[optional_match_column_name]'); ?>
</select>

Note that you will need to replace above [] enclosed with actual variable strings or usable parameters. Those parameters where the method sets them to blank (ie: $matching='',$matchcol='') are "optional", you can omit them and use ONLY if looking to list set with rows where the "matching value" is found within the "match column" ...

Hope this gives you an added perspective of PHP and functionality of methods to create a more dynamic web page or application. ;-)



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top