Forum Moderators: coopster

Message Too Old, No Replies

Output to columns produces 2 different column lengths

not sure why the columns have different lengths

         

RickRoadStar

6:25 am on Jan 28, 2006 (gmt 0)

10+ Year Member



This code was originally written by a good friend, the output displayed fine when the variable for $columnsWanted = 3, however my category names grew in length and I needed to play with the table width a bit and set the $columnsWanted = 2 so the longest category names would disblay without wrapping to a new line.

No matter what I do the column on the right side (populated with categories from the database) is shorter than the column on the left side. Currently the output result from the database is producing 69 category links in the left column and 37 category links on the right column. Everything else is fine.

Any ideas on how to adjust this? Thanks Rick
=====================================================

// retreive the results
// set by you - how many columns of results do you wish?
$columnsWanted = 2;
$estimatedScreenWidth = 620; // pixels wide
$totalRows = $totalCategories; // is determined above by the database query
$avgColumnWidth = round($estimatedScreenWidth / $columnsWanted);
$rowsPerColumn = round($totalRows / $columnsWanted ); // returns the nearest integer value
$displayedCategories = 0;
echo"\n\n\n<table><tr>\n";
$columnCount = 0;
while( $columnCount < $columnsWanted ) {
echo"<td width=\"$avgColumnWidth\" align=\"left\" valign=\"top\">\n";
for( $rowsThisCell=0; $rowsThisCell < $rowsPerColumn; $rowsThisCell++ ) {
//echo $rowsThisCell . "<br>\n";
if($grabARecord = mysql_fetch_array ($queryResultHandle)){
++$displayedCategories;
$category = stripslashes($grabARecord["category"]);
$listingsInACategory = $grabARecord["LISTINGCOUNT"];
// convert the possibly space filled text into a continuous string
$encodedCategory = urlencode($category);

// generate the hyperlink and the correct search type
echo "<a href=\"" . INSTALLPATH . "result.php?goal=$blahblahblah\">$category" . "</a><br>";
}
} // for
// if there is an "odd" number category still not included in the loop then include it
if($displayedCategories == ($totalRows - 1 ) ) {
if($grabARecord = mysql_fetch_array ($queryResultHandle)) { // if we can get a last record
$category = stripslashes($grabARecord["category"]);
$listingsInACategory = $grabARecord["LISTINGCOUNT"];
// convert the possibly space filled text into a continuous string
$encodedCategory = urlencode($category);

// generate the hyperlink and the correct search type
echo "<a href=\"" . INSTALLPATH . "result.php?goal=$blahblahblah\">$category" . "</a><br>";
}
}
echo"</td>\n";
++$columnCount; // increment the column counter
} // while
echo "</tr></table>\n";

simon2263

7:18 am on Jan 28, 2006 (gmt 0)

10+ Year Member



Can't see anything wrong with the code - tested it with dummy data and it produces two equal-length columns. By your description, there seem to be 106 categories - 69+37. So you should get 53 rows in each column. Try printing out all the values of the variables used in the loops and see if this gives you a clue.

RickRoadStar

4:55 pm on Jan 28, 2006 (gmt 0)

10+ Year Member



Thanks, I'll give that a shot. This one has been a puzzle for quite a while and it is affecting a production web site. The originator of the code hasn't been able to sort it out either.

I'll report back with what I find, and if anyone else has any clues please let us know - thanks again, Rick