// listAvatar code.
function listAvatar() {
$sql = "SELECT COUNT(*) FROM avatar";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 20;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
// get the info from the db
$sql1 = "SELECT * FROM avatar ORDER BY avatar ASC LIMIT $offset, $rowsperpage";
$result1 = mysql_query($sql1) or trigger_error("SQL", E_USER_ERROR);
//If cmd has not been initialized
if(!isset($cmd))
{
echo"
";
} // end while
if ($numrows > 0) {
/****** build the pagination links ******/
//Set the currentpage to whatever you need it to be
$_GET['currentpage'];
//Rebuild the querystring
foreach($_GET as $keyname => $value) {
$querystring.=$keyname . "=" . $value . "&";
}
//Add it to the current script name to make the new URL
$newURL = $_SERVER['PHP_SELF'] . "?" . $querystring;
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo "Goto page: First ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " < ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo "[$x]";
// if not current page...
} else {
// make it a link
echo " $x ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " > ";
// echo forward link for lastpage
echo " Last ";
} // end if
/****** end build pagination links ******/
}
}
// deleteAvatar code
function deleteAvatar() {
// Check if delete button active, start this
if(isset($_POST['delete'])) {
// Set arrays
$id_list = array();
$cat_list = array();
// Add checked items to $id_list Array
for($i=0;$i<$count;$i++){
$del_id = $_POST['checkbox'][$i];
if($del_id != NULL){
$id_list[] = $del_id;
}
}
// Build SQL WHERE IN $id_sql
$id_sql = "";
$a = "0";
foreach ($id_list as $value) {
if($a>0) $id_sql .= ",";
$id_sql .= $value;
$a++;
}
// Get Category Names
$sqla="SELECT image FROM `avatar` WHERE id IN ($id_sql)";
$resulta = mysql_query($sqla);
// Add Category Names to $cat_list Array
while($rslt = mysql_fetch_array($resulta)){
$cat_list[] = $rslt['image'];
}
// Build SQL WHERE IN $cat_sql
$cat_sql = "";
$a = "0";
foreach ($cat_list as $value) {
if($a>0) $cat_sql .= ",";
$cat_sql .= "'".$value."'";
$a++;
}
// Delete the records
$sqldel = "SELECT image FROM avatar WHERE image IN ($cat_sql)";
$resultdel = mysql_query($sqldel);
while($rowdel = mysql_fetch_array($resultdel)){
unlink("../avatars/".$rowdel['image']);
}
$sql = "DELETE FROM avatar WHERE id IN ($id_sql)";
$result = mysql_query($sql);
}
}
|