Jump to content

PHP / MYSQL - Show results from Drop Down Menu


nicelad_uk

Recommended Posts

Hello everyone, I was wondering if anone could help with a query i'm finding hard, but I guess you will find easy.

 

I'm rather new to PHP but in my job I use SQL to maintain DB2 databases, so I ok in that I guess.

 

I'm developing a website and at the minute I'm trying to show a list of products I have for sale and have these stored in a database. At the moment they have the following fields:

PROD_NAME

MANUFACTURER

PRICE

DESCRIPTION

STOCK_CODE

 

I have used the following code to get the details to populate a 'Drop Down Menu':

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM products ORDER BY prod_name;";
$result=mysql_query($query);

mysql_close();

/* Create The Drop Down Menus */
echo "<select name=myselect>"; 
while($row=mysql_fetch_assoc($result)) { 
echo "<option value=$row[iD] print ($myselect==$row[iD]) ? 'SELECTED' : ''>$row[manufacturer]</a></option>"; 
} 
echo "</select>";

 

And then some PHP Code to show the results from the above SELECT statement.

 

What I am trying to do is depending what Manufacturer is selected from the Drop Down Menu, this then changes the select statment to show only the results with the Manufacturer selected.

 

Thanks for reading this and I hope you can help.

 

Rich

Link to comment
Share on other sites

Thanks, where is the best place to add this? I tried before the </select> but get the following error:

Parse error: syntax error, unexpected T_IF, expecting ',' or ';' in D:\Server\mncsolutions.com\database.php on line 31

 

