简体   繁体   中英

Create rows and columns based on COUNT query

Simple question I believe for anyone with minimal php skills (which I don't have sufficient amounts of haha)

$numrows = $retour['nb'] / 4;
echo $numrows;

echo "<table><tr>";
while ($callback = mysql_fetch_assoc($queryLocations2))
{   
echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) .  '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' .  $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' .  utf8_encode($callback['loc_state']) . '</td>');
}
echo "</tr></table>";
}

How would I go about presenting a table that will hold 4 results(4 columns) per row, based on the value of $numrows ?

Thank you!

Output tr tags inside while loop:

$count = 0;
echo "<table>"; 
while ($callback = mysql_fetch_assoc($queryLocations2)) 
{    
    if ($count % 4 == 0)
        echo '<tr>';

    $count++;

    echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) .  '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' .  $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' .  utf8_encode($callback['loc_state']) . '</td>'); 

    if ($count % 4 == 0)
        echo '</tr>';
}

if ($count % 4 != 0)
{
    // need to add missing td-s here

    echo '</tr>';
}
echo "</table>"; 
$numrows = floor($retour['nb'] / 4);

echo $numrows;
$i=0;
echo "<table>";

while ($callback = mysql_fetch_assoc($queryLocations2))
{  
   if($i==4)

      {

         echo "<tr>"; 

         echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) .  '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' .  $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' .  utf8_encode($callback['loc_state']) . '</td>');

         echo "</tr>";

         $i=0;

      }

$i++;

}

while ($i<4) 
   { 

       echo '<td></td>';
       $i++;
   }

echo "</table>";

}



//if numrows used for # of rows then use following


$count=0;
while ($callback = mysql_fetch_assoc($queryLocations2) && $count<=$numrows)
{  

if($i==4)

echo "<tr>"; 

echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) .  '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' .  $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' .  utf8_encode($callback['loc_state']) . '</td>');

if($i==4)

{

   echo "</tr>"; 
   $i=0;
   $count++;

}

$i++;

}
while ($i<4) 

{ 
    echo '<td></td>';
    $i++;
}

echo "</table>";

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM