Jump to content

could somebody tell me how to correct this


Go to solution Solved by Psycho,

Recommended Posts

hi i would like to know why this doesnt work .the problem is that it doesnt give me multiple  tr-s like td-s.it only works for one tr and multiple td-s. help apreciated :D

  <html><head>
</head>
<body>
<?php


function myt($border,$column,$row,$words){
$table="<table border='".$border."'>";
if($column==1){
$table.='<tr>';
for($i=1;$i<=$row;$i++){
$table.="<td>".$words."</td>";




};//end of for
$table.='</tr>';
}//end of if
else{
for($i=1;$i<=$column;$i++){
$table.="<tr border='".$border."'>";
for($i=1;$i<=$row;$i++){
$table.='<td>'.$words.'</td>';
}
$table.='</tr>';
};//end of column for
};//end of else
$table.="</table>";


return $table;


};//end of function
//echo myt (1,1,10,'word'); WORKS!!!
echo myt(1,2,4,'lalalala');




?>
</body>
</html>
Edited by Maq
  • Solution

Try this

 

<?php
 
function myt($border, $total_cols, $total_rows, $words)
{
    $borderParam = ($total_cols==1) ? "border='{$border}'" : '';
 
    $table = "<table {$borderParam}>\n";
    for($row=0; $row<$total_rows; $row++)
    {
        $table .= "<tr>\n";
        for($col=0; $col<=$total_cols; $col++)
        {
            $table .= "<td>{$words}</td>\n";
        }
        $table .= "</tr>\n";
    }
    $table.="</table>";
    return $table;
}
 
?>
<html>
<head></head>
<body>
<?php echo myt(1, 2, 4, 'lalalala'); ?>
</body>
</html>

You are assembling <td>'s based on a $row loop. Perhaps you mean to use the $column parameter.

 

Then you can use the $row parameter to have an outer loop that makes $row number of <tr> rows.

ah my mistake but it doesn`t really matter. actually i figured it out: when looping and inside looping again i was using the same variable $i and im guessing the last $i (meaning the <td> one) was replacing the first $i(meaning the <tr> one) and automatically i still got only one <tr> so now i used another variable $k for example and it worked :D thank you for the reply though appreciate it .

 

Try this

<?php
 
function myt($border, $total_cols, $total_rows, $words)
{
    $borderParam = ($total_cols==1) ? "border='{$border}'" : '';
 
    $table = "<table {$borderParam}>\n";
    for($row=0; $row<$total_rows; $row++)
    {
        $table .= "<tr>\n";
        for($col=0; $col<=$total_cols; $col++)
        {
            $table .= "<td>{$words}</td>\n";
        }
        $table .= "</tr>\n";
    }
    $table.="</table>";
    return $table;
}
 
?>
<html>
<head></head>
<body>
<?php echo myt(1, 2, 4, 'lalalala'); ?>
</body>
</html>

thank you it worked, the main problem was the the variables in the loops, i didnt realise they had to be different :D 

thank you it worked, the main problem was the the variables in the loops, i didnt realise they had to be different :D

 

Give your variables meaningful names. $i and $k don't tell you what they are or what their purpose is. It may not seem like a big deal when you are actively working on the code. But, if you have to go back to the code in the future it will take longer to decypher what it is doing to make whatever bug fixes or enhancements you want to make.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.