If I have database query in function, how do I get that result out of that function?
Example of my function (pagination)
function:
function pagitop($table, $orderby, $results) {
// PAGINATION START
print("<div id=\"pagination\">");
if($_GET['page'])
{
$page = $_GET['page'];
}else{
$page = 1;
}
$max = $results;
$cur = (($page * $max) - $max);
$getdata = mysql_query("SELECT * FROM ".$table." ORDER BY ".$orderby." DESC LIMIT $cur, $max") or die(mysql_error());
$counttotal = sql_query("SELECT * FROM ".$table." ") or die(bug_mysql_error());
$counttotal = mysql_num_rows($counttotal);
$total_pages = ceil($counttotal / $max);
if($page > 1){
$prev = ($page - 1);
print("<a href=\"?page=" . $prev . "\">Previous page</a> ");
} else {
print("<span class=\"pagioff\">Previous page</span> ");
}
for($i = 1; $i <= $total_pages; $i++)
{
if($page == $i)
{
print("<b>" . $i . "</b> ");
} else {
print("<a href=\"?page=" . $i . "\">" . $i . "</a> ");
}
}
if($page < $total_pages){
$next = ($page + 1);
print(" <a href=\"?page=" . $next . "\">Next page</a>");
} else {
print(" <span class=\"pagioff\">Next page</span>");
}
print("</div>");
// PAGINATION ENDS
}
and file where I should use that function:
print("<table cellspacing=\"1\" cellpadding=\"5\" width=\"100%\">");
$c = 0;
while($users = mysql_fetch_object($getdata)) {
$img = mysql_fetch_row(sql_query("SELECT thumb45 FROM images WHERE uid = ".$users->id." AND defaultimg = 1"));
$city = mysql_fetch_row(sql_query("SELECT cityname FROM citys WHERE id = ".$users->city.""));
$showusers = ($c && $c % 2 == 0) ? "</tr><tr>\n" : "";
$showusers .= "<td width=\"5px\"><a href=\"".$NUORILLE."/profile.php?user=".$users->username."\"><img src=\"".$NUORILLE . "/" . $img['0']."\" /></a></td>\n";
$showusers .= "<td><a href=\"".$NUORILLE."/profiili.php?user=".$users->username."\">Username: ".$users->username."</a><br />\n";
$showusers .= "City: ".$city['0']."</td>\n";
print("$showusers");
++$c;
}
print("</table>");
I call this function like this:
pagitop("users", "username", "10");
where "users" are database table, "username" is order by and "10" is limit and I should get this $getdata out of that function.
Anyhow, it doesn't work like this tho.. getting this errormsg
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in......
Thanks for all help and if someone knows better way to do pagination via function, could you please share it whit me?
