Forum Moderators: phranque

Message Too Old, No Replies

where do i put the database script

         

Flolondon

6:08 pm on Jun 6, 2004 (gmt 0)

10+ Year Member



Please, i have an html script with a drop down menu and i want to now put a database script for a select function. The problem is where do I put the database script on the same page as the html tags.

Do i put it on top of the page before the ordinary html tag begins or what..

<html>
<head> </head>

</html>

coopster

11:56 am on Jun 7, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



One option is to build it, then insert it when necessary. Example using PHP:
<?php 
// execute database query and build list:
$select_list = '';
$sql = "SELECT list_item FROM table";
$rows = mysql_query($sql);
if (mysql_num_rows($rows) <> 0) {
while ($row = mysql_fetch_assoc($rows)) {
$select_list .= "<option value=\"" . htmlentities($row['list_item']) . '"';
if ($list_item == $row['list_item']) $select_list .= ' selected="selected"';
$select_list .= '>' . htmlentities($row['list_item']). '</option>';
}
}
mysql_free_result($rows);
?>
<html>
<head>
...
<form>
<select><?php print $select_list;?></select>
...
</html>

txbakers

3:39 pm on Jun 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It really depends where you need the data.

Generally I put everything script related above the <html> tag, but sometimes I need to loop through records, and use those values to call another recordset, so I'll have a connection/recrdset right in the middle of the page.

Remember, any scripting code is processed before the finished page is rendered.