Converting this to a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Converting this to a function

Post by phpPete »

I'd like to convert the following to a generic function which will return a an array of links which pass variables in the url


I'm having some trouble abstracting it to the most basic components, thus they are the hard coded items...:(

This is the code as it is in procedural form

Code: Select all

$lenRows =0;
        $fieldName = array();
        $link = array();
        $page ='localhost:8008/msc/ud6.php';
        
        $sql = "SELECT rec_code, item_name FROM menu_item";
        $result = mysql_query($sql) or die ( mysql_error());
        
        // place query results in rows array
       $rows  = array();
        while( $row = mysql_fetch_array( $result))
            {
                $rowsї] = $row; 
            }
               
        
        //place field names in fieldName array
        $numFields = mysql_num_fields($result);
        
        for($i = 0; $i < $numFields; ++$i)
            &#123;
               $fieldName&#1111;] = mysql_field_name($result, $i);
            &#125;
 
     //assign $lenRows the size of $rows array
     $lenRows = count($rows);

        for($i = 0; $i < $lenRows; ++$i)
        &#123;
            $link&#1111;] = '<a href=http://'. $page . '?recCode=' . $rows&#1111;'$i&#1111;$i]&#1111;0] . '&itemName=' . $rows&#1111;$i]&#1111;1] .">" . $rows&#1111;$i]&#1111;1] . "</a>";
        &#125;
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

I'm working on a version, but having never used mysql, and having used my own private db libraries for so long I'm having to look a lot of functions up (and php.net isn't responding to me right now :( ) But I'll have at least one version for you soon...

its a fun project... I've never used GET so I've never had the need before...
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Thanks for your efforts. I've taken a break on that code for a few...working on something else...and in serious need of coffee!!

At least it's a worthwhile function....i hope..:)
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

OK I think this works, I'm currently not at a place where I can test code, so it might need some modifications.

Code: Select all

function produceGetStyleLinks($db_result, $page, $labelColumn, $varsToPass=array())
&#123;
    $links = array();
    $numColumns = mysql_num_fields($db_result);
    for($i=0;$i<$numColumns;$i++)
    &#123;
        $keys&#1111;$i] = mysql_field_name($db_result,$i);
    &#125;
    // create a default mapping of column names to GET parameters
    if (!count($varsToPass))
    &#123;
      for ($i=0; $i < $numColumns; $i++)
      &#123;
          $varsToPass&#1111;$keys&#1111;$i]]= $keys&#1111;$i];
      &#125;
    &#125;
    $columnsToPass = array_keys($varsToPass);

    while ($row = mysql_fetch_array( $db_result))
    &#123;
         $aLink = "<a href="http://$page";
         $first = TRUE;
         for ($i=0;$i<$numColumns;$i++)
         &#123;
             if (in_array($keys&#1111;$i],$columnsToPass))
             &#123;
                $aLink .= ($first ? "?" : "&");
                $first = FALSE;
                $alink .= $varsToPass&#1111;$keys&#1111;$i]]."=".$row&#1111;$keys&#1111;$i]]";
             &#125;
         &#125;
         $aLink .= '">'.$row&#1111;$labelColumn]."</a>";
         $links&#1111;] = $aLink;
     &#125;
     return $links;
&#125;
$varsToPass is a mapping of the columnames of your table to the names of the get parameters. it defaults to passing all data retreived by the query, under the same name as the column.
{editted: to fix a missing db_ prefix before one of the $result's}
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Hey Nielsene;

That's pretty neat! Works like a charm. ( fixed a typo or 2 )

It is SO depressing that I struggle with something like this and you whip it up in 10 minutes...oh well...

Greatly appreciate it!
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Don't feel bad, everyone comes at this from different areas and backgrounds. I've been coding for years and while I can craft functions quickly, don't ask me to design a pretty webpage... The best I can do is make sure the php-generated html source looks nice :)
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

One of those days! I HAD to get away from the computer for a few hours.

Go Sox ! ( wait til next year! )

Pats going for 2!

Celts and Bruins on the rise!

Mass native...can you tell? :)
Post Reply