-1

i am making a cricket stats page and in 1 over there are 6 balls, however if i put in 0.7 id dose not make it 1.1, how can i use php to make it do this?
This is all the code i have got (im using mysql):

<?php echo $row['o'] ?>
Slindoncc
  • 11
  • 4
  • 7
    We've lost our magic spheres, so you need to debug it yourself or provide some code that demonstrates things you're talking about. – zerkms Apr 22 '11 at 11:10
  • If you've got code that doesn't work right, please share it and people will be happy to help. If you're just trying to get us to write the code for you then don't expect people here to be so helpful. – GordonM Apr 22 '11 at 11:14

3 Answers3

2
$x = 0.7;

$overs = floor(($x * 10) / 6);
$balls = ($x * 10) - ($overs * 6);

echo $overs.'.'.$balls;

But you might want to use an integer input (the number of balls bowled) rather than a decimal value.

$x = 7;

$overs = floor($x / 6);
$balls = $x - ($overs * 6);

echo $overs.'.'.$balls;

This logic can be simplified using the % modulus operator, but I've shown it longhand to try and help you understand the principle

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
1

Disclaimer: Please provide code to help people know what you have done, what is not working and what you want. But since I love Cricket, you need to do something like below. Giving C# code since you did not provide your initial PHP code.

Use equivalent PHP code:

int balls = 14;
string overs = balls/6 + "." + balls%6;

will give you "2.2" Also 5 balls comes out as 0.5 which is what you want as well.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • that is for not giving initial code. If he had done that, I would have given code in PHP – manojlds Apr 22 '11 at 11:16
  • @Mark - why? balls is int. I have given C# code not PHP ( of course ) so I don't have to "floor" stuff. – manojlds Apr 22 '11 at 11:26
  • missed the C#. A lot of languages (including PHP) would take an int/6 and return a float from that part of the formula – Mark Baker Apr 22 '11 at 11:28
0

This is the simple PHP condition for overcoming this problem. The method is Lengthy but working:-

$ball__= 15;    
$over=0;

if($ball__>=6 && $ball__<12){
  $over+=1;
  if($ball__==6){$ball__=0;}
  if($ball__==7){$ball__=1;}
  if($ball__==8){$ball__=2;}
  if($ball__==9){$ball__=3;}
  if($ball__==10){$ball__=4;}
  if($ball__==11){$ball__=5;}
}
if($ball__>=12 && $ball__<18){
  $over+=2;
  if($ball__==12){$ball__=0;}
  if($ball__==13){$ball__=1;}
  if($ball__==14){$ball__=2;}
  if($ball__==15){$ball__=3;}
  if($ball__==16){$ball__=4;}
  if($ball__==17){$ball__=5;}
}
if($ball__>=18 && $ball__<24){
  $over+=3;
  if($ball__==18){$ball__=0;}
  if($ball__==19){$ball__=1;}
  if($ball__==20){$ball__=2;}
  if($ball__==21){$ball__=3;}
  if($ball__==22){$ball__=4;}
  if($ball__==23){$ball__=5;}
}
if($ball__>=24 && $ball__<30){
  $over+=4;
  if($ball__==24){$ball__=0;}
  if($ball__==25){$ball__=1;}
  if($ball__==26){$ball__=2;}
  if($ball__==27){$ball__=3;}
  if($ball__==28){$ball__=4;}
  if($ball__==29){$ball__=5;}
}
if($ball__>=30 && $ball__<36){
  $over+=5;
  if($ball__==30){$ball__=0;}
  if($ball__==31){$ball__=1;}
  if($ball__==32){$ball__=2;}
  if($ball__==33){$ball__=3;}
  if($ball__==34){$ball__=4;}
  if($ball__==35){$ball__=5;}
}
//----and so on up to the number of balls you have to convert into overs
           echo ($over.".".$ball__); to show results
F. Müller
  • 3,969
  • 8
  • 38
  • 49