ianmc ianmc - 7 months ago 9
PHP Question

Selecting an array of rows using MySQLi

I'm trying to return results from an array of varying row numbers. Right now each row number in mysqli query is manually entered. How would you replace these values with the values of the

currentUsers
array?

$currentUsers = array('1', '3', '7', '10');

while ($row = mysqli_fetch_array ($query))
{
$interestValue = ((int) "$row[1]") +
((int) "$row[3]") +
((int) "$row[7]") +
((int) "$row[10]");
echo "$interestValue";
}

RST RST
Answer

Inside the while loop use the array like this:

$interestValue = 0;
foreach ( $currentUsers as $i ) {
    $interestValue = $interestValue + (int) $row[i];
}
echo $interestValue;
Comments