spitfire1945 Posted December 17, 2008 Share Posted December 17, 2008 Hey guys, I was just making a page for my website and its just a simple listing page. here is the code: <?php require_once("includes/functions.php"); require_once("includes/connection.php"); $numrows = 10; //if (isset($_GET['arrby']) && $_GET['arrby'] == "t") { // $query = mysql_query("SELECT * FROM games ORDER BY game_name ASC LIMIT ".$startnum.",".$numrows.""); // $rows = mysql_num_rows($query); // //} else if (isset($_GET['arrby']) && $_GET['arrby'] == "i") { // $query = mysql_query("SELECT * FROM games ORDER BY id ASC LIMIT ".$startnum.",".$numrows.""); // $rows = mysql_num_rows($query); // //} else if(isset($_GET['letter']) || isset($_GET['page'])) { $starting_letter = $_GET['letter']; $sql = "SELECT * FROM games WHERE game_name LIKE '".$starting_letter."%' ORDER BY game_name ASC "; $query = mysql_query($sql); $rows = mysql_num_rows($query); $last = ceil($rows/$numrows); if (isset($_GET['page'])){ $page = $_GET['page']; } else { $page = 1; } $page = (int)$page; if ($page > $last){ $page = $last; } if ($page < 1){ $page=1; } $limit = 'LIMIT ' .($page - 1) * $numrows .',' .$numrows. ';'; $query = mysql_query($sql.$limit); } else { $query = mysql_query("SELECT * FROM games ORDER BY game_name ASC"); $rows = mysql_num_rows($query); $last = ceil($rows/$numrows); if (isset($_GET['page'])){ $page = $_GET['page']; } else { $page = 1; } $page = (int)$page; if ($page > $last){ $page = $last; } if ($page < 1){ $page=1; } $limit = 'LIMIT ' .($page - 1) * $numrows .',' .$numrows; $query = mysql_query("SELECT * FROM games ORDER BY game_name ASC ".$limit.""); } include("includes/header.php"); ?> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td><img src="images/c_topleft.png" width="5" height="5" /></td> <td width="100%" bgcolor="#e2eaf9"></td> <td><img src="images/c_topright.png" width="5" height="5" /></td> </tr> <tr> <td bgcolor="#e2eaf9"> </td> <td bgcolor="#e2eaf9"><span class="headdings">The List!</span></td> <td bgcolor="#e2eaf9"> </td> </tr> <tr> <td><img src="images/c_bottomleft.png" width="5" height="5" /></td> <td bgcolor="#e2eaf9"></td> <td><img src="images/c_bottomright.png" width="5" height="5" /></td> </tr> </table><br /> <table width="650" border="0" cellpadding="7" cellspacing="0" style="margin-left:5px;"> <tr style="font-weight:bold; background-color:#95b84d; color:#fff;"> <td width="51"><a class="tableheads" href="list.php?arrby=i">Game ID</a></td> <td width="190"><a class="tableheads" href="list.php?arrby=t">Title</a></td> <td>ESRB Rating</td> <td>Trailer Link</td> <td>TIVs</td> <td>Edit</td> </tr> <?php $alphabet = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); echo "<div class=\"alphanumericlinks\">"; foreach ($alphabet as $letter) { echo "<a href=\"?letter=" . $letter . "\">" . $letter . "</a> "; } echo "<a href=\"list.php\">Show All</a></div><br />"; if ($rows == 0) { echo "<tr><td colspan=\"6\">No results found sorry</td></tr>"; } else { while ($info = mysql_fetch_array($query)){ echo "<tr class=\"games\">"; echo "<td width=\"51\">".$info['id']."</td>"; echo "<td><a href =\"game.php?gameid=".$info['id']."\">".$info['game_name']."</a></td>"; echo "<td>"; echo esrbvalue_list($info['esrb']); echo "</td>"; echo "<td><a href =\"".$info['trailer_url']."\">Watch a Trailer</a></td>"; echo "<td><a href =\"game.php?gameid=".$info['id']."\">Click here</a></td>"; echo "<td><a href =\"edit.php?gameid=".$info['id']."\">Edit</a></td>"; echo "</tr>"; } } ?> </table> <?php if ($rows != 0) { echo "<br/><div class=\"alphanumericlinks\">"; if ($page == 1) { echo " First Prev "; } else { echo " <a href='{$_SERVER['PHP_SELF']}?page=1&letter=$starting_letter'>First</a> "; $prevpage = $page-1; echo " <a href='{$_SERVER['PHP_SELF']}?page=$prevpage&letter=$starting_letter'>Prev</a> "; } for ($pg=1; $pg <= $last; $pg++){ if ($page == $pg) { echo " <a href='{$_SERVER['PHP_SELF']}?page=$pg&letter=$starting_letter'><u>$pg</u></a> "; } else { echo " <a href='{$_SERVER['PHP_SELF']}?page=$pg&letter=$starting_letter'>$pg</a> "; } } if ($page == $last) { echo " Next Last "; } else { $nextpage = $page+1; echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage&letter=$starting_letter'>Next</a> "; echo " <a href='{$_SERVER['PHP_SELF']}?page=$last&letter=$starting_letter'>Last</a> "; } echo "</div>"; } include("includes/footer.php"); ?> now everything on the page is working although its not complete as you can tell I haven't cleaned out my GET vars etc etc. I am trying to streamline the code and running into trouble when I try to convert: <?php $query = mysql_query($sql); $rows = mysql_num_rows($query); $last = ceil($rows/$numrows); if (isset($_GET['page'])){ $page = $_GET['page']; } else { $page = 1; } $page = (int)$page; if ($page > $last){ $page = $last; } if ($page < 1){ $page=1; } $limit = 'LIMIT ' .($page - 1) * $numrows .',' .$numrows. ';'; $query = mysql_query($sql.$limit); ?> into a function so i can use it repeatedly. When I make it a function $rows doesn't get a value and $query says invalid resource id. help? ??? Quote Link to comment https://forums.phpfreaks.com/topic/137292-solved-annoying-functions-problem/ Share on other sites More sharing options...
premiso Posted December 17, 2008 Share Posted December 17, 2008 $sql is not defined. Inside functions variables set there are local to that function. If you want to use a defined sql statement pass it as a parameter. <?php myFunc("SELECT * FROM tabl"); function myFunc($sql) { $query = mysql_query($sql); $rows = mysql_num_rows($query); $last = ceil($rows/$numrows); if (isset($_GET['page'])){ $page = $_GET['page']; } else { $page = 1; } $page = (int)$page; if ($page > $last){ $page = $last; } if ($page < 1){ $page=1; } $limit = 'LIMIT ' .($page - 1) * $numrows .',' .$numrows. ';'; $query = mysql_query($sql.$limit); return true; } ?> Hope that helps you understand. http://us3.php.net/manual/en/language.variables.scope.php For more information on what I am talking about. Quote Link to comment https://forums.phpfreaks.com/topic/137292-solved-annoying-functions-problem/#findComment-717317 Share on other sites More sharing options...
spitfire1945 Posted December 17, 2008 Author Share Posted December 17, 2008 thanks see I tried that here is the code for the list.php <?php require_once("includes/functions.php"); require_once("includes/connection.php"); $numrows = 10; //if (isset($_GET['arrby']) && $_GET['arrby'] == "t") { // $query = mysql_query("SELECT * FROM games ORDER BY game_name ASC LIMIT ".$startnum.",".$numrows.""); // $rows = mysql_num_rows($query); // //} else if (isset($_GET['arrby']) && $_GET['arrby'] == "i") { // $query = mysql_query("SELECT * FROM games ORDER BY id ASC LIMIT ".$startnum.",".$numrows.""); // $rows = mysql_num_rows($query); // //} else if(isset($_GET['letter']) || isset($_GET['page'])) { $starting_letter = $_GET['letter']; pagination("SELECT * FROM games WHERE game_name LIKE '".$starting_letter."%' ORDER BY game_name ASC "); // $query = mysql_query($sql); // $rows = mysql_num_rows($query); // $last = ceil($rows/$numrows); // // if (isset($_GET['page'])){ // $page = $_GET['page']; // } else { // $page = 1; // } // // $page = (int)$page; // // if ($page > $last){ // $page = $last; // } // if ($page < 1){ // $page=1; // } // // $limit = 'LIMIT ' .($page - 1) * $numrows .',' .$numrows. ';'; // $query = mysql_query($sql.$limit); } else { $query = mysql_query("SELECT * FROM games ORDER BY game_name ASC"); $rows = mysql_num_rows($query); $last = ceil($rows/$numrows); if (isset($_GET['page'])){ $page = $_GET['page']; } else { $page = 1; } $page = (int)$page; if ($page > $last){ $page = $last; } if ($page < 1){ $page=1; } $limit = 'LIMIT ' .($page - 1) * $numrows .',' .$numrows; $query = mysql_query("SELECT * FROM games ORDER BY game_name ASC ".$limit.""); } include("includes/header.php"); ?> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td><img src="images/c_topleft.png" width="5" height="5" /></td> <td width="100%" bgcolor="#e2eaf9"></td> <td><img src="images/c_topright.png" width="5" height="5" /></td> </tr> <tr> <td bgcolor="#e2eaf9"> </td> <td bgcolor="#e2eaf9"><span class="headdings">The List!</span></td> <td bgcolor="#e2eaf9"> </td> </tr> <tr> <td><img src="images/c_bottomleft.png" width="5" height="5" /></td> <td bgcolor="#e2eaf9"></td> <td><img src="images/c_bottomright.png" width="5" height="5" /></td> </tr> </table><br /> <table width="650" border="0" cellpadding="7" cellspacing="0" style="margin-left:5px;"> <tr style="font-weight:bold; background-color:#95b84d; color:#fff;"> <td width="51"><a class="tableheads" href="list.php?arrby=i">Game ID</a></td> <td width="190"><a class="tableheads" href="list.php?arrby=t">Title</a></td> <td>ESRB Rating</td> <td>Trailer Link</td> <td>TIVs</td> <td>Edit</td> </tr> <?php $alphabet = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); echo "<div class=\"alphanumericlinks\">"; foreach ($alphabet as $letter) { echo "<a href=\"?letter=" . $letter . "\">" . $letter . "</a> "; } echo "<a href=\"list.php\">Show All</a></div><br />"; if ($rows == 0) { echo "<tr><td colspan=\"6\">No results found sorry</td></tr>"; } else { while ($info = mysql_fetch_array($query)){ echo "<tr class=\"games\">"; echo "<td width=\"51\">".$info['id']."</td>"; echo "<td><a href =\"game.php?gameid=".$info['id']."\">".$info['game_name']."</a></td>"; echo "<td>"; echo esrbvalue_list($info['esrb']); echo "</td>"; echo "<td><a href =\"".$info['trailer_url']."\">Watch a Trailer</a></td>"; echo "<td><a href =\"game.php?gameid=".$info['id']."\">Click here</a></td>"; echo "<td><a href =\"edit.php?gameid=".$info['id']."\">Edit</a></td>"; echo "</tr>"; } } ?> </table> <?php if ($rows != 0) { echo "<br/><div class=\"alphanumericlinks\">"; if ($page == 1) { echo " First Prev "; } else { echo " <a href='{$_SERVER['PHP_SELF']}?page=1&letter=$starting_letter'>First</a> "; $prevpage = $page-1; echo " <a href='{$_SERVER['PHP_SELF']}?page=$prevpage&letter=$starting_letter'>Prev</a> "; } for ($pg=1; $pg <= $last; $pg++){ if ($page == $pg) { echo " <a href='{$_SERVER['PHP_SELF']}?page=$pg&letter=$starting_letter'><u>$pg</u></a> "; } else { echo " <a href='{$_SERVER['PHP_SELF']}?page=$pg&letter=$starting_letter'>$pg</a> "; } } if ($page == $last) { echo " Next Last "; } else { $nextpage = $page+1; echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage&letter=$starting_letter'>Next</a> "; echo " <a href='{$_SERVER['PHP_SELF']}?page=$last&letter=$starting_letter'>Last</a> "; } echo "</div>"; } include("includes/footer.php"); ?> and here is the function <?php function pagination($sql) { global $numrows; $query = mysql_query($sql); $rows = mysql_num_rows($query); $last = ceil($rows/$numrows); if (isset($_GET['page'])){ $page = $_GET['page']; } else { $page = 1; } $page = (int)$page; if ($page > $last){ $page = $last; } if ($page < 1){ $page=1; } $limit = 'LIMIT ' .($page - 1) * $numrows .',' .$numrows. ';'; $query = mysql_query($sql.$limit); return true; } ?> but i get back to square one. In this piece of code in list.php: <?php if ($rows == 0) { echo "<tr><td colspan=\"6\">No results found sorry</td></tr>"; } else { while ($info = mysql_fetch_array($query)){ echo "<tr class=\"games\">"; echo "<td width=\"51\">".$info['id']."</td>"; echo "<td><a href =\"game.php?gameid=".$info['id']."\">".$info['game_name']."</a></td>"; echo "<td>"; echo esrbvalue_list($info['esrb']); echo "</td>"; echo "<td><a href =\"".$info['trailer_url']."\">Watch a Trailer</a></td>"; echo "<td><a href =\"game.php?gameid=".$info['id']."\">Click here</a></td>"; echo "<td><a href =\"edit.php?gameid=".$info['id']."\">Edit</a></td>"; echo "</tr>"; } } ?> I get no variable for $rows even though inside the function its a value and i get mysql_fetch_array(): supplied argument is not a valid MySQL result resource for the next php statement. I have tried to return $rows and return $query as well Quote Link to comment https://forums.phpfreaks.com/topic/137292-solved-annoying-functions-problem/#findComment-717350 Share on other sites More sharing options...
spitfire1945 Posted December 17, 2008 Author Share Posted December 17, 2008 OMG NOOB FAIL all i had to do was global $numrows, $query, $rows; gah! thanks fellas! Quote Link to comment https://forums.phpfreaks.com/topic/137292-solved-annoying-functions-problem/#findComment-717360 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.