Question

This is what i have at th moment.

    <?php 
    $name = ($row1[2]); 
    if($row1[45] == 1){
            echo("<td class='nameSelect'>$name</td>");
        }
        else{
            echo("<td>$name</td>");
        } 
    ?>  

But i want the $row1[2] to change to $row2[2], $row3[2] and so on with a loop.

    <?php 
    $r = mysqli_fetch_row($result);
    for ($s = 0; $s < $r[4]; ++$s){

        $name = ($row1[2]); //Here are my problem
        if($row1[45] == 1){
                echo("<td class='nameSelect' colspan='6'>$name</td>");
            }
            else{
                echo("<td colspan='6'>$name</td>");
            } 
    }; 
    ?>  

How do i get the $row1[2] to work with $s to become something like ($row $s [2]) Is it possible? Or do i have to re-think my setup?

Was it helpful?

Solution

You can use this:

<?php
for ($s = 0; $s < $r[4]; ++$s)
{
    $name = ${"row".$s}[2]; //Here is my problem
    if (${"row".$s}[45] == 1)
    {
        echo "<td class='nameSelect' colspan='6'>$name</td>";
    }
    else
    {
        echo "<td colspan='6'>$name</td>";
    } 
}
?>

For more information check Variable variables on php.net

OTHER TIPS

$temp = 'row1[2]'; // you can vari 1,2 with variable 

$another_row = $$temp;

Use while for fetching results:

<?php 
while($row1 = mysqli_fetch_row($result)) {
   $name = $row1[ 2 ]; 
   if( $row1[45] == 1 ) {
     echo "<td class='nameSelect'>".$name."</td>";
   } else {
     echo "<td>" . $name . "</td>");
   }
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top