sort and outout array values

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

Post Reply
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

sort and outout array values

Post by me! »

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

Re: sort and outout array values

Post by me! »

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>' ;
		}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: sort and outout array values

Post by requinix »

You know that databases can sort for you, right?

Code: Select all

SELECT mfg FROM manufactures ORDER BY mfg
Post Reply