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

8
  • You mean you need another query to get all the cities? Commented Jul 27, 2016 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. Commented Jul 27, 2016 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? Commented Jul 27, 2016 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 Commented Jul 27, 2016 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. Commented Jul 27, 2016 at 14:20

1 Answer 1

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>";
        }
    }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Yeahhh. Thanks Denis So my mistake was that I was closing double quotes after <select class='form-control'>"; but I wasn't adding the "echo" again before reopening them! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.