0

I want to access while array $row2 outside the loop so that I can compare in if else condition, because $row2 array contains many number. Is there any solution make $row array accessible outside the loop or any other method?

<?php
$sql = (" SELECT * FROM result ");
$query = mysqli_query($db, $sql);

while($row = mysqli_fetch_array($query)) {
    $row2 $row['id'];
}

for($i=1;$i<=10;$i++) {
    if ($i<= $row2) {
        echo "<font color='red'><font size='50px'>".$i."</font></font>"."<br>";
        continue;
    } else {
        echo $i.'<br>';
    }
}
?>
6
  • Set $row = array(); before the while loop, you will be able to read it afterwards. Commented Apr 14, 2016 at 18:57
  • @PhiterFernandes Why would it not be available afterwards? Commented Apr 14, 2016 at 18:58
  • I don't really understand, what's the purpose of the code? It just doesn't make any sense to me. Maybe context will help. Commented Apr 14, 2016 at 19:00
  • You've also got a syntax error here: $row2 $row['id']; Commented Apr 14, 2016 at 19:01
  • @Mike i am changing color of those number which are available in database and i am saving thode number in $row2 array Commented Apr 14, 2016 at 19:05

3 Answers 3

1

If you want to loop over two dependent things you need to nest them or else $row2 will always contain the last value in the while loop:

<?php

$sql = (" SELECT * FROM result ");
$query = mysqli_query($db, $sql);

while ($row = mysqli_fetch_array($query)) {
    $row2 = $row['id']; // I added an = sign here.

    for($i=1;$i<=10;$i++) {
        if ($i<= $row2) {
            echo "<font color='red'><font size='50px'>".$i."</font></font>"."<br>";
            continue;
        } else {
            echo $i.'<br>';
        }
    }
}

If this isn't what you want please clarify your question.

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

4 Comments

yes its showing last value but i want to show every value in array that's the real problem
@Raahull So does this answer do what you would want then?
no $row contains 1,2,3,4,7 your code out put is 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
What is the output supposed to be?
0

Pass your variable to a separate function that performs whatever task you want.

<?php
while($row = mysqli_fetch_array($query))
{
     MyFunction($row2);
}

function MyFunction($passed_row2_value){
    // perform whatever task you want here.
}
?>

Comments

0

This shows with some format the ids retrieved by the query and fills the blanks without format.

<?php
$sql = (" SELECT * FROM result ");
$query = mysqli_query($db, $sql);

$row2 = [];
while($row = mysqli_fetch_array($query)) {
    $row2[$row['id']] = true;
}

for($i=1;$i<=10;$i++) {
    if ($row2[$i]) {
        echo "<font color='red'><font size='50px'>".$i."</font></font>"."<br>";
    } else {
        echo $i.'<br>';
    }
}
?>

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.