SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Returning database info across 3 columns

    I'm currently making a portfolio page which I want to use to display 3 images per row in a table.

    Basically, I want to force the database to display its list in a 3xwhatever table.

    Ie:
    image 1, image 2, image 3
    image 4, image 5, blank
    Here is what I'm doing with my code:

    Code:
    echo ('<table cellpadding="0" cellspacing="0" border="0">');
    echo ('<tr>');
    while ( $row = mysql_fetch_array($result) ) {
    	$id = $row['id'];
    	$title = $row['title'];
    	$thumb = $row['thumb'];
    
    	echo '<td><a href='.$somelink.'><img src="$someimage'" alt='.$title.' border="0" /></a></td>';
    } // wend
    
    echo ('</tr>');
    echo ('</table>');
    Although the above displays the image in a column, it won't limit it to three like I want it to, of course I need to ensure that on the last row it replaces empty td's with none breaking spaces.

    Any help will be appreciated.

  2. #2
    SitePoint Addict Avido's Avatar
    Join Date
    Jul 2003
    Location
    Kortrijk, Belgium, Europe, the world
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok,

    Code:
    $nr=1;
    
    echo ('<table cellpadding="0" cellspacing="0" border="0">');
    echo ('<tr>');
    while ( $row = mysql_fetch_array($result) ) {
    	$id = $row['id'];
    	$title = $row['title'];
    	$thumb = $row['thumb'];
    
    	echo '<td><a href='.$somelink.'><img src="$someimage'" alt='.$title.' border="0" /></a></td>';
    if($nr%3==0)
    {
    
    echo '</tr><tr>';
    $nr = 1;
    } } // wend for($x=0;$x<=3-$nr;$x++) {
    echo('<td></td>');
    } echo ('</tr>'); echo ('</table>');
    It is not completely correct, but it should be in the right direction.

  3. #3
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks alot, I shall try that. Thanks again!

  4. #4
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I'm having some problems - for some reason it offsets any row after the first.

    I changed the code a little because the for-next loop was outside the main loop.

    Code:
    $nr=1;
    echo ('<table cellpadding="0" cellspacing="0" border="0">');
    echo ('<tr>');
    while ( $row = mysql_fetch_array($result) ) {
    	$id = $row['id'];
    	$title = $row['title'];
    	$thumb = $row['thumb'];
    
    	echo '<td><a href='.$somelink.'><img src="$someimage'" alt='.$title.' border="0" /></a></td>';
    
    
    	if ($nr%3==0) {
    		echo '</tr><tr>';
    		$nr = 1;
    	} // end if
    	for($x=0;$x<=3-$nr;$x++)
    	{echo('<td>&nbsp;</td>');}
    	$nr++;
    } // wend
    echo ('</tr>');
    echo ('</table>');

  5. #5
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THis works for me, perhaps it's of any help:

    PHP Code:
    function pc_grid_horizontal($array$size)
    {
        
    //-- compute <td> width %ages
        
    $table_width 50;
        
    $width intval($table_width $size);
     
        
    //-- define how our <tr> and <td> tags appear
        //-- sprintf() requires us to use %% to get literal %
        
    $tr '<tr align="center">';
        
    $td "<td width=\"$width%%\">%s</td>";
     
        
    //-- open table
        
    $grid "<table border=\"1\" width=\"$table_width%\">$tr";
     
        
    //-- loop through entries and display in rows of size $sized
        //-- $i keeps track of when we need a new table tow
        
    $i 0;
        foreach (
    $array as $e)
        {
            
    $grid .= sprintf($td$e);
            
    $i++;
     
            
    //-- end of a row, close it up and open a new one
            
    if (!($i $size))
            {
                
    $grid .= "</tr>$tr";
            }
        }
     
        
    //-- pad out remaining cells with blanks
        
    while ($i $size)
        {
            
    $grid .= sprintf($td'&nbsp;');
            
    $i++;
        }
     
        
    //-- add </tr>, if necessary
        
    $end_tr_len strlen($tr) * -1;

        if (
    substr($grid$end_tr_len) != $tr)
        {
            
    $grid .= '</tr>';
        }
        else
        {
            
    $grid substr($grid0$end_tr_len);
        }
     
        
    //-- close table
        
    $grid .= '</table>';
        return 
    $grid;
    }

     
    //-- get data
    $sql "SELECT s_states FROM tbl_states";
    $r mysql_query($sql);

    while (
    $row mysql_fetch_array($r))
    {
       
    $states[ ] = $row['s_states'];
    }
     
    //-- generate the HTML table
    $grid pc_grid_horizontal($states8);
     
    //-- and print it out
    print $grid
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  6. #6
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I'll take a look.

  7. #7
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that helped a lot - I got it to work - its a bit slow - but I guess it doesn't matter right now. Thanks again.

  8. #8
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my pleasure and feel free to optimize it
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.


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
  •