Forum Moderators: coopster
I want to take this code and have it read out in 2 columns thats it. The results will print `40 so id like to have 20 per column.
any advice?
function topic_list_beta () {
global $DB_site, $dbprefix;
$temp=$DB_site->query("SELECT * FROM ".$dbprefix."topics order by name asc");
while ($row=$DB_site->fetch_array($temp)) {
$topic_name = str_replace("\"",""", stripslashes($row[name]));
$topic_url = str_replace("\"",""", stripslashes($row[url]));
$out .= '<img src="/images/graphics/dot.gif"> <a href="'.$topic_url.'" TITLE="'.$topic_name.'">'.$topic_name.'</a><br>';
}
return $out;
}
Are you saying you want to format the output in HTML in two columns? It sounds like an obvious question, but it has to be asked ... Do you know how to do this in HTML? Say for instance, in a table? If so, then all you have to do is apply some logic within this loop to output the data in that format.
Before your while loop, Try something like this:
Initialize a variable (replaces $out):
$_2columns = "<table>";
...then at the end of your while loop, replace $out with something like:
if ($row % 2 == 0)$_2columns .= "<tr><td>[old contents of $out]</td>";
else $_2columns .= "<td>[old contents of $out]</td></tr>";
} // while loop closed
$_2columns = $_2columns."</table>";
return $_2columns;
}
Untested, but I hope that helps,
Salsa
Before while loop:
$cell_data = array();
In your while loop, put the data for each cell in an array with no table formatting, like:
$cell_data[] = [old contents of $out];
} // close while loop
$column_length = ceil(count($cell_data)/2);
$_2columns = "<table>";
for ($i = 0; $i < $column_length; $i++){
$_2columns .= "<tr><td>".$cell_data[$i]."</td><td>".$cell_data[$i + $column_length]."</td></tr>";
} // ...could do this all on one line, without brackets
$_2columns = $_2columns."</table>";
return $_2columns;
}
Again, untested, but that could be one scheme,
Salsa