Jump to content

[SOLVED] Return Var from function


JREAM

Recommended Posts

Could anyone help me see why my variable isnt outputting? :)

 

At the bottom I have return $pagi;

Notice: Undefined variable: pagi in C:\test.php on line 22 (separate file here)

 

I put the long version first, and then just the super short version second:

<?php

function pagi ($sys_list_max, $sqlcol, $sort_notes) {
##########			  ##########
########## start:Pagi ##########
##########			  ##########
$pgLimit = $sys_list_max;

if (!isset($_REQUEST['p'])) {$newp = 1;}
else {$newp = $_REQUEST['p'];}

$sql = mysql_query("SELECT * FROM $sqlcol");
$totalrows = mysql_num_rows($sql);
$pnums = ceil ($totalrows/$pgLimit);

$start = ($newp-1) * $pgLimit;
$starting_no = $start + 1;

if ($totalrows - $start < $pgLimit) {$end_count = $totalrows;} 
elseif ($totalrows - $start >= $pgLimit) {$end_count = $start + $pgLimit;}

$pagi = "Viewing $starting_no-$end_count of $totalrows entries<br />";

if ($totalrows - $end_count > $pgLimit) {$var2 = $pgLimit;} 
elseif ($totalrows - $end_count <= $pgLimit) {$var2 = $totalrows - $end_count;}

## start:PagiDisplay ##
if ($newp > 1) {
$pagi .= "<a href='$sqlcol.php?p=".($newp-1)."'>Prev</a> ";
} else {$pagi .= "Prev ";}

for ($i = 1; $i <= $pnums; $i++) {
if ($i != $newp) {$pagi .= "<a href='$sqlcol.php?p=$i'>$i</a>";}
else {$pagi .= $i;}
}

if ($newp < $pnums) { 
$pagi .= "<a href='$sqlcol.php?p=".($newp+1)."'>Next</a>";}
else {$pagi .= "Next ";}
## end:PagiDisplay ##

$sql = mysql_query("SELECT * FROM `$sqlcol` ORDER BY $sort_notes LIMIT $start,$pgLimit");

while ($query = mysql_fetch_array($sql)) {
$listNotes[] = $query;
}
##########			  ##########
########## end:Pagi   ##########
##########			  ########

return $pagi;

}
?>

 

super short version without all the text>>

<?php

function pagi ($sys_list_max, $sqlcol, $sort_notes) {

$pagi = "Viewing $starting_no-$end_count of $totalrows entries<br />";
if ($newp > 1) {
$pagi .= "<a href='$sqlcol.php?p=".($newp-1)."'>Prev</a> ";
} else {$pagi .= "Prev ";}

return $pagi;

}
?>

Link to comment
https://forums.phpfreaks.com/topic/160236-solved-return-var-from-function/
Share on other sites

Notice: Undefined variable: pagi in C:\test.php on line 22

 

pagi($sys_list_max, $sqlcol, $sort_notes);

 

echo $pagi;

$pagi = pagi($sys_list_max, $sqlcol, $sort_notes);

echo $pagi;

The variable defined in the function is only defined within that scope. You must assign the output of the function to a separate variable.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.