I have an array
$materials
How do make it so I can take that array and find only unique instances of manufacturers
foreach($materials as $material) {
echo "\r\n";
echo '<option value="'.$material['manufacturer'].'">';
}
displays
<option value="Widget">
<option value="Widget">
<option value="Dell">
<option value="Dell">
<option value="Dell">
<option value="Dell">
<option value="Dell">
<option value="Widget">
<option value="Widget">
<option value="Widget">
<option value="Widget">
<option value="AMC">
Is the array coming from a database?
yes, do I need to run another qury
Or can I work manpulate
$materials
This can, and should be handled at the database level. I suspect your database design may have a problem. We would need to see your DB schema to give you the correct answer.
The manufacturers should be in one table and each manufacturer should only be listed once with a unique ID number.
my table
CREATE TABLE materials (
material_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
category ENUM('0','1','2','3','4','5','6','7','8','9','10'),
manufacturer VARCHAR(50),
model VARCHAR(50),
width DECIMAL(6,2),
height DECIMAL(6,2),
depth DECIMAL(6,2),
plugs TINYINT UNSIGNED,
ports TINYINT UNSIGNED,
number_of_rows TINYINT UNSIGNED,
number_of_columns TINYINT UNSIGNED,
row TINYINT UNSIGNED,
col TINYINT UNSIGNED,
PRIMARY KEY ( material_id )
);
the new query/array
$sql = "SELECT UNIQUE(manufacturer) FROM materials";
//echo $sql;
$result = $pdo->query($sql);
$material_manufacturers = $result->fetchAll(PDO::FETCH_ASSOC);
As I mentioned previously, the manufacturers should be in their own table with a unique id. That ID should then be referenced in your material table.
When you find yourself duplicating data, that’s a sure sign that there is a problem with your database design.
Look up database normalization to see how you should be doing it.
system
Closed
June 30, 2024, 9:45am
7
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.