Jump to content

using If / elseif based on $counter value


PHP_Idiot

Recommended Posts

Hi

 

The below code generates a table and a number of rows depending on the user input.

for ( $counter = 1; $counter <= $PlayerNo; $counter += 1) 
{
echo "<tr><td align='center' valign='center'>";
echo $counter;
echo "</td><td align='center' valign='center'>";
echo '<input name="Score" type="text" size="3" maxlength="3">';
echo "</td><td align='center' valign='center'>";
echo '<input name="MembershipNo" type="text" size="7" maxlength="7">';
echo "</td><td align='center' valign='center'>";
echo '<input name="FirstName" type="text" size="15" maxlength="7">';
echo "</td><td align='center' valign='center'>";
echo '<input name="LastName" type="text" size="15" maxlength="7">';
echo "</td></tr>";
}
echo "</table>";

 

This works but I also need to fill the Points column. To do this I'm think an elseif would do it but I can't get it to work. (The $counter is the finishing postion of each player, the score is dependant on the players finishing postion)

 

the basic idea would be: (and yes I know this syntax is way out but you get the idea...hopefully)

If [$counter] = 1, then [Points] = 500

Elseif [$counter] = 2, the [Points] = 350

Elseif [$counter] = 3, then [Points] = 250

and so on 4=200, 5=150, 6=100, 7=75, 8=50.

 

Is an elseif statement the correct one to use, and if so where should I place it (I presume in a $variable then call that in the code I have above already is that right?)

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/152886-using-if-elseif-based-on-counter-value/
Share on other sites

I've no clue what you're trying to do but I'd wrap it in a function if you're gonna call it from the loop to keep the code easier to see.

something like

<?php
function getScore( $counter )
{
  switch( $counter )
  {
    case 1:
      return 500;
    case 2:
      return 350;
    case 3:
      return 250;
    case 4:
      return 200;
    case 5:
      return 150;
    case 6:
      return 150;
    case 7:
      return 75;
    case 8:
      return 50;
    default:
      return 0;
  }
}
?>

 

btw, you can use $counter++ instead of += 1.

Awesome thanks a lot, I had looked at the switch method but couldn't work out how best to use it.

 

If I understand your comments correctly I place the function outside the loop and call the function into the cell where I need the result? (and if so how?)

 

Is this correct or should I be placing the function in the cell itself? (agian how?)

 

Thanks so much, sorry for the really noobish questions, but I am a total noob but slowly learning?!

You should define the function outside of a loop, because you can re-use and reload it any amount of times you want, for example:

 

function TryThis($anything) {
  echo "Hey It's: $anything <br />";
}

for ($i = 0; $i < 5; $i++) {
  TryThis($i);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.