1

Begin with selecting value from column where the value is not null. note that the value is float.

$sql = "SELECT cf_user, cf_pakar FROM chevy.gejala1 WHERE cf_user != 'null' and cf_pakar != 'null'";
$result = $conn->query($sql);

after that, I put it in array variable then do math operation with it.

if ($result->num_rows > 0 ) {
    // output data from each row
    while($row = $result->fetch_assoc()) {
        $cf_user =$row["cf_user"] ;
        $cf_pakar = $row["cf_pakar"];
        $gejala = array ($cf_pakar*$cf_user);
    }
} else {
    echo "0 results";
}

sample :

I have 3 value from $cf_user (0.2, 0.4 and 0.6) and $cf_pakar (1, 1 and 1), then, $gejala will show 3 results.

$cf_user*$cf_pakar = 0.2, 0.4 and 0.6 when I echo using foreach.

the problem is :

what I have in $gejala is: (0.20.40.6) as result array. count of $gejala is 1 and the value is float.

How do I separate the result? Because I have another math operation with each result letter on.

2 Answers 2

1

With the line: $gejala = array($cf_pakar * $cf_user); you are simply creating a new Array and thus overriding $gejala with each iteration into your loop. This might not provide the desired effect. Consider this option instead:

<?php

    // CREATE AND INITIALIZE A NEW ARRAY TO AN EMPTY VALUE.
    $gejala     = array();

    if ($result->num_rows > 0 ) {           
        while($row = $result->fetch_assoc()) {
            $cf_user    = $row["cf_user"] ;
            $cf_pakar   = $row["cf_pakar"];
            // NOW PUSH THE RESULT OF YOUR MATH OPERATION, 
            // TO THE ARRAY WITH EACH ITERATION:
            $gejala[]   = round( (cf_pakar*$cf_user), 1);
        }
    } else {
        echo "0 results";
    }
    var_dump($gejala);

Or Simply:

<?php

    // CREATE AND INITIALIZE A NEW ARRAY TO AN EMPTY VALUE.
    $gejala     = array();

    if ($result->num_rows > 0 ) {           
        while($row = $result->fetch_assoc()) {
            // NOW PUSH THE RESULT OF YOUR MATH OPERATION, 
            // TO THE ARRAY WITH EACH ITERATION:
            $gejala[]   = round( ($row["cf_user"]*$row["cf_pakar"]), 1);
        }
    } else {
        echo "0 results";
    }
    var_dump($gejala);
Sign up to request clarification or add additional context in comments.

4 Comments

doesn't work, the result still same $gejala[0] result 0.20.40.6 count still 1 and $gejala[1] is error, I need 3 result valid for do another operation. what do you say?
I am trying use implode and explode, but it did not separate the result in array. count still 1
@RyanRizqi Try doing a var_dump($gejala) to see what you get - just like in the updated Post....
thanks for your help, I decide to put $gejala in temporary database so I can use the result freely.
0

The way you currently have it overwrites $gejala with each iteration. Use the [] operator to append the result of your multiplication to the array instead.

while($row = $result->fetch_assoc()) {
    $cf_user =$row["cf_user"] ;
    $cf_pakar = $row["cf_pakar"];
    $gejala[] = $cf_pakar * $cf_user;
}

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.