Currently, each td../td prints in just 1 line which makes the source code very hard to read, any way I can print each td../td element on new line to make it much more readable.
thanks for replies. i tried the break but it prints on screen, need to put it in here but where?
$entries[$i] = '<td>'.$id[$i] .'</td>';
$entries[$i] .= '<td>'.$username[$i].'</td>';
$entries[$i] .= '<td>'.$first_name[$i].'</td>';
Further Edit:
The relevant HTML
<!-- Main hero unit for a primary marketing message or call to action -->
<div class="hero-unit">
<div class="row">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<td>2</td>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<td>3</td>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Country</th>
<th>Referrer</th>
</tr>
</thead>
<tbody>
<tr>
<?php
if (isset($content) && !empty($content) && is_array($content)) {//2
$i = 0;
$entries = array();
while(array_key_exists($i, $content)) {//1
$entries[$i] = "\n" .'<td>'.$id[$i] .'</td>';
$entries[$i] .= "\n" .'<td>'.$username[$i].'</td>';
$entries[$i] .= "\n" .'<td>'.$first_name[$i].'</td>';
$entries[$i] .= "\n" .'<td>'.$last_name[$i].'</td>';
$entries[$i] .= "\n" .'<td>'.$email_address[$i].'</td>';
$entries[$i] .= "\n" .'<td>'.$country[$i].'</td>';
$entries[$i] .= "\n" .'<td>'.$where_about[$i].'</td>';
//$entries[$i] .= '<td>'.$sample[$i].'</td>';
echo $entries[$i];
$i++;
}//1
}//2
?>
</tr>
</tbody>
</table>
concat "\n"
to the front of the statement
Edit
$entries[$i] = "\n" . '<td>'.$id[$i] .'</td>';
$entries[$i] .= "\n" . '<td>'.$username[$i].'</td>';
$entries[$i] .= "\n" . '<td>'.$first_name[$i].'</td>';
A neat trick is to define a constant with a linebreak:
define('NL', "\r\n");
Then we just concat that constant not worrying about using single our double qoutes usage:
$entries[$i] = '<td>'.$id[$i] .'</td>' . NL .
'<td>'.$username[$i].'</td>' . NL .
'<td>'.$first_name[$i].'</td>' . NL;
Same trick ofcourse can be applied to tabs:
define('TAB', "\t");
So we can do something radical like this, for instance:
$entries[$i] = '<tr>' . NL .
TAB . '<td>' . $id[$i] . '</td>' . NL .
TAB . '<td>' . $username[$i] . '</td>' . NL .
TAB . '<td>' . $first_name[$i] . '</td>' . NL .
'</tr>' . NL;
Spaces are up to personal preference ofcourse but when you're consistent it even keeps your code readable!
And in your case you could make your code even cooler by working through those columns (<td>
) with a foreach loop through all fields.
$entries[$i] = '<tr>' . NL;
foreach(array('id', 'username', 'first_name') as $key) {
$entries[$i] = TAB . '<td>' . ${$key}[$i] . '</td>' . NL;
}
$entries[$i] = '</tr>' . NL;
Now tell me that isn't cool!
Seems like your code simply lacks the intelligence to add each column in its own table row (tr
), here is your relevant PHP code with the fixes and suggested additions, it should work but I can't test it.
<?php
define('NL', "\r\n");
define('TAB', "\t");
if (isset($content) && !empty($content) && is_array($content)) {
$i = 0;
$entries = array();
$fields = array('id', 'username', 'first_name', 'last_name', 'email_address', 'country', 'where_about');
while(array_key_exists($i, $content)) {
$entries[$i] = '<tr>' . NL;
foreach($fields as $key) {
$entries[$i] = TAB . '<td>' . ${$key}[$i] . '</td>' . NL;
}
$entries[$i] = '</tr>' . NL;
echo $entries[$i];
$i++;
}
}
?>
As I am reading this, walking through $content with a while might not be good idea, try foreach($content as $index => $value)
instead then would make $index
the equivelant of $i
but you wouldn't need to use $content[$index]
because that exact value will be present in $value
in a foreach.
<tr>
tags which define the rows in an html table, check the edit for my suggested fix - sg3s 2012-04-03 21:27
<tr>
and </tr>
just before and after the php code; these will be added inside the loop in php instead for each row, making the <td>
s fall in line neatly - sg3s 2012-04-03 21:32
PHP_EOL
- leftclickben 2013-02-05 11:29
PHP_EOL
is based on the newlines used in the host OS which I do not like to depend on. Setting this, shorter, constant myself allows me to make sure it is always in \r\n format - sg3s 2013-02-05 12:42
Use '\n' after the td tag you echo/print
<?
foreach($array as $single) {
echo "<td></td> \n";
}
?>
You can add a line break to your print statement.
echo "My text\n";
You can also add tabs to get it indented neatly.
echo "\t\tMy text\n";
I would just like to add: If you're using Windows to read the code, you should use \r\n
instead of just \n
Not sure what you're asking but if you want to get rid of the :
$s .= '<td>' . $n . '</td>';
use ($co means close open)
$co = '</td><td>';
$row = '<tr><td>' . $data . $co . $data2 . $co . $data3 . '</td></tr>';