1

I have these values in the array=Array ( [0] => 3 [1] => 2 [2] => 10 [3] => 5 )

How do i get each value separately from the array and perform the below calculation?

$j=$q*100/$ytot;

And this is what i've tried so far, which isnt giving the correct value :

$resul=mysql_query("select * from post_bet where bet_id = '$id'")or die(mysql_error());
if(mysql_num_rows($resul)>0)
{
    while($row=mysql_fetch_assoc($resul))
    {
        $y_uid=$row['yes_userid'];          
        $y_uamt=$row['yes_useramount'];
        $n_uid=$row['no_userid'];
        $n_uamt=$row['no_useramount'];
        $ytot=$row['yes_total'];
        $ntot=$row['no_total'];
        $hhh=explode(',',$row['yes_useramount']);
        $q = print_r($hhh);
        echo $j=$q*100/$ytot;
    }
}
6
  • 2
    what do you want here ? Do you want to perform the operation for each array values or you want to append the operation to a single variable $j Commented Apr 8, 2014 at 6:21
  • Yessir! i do want to calculate each value. Ignore my mistakes please. Commented Apr 8, 2014 at 6:22
  • 2
    You need to specify what you mean with the calculation. What are $j, $q, and $ytot? Commented Apr 8, 2014 at 6:23
  • As far as I can tell from your code you are fetching an associative array from MySQL database that looks nothing like the array you describe in your question. This is unclear. Please clear it up. Commented Apr 8, 2014 at 6:24
  • how u can do like that? $hhh can only store 1 value and you should place exploit outside the while loop. you did wrong. Commented Apr 8, 2014 at 6:24

4 Answers 4

1

Try this-

$resul=mysql_query("select * from post_bet where bet_id = '$id'")or die(mysql_error());
if(mysql_num_rows($resul)>0)
{
    while($row=mysql_fetch_assoc($resul))
    {
        $y_uid=$row['yes_userid'];          
        $y_uamt=$row['yes_useramount'];
        $n_uid=$row['no_userid'];
        $n_uamt=$row['no_useramount'];
        $ytot=$row['yes_total'];
        $ntot=$row['no_total'];
        $hhh=explode(',',$row['yes_useramount']);

    }


foreach($hhh as $val)
{
        echo $j=$val*100/$ytot;
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm. This is what i wanted. THANKS! \m/
1

A simple foreach should do the trick:

foreach (explode(',', $row['yes_useramount']) as $q) {
    echo $q * 100 / $ytot;
}

Comments

1

You can process an array in PHP using foreach like shown below:

foreach (($hhh) as $v => $k)
{
echo $k*100/$ytot;
}

Here,

$hhh=explode(',',$row['yes_useramount']);

So $hhh will return an array of values from $row['yes_useramount'] which was imploded with ,.

1 Comment

Cool. But how do i get each value of the array after a , ? implode doesn't seem to work here.
0

try this code in your script. If there having error please tell me here.

$resul=mysql_query("select * from post_bet where bet_id = '$id'")or die(mysql_error());
if(mysql_num_rows($resul)>0)
{
 while($row=mysql_fetch_assoc($resul))
{
 $y_uid=$row['yes_userid'];         
 $y_uamt[]=$row['yes_useramount'];
 $n_uid=$row['no_userid'];
 $n_uamt=$row['no_useramount'];
 $ytot=$row['yes_total'];
 $ntot=$row['no_total'];
}

for($i = 0; $i<count($y_uamt);$i++){

$sum_uamt = $sum_uamt + int($y_uamt[$i]);
}

echo $j = $sum_uamt * 100 / $ytot;

2 Comments

echo isn't returning any values.
u try echo $y_aumt inside while loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.