View Full Version : PHP HTML Table needs more than one row
panachepad
03-24-2007, 06:47 PM
Hello. I have this code:
$tmp .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"1\"><tr>\n";
foreach ($smilies as $key => $val)
{
$tmp .= "<td width=\"17\"><img src=\"$val\" alt=\"$key\" title=\"$key\" onclick=\"insert_smilies ('$key')\" /></td>\n";
}
$tmp .= '</tr></table>';
I need to have the images display in more than one row. Can someone help me please to fix this so that the one single row displays as two, (or more) rows? I'm a php dunce. :confused:
thetestingsite
03-24-2007, 06:58 PM
$tmp .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"1\">\n";
foreach ($smilies as $key => $val)
{
$tmp .= "<tr><td width=\"17\"><img src=\"$val\" alt=\"$key\" title=\"$key\" onclick=\"insert_smilies ('$key')\" /></td></tr>\n";
}
$tmp .= '</table>';
That will place an image per row. Not sure how you would go about placing multiple images per row though.
Hope this helps.
panachepad
03-24-2007, 07:01 PM
Hi testingsite! I'm going to try that, but first maybe I should say a bit more about what I have. In the lang file for this program, we list the smilies. It came with approximately 15 smilies, but I have added about 30 more. Since the code is set up for just one row, I ended up with one extremely long row. I just want to break it up into two or more.
I hope that makes sense. Going to test your suggestion now.
panachepad
03-24-2007, 07:03 PM
Well, that made every smilie in its own row, so I have one very long column now. :eek:
thetestingsite
03-24-2007, 07:44 PM
Here we go, got it figured out. The below code will show 4 items per row.
$tmp .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"1\"><tr>\n";
foreach ($smilies as $key => $val)
{
$tmp .= "<td width=\"17\"><img src=\"$val\" alt=\"$key\" title=\"$key\" onclick=\"insert_smilies ('$key')\" /></td>\n";
if ($key % 4 == 0) $tmp .= "</tr><tr>";
}
$tmp .= '</tr></table>';
Change the part in red (in the code above) to however many images you want per row.
Hope this helps.
panachepad
03-24-2007, 07:56 PM
I have tried that now, but still it is not working. At first I thought maybe it was because you have $smile in the added part, but even when I change that to $smilies it does not work. I have tried changing the number 4 to other numbers, too, but still it does not work.
thetestingsite
03-24-2007, 08:04 PM
Sorry, typo but editted now. In that line that I added, it needs to be $key, not $smilies or any other variation. Sorry about that.
panachepad
03-24-2007, 08:11 PM
I've tried it again with the change to $key, but it is making one long column again. I have no idea why it is making that column and why it doesn't just go back to making the one long row as it did originally.
Perhaps try this?
//the way this table works, it starts with one, not zero, so first declare a null value for "boxes[0]"
$boxes[0] = "";
// now each smiley should be saved as a "box"
foreach ($smilie as $key => $val) {
$boxes[] = "<td width=\"17\"><img src=\"$val\" alt=\"$key\" title=\"$key\" onclick=\"insert_smilies ('$key')\" /></td>\n";
}
// This table will have four columns and as many rows as you need.
$cols = 4;
$rows = ceil(count($boxes) / $cols);
$i = 1;
$j = 1;
echo ("<table border='2' align='center'>\n");
while ($i <= $rows) {
echo "\t<tr class='row$i'>\n";
while ($j <= $i * $cols) {
if (!isset($boxes[$j])) {
$boxes[$j] = " ";
}
echo ("\t\t<td id='box$j' >".$boxes[$j]."</td>\n");
$j++;
}
if ($j > $i * $cols) {
echo ("\t</tr>\n");
$i++;
}
}
echo ("</table>\n\n<br/><br/>\n\n");
panachepad
03-27-2007, 10:59 PM
Hi kosi. Sorry I have taken a while to reply. I didn't see your message.
I'm trying to implement your code, but I don't know which part of the code I have quoted is replaced by it, if any. Do I just add that somewhere? I have a feeling it is replacement code, but I can't figure out for which lines exactly.
{php dunce}
Hi kosi. Sorry I have taken a while to reply. I didn't see your message.
I'm trying to implement your code, but I don't know which part of the code I have quoted is replaced by it, if any. Do I just add that somewhere? I have a feeling it is replacement code, but I can't figure out for which lines exactly.
{php dunce}
It was meant to replace the entirety. Or at least the entirety of what you've posted on this board. (btw, sorry i've taken so long to respond as well.)
And it's not nice to call yourself names, lol.
panachepad
03-29-2007, 10:46 AM
Okay! Well, I had to make one change to get it to show up, and that was
foreach ($smilie as $key => $val) {
to
foreach ($smilies as $key => $val) {
Once I did that, it made a table just as planned, except the table is now at the top of the page! LOL.
I appreciate the help from thetestingsite and kosi. I have found another program that is going to work better overall. I don't want to stay with a program that has no support on their board anyway.
Thanks again. I admire you folks with php sense. :)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.