chaseman Posted February 27, 2011 Share Posted February 27, 2011 I have this chunk of code multiple times in if and elseif statements, to make the code more manageable I'd like to compile it together: Here's an excerpt: // ALL but NO OTHER if (($select_category == 'All') || (!isset($select_category)) && (!isset($most_liked))) { $query = "SELECT * FROM con, user WHERE con.user_id = user.user_id AND nickname = '$user_name' ORDER BY contributed_date DESC"; pagination_start ($dbc, $query); $offset = $pag_array[0]; $rows_per_page = $pag_array[1]; $query = $query . " LIMIT $offset, $rows_per_page"; knuffix_list ($query, $dbc); pagination_end ($pag_array); // CATEGORY but NOT most_liked } elseif (isset($select_category) && !isset($most_liked)) { $query = "SELECT * FROM con, user WHERE con.user_id = user.user_id AND nickname = '$user_name' AND category = '$select_category' ORDER BY contributed_date DESC"; pagination_start ($dbc, $query); $offset = $pag_array[0]; $rows_per_page = $pag_array[1]; $query = $query . " LIMIT $offset, $rows_per_page"; knuffix_list ($query, $dbc); pagination_end ($pag_array); The statements continue, as you can see it's the same chunk of code over and over, and I use the same chunk on a different page as well, just with a different query, that means the only thing that changes is the query. If I would want to change one thing about that chunk, I'd have to go through all of them and change it one by one, which is a pain. But if I put everything except the query into it's own function and then call that "ultimate" function it won't work and nothing will show up. When putting them into their own function, I also made sure that the three variables after pagination_start are at their right place, it's still didn't work. Any idea how I could compile this chunk of code in more manageable manner? Just to avoid any confusion, when I say "chunk of code" I mean this portion: pagination_start ($dbc, $query); $offset = $pag_array[0]; $rows_per_page = $pag_array[1]; $query = $query . " LIMIT $offset, $rows_per_page"; knuffix_list ($query, $dbc); pagination_end ($pag_array); Link to comment https://forums.phpfreaks.com/topic/229066-compile-this-chunk-of-code/ Share on other sites More sharing options...
requinix Posted February 27, 2011 Share Posted February 27, 2011 Stick it into a function and call the function when you need it. Link to comment https://forums.phpfreaks.com/topic/229066-compile-this-chunk-of-code/#findComment-1180553 Share on other sites More sharing options...
chaseman Posted February 27, 2011 Author Share Posted February 27, 2011 As I wrote in my post, I already did that but then NOTHING will show up, I simply get a blank space. I'm guessing the query won't work anymore, and I can not figure out why. When I try this one: function fetch_all ($dbc, $query) { include ('knuffix_list_func.php'); pagination_start ($dbc, $query); $offset = $pag_array[0]; $rows_per_page = $pag_array[1]; $query = $query . " LIMIT $offset, $rows_per_page"; knuffix_list ($query, $dbc); pagination_end ($pag_array); } fetch_all ($dbc, $query); It will showcase the correct number of rows in the database with num_rows, but it also will show a SQL syntax error. If I wrap a function around the function DECLARATIONS (not the calls as above), then the num_rows won't work and it will just show a blank space, nothing will be fetched. This is the error I get if I wrap a function around the function CALLS: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Link to comment https://forums.phpfreaks.com/topic/229066-compile-this-chunk-of-code/#findComment-1180566 Share on other sites More sharing options...
chaseman Posted February 27, 2011 Author Share Posted February 27, 2011 I think I know why it won't work: see: include ('knuffix_list_func.php'); pagination_start ($dbc, $query); $offset = $pag_array[0]; $rows_per_page = $pag_array[1]; $query = $query . " LIMIT $offset, $rows_per_page"; echo "test query 2 " . $query; knuffix_list ($query, $dbc); pagination_end ($pag_array); } If I echo out that query, then it will show that the variables $offset and $rows_per_page, are not appended. Usually they will be appended, but as soon as I wrap a function around that whole thing it won't work anymore, the variables will be empty. $pag_array[0] and $pag_array[1] are set inside of pagination_start(), and they're set to global, but it seems that the global does not work when I wrap another function around the whole chunk of code. Any ideas, how I can make those variables still work and be set? Link to comment https://forums.phpfreaks.com/topic/229066-compile-this-chunk-of-code/#findComment-1180575 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.