Posted on: 02/07/05 07:58pm
By: amckay
function UPAGE_tableSQL( $sql, $func, $cols=0, $rows=0 ) // // execute the given SQL query, then for each row in // the result, call the passed function { global $PHP_SELF; global $paginate; global $QUERY_STRING; global $_CONF; global $LANG_UPAGE; // set to defaults from config.php if not specified by caller if ( empty( $cols ) ) $cols = $_CONF['upic_cols']; if ( empty( $rows ) ) $rows = $_CONF['upic_rows']; // this bit probably wants to be drawn into a function eventually // it does the pagination. Note it relies on the query string // variable "paginate" being used universally by all callers $limit = $cols * $rows; if ( ! empty( $limit ) ) { // first set out lower and upper limits for the SQL query $limlo = $paginate * $limit; $limhi = $paginate * $limit + $limit; // here is where we have to manipulate the query string // parse out the query string by & and then = // to get all variables that are found within $qarr = split( "&", $QUERY_STRING ); while ( list( $key, $value ) = each( $qarr ) ) { list( $qvar, $qval ) = split( "=", $value ); $qnew[$qvar] = $qval; } // if the pageinate variable was not specified (e.g. first page) // then we have to set it. if ( empty( $qnew['paginate'] ) ) $qnew['paginate'] = 0; // now reassemble 2 new query strings, one for 'next' and 'prev' $qnext = ""; $qprev = ""; while ( list( $key, $value ) = each( $qnew ) ) { if ( ! empty( $qnext ) ) $qnext .= "&"; if ( $key == "paginate" ) $qnext .= $key . "=" . ($value + 1); else $qnext .= $key . "=" . $value; if ( ! empty( $qprev ) ) $qprev .= "&"; if ( $key == "paginate" ) $qprev .= $key . "=" . ($value - 1); else $qprev .= $key . "=" . $value; } $sql .= " LIMIT " . $limlo . ", " . $limit; $pageit .= "" . "<tr>" . "<td colspan=$cols align=center>" . "<table align=center width=100% border=0 cellpadding=0 cellspacing=0>" . "<tr>" . "<td width=30% align=left>" . ""; if ( ! empty( $paginate ) ) $pageit .= "<a href='" . $PHP_SELF . "?" . $qprev . "'>prev</a>"; $pageit .= "" . "</td>" . "<td width=40% align=right> " . "</td>" . "<td width=30% align=right>" . "<a href='" . $PHP_SELF . "?" . $qnext . "'>next</a>" . "</td>" . "</td>" . "</tr>" . "</table>" . "</td>" . "</tr>"; // once we drop out here, the $pageit variable can be used to display // the paging information. Note below it's included in the top and // bottom of the table } // try to execute the passed in query if ( ! $result = DB_query( $sql ) ) { $retr = $LANG_UPAGE['err_db_generic']; return $retr; } // woogy! $retr = "" . "<!-- woogy -->" . "<table width=100% align=center border=0>" . $pageit . ""; // we have to count which column we are in $myCol = 1; // column width depends on number of columns $cwid = floor(100/$cols); // loop through each record of the result, keeping track // of what column and row we are in, as well as calling // the passed in function for each record while ( $ROW = DB_fetchArray( $result ) ) { switch ( $myCol ) { case 1 : $retr .= "<tr>"; break; case $cols+1 : $retr .= "</tr>"; // column width depends on number of columns $cwid = floor(100/$cols); // loop through each record of the result, keeping track // of what column and row we are in, as well as calling // the passed in function for each record while ( $ROW = DB_fetchArray( $result ) ) { switch ( $myCol ) { case 1 : $retr .= "<tr>"; break; case $cols+1 : $retr .= "</tr>"; $myCol = 1; break; } $retr .= "<td width=" . $cwid . "% valign=top align=center>"; if ( function_exists( $func ) ) $retr .= $func( $ROW ); else $retr .= $LANG_UPAGE['err_no_func']; $retr .= "</td>"; $myCol++; } // if we didn't end up in the last column because of an odd // number of entries, then we have to finish off the row if ( $myCol <= $cols ) { while ( $myCol++ <= $cols ) $retr .= "<td width=" . $cwid . "% valign=top align=center> </td>"; $retr .= "</tr>"; } $retr .= $pageit . "</table>"; return $retr; }
Posted on: 02/20/05 09:46am
By: Anonymous (duvide)
Posted on: 02/21/05 01:26am
By: newblogger
Posted on: 02/21/05 01:47am
By: newblogger
Posted on: 02/21/05 03:21am
By: Anonymous (duvide)