dynamic tables and links help
Moderator: General Moderators
dynamic tables and links help
I'm trying to print the the links of the array into a table but all I'm getting is the word array instead of the link text. how do I get the link to work in the table as I would like use this code in a function?
$links = array(1 =>"<A HREF=\"page1.html\">page1</A>", "<A HREF=\"page2.html\">page2</A>", "<A HREF=\"page3.htm\">page3</A>", "<A HREF=\"page4.html\">page4</A>", "<A HREF=\"page5.html\">page5</A>");
echo "<table border=\"1\" bordercolor=#FFFFFF>\n";
echo "<tr>\n"; #TABLE GOES OUTSIDE OF FOR LOOP AS THERE IS ONLY ONE ROW
for ($r=1; $r<=5; $r++)
{
echo "<td bgcolor=#FFFF99>\n"; #START OF THE TABLE CELLS IN THE LOOP
#echo "$links [$r]";
/*foreach($links as $key=>$val)
{
echo "$links";
}*/
echo "</td>\n";
}
echo "</tr>\n";
echo "</table>";
thanks in advance
Dai
$links = array(1 =>"<A HREF=\"page1.html\">page1</A>", "<A HREF=\"page2.html\">page2</A>", "<A HREF=\"page3.htm\">page3</A>", "<A HREF=\"page4.html\">page4</A>", "<A HREF=\"page5.html\">page5</A>");
echo "<table border=\"1\" bordercolor=#FFFFFF>\n";
echo "<tr>\n"; #TABLE GOES OUTSIDE OF FOR LOOP AS THERE IS ONLY ONE ROW
for ($r=1; $r<=5; $r++)
{
echo "<td bgcolor=#FFFF99>\n"; #START OF THE TABLE CELLS IN THE LOOP
#echo "$links [$r]";
/*foreach($links as $key=>$val)
{
echo "$links";
}*/
echo "</td>\n";
}
echo "</tr>\n";
echo "</table>";
thanks in advance
Dai
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Try this:
Mac
Code: Select all
<?php
$links = array(
1 => '<a href="page1.html">page1</a>',
2 => '<a href="page2.html">page2</a>',
3 => '<a href="page3.html">page3</a>',
4 => '<a href="page4.html">page4</a>',
5 => '<a href="page5.html">page5</a>');
echo <<<END
\n<table border="1" bordercolor="#ffffff">
<tr>
END;
foreach ($links as $value) {
echo <<<END
\n <td bgcolor="#FFFF99">$value</td>
END;
}
echo <<<END
\n</tr>
</table>\n
END;
?>- protokol
- Forum Contributor
- Posts: 353
- Joined: Fri Jun 21, 2002 7:00 pm
- Location: Cleveland, OH
- Contact:
Re: dynamic tables and links help
Ok, first make sure you uncomment the foreach() statement. Next, let me explain what foreach does.Dai wrote: /*foreach($links as $key=>$val)
{
echo "$links";
}*/
foreach ($links as $key=>$val)
Now, as you see above, the foreach statement says, "For each element in the array $links, let $key describe the array key (or index) and let $val describe that key's value."
If you use this statement:
Code: Select all
foreach ($links as $key=>$val) {
echo $key." = ".$val."<br>";
}dynamic tables and links help
As I thought using a foreach loop just prints all 5 links in each cell and I want to print each element in each cell as so :
<TABLE>
<TR>
<TD><A HREF=\"page1.html\">page1</A></TD>
<TD><A HREF=\"page2.html\">page2</A></TD>
<TD><A HREF=\"page3.htm\">page3</A></TD>
<TD><A HREF=\"page4.html\">page4</A></TD>
<TD><A HREF=\"page5.html\">page5</A></TD>
</TR>
</TABLE>
$links = array(1 =>"<A HREF=\"page1.html\">page1</A>", "<A HREF=\"page2.html\">page2</A>", "<A HREF=\"page3.htm\">page3</A>", "<A HREF=\"page4.html\">page4</A>", "<A HREF=\"page5.html\">page5</A>");
echo "<table border=\"1\" bordercolor=#FFFFFF>\n";
echo "<tr>\n"; #TABLE GOES OUTSIDE OF FOR LOOP AS THERE IS ONLY ONE ROW
for ($r=1; $r<=5; $r++)
{
echo "<td bgcolor=#FFFF99>\n"; #START OF THE TABLE CELLS IN THE LOOP
#echo "$links [$r]";
foreach($links as $val)
{
echo "$val";
}
echo "</td>\n";
}
echo "</tr>\n";
echo "</table>";
<TABLE>
<TR>
<TD><A HREF=\"page1.html\">page1</A></TD>
<TD><A HREF=\"page2.html\">page2</A></TD>
<TD><A HREF=\"page3.htm\">page3</A></TD>
<TD><A HREF=\"page4.html\">page4</A></TD>
<TD><A HREF=\"page5.html\">page5</A></TD>
</TR>
</TABLE>
$links = array(1 =>"<A HREF=\"page1.html\">page1</A>", "<A HREF=\"page2.html\">page2</A>", "<A HREF=\"page3.htm\">page3</A>", "<A HREF=\"page4.html\">page4</A>", "<A HREF=\"page5.html\">page5</A>");
echo "<table border=\"1\" bordercolor=#FFFFFF>\n";
echo "<tr>\n"; #TABLE GOES OUTSIDE OF FOR LOOP AS THERE IS ONLY ONE ROW
for ($r=1; $r<=5; $r++)
{
echo "<td bgcolor=#FFFF99>\n"; #START OF THE TABLE CELLS IN THE LOOP
#echo "$links [$r]";
foreach($links as $val)
{
echo "$val";
}
echo "</td>\n";
}
echo "</tr>\n";
echo "</table>";
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
sorted your code and got it to work why did you put in those end code bits ot just confusede me
$links = array(
1 => '<a href="page1.html">page1</a>',
2 => '<a href="page2.html">page2</a>',
3 => '<a href="page3.html">page3</a>',
4 => '<a href="page4.html">page4</a>',
5 => '<a href="page5.html">page5</a>');
echo "<table border=\"1\" bordercolor=#FFFFFF>\n";
echo " <tr>";
foreach ($links as $value)
{
echo "<td bgcolor=\"#FFFF99\">$value</td> ";
}
echo "</tr>";
echo "</table>";
$links = array(
1 => '<a href="page1.html">page1</a>',
2 => '<a href="page2.html">page2</a>',
3 => '<a href="page3.html">page3</a>',
4 => '<a href="page4.html">page4</a>',
5 => '<a href="page5.html">page5</a>');
echo "<table border=\"1\" bordercolor=#FFFFFF>\n";
echo " <tr>";
foreach ($links as $value)
{
echo "<td bgcolor=\"#FFFF99\">$value</td> ";
}
echo "</tr>";
echo "</table>";
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
I apologise for confusing you as that wasn't my intention, I thought that I had set the code out in a clear way so that you would understand that you needed to put the <td></td> bit in the foreach loop. And I was being lazy in not wanting to escape things or write echo '..'; over and over.
But I gave an example for you to try and you were able to work out what it was doing weren't you.
Personally, I like heredoc format, it gives nicely formatted HTML without worries about concenating variables, or escaping quotes or having hundreds of echo statements. It would be just as easy to drop out of the PHP and straight into HTML.
If you look in the manual for echo() it explains how heredoc works. But it's not all that confusing basically it echos everything between the <<<END and the END; and the END; has to be on it's own line with no spaces or tabs in front of it.
Oh and if you don't understand I'd much rather you said so first off so that I could make it clearer than ignore my post altogether and ask the question that it answered again.
Mac
But I gave an example for you to try and you were able to work out what it was doing weren't you.
Personally, I like heredoc format, it gives nicely formatted HTML without worries about concenating variables, or escaping quotes or having hundreds of echo statements. It would be just as easy to drop out of the PHP and straight into HTML.
If you look in the manual for echo() it explains how heredoc works. But it's not all that confusing basically it echos everything between the <<<END and the END; and the END; has to be on it's own line with no spaces or tabs in front of it.
Oh and if you don't understand I'd much rather you said so first off so that I could make it clearer than ignore my post altogether and ask the question that it answered again.
Mac