PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
me!
Forum Contributor
Posts: 133 Joined: Sat Nov 04, 2006 8:45 pm
Post
by me! » Tue Mar 14, 2017 1:06 pm
I have looked at and tried several examples but keep getting an output of "Array" no matter how I try it?
What it want to happen is get values from DB and put into array, then sort the array, then output all of the array for a drop down list.
Code: Select all
$mfg_array = array();
$stmt = $db->prepare('SELECT mfg FROM manufactures');
$stmt->execute();
// put db values into array
while($row = $stmt->fetch()) {
$mfg_array[] = $row;
}
// sort array
sort($mfg_array);
// output array
foreach ($mfg_array as $mfg) {
echo '<option value="'.$mfg.'">'.$mfg.'</option>' ;
}
me!
Forum Contributor
Posts: 133 Joined: Sat Nov 04, 2006 8:45 pm
Post
by me! » Tue Mar 14, 2017 2:24 pm
Got it!
It had nothing to do with the output but it was bad creation of the array in the first place.
Went to this:
Code: Select all
$mfg_array = array();
$stmt = $db->prepare('SELECT mfg FROM manufactures');
$stmt->execute();
while($row = $stmt->fetch()) {
$mfg_array[] = $row['mfg'];
}
sort($mfg_array);
foreach ($mfg_array as $mfg) {
echo '<option value="'.$mfg.'">'.$mfg.'</option>' ;
}
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Mar 14, 2017 7:24 pm
You know that databases can sort for you, right?
Code: Select all
SELECT mfg FROM manufactures ORDER BY mfg