Forum Moderators: coopster

Message Too Old, No Replies

loop for 2 columns inside of a function

loop for 2 columns inside of a function

         

allhandl

9:02 pm on Jan 27, 2006 (gmt 0)

10+ Year Member



Im going mad.. Instead of showing you how far Ive made it Id like to see the best way to do this.

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;
}

coopster

12:11 am on Jan 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, allhandl.

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.

Salsa

12:26 am on Jan 28, 2006 (gmt 0)

10+ Year Member



Here's one possiblily, for example.

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

Salsa

1:01 am on Jan 28, 2006 (gmt 0)

10+ Year Member



...wasn't thinking...$row % wont give you what you need...

initialize $i = 0 before loop
if ($i % 2 == 0).... instead of if ($row % 2 == 0)...
$i++ before loop closes

allhandl

1:59 am on Jan 28, 2006 (gmt 0)

10+ Year Member



absolutely perfect.. thanks a million

how would I go about putting the output in 1 colum of 20 and then the second column of 20?

Whats it doing now is printing a row like so
1 2
3 4

like to see
1 4
2 5
3 6

Salsa

3:10 am on Jan 28, 2006 (gmt 0)

10+ Year Member



Of course, the simplest thing would be to put the first half of the data in one <td> with a <br> after each entry, and the second have in another, but you could also put each item in a separate cell with something like this.

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

allhandl

1:01 am on Jan 30, 2006 (gmt 0)

10+ Year Member



ok perfect,. I see what your saying now. Thanks again for evetyhing. Gotta say I was heading differ direction with it which is why it didnt get anywhere.

;o)