Skip to content
Advertisement

PHP MYSQL print

I have a list of results depending on the store numbers, each store number is printed with all its info, the first option “ALL” displays info from ALL the store numbers, I want the option “ALL” to just display info from store number 1, 2, and 3, not ALL of them.

This is my code:

<?php $store_number=$_REQUEST["store_number"]; ?>
<form name="selecciona" action="despl_probl.php" method="post">
<td> <select name="store_number" id="store_number">
<?php print(($store_number=='0'?'<option value="0" 
selected>ALL</option>':'<option value="0">ALL</option>')) ?>
<?php print(($store_number=='1'?'<option value="1" selected>Store 
1</option>':'<option value="1">Store 1</option>')) ?> 
<?php print(($store_number=='2'?'<option value="2" selected>Store 
2</option>':'<option value="2">Store 2</option>')) ?> 
<?php print(($store_number=='3'?'<option value="3" selected>Store 
3</option>':'<option value="3">Store 3</option>')) ?> 
<?php print(($store_number=='4'?'<option value="4" selected>Store 
4</option>':'<option value="4">Store 4</option>')) ?> 
<?php print(($store_number=='5'?'<option value="5" selected>Store 
5</option>':'<option value="5">Store 5</option>')) ?>      

Advertisement

Answer

To expand from my comment, you can do something such as the following to have a good way to display the data you need;

<form name="selecciona" action="despl_probl.php" method="post">
    <td> 
        <select name="store_number" id="store_number">
            <?php
            $store_number=$_REQUEST["store_number"];
            // Select the right option for this - will only print the one it finds
            switch ($store_number)
            {
                case "0" : {
                    print "<option value='0' selected>Store 0</option>";
                    break;
                }
                case "1" : {
                    print "<option value='1' selected>Store 1</option>";
                    break;
                }
                case "2" : {
                    print "<option value='2' selected>Store 2</option>";
                    break;
                }
                case "3" : {
                    print "<option value='3' selected>Store 3</option>";
                    break;
                }
                case "4" : {
                    print "<option value='4' selected>Store 4</option>";
                    break;
                }
                case "5" : {
                    print "<option value='5' selected>Store 5</option>";
                    break;
                }
                default : {
                    // Default to print the right one at the right time
                    print "<option value='{$store_number}'>Store {$store_number}</option>";
                }
            }
            ?>

A little neater, and better for the order, would be to do something such as below;

            <?php
            $store_number=$_REQUEST["store_number"];
            $max_option_num = 5;
            for ($i = 0; $i <= $max_option_num; $i++)
            {
                $selected = "";
                if ($i == $store_number) $selected = "selected";
                print "<option value='{$store_number}' {$selected}>Store {$store_number}</option>";
            }
            ?>

From new explanations;

            $store_number=$_REQUEST["store_number"];
            if ($store_number == "0")
            {
                print "<option value='0' selected>All</option>";
            }
            else
            {
                // Print the "All" first to have this there ready
                print "<option value='0'>All</option>";
                // These will be your stored & retrieved values
                $numbers_from_database = array(1, 4, 6, 28, 33, 56);
                foreach ($numbers_from_database as $store_num)
                {
                    $selected = "";
                    if (store_num == $store_number) $selected = "selected";
                    print "<option value='{$store_num}' {$selected}>Store {$store_num}</option>";
                }
            }
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement