SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot gregs's Avatar
    Join Date
    Aug 2002
    Posts
    161
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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
    PHP Code:
    SELECT year FROM table 
    one time then the next time send
    PHP Code:
    SELECT idproduct 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.

    PHP Code:
    function dropdown$name$options$selected )
    {
        
    $dropdown "<td> ".$name.": \n";
        
    $dropdown .= "<select name=\"".$name."\">\n";
          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>\n";
          }
        
    $dropdown .= "</select></td>\n";
        return 
    $dropdown;

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

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

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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'].
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Zealot gregs's Avatar
    Join Date
    Aug 2002
    Posts
    161
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •