PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Apr 28, 2017 3:23 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Mar 14, 2017 1:06 pm 
Offline
Forum Contributor

Joined: Sat Nov 04, 2006 9:45 pm
Posts: 133
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.

Syntax: [ Download ] [ Hide ]
$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>' ;
    }


Top
 Profile  
 
PostPosted: Tue Mar 14, 2017 2:24 pm 
Offline
Forum Contributor

Joined: Sat Nov 04, 2006 9:45 pm
Posts: 133
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:
Syntax: [ Download ] [ Hide ]
                  $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>' ;
                }


Top
 Profile  
 
PostPosted: Tue Mar 14, 2017 7:24 pm 
Online
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6397
Location: WA, USA
You know that databases can sort for you, right?
Code:
SELECT mfg FROM manufactures ORDER BY mfg


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group