Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Casting" query to array
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>EDIT: Here's my code (a bit modified from production, but you'll get the gist).</p> <pre><code>function phpSelect($varName, &amp;$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 = "&lt;select name=\"$varName\" id=\"$varName\" " .$additionalAttributes .'&gt; '; $thisSelect .= ' &lt;option value=""&gt; &lt;/option&gt; ';// 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 '&lt;!-- Query passed was empty --&gt;';//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 = '&lt;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 .='&gt;'; $thisOption .= $row_thisQuery[$displayField]; $thisOption .= '&lt;/option&gt; '; $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 .= '&lt;/select&gt; '; } return $thisSelect; } elseif( is_array($queryName) ){ // use this for arrays foreach ($queryName as $i =&gt; $values) { $thisOption = '&lt;option value="' .$values[$valueField] .'"'; if($values[$valueField] == $selectedValue){ $thisOption .=" selected"; } $thisOption .='&gt;'; $thisOption .= $values[$displayField]; $thisOption .= '&lt;/option&gt; '; $thisSelect .= $thisOption; } //If they request a full tag, end it here// if($returnFullSelect){ $thisSelect .= '&lt;/select&gt; '; } return $thisSelect; } else { // didn't pass a query or array, return failure return false; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload