0

I'm developing a simple application that allows to insert, modify and delete employees.

The problem I have is with the Person Edit Page. Here I have some text inputs and some dropdowns.

When I edit the person details I'm loading data from the DB and display them in the respective fields.

The problem is with the dropdowns, the query that contains the person details, contains data just for that person where as for the City perhaps, I would need to load all the cities from the DB, here an example:

<?php
if ($result2->num_rows > 0) {
  // output data of each row
    while($row = $result2->fetch_assoc()) {
          echo "<div class='form-group'>
                    <label>Name</label>
                    <input class='form-control' value='". $row["Name"] . "'>
                </div>
                <div class='form-group'>
                    <label>email</label>
                    <input class='form-control' value='". $row["email"] . "'>
                </div>
                <div class='form-group'>
                    <label>City</label>
                    <select class='form-control'>
                        <option selected>". $row["City"] . "</option>
                        <option>New York</option>
                        <option>Boston</option>
                        <option>San Francisco</option>
                    </select>
                </div>";
        }
    }
?>

In the "while", I loop $result2 which contains a query like this:

Select name, email, city from emp where id = 100;

When I edit the person details from the app, the city dropdown will load the right city but I would need to load all the cities (that I don't have in $result2) in order to be able to modify it.

Would be like a nested loop but I'm not sure how to implement it.

Any help appreciated

Thank you

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
ChrisA
  • 81
  • 11
  • You mean you need another query to get all the cities? – Denis Slonovschi Jul 27 '16 at 14:07
  • why don't you just use the select itself to output all other cities? It seems like you're hard-coding the other ones. – Funk Forty Niner Jul 27 '16 at 14:11
  • @Denis I guess, but I'm not sure how to implement it. Should have another php tag outside that one and loop another variable containing all the cities? – ChrisA Jul 27 '16 at 14:16
  • @Fred The other cities are hard coded as an example. I would need to get rid of those hard coded and load them from the DB actually. What do you mean use the select to output the cities? Thanks – ChrisA Jul 27 '16 at 14:17
  • @ChrisA before your existing query, write another one to get all your cities, and store them into an array. Instead of hardcoded HTML for the cities perform a foreach loop and echo your options in html. – Denis Slonovschi Jul 27 '16 at 14:20
  • See these Q&A's for examples http://stackoverflow.com/q/8022353/ --- http://stackoverflow.com/q/27881842/ --- http://stackoverflow.com/q/10009464/ - just a few here. You can base yourself on those. So, there's no need for a separate query/select. Plus, a ternary operator could also be of use. – Funk Forty Niner Jul 27 '16 at 14:22
  • @Denis I did that, I have all the cities stored in $result2, the problem is that I need to loop that $result2 in the php tag where I'm already looping $result in order to echo them. – ChrisA Jul 27 '16 at 14:27
  • The thing is, I should add a loop in the cities dropdown section but I'm inside an echo so it's not possible. – ChrisA Jul 27 '16 at 14:39

1 Answers1

0

Assuming you have your cities stored into an array $cities

<?php
if ($result2->num_rows > 0) {
  // output data of each row
    while($row = $result2->fetch_assoc()) {
          echo "<div class='form-group'>
                    <label>Name</label>
                    <input class='form-control' value='". $row["Name"] . "'>
                </div>
                <div class='form-group'>
                    <label>email</label>
                    <input class='form-control' value='". $row["email"] . "'>
                </div>
                <div class='form-group'>
                    <label>City</label>
                    <select class='form-control'>";
                        foreach($cities as $city){
                           echo "<option".(($city == $row["City"])?" selected":"").">". $city ."</option>";
                        }
                    echo "</select>
                </div>";
        }
    }
?>
Denis Slonovschi
  • 879
  • 9
  • 16
  • Yeahhh. Thanks Denis So my mistake was that I was closing double quotes after – ChrisA Jul 27 '16 at 15:10