1

I use this code to echo values from an array generated from a mysql query

while($row = mysql_fetch_array($result)) {

            echo $row[0]; 
            echo ' ';
            echo $row[1];
            echo "%  ";
            echo "<br>";
    }

to produce a result like:

CC 77.6% 
CT 21.9% 
TT 0.5% 

Which is fine.

I would like to have the 3 pairs of values available to use in a bootstrap progress bar div so that I may represent the information in a more visual manner.

How can assign the values from the array into variables?

EDIT

Sample result set

Array ( 
[0] => AA 
[alleles] => AA 
[1] => 6 
[total] => 6 
[2] => 25.00 
[percentage] => 25.00 ) 

Array ( 
[0] => AG 
[alleles] => AG 
[1] => 11 
[total] => 11 
[2] => 45.83 
[percentage] => 45.83 ) 

Array ( 
[0] => GG 
[alleles] => GG 
[1] => 7 
[total] => 7 
[2] => 29.17 
[percentage] => 29.17 )
2
  • is the result of your query a single row ? or you need to aggregate data in any way? Commented Mar 28, 2015 at 7:34
  • Array ( [0] => AA [alleles] => AA [1] => 6 [total] => 6 [2] => 25.00 [percentage] => 25.00 ) Array ( [0] => AG [alleles] => AG [1] => 11 [total] => 11 [2] => 45.83 [percentage] => 45.83 ) Array ( [0] => GG [alleles] => GG [1] => 7 [total] => 7 [2] => 29.17 [percentage] => 29.17 ) Commented Mar 28, 2015 at 7:36

3 Answers 3

1

Try this:

$values = array();
while($row = mysql_fetch_array($result))
{
   array_push($values, array($row[0], $row[1]))
}

Now $values is an array of values. For example $values[0][0] is equal to "CC", $values[0][1] is equal to "77.6" and etc;

Sign up to request clarification or add additional context in comments.

Comments

0

In your case, assuming alleles is unique, you can buffer your results and create an key-value array:

$results = array(); 
while($row = mysql_fetch_array($result)) {
    $results[ $row['alleles'] ] = array(
        'total' => $row['total'],
        'percentage' => $row['percentage']
    ); }

after that, you can access your data in the following way:

$results['AA']['total']

and to display all of them:

foreach ($results as $k => $data) {
    printf("%s: %s%%<br/>\n", $k, $data['percentage']);
}

Comments

0

Use This First of all, use two div. first for name of that progress bar. Other one for value. This is the code dude.. use this..

Comments

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.