GL Posted May 23, 2008 Share Posted May 23, 2008 How do I loop the query into 2nd page cause upon clicking on the 2nd page, it will return the last else statement instead of say the 1st if statement. Everything is correct except for the above statement, I just cut down a few stuff to make it shorter. <?php include 'config.php'; include 'opendb.php'; include 'function.php'; $rowsPerPage = 10; //Variable for how many rows to display per page $pageNum = 1; //This is the first page // If this is defined, use it as page number. if(isset($_GET['page'])) { $pageNum = $_GET['page']; } //Counting the offset $offset = ($pageNum - 1) * $rowsPerPage; if ($SearchSelect == 1){ $query = "blah blah"; } else $query = "blah blah"; ; // Searches Locations in the database $result = mysql_query($query) or die; //while loop condition to loop out all the results from the query while($row = mysql_fetch_array($result)) { all results } $query = "SELECT Asset_ID FROM Asset"; //Select all Rows from Asset Table $result = mysql_query($query) or die; //Execute Query $TableRows = mysql_num_rows($result); // Count the total amount of rows in database $maxPage = ceil($TableRows/$rowsPerPage);//Calculates how many pages to use when paging $self = $_SERVER['PHP_SELF']; $nav = ''; for($page = 1; $page <= $maxPage; $page++) { if ($page == $pageNum) { $nav .= " $page "; // no need to create a link to current page } else { $nav .= " <a href=\"$self?page=$page\">$page</a> "; } } if ($pageNum > 1) { $page = $pageNum - 1;// Defines which page we are in $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";//Creates the Previous Link $first = " <a href=\"$self?page=1\">[First Page]</a> ";//Creates the First Page Link } else { $prev = ' [Prev] '; // If on 1st Page, disable the link $first = ' [First Page] '; // As Above } if ($pageNum < $maxPage) { $page = $pageNum + 1;//Calculates next page $next = " <a href=\"$self?page=$page\">[Next]</a> ";//Link to next page $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";//Link to Last Page } else { $next = ' [Next] '; // If on Last page, disable link $last = ' [Last Page] '; //As Above } // Navigation Link echo $first . $prev . " Showing page <b>$pageNum</b> of <b>$maxPage</b> pages " . $nav . $next . $last; include 'closedb.php'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/107010-just-a-quick-qestion/ Share on other sites More sharing options...
.josh Posted May 23, 2008 Share Posted May 23, 2008 I'm not seeing where $searchselect is being defined, so it's always going to execute the query in the else statement. Also, where is your query that actually pulls the 10 rows? Quote Link to comment https://forums.phpfreaks.com/topic/107010-just-a-quick-qestion/#findComment-548585 Share on other sites More sharing options...
GL Posted May 23, 2008 Author Share Posted May 23, 2008 Sorry, I accidentally deleted the $searchSelect. it should be on the top as $Search = $_POST ['Search']; $SearchSelect = $_POST ['SearchSelect']; and the 10 rows is being executed in the query as LIMIT $offset, $rowsPerPage. Since I am doing a search engine, the headache now is that if $SearchSelect == 1 is selected, the first page will be displayed correctly, but when I click on the next page, it will go on and execute the ELSE statement instead. I have no idea on what should I do to make the $SearchSelect == 1 query loop into the 2nd page on so on. Quote Link to comment https://forums.phpfreaks.com/topic/107010-just-a-quick-qestion/#findComment-548605 Share on other sites More sharing options...
.josh Posted May 24, 2008 Share Posted May 24, 2008 umm..well, if you are wanting the query in the if $SearchSelect == 1 to be ran every single time, then...remove the condition altogether, and just have it run every time? I'm going to assume that's not what you're asking though, as that's kind of a given...so I guess if you could rephrase and/or be more specific, I'll take another stab at it. Quote Link to comment https://forums.phpfreaks.com/topic/107010-just-a-quick-qestion/#findComment-548617 Share on other sites More sharing options...
GL Posted May 24, 2008 Author Share Posted May 24, 2008 Ok, Sorry for the confusion. I shall rephrase it. I am currently doing a Search Engine where the form design consist of a textbox and a dropdown box. Lets Say if I click on 1 on the dropdown box, the expected result would be that it run the IF ($SearchSelect == "1") statement. Everything is fine till I add the paging feature in it, with the paging feature, the statement IF ($SearchSelect == "1") would appear as expected on page 1 but when i click on the hyperlink to page 2, it will run the ELSE condition instead. <?php include 'config.php'; include 'opendb.php'; include 'function.php'; showindexhyperlink(); $Search = $_POST ['Search']; //echo "$Search"; //Proves that Retreival of Query is Operational $SearchSelect = $_POST ['SearchSelect']; //echo "$SearchSelect"; //Proves that Selection is Operational $rowsPerPage = 10; //Variable for how many rows to display per page $pageNum = 1; //This is the first page // If this is defined, use it as page number. if(isset($_GET['page'])) { $pageNum = $_GET['page']; } //Counting the offset $offset = ($pageNum - 1) * $rowsPerPage; if ($SearchSelect == 1){ $query = "SELECT * FROM Asset WHERE Asset_ID LIKE '%$Search%' OR Name LIKE '%$Search%' OR Description LIKE '%$Search%' OR Quantity LIKE '%$Search%' OR Location LIKE '%$Search%' OR Cost LIKE '%$Search%' OR FailureCons LIKE '%$Search%' LIMIT $offset, $rowsPerPage"; } else $query = "SELECT * FROM Asset WHERE Location LIKE '%$Search%' ORDER BY Location ASC LIMIT $offset, $rowsPerPage"; // Searches Locations in the database $result = mysql_query($query) or die; $numrows = mysql_num_rows($result); //Calculates the total amount of rows the query returned //echo "$numrows"; //To test out if the Query is operational if ($numrows == 0){echo "No results found, please try again";}else{ ResultTable();//Prints our the Header for Result Table //while loop condition to loop out all the results from the query while($row = mysql_fetch_array($result)) { $Asset_ID = $row['Asset_ID']; $Name = $row['Name']; $Description = $row['Description']; $Quantity = $row['Quantity']; $Location = $row['Location']; $Image = $row['Image']; $Cost = $row['Cost']; $FailureCons = $row['FailureCons']; echo "<tr>"; echo "<td>".$Asset_ID."</td>"; echo "<td>".$Name."</td>"; echo "<td width = 20%>".$Description."</td>"; echo "<td>".$Quantity."</td>"; echo "<td>".$Location."</td>"; echo "<td><img src= '$Image' height = 80 width = 80></td>"; echo "<td>".$Cost."</td>"; echo "<td width = 20%>".$FailureCons."</td>"; echo "</tr>"; } } $query = "SELECT Asset_ID FROM Asset"; //Select all Rows from Asset Table $result = mysql_query($query) or die; //Execute Query $TableRows = mysql_num_rows($result); // Count the total amount of rows in database $maxPage = ceil($TableRows/$rowsPerPage);//Calculates how many pages to use when paging $self = $_SERVER['PHP_SELF']; $nav = ''; for($page = 1; $page <= $maxPage; $page++) { if ($page == $pageNum) { $nav .= " $page "; // no need to create a link to current page } else { $nav .= " <a href=\"$self?page=$page\">$page</a> "; } } if ($pageNum > 1) { $page = $pageNum - 1;// Defines which page we are in $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";//Creates the Previous Link $first = " <a href=\"$self?page=1\">[First Page]</a> ";//Creates the First Page Link } else { $prev = ' [Prev] '; // If on 1st Page, disable the link $first = ' [First Page] '; // As Above } if ($pageNum < $maxPage) { $page = $pageNum + 1;//Calculates next page $next = " <a href=\"$self?page=$page\">[Next]</a> ";//Link to next page $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";//Link to Last Page } else { $next = ' [Next] '; // If on Last page, disable link $last = ' [Last Page] '; //As Above } // Navigation Link echo "</br>"; echo "<table>"; echo "<tr>"; echo "<th width = 300>".$first.$prev."</th>"; echo "<th width = 600> Showing page <b>$pageNum</b> of <b>$maxPage</b> pages</th>"; echo "<th width = 300>".$next.$last."</th>"; echo "</tr>"; echo "<tr>"; echo "<th width = 300> Page Navigation: </th>"; echo "<th width = 900>".$nav."</th>"; echo "</table>"; include 'closedb.php'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/107010-just-a-quick-qestion/#findComment-548719 Share on other sites More sharing options...
.josh Posted May 24, 2008 Share Posted May 24, 2008 okay, so on paper (not in the code, but rather, what you expect it to do) what is the difference between clicking on the first page and clicking on page 2,3, 4, ...? Because I'm still kind of leaning towards that if statement not really being necessary, for what your trying to say. That is, according to my best interpretation of what you're saying, you should get rid of the if...else statement and 2nd query altogether and just use the first query straight up.... ...unless there was some reason you needed to check whether the person was on page 1 or not (other than say, the pagination link generation part, where you have to figure out if user is on page one, don't generate a page 0 link, sort of thing). So, back to my first question: why is this if..else statement here? What's the plan for it on paper? Quote Link to comment https://forums.phpfreaks.com/topic/107010-just-a-quick-qestion/#findComment-548861 Share on other sites More sharing options...
GL Posted May 24, 2008 Author Share Posted May 24, 2008 On paper, I plan to do something like the search engine www.myspace.com has. Where user can enter his/her query and then select by the relevant category to be executed. So, the IF ELSE statement is there basically to differentiate which category should the search engine go to. Quote Link to comment https://forums.phpfreaks.com/topic/107010-just-a-quick-qestion/#findComment-549025 Share on other sites More sharing options...
.josh Posted May 24, 2008 Share Posted May 24, 2008 okay so if the 2nd query is more of a "fine tuning" of the search, then you still need to run the first query no matter what, so that shouldn't be inside the condition of the first one. It should be ran no matter what. Or rather, all your conditions should be built upon it, so that that part is ran all the time, not just on the first condition, right? Quote Link to comment https://forums.phpfreaks.com/topic/107010-just-a-quick-qestion/#findComment-549043 Share on other sites More sharing options...
GL Posted May 25, 2008 Author Share Posted May 25, 2008 So in short, I only needed the first query. But what I am supposed to do with the remaining query should I want the fine tuning feature to be implemented? Quote Link to comment https://forums.phpfreaks.com/topic/107010-just-a-quick-qestion/#findComment-549846 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.