Jump to content

switch statement and loop not working


Ruth

Recommended Posts

for($rating_num; $rating_num <= 5; $rating_num++)

{

switch($rating)

{

case 'star':

echo '<img src = "images/star.gif">';

break;

 

}

}

 

I have a drop down box that will give a numeric rating($rating_num) 1-5. When I select 5 it will display 1 start, when I select 1 it will display 5 starts. It seems to be running backward. Any ideas on how to fix this? $rating is the way the user wishes to display the rating stars, thumbs up, or numeric.

Link to comment
https://forums.phpfreaks.com/topic/97975-switch-statement-and-loop-not-working/
Share on other sites

the use of a switch on a single condition is pretty pointless

try

<?php
for($rating_number = 1; $rating_num <= 5; $rating_num++}
if($rating[$rating_number] == "star"){
echo "<img src=\"images/star.gif\"  alt=\"star\" />";
}
else{
#echo No star img
}
}
?>

for($rating_num = 1; $rating_num <= 5; $rating_num++)

{

  switch($rating)

  {

      case 'star':

        echo '<img src = "images/star.gif">';

        break;

       

  }

}

 

I tired this and no matter which number I select I get 5 stars. I've also tired $rating_num = 0 and get the same result

Here is my entire code

 

$movie = $_POST['movie'];

$rating = $_POST['rating'];

$rating_num = $_POST['rating_num'];

$comment = $_POST['comments'];

 

echo "Move: $movie <br />";

 

for($rating_num=1; $rating_num <= 5; $rating_num++)

{

switch($rating)

{

case 'star':

echo '<img src = "images/star.gif">';

break;

 

}

}

 

The rating is being defined in the post

Forgot to take out a suggestion I tired here is my orginal code

 

<?php

$movie = $_POST['movie'];

$rating = $_POST['rating'];

$rating_num = $_POST['rating_num'];

$comment = $_POST['comments'];

 

echo "Move: $movie <br />";

 

for($rating_num; $rating_num <= 5; $rating_num++)

{

switch($rating)

{

case 'star':

echo '<img src = "images/star.gif">';

break;

 

}

}

 

?>

 

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.