$row = $query->fetchAll(PDO::FETCH_ASSOC);
$num_rows = count($row);
for ($i = 0; $i < $num_rows; $i++)
{
$title = htmlspecialchars($row[$i]['title']);
$author =htmlspecialchars($row[$i]['author']);
$school =htmlspecialchars($row[$i]['school']);
$solution = $row[$i]['solution'];
$notes = $row[$i]['notes'];
$ad = array($title, $price, $author, $school, $contact, $content, $date);
$inlcude = array($solutions, $notes);
$field = 0;
echo "<table border='1'>";
// foreach($inlcude as $in) This failled miserably
foreach ($ad as $post)
{
if ($field < 3) //The first three values are placed in the first row
{
echo "<td>$post</td>";
}
if ($field >= 3)
{
echo "<tr><td>$post</td><td>$in</td></tr>";
}
$field++;
}
echo '</table>';
}
I have two arrays and I would like to display them in different columns in my table. $ad displays perfectly fine but I'm having trouble displaying the contents in $inlcude in the second column. I've tried putting another foreach loop to iterate through contents of the second array but that really screws up my table by placing random values in different places on the table. Besides the foreach loop, I don't know of any other way to iterate through the array. Any suggestions would be appreciated.Thanks!
我有两个数组,我想在我的表中的不同列中显示它们。 $ ad显示完全正常,但我在第二列中显示$ inlcude中的内容时遇到问题。我已经尝试使用另一个foreach循环来迭代第二个数组的内容,但这实际上是通过将随机值放在表的不同位置来搞砸我的表。除了foreach循环,我不知道有任何其他方法来遍历数组。任何建议将不胜感激。谢谢!
I want the graph to look like this where $p=$post and $i=$in. Moreover, three columns in first row and two columns in every row after that
我希望图形看起来像$ p = $ post和$ i = $ in。此外,在第一行中有三列,之后在每行中有两列
$p $p $p
$p $i
$p $i
1 个解决方案
#1
1
Assuming that your arrays are formatted correctly, you probably want to use array_shift(). Try something like this:
假设您的数组格式正确,您可能想使用array_shift()。尝试这样的事情:
// Start by copying the $include array, because array_shift() is a destructive
// operation and you might want to use $includes again.
$includes_copy = $include;
// Start with your leading <tr> cell.
echo "<tr>";
// Now loop your ad array.
foreach ($ad as $post) {
//The first three values are placed in the first row.
if ($field < 3) {
echo "<td>$post</td>";
$field++;
}
if ($field == 3) {
echo "</tr>"; // Closing tags are good form.
}
if ($field >= 3) {
// Using array_shift() will return the first element from the array.
// The returned element will be removed from the array.
$in = array_shift($includes_copy);
// $post is populated from foreach(), $in is populated by array_shift().
echo "<tr><td>$post</td><td>$in</td><td></td></tr>";
$field += 3;
}
}
Basically, the concept is that foreach($array as $val) is logically equivalent to while($val = array_shift($array)), meaning that you can run two foreach() at the same time. The only difference is that array_shift() is destructive.
基本上,概念是foreach($ array as $ val)在逻辑上等同于while($ val = array_shift($ array)),这意味着你可以同时运行两个foreach()。唯一的区别是array_shift()具有破坏性。
#1
1
Assuming that your arrays are formatted correctly, you probably want to use array_shift(). Try something like this:
假设您的数组格式正确,您可能想使用array_shift()。尝试这样的事情:
// Start by copying the $include array, because array_shift() is a destructive
// operation and you might want to use $includes again.
$includes_copy = $include;
// Start with your leading <tr> cell.
echo "<tr>";
// Now loop your ad array.
foreach ($ad as $post) {
//The first three values are placed in the first row.
if ($field < 3) {
echo "<td>$post</td>";
$field++;
}
if ($field == 3) {
echo "</tr>"; // Closing tags are good form.
}
if ($field >= 3) {
// Using array_shift() will return the first element from the array.
// The returned element will be removed from the array.
$in = array_shift($includes_copy);
// $post is populated from foreach(), $in is populated by array_shift().
echo "<tr><td>$post</td><td>$in</td><td></td></tr>";
$field += 3;
}
}
Basically, the concept is that foreach($array as $val) is logically equivalent to while($val = array_shift($array)), meaning that you can run two foreach() at the same time. The only difference is that array_shift() is destructive.
基本上,概念是foreach($ array as $ val)在逻辑上等同于while($ val = array_shift($ array)),这意味着你可以同时运行两个foreach()。唯一的区别是array_shift()具有破坏性。