Page 1 of 1

sort and outout array values

Posted: Tue Mar 14, 2017 1:06 pm
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>' ;
    } 

Re: sort and outout array values

Posted: Tue Mar 14, 2017 2:24 pm
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>' ;
		}

Re: sort and outout array values

Posted: Tue Mar 14, 2017 7:24 pm
by requinix
You know that databases can sort for you, right?

Code: Select all

SELECT mfg FROM manufactures ORDER BY mfg