Re: ->>coding help

From: Date: Sun, 24 Jun 2001 20:05:37 +0000
Subject: Re: ->>coding help
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-55046@lists.php.net to get a copy of this message
""McShen"" <webmaster@celeritieszones.com> wrote in message news:9h3lrv$dn$1@w153.z064000151.sjc-ca.dsl.cnc.net... > Hi > > I have a script which queries mySQL and outputs 32 links at once. How 'bout this? ======================================= define("PER_ROW", 2); $row = Array(); $in_row = 0; // accepts nothing, returns nothing function ClearRow() { global $in_row; $in_row = 0; } // accepts text string, returns true if row is full function AddToRow($val) { global $row, $in_row; $row[$in_row++] = $val; return($in_row >= PER_ROW); } // accepts nothing, returns row string function MakeRow() { global $row, $in_row; $str = ""; for ($i = 0; $i < PER_ROW; $i++) $str .= "\n\t\t<td>".($i < $in_row ? $row[$i] : "")."</td>"; ClearRow(); return "\n\t<tr>$str\n\t</tr>"; } // accepts database result handle, returns table string function MakeTable($result) { $str = ""; while($row = mysql_fetch_array($result)) if (AddToRow($row["title"])) $str .= MakeRow(); return "\n<table border=\"0\">$str\n</table>"; } $query = "SELECT * FROM refer ORDER BY hits, desc LIMIT $list, $end"; $result = mysql_db_query ("celebzone", $query); ClearTable(); echo MakeTable($result); ==================================== This separates things out so you can see that each pair of tags matches; it also doesn't leave incomplete rows in your table (ie if there is an odd number of links returned) and makes it easy to change the number of columns and rows.

« previous php.general (#55046) next »