crazzypappy Posted April 28, 2009 Share Posted April 28, 2009 Ok heres what i want to do i have a race script and would like to work out the chances of beating each person car speed vs car speed , the script give mph for both users i have worked out how to show a percentage but sometimes its showing 200 % so i want to do is create an if - elseif to limit the percentage shown as 100% this is what i have got so far but it is giving me errors else { if ( $_GET['race'] ) { $racer = $_GET['race']; if ( $racer == 'chris' ) { $fuel = rand(5,12); $raced = "Chris"; $speeds = "20"; $ra = "1"; $percent = mph/$speeds*100; $Percent2 = $percent; $nextrace = time()+120; $reps = rand(500,1200); $mns = rand(200,1500); //this is the section im having trouble with $percent3 = (if ($percent >= 100){ echo "100" } elseif ($percent < 100){ echo $Percent2}) } elseif ( $racer == 'jon' ) { Thanks for any help you can offer Quote Link to comment https://forums.phpfreaks.com/topic/155999-solved-if-statment-inside-an-if-statment/ Share on other sites More sharing options...
Cosizzle Posted April 28, 2009 Share Posted April 28, 2009 im confused why you have an if inside a variable. should'nt need to be. try <?php if ($percent >= 100) { echo "100"; } else if ($percent < 100) { echo $Percent2; } ?> or (not sure what your trying to accomplish but... <?php if ($percent >= 100) { $percent3 = 100; } else if ($percent < 100) { $percent3 = $Percent2; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155999-solved-if-statment-inside-an-if-statment/#findComment-821200 Share on other sites More sharing options...
Zhadus Posted April 28, 2009 Share Posted April 28, 2009 A little cleaner version of Cosizzle's response: <?php $percent3 = ($percent >= 100) ? 100 : $Percent2; ?> By the way, what's the reason for having $percent, $Percent2, and $percent3? Reuse variables if you can. Quote Link to comment https://forums.phpfreaks.com/topic/155999-solved-if-statment-inside-an-if-statment/#findComment-821259 Share on other sites More sharing options...
nogginj Posted April 28, 2009 Share Posted April 28, 2009 also: $percent = mph/$speeds*100; should probably be $mph? Quote Link to comment https://forums.phpfreaks.com/topic/155999-solved-if-statment-inside-an-if-statment/#findComment-821264 Share on other sites More sharing options...
crazzypappy Posted April 28, 2009 Author Share Posted April 28, 2009 no the mph comes from a sql table the original script that does give a result is if ( $racer == 'chris' ) { $fuel = rand(5,12); $raced = "Chris"; $speeds = "20"; $ra = "1"; $percent = mph/$speeds*100; $nextrace = time()+120; $reps = rand(500,1200); $mns = rand(200,1500); } but what happens is if mph is like 300 it would give me a result larger than 100 % and i want to limit the ansver no greater than 100 Quote Link to comment https://forums.phpfreaks.com/topic/155999-solved-if-statment-inside-an-if-statment/#findComment-821414 Share on other sites More sharing options...
Zhadus Posted April 30, 2009 Share Posted April 30, 2009 If mph comes from a MySQL table, you need to assign the value to a PHP variable otherwise it doesn't know what "mph" stands for. Once you do that, the code I provided will work. Quote Link to comment https://forums.phpfreaks.com/topic/155999-solved-if-statment-inside-an-if-statment/#findComment-822908 Share on other sites More sharing options...
crazzypappy Posted May 1, 2009 Author Share Posted May 1, 2009 Sorry for the delay in replying back , been working :-( anyways yep that fixed it thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/155999-solved-if-statment-inside-an-if-statment/#findComment-823276 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.