I've used this many times and it's always worked, however for some reason I'm missing something.

$ddType_query="SELECT DISTINCT Type from dbo.LTN_HARDWARE";
$ddType_result=odbc_exec($conn, $ddType_query);

while ($ddTypeOptionsRows = odbc_fetch_array($ddType_result)) {
//echo $ddTypeOptionsRows['Type'], "\n";
$ddTypeOptions = $ddTypeOptionsRows['Type'] ;

}

odbc_close($conn);

I'm trying to get the $ddTypeOptions to contain all the distinct results of the Type column in LTN_HARDWARE. It's only returning one value and I know there are more.

If I echo $ddTypeOptionsRows it shows correctly so I know it's something with assigning the value to $ddTypeOptions.

Any help would be GREAT!

Thank you.

Recommended Answers

All 7 Replies

Member Avatar for diafol

I am not aware of the odbc... functions, but mysql functions have a mysql_num_rows($resultset) which returns the number of records in the resultset. If you have a similar function, echo it out - maybe something like:

echo "Num recs: " . odbc_num_rows($resultset) . "<br />";

ardav

Thanks a lot for the reply.

There is an odbc_num_rows function and using it I can see that it does in fact return more than one row. My issue is that when trying to assign those values to the $ddTypeOptions variable, I only get 1 value.

Thanks again1

Member Avatar for diafol

Ah. I think I've spotted a mistake:

echo $ddTypeOptionsRows['Type'], "\n"

SHould be:

echo $ddTypeOptionsRows['Type'] . "<br />";

Sorry, that part of coding was in there from my testing, it should have been removed as I had it commented out.
Here is my active code:

$ddType_query="SELECT DISTINCT Type from dbo.LTN_HARDWARE";
    $ddType_result=odbc_exec($conn, $ddType_query);
     
    while ($ddTypeOptionsRows = odbc_fetch_array($ddType_result)) {
    $ddTypeOptions = $ddTypeOptionsRows['Type'] ;
     }
     
    odbc_close($conn);

Thanks again.

Member Avatar for diafol

Ok well, you're overwriting $ddTypeOptions every time. You can do this:

$ddTypeOptions = "";
while ($ddTypeOptionsRows = odbc_fetch_array($ddType_result)) {
    $ddTypeOptions .= $ddTypeOptionsRows['Type'] . "<br/>";
}
...
echo $ddTypeOptions;

Everything you have said has worked, so maybe I'm just not explaining this correctly.

In the end, I want to be able to echo $ddTypeOptions for <SELECT> options.

So it would be like this:

$all_query="SELECT * from dbo.LTN_HARDWARE";                                  
                                                          
$all_result=odbc_exec($conn, $all_query);                                    
                                                                   
                                                                              
//      SETUP SOME VARIABLES FOR THE DROPDOWN                                 
                                                                               
while ($all_rows = odbc_fetch_array($all_result)) {                                                                                     
$hardwareID =$all_rows['HARDWARE_ID'];                                       
$hardwareType = $all_rows['Type'];                                            
$hardwareOptions = "<option value=\"$ddTypeOptions\">".$ddTypeOptions.'</option>';
}                                                                            
                                                                                                                                       
odbc_close($conn);                                                            
                                                                              
?>

<form name="form1" method="post" action="mmupdate_ac.php">
<table border="0" align="center" cellspacing="1" cellpadding="0">
<tr>
  <th>Make / Model</th><th>Type</th>
</tr>
<tr>
  <td><input name="MM" size="20" type="text" id="MM" value="<?php echo $current_rows['Make_Model']; ?>" > </td>
  <td><select name="Type">
      <option value="<?php echo $currentHardware; ?>" selected="selected"> <?php echo $current_rows['Type'];?></option>
      <?php echo $hardwareOptions; ?>
      </select>

Does that make more sense?

Member Avatar for diafol

It would help considerably if you explained your issue at the start...

$ddTypeOptions .= "<option value= \"{$someotherfield}\">{$ddTypeOptionsRows['Type']}</option>";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.