ginerjm Posted May 19, 2019 Share Posted May 19, 2019 (edited) Start small. Focus on one task at a time. But produce some code for us to look at because we are not here to "write code for you". This forum is geared towards helping others to become coders as well as to become better coders. It is not a free labor pool, although we do labor and we do it for free. Edited May 19, 2019 by ginerjm Quote Link to comment Share on other sites More sharing options...
mahenda Posted May 20, 2019 Author Share Posted May 20, 2019 //the problem i'm experiencing here is the 1,2 ,3.... pages are black how to solve this help me if there is any mistake and how to get the url of the logical page and how http_build_query() work <?php if(isset($_GET['submit'])){ $names = $_GET['search']; $escapeString = mysqli_real_escape_string($conn, $names); if(!empty($escapeString)){ $resultPerPage = 8; if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; } $startPage = ($page-1) * $resultPerPage; $sql = "SELECT * FROM movies WHERE name LIKE '%".$escapeString."%' OR producer LIKE '%".$escapeString."%' LIMIT ".$startPage.",".$resultPerPage; $result = mysqli_query($conn, $sql); while($row = mysqli_fetch_assoc($result)){ echo ' <div> <span>'.$row['name'].'</span> <span>'.$row['producer'].'</span> <p>'.$row['shortDescription'].'</p> </div> '; } echo '<ul class="pagination">'; if ($page == 1) { echo '<li class="disabled"><a href="#">First</a></li> <li class="disabled"><a href="#">«</a></li>'; } else { $linkPrev = ($page > 1) ? $page - 1 : 1; echo '<li><a href="search.php?page=1">First</a></li> <li><a href="search.php?page='.$linkPrev.'">«</a></li>'; } $cSql = "SELECT COUNT(*) FROM movies WHERE name LIKE '%".$escapeString."%' OR producer LIKE '%".$escapeString."%'"; $cResult = mysqli_query($conn, $cSql); $row = mysqli_fetch_row($cResult); $totalRecords = $row[0]; $noOfpage = ceil($totalRecords/ $resultPerPage); $linkNumber = 7; $startNumber = ($page > $linkNumber) ? $page - $linkNumber : 1; $endNumber = ($page < ($noOfpage - $linkNumber)) ? $page + $linkNumber : $noOfpage; for ($i = $startNumber; $i <= $endNumber; $i++) { $linkActive = ($page == $i) ? 'class="active"' : ''; echo '<li '.$linkActive.'><a href="search.php?page='.$i.'">'.$i.'</a></li>'; } if ($page == $noOfpage) { echo '<li class="disabled"><a href="#">»</a></li> <li class="disabled"><a href="#">Last</a></li>'; } else { $linkNext = ($page < $noOfpage) ? $page + 1 : $noOfpage; echo '<li><a href="search.php?page='.$linkNext.'">»</a></li> <li><a href="search.php?page='.$noOfpage.'">Last</a></li>'; } echo '</ul>'; } else { echo ' <div class="head"> <h6 class="ml-0"> RESULTS REPORT:</h6> </div>'; echo '<span> The keyword </span><span class="text-warning"> '.$escapeString.' </span><span> is not available in our database </span>'; } } else { echo 'You have to Enter movie name or producer'; } ?> help @Barand On 5/19/2019 at 3:22 PM, ginerjm said: Start small. Focus on one task at a time. But produce some code for us to look at because we are not here to "write code for you". This forum is geared towards helping others to become coders as well as to become better coders. It is not a free labor pool, although we do labor and we do it for free. @ginerjm@cyberRobot@mac_gyver Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 20, 2019 Share Posted May 20, 2019 On 5/18/2019 at 8:00 AM, ginerjm said: I suggest that you re-read what I gave you on how to write that query statement. @mahenda - Are you still working on correcting the query, as ginerjm suggested? If so, the issue with the query is that you removed the double quotes before and after the $escapeString variables, but you didn't remove the concatenation characters. For what it's worth, the query you had before ginerjm's advice should be fine. $sql = "SELECT name, producer FROM movies WHERE name LIKE '%".$escapeString."%' OR producer LIKE '%".$escapeString."%' "; Or you could use ginerjm's suggestion. $sql = "SELECT name, producer FROM movies WHERE name LIKE '%$escapeString%' OR producer LIKE '%$escapeString%'"; They're both the same query. Ginerjm's suggestion just has the PHP variables embedded in a string. Your's uses string concatenation. With that said, I would recommend renaming $escapeString to something more meaningful. At some point in time, you'll likely need to write a script where you are escaping more than one piece of information. On 5/16/2019 at 8:10 AM, ginerjm said: PPS - if you haven't realized it yet PHP is a case-sensitive language. That means "$escapeString" is a different variable than "$escapestring". That means when you needlessly add caps to your names you force yourself to remember how you did it on every one of your variables and it really is not necessary. I personally use camel case because that's how I was taught. Granted my initial learning experience wasn't with PHP. I just prefer camel case because it's easier to read (for me) then using all lower cases. I've tried switching to other recommended formats (e.g. using underscores or hyphens between words $escape_string), but I always find myself switching back to camel case. In the end, consistency is key. Using all lower-case letters, in and of itself, isn't going to save you from the headaches of mistyping a variable name. I've had many frustrations with plural vs. singular words (e.g. $name and $names). I've also had problems with words in different tenses (e.g. $escapeString and $escapedString). If you're worried about mistyping a variable, try working with a code editor that auto-completes variable names as you're typing. Mistyping should also be something you're looking at when debugging a script. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 20, 2019 Share Posted May 20, 2019 34 minutes ago, mahenda said: pages are black how to solve this help me if there is any mistake and how to get the url of the logical page and how http_build_query() work @mahenda - Is PHP set to display errors? You can add the following to the top of your script so that errors are displayed: <?php error_reporting(E_ALL); ini_set('display_errors', 1); ?> Note that the above should only be present when you're developing the script or debugging. The errors generated by PHP shouldn't be displayed to your visitors. They're not very user friendly and they can potentially reveal information to someone trying to compromise your website. Quote Link to comment Share on other sites More sharing options...
mahenda Posted May 21, 2019 Author Share Posted May 21, 2019 10 hours ago, cyberRobot said: @mahenda - Is PHP set to display errors? You can add the following to the top of your script so that errors are displayed: <?php error_reporting(E_ALL); ini_set('display_errors', 1); ?> Note that the above should only be present when you're developing the script or debugging. The errors generated by PHP shouldn't be displayed to your visitors. They're not very user friendly and they can potentially reveal information to someone trying to compromise your website. let's us leave the issue of escape string first i'll implement it later , i'm already changed the sql statement as ginerjm said, but the issue here is when you click the pagination number it show the first page only and the remaining page link show blank page i want to match the returned results with the page please try testing my code if i don't understand show me just an example or use my code to show me any problem you think it may have as you quoted sql line Quote Link to comment Share on other sites More sharing options...
mahenda Posted May 21, 2019 Author Share Posted May 21, 2019 <?php if(isset($_GET['submit'])){ $names = $_GET['search']; $escapeString = mysqli_real_escape_string($conn, $names); if(!empty($escapeString)){ $resultPerPage = 8; if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; } $startPage = ($page-1) * $resultPerPage; $sql = "SELECT * FROM movies WHERE name LIKE '%$escapeString%' OR producer LIKE '%$escapeString%' LIMIT ".$startPage.",".$resultPerPage; $result = mysqli_query($conn, $sql); while($row = mysqli_fetch_assoc($result)){ echo ' <div> <span>'.$row['name'].'</span> <span>'.$row['producer'].'</span> <p>'.$row['shortDescription'].'</p> </div> '; } echo '<ul class="pagination">'; if ($page == 1) { echo '<li class="disabled"><a href="#">First</a></li> <li class="disabled"><a href="#">«</a></li>'; } else { $linkPrev = ($page > 1) ? $page - 1 : 1; echo '<li><a href="search.php?page=1">First</a></li> <li><a href="search.php?page='.$linkPrev.'">«</a></li>'; } $cSql = "SELECT COUNT(*) FROM movies WHERE name LIKE '%$escapeString%' OR producer LIKE '%$escapeString%'"; $cResult = mysqli_query($conn, $cSql); $row = mysqli_fetch_row($cResult); $totalRecords = $row[0]; $noOfpage = ceil($totalRecords/ $resultPerPage); $linkNumber = 7; $startNumber = ($page > $linkNumber) ? $page - $linkNumber : 1; $endNumber = ($page < ($noOfpage - $linkNumber)) ? $page + $linkNumber : $noOfpage; for ($i = $startNumber; $i <= $endNumber; $i++) { $linkActive = ($page == $i) ? 'class="active"' : ''; echo '<li '.$linkActive.'><a href="search.php?page='.$i.'">'.$i.'</a></li>'; } if ($page == $noOfpage) { echo '<li class="disabled"><a href="#">»</a></li> <li class="disabled"><a href="#">Last</a></li>'; } else { $linkNext = ($page < $noOfpage) ? $page + 1 : $noOfpage; echo '<li><a href="search.php?page='.$linkNext.'">»</a></li> <li><a href="search.php?page='.$noOfpage.'">Last</a></li>'; } echo '</ul>'; } else { echo ' <div class="head"> <h6 class="ml-0"> RESULTS REPORT:</h6> </div>'; echo '<span> The keyword </span><span class="text-warning"> '.$escapeString.' </span><span> is not available in our database </span>'; } } else { echo 'You have to Enter movie name or producer'; } ?> - Quote Link to comment Share on other sites More sharing options...
chhorn Posted May 21, 2019 Share Posted May 21, 2019 If you get a "blank page" you should first have a look in the error.log of your server. Quote Link to comment Share on other sites More sharing options...
mahenda Posted May 21, 2019 Author Share Posted May 21, 2019 1 hour ago, chhorn said: If you get a "blank page" you should first have a look in the error.log of your server. Let me check it Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 21, 2019 Share Posted May 21, 2019 OR enable error checking to report directly to the client so you can see the errors immediately. Remember to turn it off once you are done with development. error_reporting(E_ALL); ini_set('display_errors', '1'); Quote Link to comment Share on other sites More sharing options...
Barand Posted May 21, 2019 Share Posted May 21, 2019 ... which probably won't work if you are getting a blank page. You will need to catch any startup errors so the settings need to be in your php.ini file. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 21, 2019 Share Posted May 21, 2019 1 hour ago, Barand said: ... which probably won't work if you are getting a blank page That depends on the error / setup. If all the processing is done up front, before anything is displayed, the page could be blank when there's a fatal error. Quote Link to comment Share on other sites More sharing options...
mahenda Posted May 21, 2019 Author Share Posted May 21, 2019 i have turned on the display_errors in php.in and then paste the error_reporting( E_ALL ); in my script but nothing shown xampp control panel v3.2.2 PHP version: 7.2.11 Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 21, 2019 Share Posted May 21, 2019 what does the 'view source' in your browser of this blank page show? i'm betting it has your - 'You have to Enter movie name or producer', because there are two apparent problems in your current code . the 1st problem is $_GET['search'] isn't being added to the pagination links, which you would have noticed if you had looked at the URL in your browser's address bar. you can edit all the places where you are producing pagination links or you can use http_build_query() like has been suggested (if you search the phpfreaks forum, you will find examples showing how to use it with pagination.) the 2nd problem is you have too many different $_GET variables from the search form. you only need $_GET['search']. remove $_GET['submit'] and change your logic to only use $_GET['search']. Quote Link to comment Share on other sites More sharing options...
mahenda Posted May 22, 2019 Author Share Posted May 22, 2019 12 hours ago, mac_gyver said: what does the 'view source' in your browser of this blank page show? i'm betting it has your - 'You have to Enter movie name or producer', because there are two apparent problems in your current code . the 1st problem is $_GET['search'] isn't being added to the pagination links, which you would have noticed if you had looked at the URL in your browser's address bar. you can edit all the places where you are producing pagination links or you can use http_build_query() like has been suggested (if you search the phpfreaks forum, you will find examples showing how to use it with pagination.) the 2nd problem is you have too many different $_GET variables from the search form. you only need $_GET['search']. remove $_GET['submit'] and change your logic to only use $_GET['search']. ok let me try your suggestion , after that i'll return back, but after clicking page link and looking on the url bar this is showing http://localhost/movies_dbase/search.php?page=1 with no data on the page Quote Link to comment Share on other sites More sharing options...
mahenda Posted May 22, 2019 Author Share Posted May 22, 2019 On 5/21/2019 at 6:24 PM, mac_gyver said: what does the 'view source' in your browser of this blank page show? i'm betting it has your - 'You have to Enter movie name or producer', because there are two apparent problems in your current code . the 1st problem is $_GET['search'] isn't being added to the pagination links, which you would have noticed if you had looked at the URL in your browser's address bar. you can edit all the places where you are producing pagination links or you can use http_build_query() like has been suggested (if you search the phpfreaks forum, you will find examples showing how to use it with pagination.) the 2nd problem is you have too many different $_GET variables from the search form. you only need $_GET['search']. remove $_GET['submit'] and change your logic to only use $_GET['search']. So how to pass $_GET['search'] in pagination I don't understand because I have another $_GET['page'] Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 22, 2019 Share Posted May 22, 2019 Actually it takes two lines to enable PHP error checking. You made the selection of what to show, but did you turn on the display to the client? ini_set('display_errors', '1'); Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 22, 2019 Share Posted May 22, 2019 5 hours ago, mahenda said: So how to pass $_GET['search'] in pagination I don't understand because I have another $_GET['page'] i noticed you viewing a forum thread earlier that used http_build_query() to produce pagination links with any existing get parameters. what issue are you still having with doing this yourself? Quote Link to comment Share on other sites More sharing options...
mahenda Posted May 24, 2019 Author Share Posted May 24, 2019 (edited) can you put a link here because I haven't found it @mac_gyver Edited May 24, 2019 by mahenda Quote Link to comment 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.