I have a function as follows to populate aselect
input.
$query1 = "SELECT id, address_line1, address_line2, city, state, country FROM addresses";
if($stmt = $connection->prepare($query1)){
$stmt->execute();
$stmt->bind_result($id, $addressLine1, $addressLine2, $city, $state, $country);
while($stmt->fetch()){
$addressOptions .= '<option value="'.$id.'">'.$addressLine1.", ".$addressLine2.", ".$city.", ".$state.", ".$country.'</option>';
}
$stmt->close();
}
This works fine except now I want to make it so it doesn't make an<option>
if it has an id, that is in one of the objects in my foreach loop.
This is what i have tried
$query1 = "SELECT id, address_line1, address_line2, city, state, country FROM addresses";
if($stmt = $connection->prepare($query1)){
$stmt->execute();
$stmt->bind_result($id, $addressLine1, $addressLine2, $city, $state, $country);
while($stmt->fetch()){
foreach($takenAddresses as $taken) {
if($id != $taken['id']) {
$addressOptions .= '<option value="'.$id.'">'.$addressLine1.", ".$addressLine2.", ".$city.", ".$state.", ".$country.'</option>';
}
}
}
$stmt->close();
}
```
The issue I am facing is that it shows the correct options, but each of them multiple times. If I switch it to `==` then it shows the options i want to NOT be there. But `!=` shows the options I want to be there but duplicated
EDIT
```
$query1 = "SELECT id, address_line1, address_line2, city, state, country FROM addresses";
if($stmt = $connection->prepare($query1)){
$stmt->execute();
$stmt->bind_result($id, $addressLine1, $addressLine2, $city, $state, $country);
while($stmt->fetch()){
if(!in_array($id, $takenAddresses)) {
$addressOptions .= '<option value="'.$id.'">'.$addressLine1.", ".$addressLine2.", ".$city.", ".$state.", ".$country.'</option>';
}
}
$stmt->close();
}
```
You can filter the ids you don't want directly from the sql query using aWHERE NOT IN (...)
clause.
This is a bit tricky to parameterize aWHERE NOT IN ()
query, but, based on this excellent answer this can be done this way :
// You want a WHERE NOT IN (?, ?, ?...) with as much ? as there are elements in your array $takenAddresses
// extract the unwanted ids from the original array :
$unWantedIds = [];
foreach($takenAddresses as $taken) {
$unWantedIds[] = taken['id'];
}
$params = implode(",", array_fill(0, count($unWantedIds), "?")); // This provide a string "?,?,?,?" if you have 4 elements in $unWantedIds
// TODO : Ensure that $params or $unWantedIds aren't empty
// prepare the query
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.