woodplease Posted February 1, 2010 Share Posted February 1, 2010 i'm trying to create an else of statement that will echo out database entries if certain conditions are met, but i cant get it to work. I'm also not sure if i've even got the correct syntax. could anyone help? $percentage=$total/$questiontotal*100; echo '<br/>' .$percentage; $query="SELECT * FROM results WHERE quizref= " .$quizref; $result=pg_query($query) or die ("Query failed"); $row=pg_fetch_array($result); if ($percentage<=25); echo "you are " .$row['poor']; else if ($percentage >25<=50); echo "you are " .$row['acceptable']; else if ($percentage>50<=75); echo "you are " .$row['good']; else if ($percentage>75<=100); echo "you are " .$row['geek']; Thanks Link to comment https://forums.phpfreaks.com/topic/190585-else-if-statement/ Share on other sites More sharing options...
Hybride Posted February 1, 2010 Share Posted February 1, 2010 Am not entirely sure what you're doing with that query in the first part (it looks backwards to me), but this is your if/elseif/else statement fixed up: if ($percentage<=25) { echo "you are " .$row['poor']; } elseif ($percentage >25<=50) { echo "you are " .$row['acceptable']; } elseif ($percentage>50<=75) { echo "you are " .$row['good']; } else { echo "you are " .$row['geek']; } More than that, I would recommend using switch/case statements for sanity's/readable sake. Link to comment https://forums.phpfreaks.com/topic/190585-else-if-statement/#findComment-1005227 Share on other sites More sharing options...
tim87 Posted February 1, 2010 Share Posted February 1, 2010 Your code doesn't make it totaly clear what your trying to but I can offer some help. $questiontotal = 30; $query = "SELECT * FROM results WHERE quizref= '$quizref'"; $result = pg_query($query) or die ("Query failed"); while ($row = pg_fetch_array($result)){ $total = $row['total']; $var2 = $row['Field_Name_2']; echo $result."<br>" $percentage = ($total/$questiontotal)*100; if ($percentage <= 25){ echo "you are poor"; } elseif ($percentage > 25 && $percentage <= 50) { echo "you are acceptable"; } elseif ($percentage > 50 && $percentage <= 75) { echo "you are good"; } else { echo "you are a geek!"; } } You can retrieve more fields from the database by replacing the line $var2 = $row['Field_Name_2']; with the name of your database fields and a sensible variable name. Hope this is of use. Link to comment https://forums.phpfreaks.com/topic/190585-else-if-statement/#findComment-1005237 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.