Page 1 of 1

Is there a better way? mssql

Posted: Wed Jan 13, 2010 11:22 pm
by jeremyclover
I'm trying to have one query populate what will eventually be multiple input list. Right now I'm just cycling through the arrays. I want each list to be filtered differently based on the ITEM_SRC field.

I feel there has to be a better way to do this. Thoughts?

Code: Select all

    $query = "SELECT ITEM_SRC, ITEM_ID, ITEM_DESC ";
    $query .= "FROM vWEB_LIST ";
 
    //execute the SQL query and return records
    $result = mssql_query($query);
 
    //Return AD_SIZE list
    while($row = mssql_fetch_array($result))
    {
        if($row[0]=='AD_SIZE')
        {
            echo $row['ITEM_SRC']." - ".$row['ITEM_ID']." - ".$row['ITEM_DESC']."<br>";
        }
    }
    mssql_data_seek($result,0);
    while($row = mssql_fetch_array($result))
    {
        echo $row['ITEM_SRC']." - ".$row['ITEM_ID']." - ".$row['ITEM_DESC']."<br>";
    }

Re: Is there a better way? mssql

Posted: Wed Jan 13, 2010 11:49 pm
by requinix
You can do the sorting in the query itself. No PHP code needed.

What other item_src's are there? Do you want the results sorted by that field?

Re: Is there a better way? mssql

Posted: Thu Jan 14, 2010 12:21 am
by jeremyclover
It's not about sorting the lists as much as subdividing them. There's about 7 different ITEM_SRC values, each representing a different lookup list.

More expanded code example:

Code: Select all

mssql_data_seek($result,0);
echo "<form method=post name=prod action=insert.php>";
 
echo "<span class='select_title'>Select Ad Size:</span>";
echo "<select name='ad_size'><option value='' selected='selected'>-</option>";
 
while($row=mssql_fetch_array($result)) 
    {
    if($row[0]=='AD_SIZE') 
        {
        echo "<option value='$row[ITEM_ID]'>$row[ITEM_DESC]";
        echo "</option><br>";
        }
    }       
echo "</select>";
 
mssql_data_seek($result,0);
 
echo "<span class='select_title'>Select Color:</span>";
echo "<select name='color'><option value='' selected='selected'>-</option>";
 
while($row=mssql_fetch_array($result)) 
    {
    if($row[0]=='COLOR') 
        {
        echo "<option value='$row[ITEM_ID]'>$row[ITEM_DESC]";
        echo "</option><br>";
        }
    }       
echo "</select>";
 
echo "<input type=submit value=Submit>";
echo "</form>";
Make a little more sense. I'm filtering the array for each list, then resetting the index, and filtering again for the next list.