Jump to content

php echoing in html form list box


unistake

Recommended Posts

Hi all,

 

I have a html form list box that needs to show all the types of products that are for sale and have a number in the list box saying how many of that type are for sale.

 

eg.

If the mysql database looks like this

productType  |  productDetails

chair              |  outdoor large chair

table              |  2m x 1m

chair              |  indoor chair

chair              |  childs chair

 

I would want the form list box to show the values inside as:

chair (3)

table (1)

 

could someone give me the heads up on how to do this

 

Thanks

 

 

 

Link to comment
Share on other sites

You'd first use an SQL query like this

SELECT productType, COUNT(productType) as productQuantity FROM products_table GROUP BY productType

That will select the product type and count how many there are, eg 3 chairs, 1 table

 

Now in PHP you'd do something like this

$sql = 'SELECT productType as type, 
               COUNT(productType) AS quantity 
        FROM products_table 
        GROUP BY productType';
        
$result = mysql_query($sql);

if(mysq_num_rows($result) > 0)
{
    echo '<select name="product">';
    while($product = mysql_fetch_assoc($result))
    {
        echo sprintf('<option value="%1$s">%1$s (%2$d)</option>', $product['type']
                                                                , $product['quantity']);
    }
    echo '</select>';
}
else
{
    echo 'No products to list';
}

Link to comment
Share on other sites

Works great but the quantity is always showing '0'.

 

Do you know why that would be, there are more than one type of 'manufactuer' with the same name in the database.

 

Must be something wrong with $manufactuer['quantity'] but looks fine to me.

 

<?php
$sql = "SELECT manufacturer as type, COUNT(manufacturer) AS quantity FROM sales GROUP BY manufacturer";
        
$result = mysqli_query($cxn,$sql)
or die ("I just cant do it capt'n!");

if(mysqli_num_rows($result) > 0)
{
    echo '<select name="manufacturer">';
    while($manufacturer = mysqli_fetch_assoc($result))
    {
        echo sprintf('<option value="%1$s">%1$s (%d)</option>', $manufacturer['type']
                                                              , $manufacturer['quantity']);
    }
    echo '</select>';
?>

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.