Line 31 being if  ($myselect==$row[iD]) {echo " SELECTED'";

Link to comment
Share on other sites

echo "<select name=myselect>"; 
while($row=mysql_fetch_assoc($result)) { 
    echo "<option value=".$row[iD];
    if  ($myselect==$row[iD]) {echo " SELECTED'";}
    echo ''>".$row[manufacturer]."</a></option>"; 
} 
echo "</select>";

Link to comment
Share on other sites

Thanks jvrothjr,

I don't know how to get this to reload the results though, this is the problem I'm having, its me that doesnt really know what im doing, id be better leaving it to the pros, but I only need this page.

 

Could anyone help further, making this so when the drop down menu is selected, it brings through the results.

 

This is what I have in total:

 

<?php include 'header.php' ; ?>

<!--  This is the main content table -->
<table width="800" bgcolor="#808080" border="0" cellpadding="0" cellspacing="0" align="center" class="main">
  <tr class="mainmargin">
<td>
<br /><br />

    <?php
$username="root";
$password="";
$database="test";

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM products ORDER BY prod_name;";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<select name=myselect>"; 
while($row=mysql_fetch_assoc($result)) { 
    echo "<option value=".$row[iD];
    if  ($myselect==$row[iD]) {echo " SELECTED'";}
    echo ''>".$row[manufacturer]."</a></option>"; 
} 
echo "</select><br /><br />";

$i=0;
while ($i < $num) {

$prod_name=mysql_result($result,$i,"prod_name");
$category=mysql_result($result,$i,"category");
$manufacturer=mysql_result($result,$i,"manufacturer");
$description=mysql_result($result,$i,"description");
$retail_price=mysql_result($result,$i,"retail_price");
$picture=mysql_result($result,$i,"picture");
$stock_code=mysql_result($result,$i,"stock_code");

echo "Product: <b>$prod_name</b><br>
Category: $category<br>
Manufacturer:  $manufacturer<br>
Description: $description<br>
Retail Price: £$retail_price<br>
<img src=\"productimages/" . $picture . "\" border=0 alt=\"" . $picture . "\"><br>
Code: $stock_code<br><hr><br>";

$i++;
}

?>
    <br /><br />
    </td>
  </tr>
</table>

<?php include 'footer.php' ; ?>

Link to comment
Share on other sites

<?php include 'header.php' ; ?>

<!--  This is the main content table -->
<table width="800" bgcolor="#808080" border="0" cellpadding="0" cellspacing="0" align="center" class="main">
  <tr class="mainmargin">
<td>
<br /><br />

    <?php
$username="root";
$password="";
$database="test";

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM products ORDER BY prod_name;";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<form name='form1' method='post'>";
echo "<select name='myselect' onchange=form1.submit()>";
while($row=mysql_fetch_assoc($result)) { 
    echo "<option value=".$row[iD];
    if  ($myselect==$row[iD]) {echo " SELECTED";}
    echo ">".$row[manufacturer]."</a></option>"; 
} 
echo "</select><br /><br />";
echo "</form>";
$i=0;
while ($i < $num) {

$prod_name=mysql_result($result,$i,"prod_name");
$category=mysql_result($result,$i,"category");
$manufacturer=mysql_result($result,$i,"manufacturer");
$description=mysql_result($result,$i,"description");
$retail_price=mysql_result($result,$i,"retail_price");
$picture=mysql_result($result,$i,"picture");
$stock_code=mysql_result($result,$i,"stock_code");

echo "Product: <b>$prod_name</b><br>
Category: $category<br>
Manufacturer:  $manufacturer<br>
Description: $description<br>
Retail Price: £$retail_price<br>
<img src="productimages/" . $picture . "\" border=0 alt=\"" . $picture . "\"><br>
Code: $stock_code<br><hr><br>";

$i++;
}

?>
    <br /><br />
    </td>
  </tr>
</table>

<?php include 'footer.php' ; ?>

Link to comment
Share on other sites

Thank for that, thats got the auto submitting working a treat, the only thing I cant do is get it to remember what selection was selected and query it in a SELECT statement.

 

I had a bash at it last night and thoght I had it with your auto submit added, but no :(

 

Could anyone point me in the right direction with this....

 

<?php include 'header.php' ; ?>

<!--  This is the main content table -->
<table width="800" bgcolor="#808080" border="0" cellpadding="0" cellspacing="0" align="center" class="main">
  <tr class="mainmargin">
<td>
<br /><br />

    <?php
$username="root";
$password="";
$database="test";

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$dropquery="SELECT DISTINCT(manufacturer) FROM products;";
$dropdownlist=mysql_query($dropquery);

$query = 'SELECT * FROM products WHERE manufacturer = "'.$_POST['myselect'].'"';
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<form name='form1' method='post'>";
echo "<select name='myselect' onchange=form1.submit()>";
while($row=mysql_fetch_assoc($dropdownlist)) { 
    echo "<option value=".$row[iD];
    if  ($myselect==$row[iD]) {echo " SELECTED";}
    echo ">".$row[manufacturer]."</a></option>"; 
} 
echo "</select><br /><br />";
echo "</form>";
$i=0;
while ($i < $num) {

$prod_name=mysql_result($result,$i,"prod_name");
$category=mysql_result($result,$i,"category");
$manufacturer=mysql_result($result,$i,"manufacturer");
$description=mysql_result($result,$i,"description");
$retail_price=mysql_result($result,$i,"retail_price");
$picture=mysql_result($result,$i,"picture");
$stock_code=mysql_result($result,$i,"stock_code");

echo "Product: <b>$prod_name</b><br>
Category: $category<br>
Manufacturer:  $manufacturer<br>
Description: $description<br>
Retail Price: £$retail_price<br>
<img src=\"productimages/" . $picture . "\" border=0 alt=\"" . $picture . "\"><br>
Code: $stock_code<br><hr><br>";

$i++;
}

?>
    <br /><br />
    </td>
  </tr>
</table>

<?php include 'footer.php' ; ?>

Link to comment
Share on other sites

Hello,

 

I managed to get it to show something, but not the actual result that is selected.

 

Can anyone point me in the right direction for this, so the select statement changes to what it is selected from the drop down menu, I think its something to do with the $_POST but I cant figure it out

 

Thanks

Link to comment
Share on other sites

Thanks jvrothjr, you've been a great help. All I need to do now is make it so when the page refreshes, it selects the actual selection that was made. At the minute, the menu just goes back to the 1st option in there, any ideas?

 

<?php include 'header.php' ; ?>

<!--  This is the main content table -->
<table width="800" bgcolor="#808080" border="0" cellpadding="0" cellspacing="0" align="center" class="main">
  <tr class="mainmargin">
<td>
<br /><br />

    <?php
$username="root";
$password="";
$database="test";

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$dropquery="SELECT DISTINCT(manufacturer) FROM products;";
$dropdownlist=mysql_query($dropquery);

$query = 'SELECT * FROM products WHERE manufacturer = "'.$_POST['myselect'].'"';
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo $query;
echo "<form name='form1' method='post' action='$PHP_SELF'>";
echo "<select name='myselect' onchange=form1.submit()>";
while($row=mysql_fetch_assoc($dropdownlist)) { 
    echo "<option value=".$row[iD];
    if  ($_POST[myselect]==$row[iD]) {echo $row[manufacturer];}
    echo ">".$row[manufacturer]."</a></option>"; 
} 
echo "</select><br /><br />";
echo "</form>";
$i=0;
while ($i < $num) {

$prod_name=mysql_result($result,$i,"prod_name");
$category=mysql_result($result,$i,"category");
$manufacturer=mysql_result($result,$i,"manufacturer");
$description=mysql_result($result,$i,"description");
$retail_price=mysql_result($result,$i,"retail_price");
$picture=mysql_result($result,$i,"picture");
$stock_code=mysql_result($result,$i,"stock_code");

echo "Product: <b>$prod_name</b><br>
Category: $category<br>
Manufacturer:  $manufacturer<br>
Description: $description<br>
Retail Price: £$retail_price<br>
<img src=\"productimages/" . $picture . "\" border=0 alt=\"" . $picture . "\"><br>
Code: $stock_code<br><hr><br>";

$i++;
}

?>
    <br /><br />
    </td>
  </tr>
</table>

<?php include 'footer.php' ; ?>

Link to comment
Share on other sites

Needs to be

(This one will show the selected one

echo "<form name='form1' method='post' action='$PHP_SELF'>";
echo "<select name='myselect' onchange=form1.submit()>";
while($row=mysql_fetch_assoc($dropdownlist)) { 
    echo "<option value=".$row[iD];
    if  ($_POST[myselect]==$row[iD]) {echo " SELECTED";}
    echo ">".$row[manufacturer]."</a></option>"; 

 

or

(This one will show the first as the selection)

echo "<form name='form1' method='post' action='$PHP_SELF'>";
echo "<select name='myselect' onchange=form1.submit()>";
if ($_POST[myselect] != Null)   {echo "<option value=".$_POST[myselect].">".$_POST[myselect]."</a></option>";}
while($row=mysql_fetch_assoc($dropdownlist)) { 
    echo "<option value=".$row[iD].">".$row[manufacturer]."</a></option>"; 
} 
echo "</select><br /><br />";
echo "</form>";

 

 

and not

echo "<form name='form1' method='post' action='$PHP_SELF'>";
echo "<select name='myselect' onchange=form1.submit()>";
while($row=mysql_fetch_assoc($dropdownlist)) { 
    echo "<option value=".$row[iD];
    if  ($_POST[myselect]==$row[iD]) {echo $row[manufacturer];}
    echo ">".$row[manufacturer]."</a></option>"; 
} 
echo "</select><br /><br />";
echo "</form>";

Link to comment
Share on other sites

Thanks for your help with this again, the only problem is the 2 new statements you have put in dont work on my script.

 

The first one changes the select statement to WHERE manufacturer = "selected"

and the second one doesnt show any results and doesnt change the select statement.

 

Any ideas?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.