JREAM Posted May 30, 2009 Share Posted May 30, 2009 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 More sharing options...
Michdd Posted May 30, 2009 Share Posted May 30, 2009 Show where you're calling it/where the error is. Link to comment https://forums.phpfreaks.com/topic/160236-solved-return-var-from-function/#findComment-845524 Share on other sites More sharing options...
JREAM Posted May 30, 2009 Author Share Posted May 30, 2009 Heya, the error I get is: Notice: Undefined variable: pagi in C:\test.php on line 22 Its not spitting $pagi out of the function. In test.php its being called like this: pagi($sys_list_max, $sqlcol, $sort_notes); echo $pagi; Link to comment https://forums.phpfreaks.com/topic/160236-solved-return-var-from-function/#findComment-845526 Share on other sites More sharing options...
Michdd Posted May 30, 2009 Share Posted May 30, 2009 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. Link to comment https://forums.phpfreaks.com/topic/160236-solved-return-var-from-function/#findComment-845528 Share on other sites More sharing options...
JREAM Posted May 30, 2009 Author Share Posted May 30, 2009 Ah I see, i global $pagi thanks, i see I am reading up on scope now Link to comment https://forums.phpfreaks.com/topic/160236-solved-return-var-from-function/#findComment-845530 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.