skip to Main Content

Display one or two column only from database in PHP.

I have a MySQL database in PHPMyAdmin. There is a table and in that table, there are multiple columns like salary, salary advance, etc. I want to display when user select salary it only show salary column, that is all data in the salary column. Here is my code. But it doesn’t work. Please help.

<?Php
error_reporting(0);
include "config_1.php";
echo "<form  method=post action='viewexpensebagnan.php' >
    <select name='cn'>
       <option value='None'>choose option </option>
       <option value='salary'> salary </option>
       <option value='sa'> sa </option>
       <option value='house'> house </option>
       <option value='gr'> gr </option>
       <option value='bo'> bo </option>
       <option value='goutam'> goutam </option>
    </select>
    <input type=submit ><br>
</form>";
if(value=='salary')
{
    $query="select salary from expensebagnan";
    $count=$dbo->prepare($query);
    $count->execute();
    $no=$count->rowCount();

    echo "<table class='border' style='text-align:center;' width='100%'  >";
    echo "</td><td>";
    echo "<tr><th>Salary</th></tr>";
    foreach ($dbo->query($query) as $row){           
       echo "<tr><td>$row[salary]</td></tr>";
    }


    echo "</table>";
}
?>

For example: If I select salary from the dropdown and submit then it will show only salary column data.

2

Answers


  1. There are so many issues with this code, but the thing why you cannot see the value from salary-key is that with this array-notation you need to wrap the array-access to curly-brackets, like this:

    echo "<tr><td>{$row['salary']}</td></tr>";
    
    Login or Signup to reply.
  2. Using Ajax you can do it easily on change event of the drop-down list you can send the request with the value of drop down to the PHP code and PHP code will return you the values of the selected option.

    Eg. Consider the following code (Put all code in one file, eg.index.php)

     <select id='value'>
       <option value='None'>choose option </option>
       <option value='salary'> salary </option>
       <option value='sa'> sa </option>
       <option value='house'> house </option>
       <option value='gr'> gr </option>
       <option value='bo'> bo </option>
       <option value='goutam'> goutam </option>
    </select>
    <button type=button value="submit" id="submit" > Submit </button>
    <div id="showData"></div>
    

    Now using jquery and ajax send the request to PHP as:

    <script type="text/javascript">
    
    $(document).ready(function() {
        $("#submit").click(function() {
            $.ajax({
                url : "getValues.php",
                cache: false,
                type: "get",
                data: "value=" + $("#value").val(),
                success: function(data) {
                    $("#showData").html(data);
                }
            });
        })
    });
    
    </script>
    

    Your PHP code may look like this (getValues.php):

    <?php
    if (isset($_REQUEST['value']) && $_REQUEST['value'] == 'salary')
    {
        $query = "select salary from expensebagnan";
        $count = $dbo->prepare($query);
        $count->execute();
        $no = $count->rowCount();
    
        echo "<table class='border' style='text-align:center;' width='100%'  >";
        echo "</td><td>";
        echo "<tr><th>Salary</th></tr>";
    
        foreach ($dbo->query($query) as $row){           
           echo "<tr><td>" . $row[salary] . "</td></tr>";
        }
    
        echo "</table>";
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search