Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: PHP HTML Table needs more than one row

  1. #1
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP HTML Table needs more than one row

    Hello. I have this code:
    PHP 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.

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Code:
    $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.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well, that made every smilie in its own row, so I have one very long column now.

  5. #5
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Here we go, got it figured out. The below code will show 4 items per row.

    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";
    
    if ($key &#37; 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.
    Last edited by thetestingsite; 03-24-2007 at 08:00 PM.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. #6
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  7. #7
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    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.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  8. #8
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  9. #9
    Join Date
    May 2006
    Location
    New York City
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Perhaps try this?

    Code:
    //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] = "&nbsp;";
    		}
    		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");
    Last edited by kosi; 03-26-2007 at 03:28 AM. Reason: fixed a typo

  10. #10
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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}

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •