I'm not sure how difficult this but I have an array and would like to put it into an html table. I need to have two array strings per row, so if this were the array:
$array1 = array(
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => "five",
6 => 'six',
);
And I need the html table to look like this:
| one | two |
|three| four |
|five | six |
This is my code:
$db开发者_如何学Go = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
$db->connect();
$sql = "SELECT ID, movieno
FROM movies
ORDER BY ID DESC
LIMIT 6 ";
$rows = $db->query($sql);
print '<table width="307" border="0" cellspacing="5" cellpadding="4">';
while ($record = $db->fetch_array($rows)) {
$vidaidi = $record['movieno'];
print <<<END
<tr>
<td>
<a href="http://www.youtube.com/watch?v=$vidaidi" target="_blank">
<img src="http://img.youtube.com/vi/$vidaidi/1.jpg" width="123" height="80"></a>
</td>
</tr>
END;
}
print '</table>';
I want to put it on two columns.
Try this code ...
<?php
$array1 = array(
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => "five",
6 => 'six',
);
$val = current ( $array1 ) ;
print "<table border=1>";
while ( $val )
{
print "<tr> <td> $val </td> ";
$val = next ( $array1 ) ;
print "<td> $val </td> </tr> ";
print "\n";
$val = next ( $array1 );
}
print "</table>";
?>
$array1 = array(
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => "five",
6 => 'six',
);
echo '<table>';
for ($i = 1; $i <= 7; $i++) {
if ($i % 2 == 1) echo '<tr>';
echo "<td>{$array1[$i]}</td>";
if ($i % 2 == 2) echo '</tr>';
}
echo '</table>';
You can do it with a multidimensional array like this: http://www.terrawebdesign.com/multidimensional.php
Apart from writing your own code to create , and tags though I don't think there is a built in way.
You can use print_r() to print the contents of the array as a built in way of viewing the array.
You can do something like:
print "<table>";
for($i=1;$i<=count($arr);$i++) {
if($i%2)
print"<tr><td>$arr[$i]</td>";
else
print"<td>$arr[$i]</td></tr>\n";
}
print "</table>";
echo "<table>";
for($i=0;$i+=2;$i<count($array1))
{
echo "<tr><td>".$array1[$i]."</td><td>".isset($array1[$i+1])?$array1[$i+1]:'no value'."</td></tr>";
}
echo "</table>"
精彩评论