Jump to content


Query in function..


2 replies to this topic

#1 Marxx

    Young Padawan

  • Members
  • Pip
  • 116 posts
  • Gender:Male
  • Location:Finland

Posted 01 September 2007 - 08:25 AM

Hello all..

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? :g[1]:

#2 Balor

    PHP Nerd

  • Members
  • Pip
  • 63 posts
  • Gender:Male
  • Location:Germany-&gt;Frankfurt
  • Interests:My beautyful girl, my son and webcoding. I'm also very interested in art but I'm not really good at art... it's sad but true ^^

Posted 01 September 2007 - 08:34 AM

An example of an function:
function sample() {
$sql = "SELECT * FROM test";
$result = mysql_query($sql);
if(mysql_num_rows($result)) {
$i=0;
while($row=mysql_fetch_assoc($result)) {
BUILD YOUR ARRAY HERE
$array[$i]['xyz'] = $row['xyz'];
$i++;
}
return $array; // The important thing for you :)
} else {
return false;
}
}

Edited by Balor, 01 September 2007 - 08:36 AM.


#3 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 01 September 2007 - 12:29 PM

You should use the query independent of the function, so you can actually use the results.

From what I can see, you are trying to use $getdata outside the function, and since it is not a global variable, it won't do you any good.

I would perform my queries and get the count for the whole table, then call the pagination function, passing it the total found. Not only would this make it easier, but it would make the pagination function more portable for other things, so it wouldn't be dependent on that one table.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users