PDA

View Full Version : Web Pages...again!


ianhull
06-14-2005, 10:47 AM
Can anyone point me in the right direction to populate a dropdown menu from a mysql database table using php

I would like to select a companyname from the dropdown and then post this value to the next page.

here is what I have but it only return 1 record

<select name="select_company">
<option value="0" selected>Select Dept</option>

<?php include("connect.php");

$s_id = $_GET['s_id'];

$query = "select DISTINCT companyname from Customers";

$result = mysql_query($query);

$num_results = mysql_num_rows($result);
for
($i=0; $i <$num_results; $i++)

$row = mysql_fetch_array($result);

$companyname = $row['companyname'];?> <option value="CompanyName">

<?php echo $companyname; ?> </option>
<?php

#125;?> </select>


any help will be great.

Thanks in advance

Ted Sullivan
06-14-2005, 12:00 PM
Admin Note: Moved topic. Please post these sort of questions in the Developer's Den.

Corey
06-14-2005, 01:52 PM
In PHP the structure of a for loop is like this:

for ( initialize a counter; conditional statement; increment a counter){
do this code;
}

You have to put your condition in the first set of parantheses and then all your code "to be executed" in the curly braces. :yes

eric_darling
06-14-2005, 03:49 PM
Corey, you have a knack for describing PHP better than any other source I've run across - when will you get to doing a CD on PHP? I know you're not without commitments, but I beg you to shave an hour off a week for a few months to knock something out along these lines! Just a good intro. to PHP course would be all I need to get rolling.

Corey
06-14-2005, 03:51 PM
Thanks. FWIW I pasted that from the web but I get your point, sometimes it's nice to cut the chaff. I bogged myself down bad this year and am currently climbing out of the rubble, but actually I got some work done on the first PHP training CD this weekend, I'll make sure to keep you posted. :)

eric_darling
06-14-2005, 04:32 PM
Yeah - it's not that I can't find good info. on PHP all over the web - it's how do I know what to look for in the first place? Like you said - cutting the the chase is so crucial. There's just no time in my life for learning what I need to know to begin learning something. So exasperating.

Corey
06-14-2005, 05:01 PM
True, I would say it's time someone did the same for video too. [wiggles eyebrows]

But seriously if you ever do a training CD we would gladly stock it at Speedy for 50% of net. Heck if you did some good ones we'd probably even meet you halfway on some advertising. :yes

ianhull
06-16-2005, 07:26 AM
Thanks Corey,

I now have a new problem if anyone can help

I have this on my page but it is only printing the first record in the echo $address

Can anyone help me in updating the echo $address each time a new company is selected?

Thanks in advance

Here is what I have:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><?php
include_once("connect.php");

?>
<title>Untitled Document</title>
<meta **********="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
body,td,th {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 12px;
color: #666666;
}
.style9 {
color: #FFFFFF;
font-weight: bold;
}
.style10 {color: #000000}
-->
</style></head>

<body>
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td><table width="100%" cellpadding="0">
<tr>
<td width="9%"><a href="index.php"><img src="Images/home.jpg" width="88" height="72" border="0" /></a></td>
<td width="9%"><a href="view_companies.php"><img src="Images/viewcustomers.jpg" width="88" height="72" border="0" /></a></td>
<td width="9%"><a href="add_company.php"><img src="Images/addcustomers.jpg" width="88" height="72" border="0" /></a></td>
<td width="9%"><a href="send_letter.php"><img src="Images/sendletter.jpg" width="88" height="72" border="0" /></a></td>
<td width="9%"><a href="add_letter.php"><img src="Images/addletter.jpg" width="88" height="72" border="0" /></a></td>
<td width="9%"><a href="send_email.php"><img src="Images/sendemail.jpg" width="88" height="72" border="0" /></a></td>
<td width="10%"><div align="left"><a href="logout.php"><img src="Images/logout.jpg" width="88" height="72" border="0" /></a></div></td>
<td width="36%" valign="bottom"></td>
</tr>
<tr bgcolor="#D51B0C">
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><span class="style9">
</span>
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="3%"><span class="style9">
</span></td>
<td width="97%"><span class="style9">
</span></td>
</tr>
</table> </td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="68%"><table width="97%" cellspacing="0" cellpadding="0">
<tr>
<td width="2%" height="75" valign="top"><div align="left"><span class="style10"></span></div></td>
<td width="98%" valign="top"><table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td><strong>Today is:</strong>
<?php
echo date("l dS of F Y h:i:s A");
?>
</td>
</tr>
<tr>
<td class="style10">&nbsp;</td>
</tr>
<tr>
<td class="style10">&nbsp;</td>
</tr>
<tr>
<td class="style10">&nbsp;</td>
</tr>
<tr>
<td class="style10">Please Select a Company </td>
</tr>
</table></td>
</tr>
<tr>
<td height="30"><div align="left"></div></td>
<td valign="top">
<?php
$sql = "SELECT DISTINCT companyname, address from customers ORDER BY companyname";

$result = mysql_query($sql)
or die ("Couldn't select Companies");

echo "

<select name='companyname'>\n";

//-------------get each event type ------

while ($line = mysql_fetch_array($result))
{
extract($line); // extracts all line into variables with same name as fields
echo "<option value='$companyname' ";
if ($companyname==$companyname) echo "selected";
echo ">$companyname\n";
}
echo "</select> </td></tr>\n";


?>
</td>
</tr>
<tr>
<td><div align="left"></div></td>
<td>&nbsp;
</td>
</tr>
<tr>
<td><div align="left"></div></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><div align="left"></div></td>
<td>The Address for the company you selected is: <?php echo $address ?></td>
</tr>
</table></td>
<td width="32%">
</td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>

Corey
06-16-2005, 01:48 PM
Just at a glance I'd say this is one problem:

if ($companyname==$companyname)

That statement will always result in "true" when compared because you are assessing a value against itself...