dynamic tables and links help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dai
Forum Newbie
Posts: 16
Joined: Wed May 15, 2002 6:12 am

dynamic tables and links help

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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) &#123;
	echo <<<END
\n	<td bgcolor="#FFFF99">$value</td>
END;
&#125;

echo <<<END
\n</tr>
</table>\n
END;
?>
Mac
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Re: dynamic tables and links help

Post 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) &#123;
   echo $key." = ".$val."<br>";
&#125;
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.
Dai
Forum Newbie
Posts: 16
Joined: Wed May 15, 2002 6:12 am

dynamic tables and links help

Post 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>";
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Did you try my code - it does exactly what you are looking for.

Mac
Dai
Forum Newbie
Posts: 16
Joined: Wed May 15, 2002 6:12 am

Post 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 ? :lol:
Dai
Forum Newbie
Posts: 16
Joined: Wed May 15, 2002 6:12 am

Post 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>";
honkyinc
Forum Newbie
Posts: 19
Joined: Tue Jun 04, 2002 10:30 am
Location: Maryland, USA

Post 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!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply