Function with varying information

I was wondering if this is safe or not. By that, I mean I send it different length information.
For example, I may send it

SELECT year FROM table

one time then the next time send

SELECT id, product FROM table

In the first one, it only gathers one column, so I make ( $row[1] ) equal to the column sent ( $row[0] ).
The second one has an ID already. It works, but I am unsure if I may have troubles with it down the road.


function dropdown( $name, $options, $selected )
{
    $dropdown = "<td> ".$name.": \
";
    $dropdown .= "<select name=\\"".$name."\\">\
";
      while ($row = mysql_fetch_array($options)) {
      $select = $selected==$row[0] ? ' selected' : null;
      if (!isset($row[1])){ $row[1]=$row[0]; }
        $dropdown .= "<option value=".$row[0]." ".$select.">".$row[1]."</option>\
";
      }
    $dropdown .= "</select></td>\
";
    return $dropdown;
}

I guess another question would be is me sending a resource as $options bad or is there another way?


  $week = 1;
  $query = "SELECT DISTINCT " .
           "week " .
           "FROM {{table}} " .
           "ORDER BY week ASC";
  $result = doquery($query, "games");
  
  $page .= dropdown( 'week', $result, $week );

If you create an associative array, the order in which the value is stored would be irrelevant. You would just address it by its key $array[‘key’]. :slight_smile:

I don’t guess I understand what you are saying.

I have these two records in a table:
fname, lname, year
Bob, Smith, 2009
Jake, Thomas, 2011

I run a query similiar to this: SELECT DISTINCT year FROM table. That gives me a column with two dates. 2009, 2011
I want to use those two in the function, but they don’t have an ID, so I make $row[0] and $row[1] both equal to each date.

I run another query, this time with this table:
id, sport
1, Soccer
2, Hockey

SELECT id, sport FROM table. This gives me an id from the table to use, whereas the first table doesn’t have one, me just pulling a column from the middle of each record.

See. With the second one, I can use the ID. With the first one, it doesn’t have an ID so I have to make the ID from the value ( year ).

I don’t know how to explain it any better. How would you make an associative array that would deal with both of these.

If I have a state file I want to use in the dropdown box, I do the same thing:
id, state
1, Alabama
2, Alaska
3, Arizona
4, Arkansas
That is easy to figure out. Seems the only way for me to get a value for the ID is to do like I did at the top. Extract it with a query then use its value as the id.

Making general purpose functions just seems like a headache.