Forum Moderators: coopster

Message Too Old, No Replies

Help with tables row and columns

Tables row and columns

         

boboman

5:08 pm on Mar 12, 2009 (gmt 0)

10+ Year Member



I have a small page of php page which grab my images from the SQL tables and display them in a page. The problem now is that the output display only display 3 columns and 2 rows. I want to make it to display 3 rows but it just wont work.

How can I modify the script to display 3 rows instead of fixed 2 rows?

Below are the code of the page,

<?php

$link = DB_connect();
$thumb_width = 230;

$rows = 3;
$cols = 3;

// pagination part 1
$TotalToShow = $rows * $cols;
$StartLimit = 0;

if(isset($_GET['Total']))
$TotalToShow = $_GET['Total'];

if(isset($_GET['Start'])) {
if( $_GET['Start'] < 0)
$StartLimit = 0;
else
$StartLimit = $_GET['Start'];
}

if ($TotalToShow == 1) {
if($StartLimit > 0)
$Limit = $StartLimit;
else
$Limit = 1;
} else {
$Limit = $StartLimit . ", " . $TotalToShow;
}

$sql = "SELECT a.*, b.username FROM photos AS a, users AS b WHERE a.userid=b.userid ORDER BY b.username ASC, a.photoid DESC";

if (!$res = mysql_query($sql)) {

$msg = mysql_errno($link) . ": " . mysql_error($link);

} else {

$NumRecords = mysql_num_rows($res);
$sql = $sql." LIMIT $Limit";
$res = mysql_query($sql);

// pagination part 2
$TotalPages = (int)($NumRecords / $TotalToShow);

$NumRemaning = $NumRecords % $TotalToShow;
if($NumRemaning >= "1")
$TotalPages++;

// pagination part 3
$pagination = "";
if($StartLimit != 0) { // Show the previous Link

$No = $StartLimit-$TotalToShow;
$pagination .= '<a href="'.$php_self.'?Total='.$TotalToShow.'&Start='.$No.'">Previous</a> ';

} else {

if ($TotalPages > 1)
$pagination .= "Previous ";

}

// show the page numbers
$NextStartPage = 0;
$pagination .= " ";
for( $x=0; $x < $TotalPages; $x++) {

if( $StartLimit == $NextStartPage) {
if ($TotalPages > 1)
$pagination .= ' '.($x+1).' ';
} else {
$pagination .= ' <a href="'.$php_self.'?Total='.$TotalToShow.'&Start='.$NextStartPage.'">'.($x+1).'</a> ';
}
$NextStartPage = $NextStartPage+$TotalToShow;
}

if(($StartLimit + $TotalToShow) <= ($NumRecords - 1)) { // Show the Next Link

$No = $TotalToShow + $StartLimit;
$pagination .= ' <a href="'.$php_self.'?Total='.$TotalToShow.'&Start='.$No.'">Next</a>';

} else {

if ($TotalPages > 1)
$pagination .= " Next";

}

}

?>

<h1>Photo gallery</h1>

<?

if ($msg <> "") {

echo '<div id="error">';
echo '<h2>System Message</h2>';
echo $msg;
echo '<p><a href="'.$php_self.'">[Back]</a></p>';
echo '</div>';

} else {

echo '<table id="photos">';
if (!mysql_num_rows($res)) {

echo '<tr><td>No photo record found</td></tr>';

} else {

$cols_count = 0;
$rows_count = 0;
while ($row = mysql_fetch_array($res)) {

while (list($key, $val) = each($row))
$$key = $val;

// image related
$real_filepath = IMG_DIR_PHP."/".$filename;
list($width, $height, $type, $attr) = getimagesize($real_filepath);
// do resize here
if ($width > $thumb_width) {
$height = ($thumb_width / $width) * $height;
$width = $thumb_width;
}
$width = round($width, 0);
$height = round($height, 0);

$imgsrc = "get_image.php?id=$photoid&width=$width&height=$height";
$url = "photo.php?id=$photoid";

if (!$modified_date)
$mod_date_text = convertdate($creation_date, false);
else
$mod_date_text = convertdate($modified_date, false);

if ($cols_count == 0)
echo '<tr valign"top">';

$cols_count++; // increment cols count

echo '<td>

<img src="'.$imgsrc.'" alt="'.$filename.'" title="'.$title.'" width="'.$width.'" height="'.$height.'" border="1" />

</td>';

if ($cols_count >= $cols) {
$cols_count = 0;
echo '</tr>';
}

}

}
echo '</table>';
echo $pagination;

}

DB_close();

?>

coopster

10:35 pm on Mar 21, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you tried dumping your query to see if the LIMIT clause looks correct?
If so, next would be to dump the raw result set after the query and see if that looks correct. Of course, we are assuming you have at least 3 rows of data in the table ;)

gettopreacherman

4:37 pm on Mar 26, 2009 (gmt 0)

10+ Year Member



I don't see anything in your code that limits what could be displayed, it seems like you may only have six items to show...did you check your query results? In the future, Pagination is usually more effecient using LIMIT in your query and only showing the part you need and not pulling the entire set of data, if you get too large you will have scaling issues with that query now.