computernerd21 Posted May 16, 2014 Share Posted May 16, 2014 (edited) 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 <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 May 16, 2014 by Maq Quote Link to comment Share on other sites More sharing options...
Maq Posted May 16, 2014 Share Posted May 16, 2014 Moved topic to appropriate forum, please be careful of where you're posting. Also use code tags when posting code. Quote Link to comment Share on other sites More sharing options...
bsmither Posted May 16, 2014 Share Posted May 16, 2014 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. Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted May 16, 2014 Solution Share Posted May 16, 2014 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> Quote Link to comment Share on other sites More sharing options...
computernerd21 Posted May 17, 2014 Author Share Posted May 17, 2014 Moved topic to appropriate forum, please be careful of where you're posting. Also use code tags when posting code. sorry for the inconvenience ill try to be careful next time. Quote Link to comment Share on other sites More sharing options...
computernerd21 Posted May 17, 2014 Author Share Posted May 17, 2014 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 thank you for the reply though appreciate it . Quote Link to comment Share on other sites More sharing options...
computernerd21 Posted May 17, 2014 Author Share Posted May 17, 2014 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 17, 2014 Share Posted May 17, 2014 thank you it worked, the main problem was the the variables in the loops, i didnt realise they had to be different 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.