Using functions with database queries and table displays

I just encountered a problem that has me stumped. I’m using a database query to create a series of rather complex reference tables, with lots of custom styling, superscripts and footnotes - most of which need to be set up inside a while loop. To deal with the resulting code bloat, I put most of this custom code in a separate PHP file, then included it. However, I discovered that the require_once command overrides the while loop, only displaying one item in a series. If I change require_once to require, the entire file is included over and over.

So I did a little research and couldn’t find a workaround except for somehow doing all this stuff in a database or using a function - something I’m not familiar with. I’ve played with a couple tutorials but haven’t got very far. I’m getting an error message that says functions don’t work in while loops, and I don’t understand how to plug a function into a dynamic table.

So I wondered if anyone could show me how you would convert the following to a function. The idea is to simply insert a red plus sign in rows matching birds with the scientific names Aix sponsa or Gavia immer. Below is the code I’m currently using. How would you convert that to a function, and how would you insert that function in the table cell? Thanks!


  switch($Latin)
  {
   case 'Aix sponsa':
   case 'Gavia immer':
   $star_rating = '<sup><span style="color: #f00;"><b>+</b></span></sup>';
   break;
   default:
   break;
  }

<td class="Symbol"><a href="$GZ_URL/life/'.$LatinL.'" title="'.$Symbol.' ('.$Latin.')">'.$row['Symbol'].'</a>'.$star_rating.''.$DesigAst.'</td>

Hi Chavista,

What about doing something like this,

In an include file:


function getStarRating($latinName)
{
    switch($latinName) {
	    case 'Aix sponsa':
	    case 'Gavia immer':
	        return '<sup><span style="color: #f00;"><b>+</b></span></sup>';
	        break;
	    default:
            return '';
    }
}

and then in your loop:


echo '<td class="Symbol"><a href="$GZ_URL/life/'.$LatinL.'" title="'.$Symbol.' ('.$Latin.')">'.$row['Symbol'].'</a>'.getStarRating($Latin).$DesigAst.'</td>';

Thanks - I see what you’re doing, though it isn’t working for me for some reason. I’ve been playing with the variables. This is the code I’m using at the moment:


function get_StarRating($latinName) 
{ 
    switch($latinName) { 
        case 'Aix sponsa': 
        case 'Gavia immer': 
            return '<sup><span style="color: #f00;"><b>+</b></span></sup>'; 
            break; 
        default: 
            return $StarRating; 
    } 
}

The above code is in a file that I’ve included both before the while loop and inside the loop. Below is my table code…


<td class="Symbol"><a href="'.$GZ_URL.'/life/'.$LatinL.'" title="'.$Symbol.' ('.$Latin.')">'.$row['Symbol'].'</a>'.get_StarRating($Latin).$DesigAst.'</td>

The variable you’re returning by default, $StarRating, isn’t initialized… what should be the value?

You usually don’t want to be including files within a loop. Just include/require the file at the top of your script, and you still be able to call the function from within the loop.