Page 1 of 1
dynamic tables and links help
Posted: Fri Jul 05, 2002 8:44 am
by Dai
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
Posted: Fri Jul 05, 2002 8:53 am
by twigletmac
Try this:
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;
?>
Mac
Re: dynamic tables and links help
Posted: Fri Jul 05, 2002 8:57 am
by protokol
Dai wrote:
/*foreach($links as $key=>$val)
{
echo "$links";
}*/
Ok, first make sure you uncomment the foreach() statement. Next, let me explain what foreach does.
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>";
}
Then you will see what keys and values are available in the $links array. When using foreach() in your case, you don't print or echo $links. Instead, you echo or print $key or $val, depending on whether or not you are working with the key or the value of the array respectively.
dynamic tables and links help
Posted: Fri Jul 05, 2002 9:56 am
by Dai
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>";
Posted: Fri Jul 05, 2002 10:02 am
by twigletmac
Did you try my code - it does exactly what you are looking for.
Mac
Posted: Fri Jul 05, 2002 10:25 am
by Dai
yep tried it all I got was errors with the echo <<<END; bit of code is there a simpler way for you to write your code ?

Posted: Fri Jul 05, 2002 10:45 am
by Dai
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>";
Posted: Fri Jul 05, 2002 12:06 pm
by honkyinc
I'm not a big fan of heredoc for small bits. I use it for large forms and large tables, but not for simple one line output.
Much cleaner on the array def though!
Posted: Fri Jul 05, 2002 1:02 pm
by twigletmac
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