Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7550763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:17:57+00:00 2026-05-30T10:17:57+00:00

I have a function that generates a dropdown based on a query or array

  • 0

I have a function that generates a dropdown based on a query or array that is passed to it (it does more than that, but it’s immaterial to this question). I currently have both working fine, but there is a lot of redundancy in the function to make both queries and arrays work and I’d like to streamline it.

What I’m wondering if there is a way to “cast” or convert a query result to be treated as an associative array instead of a resource without having to loop over the query results. By the way, we’re currently on ms sql with no plans to move to another RDBMS.

EDIT:
Here’s my code (a bit modified from production, but you’ll get the gist).

function phpSelect($varName, &$queryName,$valueField, $displayField, $selectedValue, $returnFullSelect = true, $additionalAttributes = "", $distinctOnly = false){
/**
 * Creates select dropdown from SQL results and selects given value
 */
    $thisSelect = '';
    $resultsArray = array();

    //If they request a full tag, start it here//
    if($returnFullSelect){
        $thisSelect = "<select name=\"$varName\" id=\"$varName\" " .$additionalAttributes .'>
        ';
        $thisSelect .= '    <option value=""> </option>
        ';// clean up - decide if we should put a blank option first or not
    }

    if(is_resource($queryName) ){  // use this for queries
        if( mssql_num_rows($queryName) == 0) return '<!-- Query passed was empty -->';//If the query is empty, just return empty string
        mssql_data_seek($queryName,0);
        //new dBug($queryName);
        $row_thisQuery = mssql_fetch_assoc($queryName);
        $totalRows_thisQuery = mssql_num_rows($queryName);


        do {
            $tempAlreadyExists = array_search(trim(strtoupper($row_thisQuery[$valueField])),$resultsArray);
            if(!$distinctOnly or !is_numeric($tempAlreadyExists) ){ // if Distinct is set to false or this value is not already in the array, add to the array, otherwise, skip it
                $thisOption = '<option value="' .$row_thisQuery[$valueField] .'"';
                if( is_string($selectedValue) ){ //perform case insensitive check
                    if( strtoupper( trim($row_thisQuery[$valueField]) ) == strtoupper( trim($selectedValue) ) ){
                        $thisOption .=" selected";
                    }
                } else {
                    if($row_thisQuery[$valueField] == $selectedValue){
                        $thisOption .=" selected";
                    }
                }
                $thisOption .='>';
                $thisOption .= $row_thisQuery[$displayField];
                $thisOption .= '</option>
                    ';
                $thisSelect .= $thisOption;

                array_push($resultsArray,trim( strtoupper($row_thisQuery[$valueField]) )  );
            } //End checking for distinct values
        } while ($row_thisQuery = mssql_fetch_assoc($queryName)); 

        if($returnFullSelect){
            $thisSelect .= '</select>
            ';
        }
        return $thisSelect;

    } elseif( is_array($queryName) ){  // use this for arrays

        foreach ($queryName as $i => $values) {
            $thisOption = '<option value="' .$values[$valueField] .'"';
            if($values[$valueField] == $selectedValue){
                $thisOption .=" selected";
            }
            $thisOption .='>';
            $thisOption .= $values[$displayField];
            $thisOption .= '</option>
                ';
            $thisSelect .= $thisOption;


        }


        //If they request a full tag, end it here//
        if($returnFullSelect){
            $thisSelect .= '</select>
            ';
        }
        return $thisSelect;
    } else { // didn't pass a query or array, return failure
        return false;   
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:17:58+00:00Added an answer on May 30, 2026 at 10:17 am

    Without lopping through here is no way to cast it all, however this is how you would do it looping, which is generally how everyone does it.

    $arr=array();
    while($row = mssql_fetch_assoc($result))
        $arr[]=$row;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that generates a random combination. my function looks like this:
I have a function that generates a key of 4 characters that has to
I have a function that receives three different people objects and generates a new
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I have a php function that generates HTML code like below function j_uf_SomeFunction($some_var) {
I have a function generate_username that generates a username (obviously). The values fname and
I have a function that generates a BouncyCastle RSA key pair. I need to
If I have a PHP function that generates a random number, is it possible
I have a JS function below that generates an href which includes the JQ
So I have function that formats a date to coerce to given enum DateType{CURRENT,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.