-2

I'm getting data from my database, i want to show one field into a dropdown menu and the rest(according to the selected user in the dropdown menu) in a paragraph below it as im trying to do in the code below, what is missing please?

<div class="w3-display-bottomleft w3-container w3-text-black">

<?php
    echo "<select name='users' id='users' placeholder='Employee'>";
    while ($row = mysqli_fetch_array( $result, MYSQLI_ASSOC)) {
        echo "<option value='" . $row['id'] ."'>" . $row['name'] ."</option>";
    }
    echo "</select>";

    echo"</div>";
    echo"</div>";
    echo"<div class='w3-container'>";
    while ($row = mysqli_fetch_array( $result, MYSQLI_ASSOC)) {

        echo "<p><i class='fa fa-briefcase fa-fw w3-margin-right w3-large w3-text-teal'></i>". $row['department'] ."</p>";
        echo "<p><i class='fa fa-home fa-fw w3-margin-right w3-large w3-text-teal'></i>Malta</p>";
        echo "<p><i class='fa fa-envelope fa-fw w3-margin-right w3-large w3-text-teal'></i>user@email.com</p>";
    }

?>
      <hr>

I have now tried with a foreach loop, but i need it to only display the data of the selected user. How can i do that? (code below)

    <div class="w3-display-bottomleft w3-container w3-text-black">

<?php
    $my_rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
        echo "<select name='users' id='users' placeholder='Employee'>";
        foreach($my_rows as $row) {
    echo "<option value='" . $row['id'] ."'>" . $row['name'] ."</option>";
        }
        echo "</select>";

        echo"</div>";
        echo"</div>";
        echo"<div class='w3-container'>";
        foreach($my_rows as $row) {

            echo "<p value='" . $row['id'] ."'><i class='fa fa-briefcase fa-fw w3-margin-right w3-large w3-text-teal'></i>". $row['department'] ."</p>";
            echo "<p><i class='fa fa-home fa-fw w3-margin-right w3-large w3-text-teal'></i>Malta</p>";
            echo "<p><i class='fa fa-envelope fa-fw w3-margin-right w3-large w3-text-teal'></i>user@email.com</p>";
            }

                    ?>
        <hr>
Julian Zahra
  • 63
  • 1
  • 9
  • 1
    What is the current result of your code? How does it differ from the desired result? – Patrick Q Nov 27 '19 at 15:13
  • right now im only getting the dropdown menu with the db values, so thats working. Now i want the paragraph below it to display the other fields in the db according to the selected value in the dropdown menu – Julian Zahra Nov 27 '19 at 15:15
  • *"what is missing please?"* ... if you're trying to update the data in the paragraphs based on what's selected in the dropdown, then what's missing is a basic understanding of how [server/client](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) web applications actually work. PHP runs on the server, nothing you do in the HTML (client) can affect it in any way... unless you make another request to the server - possibly using Ajax. – CD001 Nov 27 '19 at 15:16
  • Then you're going to need to implement ajax to fetch the related values based on the selected option. Do some research on that (there are plenty of questions/answers here, plus off-site tutorials/guides) and make an attempt. If you get stuck, feel free to update your question with your attempted code and the result. – Patrick Q Nov 27 '19 at 15:17
  • 1
    Your first while loop comsumes ALL the resultset so the second query has nothing left to process – RiggsFolly Nov 27 '19 at 15:23
  • How can i change that RiggsFolly ? Thanks – Julian Zahra Nov 27 '19 at 15:26

1 Answers1

0

Instead of calling mysqli_fetch_array() twice which will consume the entire resultset, use mysqli_fetch_all() to get an array of results, then use a foreach loop over those results instead of a while loop.

<div class="w3-display-bottomleft w3-container w3-text-black">

<?php
    //Safely get the currently selected user, assuming GET for now
    $currently_selected_user_id = filter_input(INPUT_GET, 'users', FILTER_SANITIZE_NUMBER_INT);

    $my_rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
    echo "<select name='users' id='users' placeholder='Employee'>";
    foreach($my_rows as $row) {
        echo "<option value='" . $row['id'] ."'>" . $row['name'] ."</option>";
    }
    echo "</select>";

    echo"</div>";
    echo"</div>";
    echo"<div class='w3-container'>";
    foreach($my_rows as $row) {
        //Using the weak equality but ideally this would be !==
        if($currently_selected_user_id != $row['id']){
            continue;
        }
        echo "<p><i class='fa fa-briefcase fa-fw w3-margin-right w3-large w3-text-teal'></i>". $row['department'] ."</p>";
        echo "<p><i class='fa fa-home fa-fw w3-margin-right w3-large w3-text-teal'></i>Malta</p>";
        echo "<p><i class='fa fa-envelope fa-fw w3-margin-right w3-large w3-text-teal'></i>user@rhinocerosltd.com</p>";
    }

?>
      <hr>
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • " Now i want the paragraph below it to display the other fields in the db according to the selected value in the dropdown menu". I don't think OP wants to display _all_ the results in the second section. Only the result for the selected user. – Patrick Q Nov 27 '19 at 15:56
  • Yes i want to display only the data of the selected user, not all. But thanks for the reply. I believe i will still need to use a foreach anyway so i can use the array more than once. – Julian Zahra Nov 27 '19 at 16:24
  • To the OP, this has been updated to include a check assuming that you've got form logic that submits the ` – Chris Haas Nov 27 '19 at 17:08
  • only the dropdown menu is being displayed with that code – Julian Zahra Nov 28 '19 at 09:17
  • @JulianZahra, try selecting a user. You'll need to include actual `
    ` code and probably JavaScript to auto-submit on select.
    – Chris Haas Dec 02 '19 at 14:37