Asked  1 Year ago    Answers:  5   Viewed   7 times
<?php

    $i=0;
    while($i < 101){
        if($i%2==0){
            echo "<tr>".PHP_EOL;
        }

        echo "<td>".$i."</td>".PHP_EOL;
        $i++;

        if($i%2==0){
            echo "</tr>".PHP_EOL;
        }
    }
?>

This code generates a table with 100 rows and 2 columns. But what I want to do is that show ordered numbers (upp to 100) in the left side of the rowcells and show something else (ex. pow(rownumber) ) in the right side of the rowcells. How can I do that?

 Answers

2

Try this, Will output 100 rows with the number and its power in two columns

<table>
<?php 
for($i = 0; $i <= 100; $i++){

    echo sprintf('<tr><td>%s</td><td>%s</td></tr>',
            $i,
            pow($i, 2)
         );
}
?>
</table>
Saturday, May 29, 2021
 
Norgul
 
2

First off, you should learn the HTML code for tables. Your code is putting a Table Header (th) next to a normal column item (td). You need to loop through the headers first then next row loop through the column items or build the strings to echo out.

$headers = $col = "";
while($pickresults= mysql_fetch_assoc($picksquery)){
    $headers .= "<th> {$pickresults['username']} </th>";
    $col .= "<td> {$pickresults['firstgame']} </td>";
}

echo "<table><tr>$headers</tr><tr>$col</tr></table>";
Thursday, April 1, 2021
 
4

Wrap the result in another table.

echo "<table>";
$count = 0;
$num_columns = 2;  // or 3
while ($rc = mysql_fetch_array($results_course)) {
    if ($count++ % $num_columns == 0) {
        echo "<tr>";
    }
    echo "<td>";
    // previous table code here
    echo "</td>";
    if ($count % $num_columns == 0) {
      echo "</tr>";
    }
}
if ($count % $num_columns > 0) {
  echo "</tr>";
}
echo "</table>";
Friday, May 28, 2021
 
Xatoo
 
5

Nothing wrong with having a for inside a while.

Your "for" would be better a bit clearer as

while(strlen($string) < 4) {
    $string = $string.'1';
}
Saturday, May 29, 2021
 
seaders
 
2

Try changing:

for($counter = 0; $counter = $numRow; $counter++){

to:

for($counter = 0; $counter =< $numRow; $counter++){

You are checking if $counter equals $numRow, and you should check if it equals and is less than...

Hope this helps.

EDIT Based on user requests

Try storing the results in a variable and return that variable at the end of both loops, but im not sure what you are trying to achieve here, try running the following code:

//HERE I RETRIVE ALL THE DATA FROM THE DATABASE.
        $query = $dbh->query(" SELECT *
        FROM table
        WHERE ID= '{$ID}'
        ");

//THIS CODE SHOWS THE NUMBER OF ROWS WITH THE CURRENT ID 
        $sql = "SELECT COUNT( * ) FROM table WHERE ID= '{$ID}'";
        $result = $dbh->prepare($sql);
        $result->execute();
        $number_of_rows = $result->fetchColumn();

//THIS IS THE FOREACH IN WHICH I CALL ALL THE ROWS BY THEIR VALUE
        $return = "";
        foreach ($query as $key => $value) {
            $value['wFname'][$key] = !empty($value['wFname'][$key]) ? $value['wFname'][$key] : '';
            $numRow = $number_of_rows - 2; // THE ITEM BEFORE THE LAST ONE IN THE ARRAY
            $lastItem = $number_of_rows - 1; //LAST INDEX IN THE ARRAY
            for ($counter = 0; $counter <= $numRow; $counter++) {
                $return .= $value['wLname'][$counter] . ', ' . $value['wFname'][$counter];
            }
            $return .= "&" . $value['wLname'][$lastItem] . ', ' . $value['wFname'][$lastItem];
            $return .= ' ';
        }
        return $return;
Saturday, May 29, 2021
 
ritch
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :