Jump to content
Larry Ullman's Book Forums

Wagtail

Members
  • Posts

    136
  • Joined

  • Last visited

  • Days Won

    1

Everything posted by Wagtail

  1. Hi HartleySan, I have tried that before and it did work - now it doesn't. I have posted the code below. If you have a moment could you please look it over? Thank you very much! if (isset($string)) { // Number of records to show per page: $display2 =1; // Determine how many pages there are... if (isset($_GET['p2']) && is_numeric($_GET['p2'])) { // Already been determined. $pages2 = $_GET['p2']; } else { $q = "SELECT COUNT(user_id), ... WHERE $string "; $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records2 = $row[0]; // Calculate the number of pages... if ($records2 > $display2) { // More than 1 page. $pages2 = ceil ($records2/$display2); } else { $pages2 = 1; } } // Determine where in the database to start returning results... if (isset($_GET['s2']) && is_numeric($_GET['s2'])) { $start2 = $_GET['s2']; } else { $start2 = 0; } $q = "SELECT * FROM... WHERE... $string LIMIT $start2, $display2"; $r = @mysqli_query($dbc, $q); while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { Print' loop content here '; } // Make the links to other pages, if necessary. if (isset ($pages2, $start2, $display2)) { if ($pages2 > 1) { echo '<br /><p id="pagination">'; $current_page2 = ($start2/$display2) + 1; // If it's not the first page, make a Previous button: if ($current_page2 != 1) { echo '<a href="test.php?s2=' . ($start2 - $display2) . '&p2=' . $pages2 . '&'.$string.'">Previous</a> '; } // Make all the numbered pages: for ($i = 1; $i <= $pages2; $i++) { if ($i != $current_page2) { echo '<a href="test.php?s2=' . (($display2 * ($i - 1))) . '&p2=' . $pages2 . '&'.$string.'">' . $i . '</a> '; } else { echo $i . ' '; } } // End of FOR loop. // If it's not the last page, make a Next button: if ($current_page2 != $pages2) { echo '<a href="test.php?s2=' . ($start2 + $display2) . '&p2=' . $pages2 . '&'.$string.'">Next</a>'; } echo '</p>'; // Close the paragraph. } // End of links section. } } else { // normal pagination code here
  2. Hi everyone, I am unfortunately still stuck with combining pagination with checkboxes. After clicking a checkbox, the page will load with the results. Depending on the number of records there might be one or more pagination links. Now if I click on, say, the 3rd pagination link, page 3 will load. So far so good. Now I click another checkbox - the page will load but the current pagination number will still be 3. In other words, the pagination number continues from the previous results instead of starting from page 1. If I'm not mistaken, the problem lies with the $start variable. When a checkbox has been selected for the second time, $_GET['s'] will already be set and so $start will begin with that number. I need some code that says if $_GET['s'] has a value, and this is followed by setting a checkbox value, then $start = 0;. Or whatever else might work. Could someone please help me? Thank you. $string contains the value from the checkboxes, together with a column name (e.g., name='amy'). if (isset($string)) { // Determine where in the database to start returning results... if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; }
  3. Hi everyone, could someone please tell me how I can identify form inputs with jQuery code? My form has checkboxes with different name inputs. Presently I'm able to receive the value of the currently selected checkbox with this code: var checkedBoxes = $(this).val(); This I then use in the Ajax data setting: data: 'hobby[]=' + checkedBoxes, But I need to process the checkboxes with the other names as well. Thank you very much in advance! Herewith the code: The form: <form method="post" action="" id="checkform"> <input type="checkbox" name="hobby[]" value="fishing">fishing <input type="checkbox" name="hobby[]" value="bowling"> bowling <input type="checkbox" name="income[]" value="average">average <input type="checkbox" name="income[]" value="high">high <input type="submit" value="select"> </form> The JavaScript: <script type="text/javascript"> $('#checkform').on('click', 'input', function(){ var checkedBoxes = $(this).val(); $.ajax({ type: "POST", url: // some url cache: false, data: 'hobby[]=' + checkedBoxes, success: function(response){ $('#someid').html(response); } }); }) </script> The PHP: $arguments =''; if (isset($_POST['hobby'])){ foreach ($_POST['hobby'] as $k) { $arguments[] .= "hobby='$k'"; } } if (isset($_POST['income'])){ foreach ($_POST['income'] as $a) { $arguments[] .= "income='$a'"; } } if(!empty($arguments)) { $string = implode(' && ',$arguments); }
  4. Hi HartleySan, how are you doing? I'm sorry to bother you with this, but I hope you could assist me please. I'm using Javascript/Ajax to send the values of “checked” checkboxes to a PHP script which uses the pagination script (10.4) from chapter10. The Ajax is correctly updating the query on the first page but there's a problem with the subsequent pages. The query that fetches data from the database ends with variable called “$string”. This variable contains all of the column name – value statements. Depending on the selected checkboxes, the echoed out $string variable might be hobby = 'fishing' && income = 'average'. The problem arises when I click a numbered pagination link to go to the next page. The page will load the records from the original query and not those that have been filtered via the checkboxes. I'm thinking that I need to transfer the $string variable to the other pages, so I tried appending it to the URL: &string=$string. Then I could retrieve it and assign it's contents to the original $string variable in the query. I tried something like the following: $newString = filter_input(INPUT_GET, 'string', FILTER_SANITIZE_STRING); $string = $newString; I have spent hours on this and am getting nowhere. If you have any idea what I could then please let me know. Thank you very much. <form method="post" action="" id="checkform"> <input type="checkbox" name="hobby[]" value="fishing">fishing <input type="checkbox" name="hobby[]" value="bowling"> bowling <input type="checkbox" name="income[]" value="average">average <input type="checkbox" name="income[]" value="high">high <input type="submit" value="select"> </form> $arguments =''; if (isset($_POST['hobby'])){ foreach ($_POST['hobby'] as $k) { $arguments[] .= "hobby='$k'"; } } if (isset($_POST['income'])){ foreach ($_POST['income'] as $a) { $arguments[] .= "income='$a'"; } } if(!empty($arguments)) { $string = implode(' && ',$arguments); } // Make all the numbered pages: for ($i = 1; $i <= $pages2; $i++) { if ($i != $current_page2 && isset($string)) { echo '<a href="mypage.php?s2=' . (($display2 * ($i - 1))) . '&p2=' . $pages2 . '&string=$string">' . $i . '</a> '; } else { echo $i . ' '; } } // End of FOR loop. <script type="text/javascript"> $('#checkform').on('click', 'input', function(){ var checkedBoxes = $(this).val(); $.ajax({ type: "POST", url: // some url cache: false, data: 'hobby[]=' + checkedBoxes, success: function(response){ $('#someid').html(response); } }); }) </script>
  5. Hi HartleySan, I must apologize profusely for not getting back to you sooner. You have made a lot of effort to help me, so I hope you don't find me too inconsiderate for not replying earlier to your previous post. After playing around with the code I discovered that the Ajax pagination works if the 'paginated content' is loaded into the body of the webpage. The body tag needs to have an id: <body id="container">. This way there aren't any repeating headings or links. I hope someone can test this out for themselves and confirm that it all works fine. $(function(){ $('#pagination-links').on('click', 'a', function(){ var $this = $(this); var url = $this.attr('href'); $('#container').load(url +'#paginated-content'); return false; }); }); Thanks again HartleySan.
  6. Hello HartleySan, I have discovered that my original script does work, but again with the heading and pagination links repeating themselves. I think it had something to do with the dash in the "view_users - test with ajax.php" file. I can't be sure. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>test ajax</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $('#pagination-links').on('click', 'a', function(){ var $this = $(this); var url = $this.attr('href'); $('#container').load(url +'#paginated-content'); return false; }); }); </script> </head> <body> <?php echo '<h1>Registered Users</h1>'; require ('mysqli_connect.php'); // Number of records to show per page: $display = 8; // Determine how many pages there are... if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined. $pages = $_GET['p']; } else { // Need to determine. // Count the number of records: $q = "SELECT COUNT(user_id) FROM users"; $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; // Calculate the number of pages... if ($records > $display) { // More than 1 page. $pages = ceil ($records/$display); } else { $pages = 1; } } // End of p IF. // Determine where in the database to start returning results... if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } // Table header: echo '<div id="container"> <div id="paginated-content"> '; // Define the query: $q = "SELECT last_name, first_name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr, user_id FROM users LIMIT $start, $display"; $r = @mysqli_query ($dbc, $q); // Run the query. // Fetch and print all the records.... $bg = '#eeeeee'; while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { print'<a href="edit_user.php?id=' . $row['user_id'] . '">Edit</a> <a href="delete_user.php?id=' . $row['user_id'] . '">Delete</a> ' . $row['last_name'] . ' ' . $row['first_name'] . ' ' . $row['dr'] . ' '; } // End of WHILE loop. echo'</div></div>'; mysqli_free_result ($r); mysqli_close($dbc); // Make the links to other pages, if necessary. if ($pages > 1) { echo '<br /><p id="pagination-links">'; $current_page = ($start/$display) + 1; // If it's not the first page, make a Previous button: if ($current_page != 1) { echo '<a href="testajax.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> '; } // Make all the numbered pages: for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="testajax.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> '; } else { echo $i . ' '; } } // End of FOR loop. // If it's not the last page, make a Next button: if ($current_page != $pages) { echo '<a href="testajax.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>'; } echo '</p>'; // Close the paragraph. } // End of links section. ?> </body> </html> I have tried that but it would only return a single row. Do you mean like this? while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $hello = '<a href="edit_user.php?id=' . $row['user_id'] . '">Edit</a> <a href="delete_user.php?id=' . $row['user_id'] . '">Delete</a> ' . $row['last_name'] . ' ' . $row['first_name'] . ' ' . $row['dr'] . ' '; } // End of WHILE loop. echo $hello; Will keep on trying. Please let me know if there's anything else that might work. Thank you for all of your help.
  7. Hi HartleySan, thank you for posting and explaining the code. You have gone to some lengths to help me out! What I can say is that the Ajax is working but there is some weird behaviour going on. When I first click a link, a second “registered users” heading appears, along with a second set of pagination links. The “view_users – try with ajax.php” file also continues to work even if I move the “ajax-requested-php-file.php” into another folder and clear the browser cache. Perhaps I have left something out in the second script? I have renamed this file to "spot.php" to simplify things. I have modified the closing braces and parentheses in your function code, or are they correctly positioned? Thanks again! View_users - try with ajax.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>test ajax</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> $(function () { $('#pagination-links').on('click', 'a', function (e) { $('#container').load(this.href.replace('view_users - try with ajax.php', 'spot.php')); e.preventDefault(); }); }); </script> </head> <body> <?php echo '<h1>Registered Users</h1>'; require ('mysqli_connect.php'); // Number of records to show per page: $display = 8; // Determine how many pages there are... if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined. $pages = $_GET['p']; } else { // Need to determine. // Count the number of records: $q = "SELECT COUNT(user_id) FROM users"; $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; // Calculate the number of pages... if ($records > $display) { // More than 1 page. $pages = ceil ($records/$display); } else { $pages = 1; } } // End of p IF. // Determine where in the database to start returning results... if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } // Table header: echo '<div id="container"> <div id="paginated-content"> '; // Define the query: $q = "SELECT last_name, first_name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr, user_id FROM users LIMIT $start, $display"; $r = @mysqli_query ($dbc, $q); // Run the query. // Fetch and print all the records.... while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { print'<a href="edit_user.php?id=' . $row['user_id'] . '">Edit</a> <a href="delete_user.php?id=' . $row['user_id'] . '">Delete</a> ' . $row['last_name'] . ' ' . $row['first_name'] . ' ' . $row['dr'] . ' '; } // End of WHILE loop. echo'</div></div>'; mysqli_free_result ($r); mysqli_close($dbc); // Make the links to other pages, if necessary. if ($pages > 1) { echo '<br /><p id="pagination-links">'; $current_page = ($start/$display) + 1; // If it's not the first page, make a Previous button: if ($current_page != 1) { echo '<a href="view_users - try with ajax.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> '; } // Make all the numbered pages: for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="view_users - try with ajax.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> '; } else { echo $i . ' '; } } // End of FOR loop. // If it's not the last page, make a Next button: if ($current_page != $pages) { echo '<a href="view_users - try with ajax.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>'; } echo '</p>'; // Close the paragraph. } // End of links section. ?> </body> </html> spot.php <?php // Include DB connection. require ('mysqli_connect.php'); $start = $_GET['s']; $pages = $_GET['p']; // Calculate $display variable here. $display = 8; $q = "SELECT last_name, first_name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr, user_id FROM users LIMIT $start, $display"; $r = @mysqli_query ($dbc, $q); // Run the query. // Fetch and print all the records.... while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { print'<a href="edit_user.php?id=' . $row['user_id'] . '">Edit</a> <a href="delete_user.php?id=' . $row['user_id'] . '">Delete</a> ' . $row['last_name'] . ' ' . $row['first_name'] . ' ' . $row['dr'] . ' '; } // End of WHILE loop.
  8. Hi HartleySan, I'm not quite sure how to proceed with your suggestions. What must the second script contain? Only the URL parameters? If so, how does this file receive the parameters from the “view users” PHP file? Would this script have to reproduce all of the PHP code up until the part where the query begins? That is, from the “number of records to show per page” up to and including “determine where in the database to start returning results”. If a link is clicked must the URL parameters first be sent to the second script, which performs the query, and then the .load() method retrieves the response in JSON format? Please excuse all of these questions but I had to ask. Ideally the pagination has to continue working as normal when JavaScript is disabled. I don't know if this is relevant to the issue at hand. I'm sure you are busy, so I appreciate your help. Thank you.
  9. Hi HartleySan, thanks for getting back to me on this, and for the recommendations. You asked me about the "view_users - try with ajax.php" script. That is the script that I'm using - all of the code that I posted above is contained in that script. In the book it's called "view_users.php". The anchor/link goes back to the same "view_users - try with ajax.php" file. I'll look for the Chrome browser. I'm already using Firebug and the Web Developer plugin. Do you know if I can use those as well, or must it be Chrome? Thank you again for your help!
  10. Hello Larry and forum members, I am trying to use Ajax with the pagination script (10.4) from chapter 10. Having read that the ajax .load() method is relatively easy to implement, I tried to see if it would work with the pagination script. Instead of the page reloading when a link is clicked, ajax should fetch the content from the URL and place it in the div with the "paginated content". What I have tried does not work. Could someone PLEASE have a look at my script and tell me what I'm doing wrong? Thank you for your assistance. Herewith the faulty code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>test ajax</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $('#pagination-links').on('click', 'a', function(e){ e.preventDefault(); var $this = $(this); var url = $this.attr('href'); $('#container').load(url + '#paginated-content'); }); }); </script> </head> <body> <?php echo '<h1>Registered Users</h1>'; require ('mysqli_connect.php'); // Number of records to show per page: $display = 8; // Determine how many pages there are... if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined. $pages = $_GET['p']; } else { // Need to determine. // Count the number of records: $q = "SELECT COUNT(user_id) FROM users"; $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; // Calculate the number of pages... if ($records > $display) { // More than 1 page. $pages = ceil ($records/$display); } else { $pages = 1; } } // End of p IF. // Determine where in the database to start returning results... if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } // Table header: echo '<div id="container"> <div id="paginated-content"> '; // Define the query: $q = "SELECT last_name, first_name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr, user_id FROM users LIMIT $start, $display"; $r = @mysqli_query ($dbc, $q); // Run the query. // Fetch and print all the records.... $bg = '#eeeeee'; while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { print'<a href="edit_user.php?id=' . $row['user_id'] . '">Edit</a> <a href="delete_user.php?id=' . $row['user_id'] . '">Delete</a> ' . $row['last_name'] . ' ' . $row['first_name'] . ' ' . $row['dr'] . ' '; } // End of WHILE loop. echo'</div></div>'; mysqli_free_result ($r); mysqli_close($dbc); // Make the links to other pages, if necessary. if ($pages > 1) { echo '<br /><p id="pagination-links">'; $current_page = ($start/$display) + 1; // If it's not the first page, make a Previous button: if ($current_page != 1) { echo '<a href="view_users - try with ajax.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> '; } // Make all the numbered pages: for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="view_users - try with ajax.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> '; } else { echo $i . ' '; } } // End of FOR loop. // If it's not the last page, make a Next button: if ($current_page != $pages) { echo '<a href="view_users - try with ajax.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>'; } echo '</p>'; // Close the paragraph. } // End of links section. ?> </body> </html>
  11. Hello again HartleySan, hope you are well. I have followed your advice, and the correct results are now loading on the second page, which is great. But now the all of the pagination links (previous, next etc) are gone on the second page. So I can't go back to the previous page or forward to the next pages. I'm sure getting sick of this but hopefully there'll be a breakthrough soon, and then other forum members can make use of pagination with check boxes. Sorry for taking up all of your time on this. The updated code: // Number of records to show per page: $display =3; // Determine how many pages there are... if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined. $pages = $_GET['p']; } else { // Need to determine. // Count the number of records: $q = "SELECT COUNT(user_id), other columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age'"; $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; // Calculate the number of pages... if ($records > $display) { // More than 1 page. $pages = ceil ($records/$display); } else { $pages = 1; } } // End of p IF. // Determine where in the database to start returning results... if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } // if a checkbox has been checked assign its value to the $cityname variable otherwise... if (isset($_POST['city'])) { $cityname = $_POST['city'];} else { $cityname = filter_input(INPUT_GET, 'cityname', FILTER_SANITIZE_STRING);} // if the check box 'all' has been checked, all the records are returned if (isset($_POST['all'])) { $q = "SELECT columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age' LIMIT $start, $display"; } // if the $cityname variable has been set... if (isset($cityname)) { { $q = "SELECT COUNT(user_id), other columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age' && city = '$cityname' LIMIT $start, $display"; } $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; echo '<p>'.$records.'records</p>'; echo '</br>'; echo '<p>display='.$display.'</p>'; echo '</br>'; // Calculate the number of pages... if ($records > $display) { // More than 1 page. $pages = ceil ($records/$display); echo '<p>'.$pages.'pages</p>'; echo '</br>'; } else { $pages = 1; echo '<p style="color:red">'.$pages.'pages</p>'; } $q = "SELECT columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age' && city = '$cityname' LIMIT $start, $display"; echo $q; }} $r = @mysqli_query($dbc, $q); while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { Print '<p>loop content here</p>'; } // Make the links to other pages, if necessary. if ($pages > 1) { echo '<br /><p>'; $current_page = ($start/$display) + 1; // If it's not the first page, make a Previous button: if ($current_page != 1 && isset($cityname)) { echo '<a href="view_users.php?s=' . ($start - $display) . '&p=' . $pages . '&name='.$name.'&age='.$age.'&cityname='.$cityname.'">Previous</a> '; } else { echo '<a href="view_users.php?s=' . ($start - $display) . '&p=' . $pages . '&name='.$name.'&age='.$age.'">Previous</a> '; } // Make all the numbered pages: for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page && isset($cityname)) { echo '<a href="view_users.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&name='.$name.'&age='.$age.'&cityname='.$cityname.'">' . $i . '</a> '; } elseif ($i != $current_page) { echo '<a href="view_users.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&name='.$name.'&age='.$age.'">' . $i . '</a> '; } else { echo $i . ' '; } } // End of FOR loop. // If it's not the last page, make a Next button: if ($current_page != $pages && isset($cityname)) { echo '<a href="view_users.php?s=' . ($start + $display) . '&p=' . $pages . '&name='.$name.'&age='.$age.'&cityname='.$cityname.'">Next</a>'; } else { echo '<a href="view_users.php?s=' . ($start + $display) . '&p=' . $pages . '&name='.$name.'&age='.$age.'">Next</a>'; } echo '</p>'; // Close the paragraph. } // End of links section.
  12. Hi HartleySan, the query I posted was for counting the total number of records in order to determine the number of pages. A while loop wasn't necessary, but your advice did remind me that I hadn't actually used a query after determining the number of pages. Now the code returns the correct number of results on the first page, after a check box has been checked. But clicking to the next page loads the wrong results. I have echoed out my queries so I can see that the query on the first page includes the necessary WHERE...name=$_POST['city'] from the $string variable. However, this condition is missing from the query on the second page. Does the value from the check box “($_POST['city'])” carry over to the other pages? If it doesn't then the $string variable won't be created and the query won't have the extra condition in the WHERE clause. Or maybe there is some other reason.... Thank you for your help. The updated code with the second query: if (isset($string)) { { $q = "SELECT COUNT(user_id), other columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age' && '.$string.' LIMIT $start, $display"; } $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; echo '<p>'.$records.'records</p>'; echo '</br>'; echo '<p>display='.$display.'</p>'; echo '</br>'; // Calculate the number of pages... if ($records > $display) { // More than 1 page. $pages = ceil ($records/$display); echo '<p>'.$pages.'pages</p>'; echo '</br>'; } else { $pages = 1; echo '<p style="color:red">'.$pages.'pages</p>'; } $q = "SELECT columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age' && '.$string.' LIMIT $start, $display"; echo $q; }}
  13. Hi HartleySan, I have added the following code to the code I use if a check box has been checked: if (isset($string)) { { $q = "SELECT columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age' && '.$string.' LIMIT $start, $display"; } $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; echo '<p>'.$records.'records</p>'; echo '</br>'; echo '<p>display='.$display.'</p>'; echo '</br>'; // Calculate the number of pages... if ($records > $display) { // More than 1 page. $pages = ceil ($records/$display); echo '<p>'.$pages.'pages</p>'; echo '</br>'; } else { $pages = 1; echo '<p style="color:red">'.$pages.'pages</p>'; } echo $q; }} I have echoed out the $records, $display and $pages variables to see what is going on. These variables are all correct, but now only 1 record is displayed on the first page, even though $display might be 2 or 3. I added print_r($r); before my while loop begins and what it is telling me is that only 1 row is being returned: [num_rows] => 1. The correct number of rows are returned when using the original 'unfiltered' query. I can understand that you are busy so thank you for your help so far! Larry, if you have a moment, could use please offer some insight? Thank you.
  14. Hi HartleySan, thank you for getting back to me. I've spent hours trying to get the code to work, and its still not working. Once I have checked one of the checkboxes, the page will load with the filtered results, but there are still numbered pagination links when there shouldn't be any. At the moment the original 'unfiltered' query will fetch 10 results (rows). I have set $display =3, which will show 3 results per page for the first 3 pages, and 1 result on the fourth page. Even if only 2 filtered results are returned on the first page after selecting a checkbox, there will still be links to 3 other pages, with each link having a p=4 at the end of the URL. It appears that selecting a checkbox will display the filtered records on the first page with the records from the original query being shown over the following pages. So there must be some kind of mix up. Perhaps I need to recalculate the number of pages for the filtered results? I don't know – I'm running out of ideas. I think that the start and display variables are okay. Thanks again for taking the time to help me.
  15. Hi there HartleySan, I'm hoping you could please help me. I did get the checkboxes to work with the pagination script from chapter 10, but there does seem to be a problem. When a checkbox is "checked" the filtered results will load but there are still numbered pagination links to other pages when there shouldn't be any. Perhaps the filter query is being mixed up with the original query (that displays all of the results)? This is from one of my posts a few days ago: Posted 29 October 2013 - 1:13 PM Hi Larry, I do need some help after all. I've reached my 'PHP limit' so to speak. What I'm now attempting is to use check boxes to filter the paginated results. The code does work, albeit not completely. When a check box has been checked, the page will load with the desired results but I still have links to subsequent pages when there shouldn't be any. I again added a variable (the filter variable) to the end of the URLs in case there is more than one page. Do you know what the problem could be? Thank you very much. // Number of records to show per page: $display =3; // Determine how many pages there are... if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined. $pages = $_GET['p']; } else { // Need to determine. // Count the number of records: $q = "SELECT COUNT(user_id), other columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age'"; $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; // Calculate the number of pages... if ($records > $display) { // More than 1 page. $pages = ceil ($records/$display); } else { $pages = 1; } } // End of p IF. // Determine where in the database to start returning results... if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } // if a check box has been checked, it is then assigned to the $string variable if (isset($_POST['city'])) { $start = 0; // added this here to start filtering from page1 $string = "name = '{$_POST['city']}'"; } // if the check box 'all' has been checked, all the records are returned if (isset($_POST['all'])) { $q = "SELECT columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age' LIMIT $start, $display"; } // if the $string variable is set, then the query will be used with the $string variable, otherwise all the records are returned if (isset($string)) { { $q = "SELECT columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age' && '.$string.' LIMIT $start, $display"; } else { $q = "SELECT columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age' LIMIT $start, $display"; } } $r = @mysqli_query($dbc, $q); while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { Print '<p>loop content here</p>'; } // Make the links to other pages, if necessary. if ($pages > 1) { echo '<br /><p>'; $current_page = ($start/$display) + 1; // If it's not the first page, make a Previous button: if ($current_page != 1 && isset($string)) { echo '<a href="view_users.php?s=' . ($start - $display) . '&p=' . $pages . '&name='.$name.'&age='.$age.'&'.$string.'">Previous</a> '; } else { echo '<a href="view_users.php?s=' . ($start - $display) . '&p=' . $pages . '&name='.$name.'&age='.$age.'">Previous</a> '; } // Make all the numbered pages: for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page && isset($string)) { echo '<a href="view_users.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&name='.$name.'&age='.$age.'&'.$string.'">' . $i . '</a> '; } elseif ($i != $current_page) { echo '<a href="view_users.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&name='.$name.'&age='.$age.'">' . $i . '</a> '; } else { echo $i . ' '; } } // End of FOR loop. // If it's not the last page, make a Next button: if ($current_page != $pages && isset($string)) { echo '<a href="view_users.php?s=' . ($start + $display) . '&p=' . $pages . '&name='.$name.'&age='.$age.'&'.$string.'">Next</a>'; } else { echo '<a href="view_users.php?s=' . ($start + $display) . '&p=' . $pages . '&name='.$name.'&age='.$age.'">Next</a>'; } echo '</p>'; // Close the paragraph. } // End of links section. I'm really stuck with this and could use some help. Thank you for your time!
  16. Hi Larry, I do need some help after all. I've reached my 'PHP limit' so to speak. What I'm now attempting is to use check boxes to filter the paginated results. The code does work, albeit not completely. When a check box has been checked, the page will load with the desired results but I still have links to subsequent pages when there shouldn't be any. I again added a variable (the filter variable) to the end of the URLs in case there is more than one page. Do you know what the problem could be? Thank you very much. // Number of records to show per page: $display =3; // Determine how many pages there are... if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined. $pages = $_GET['p']; } else { // Need to determine. // Count the number of records: $q = "SELECT COUNT(user_id), other columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age'"; $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; // Calculate the number of pages... if ($records > $display) { // More than 1 page. $pages = ceil ($records/$display); } else { $pages = 1; } } // End of p IF. // Determine where in the database to start returning results... if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } // if a check box has been checked, it is then assigned to the $string variable if (isset($_POST['city'])) { $start = 0; // added this here to start filtering from page1 $string = "name = '{$_POST['city']}'"; } // if the check box 'all' has been checked, all the records are returned if (isset($_POST['all'])) { $q = "SELECT columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age' LIMIT $start, $display"; } // if the $string variable is set, then the query will be used with the $string variable, otherwise all the records are returned if (isset($string)) { { $q = "SELECT columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age' && '.$string.' LIMIT $start, $display"; } else { $q = "SELECT columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age' LIMIT $start, $display"; } } $r = @mysqli_query($dbc, $q); while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { Print '<p>loop content here</p>'; } // Make the links to other pages, if necessary. if ($pages > 1) { echo '<br /><p>'; $current_page = ($start/$display) + 1; // If it's not the first page, make a Previous button: if ($current_page != 1 && isset($string)) { echo '<a href="view_users.php?s=' . ($start - $display) . '&p=' . $pages . '&name='.$name.'&age='.$age.'&'.$string.'">Previous</a> '; } else { echo '<a href="view_users.php?s=' . ($start - $display) . '&p=' . $pages . '&name='.$name.'&age='.$age.'">Previous</a> '; } // Make all the numbered pages: for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page && isset($string)) { echo '<a href="view_users.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&name='.$name.'&age='.$age.'&'.$string.'">' . $i . '</a> '; } elseif ($i != $current_page) { echo '<a href="view_users.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&name='.$name.'&age='.$age.'">' . $i . '</a> '; } else { echo $i . ' '; } } // End of FOR loop. // If it's not the last page, make a Next button: if ($current_page != $pages && isset($string)) { echo '<a href="view_users.php?s=' . ($start + $display) . '&p=' . $pages . '&name='.$name.'&age='.$age.'&'.$string.'">Next</a>'; } else { echo '<a href="view_users.php?s=' . ($start + $display) . '&p=' . $pages . '&name='.$name.'&age='.$age.'">Next</a>'; } echo '</p>'; // Close the paragraph. } // End of links section.
  17. Hi all, I have figured it out. Subsequent pages weren't working because the query no longer received the values from the $name and $age variables. I thus appended these to the end of the URLs for the previous button, all the numbered pages and the next button. echo '<a href="view_users.php?s=' . ($start - $display) . '&p=' . $pages . '&name='.$name.'&age='.$age.'">Previous</a> '; There might be another solution, but this works. Thanks.
  18. Hi Larry and forum members, I'm using the pagination script (script 10.4 from chapter 10) and have run into a few problems. When I click next or another number, the page will load but not display any records. If I then click the number one, page one will load but also not display any records. The first time around the page will load and display the number of records as per $display =somenumber; My query returns columns from multiple tables. Ordinarily, my query looks something like this: $q = "SELECT a number of columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age'"; The above query has been modified below, but I'm not sure if the query correctly counts the number of records in the database. The users table includes the foreign keys from the other tables. Must I also use the COUNT() function on these columns? $q = "SELECT COUNT(user_id), other columns FROM table A INNER JOIN table B JOIN_CLAUSE WHERE name = '$name' && age = '$age' LIMIT $start, $display"; What I'm looping out consists of a paragraph, an image and other elements which are contained within div tags. Don't know if this is relevant but I thought I'd mention it. I hope the information I provided is not too cryptic. Thank you in advance!
  19. Hi Larry, sorry to do this, but I hope that you could please help me out with another issue that I'm experiencing. I'm using a while loop to print out the current records of a user, which the user can then update themselves. For this example I'm just looping out a user's skills and an empty text input for each new skill. The problem is that the text inputs all have the same structure – name='skill'. When I do an update, all of that user's skills will have the same skill (from the last text input). What must I do so that each text input updates only its associated row? This probably entails setting the name to an array – name='skill[$i]'? Thank you! if ($num > 0) { while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { print'<p>'.$row['skill'].'</p> <input type="text" name="skill" size="" maxlength="" value="'; if (isset($_POST['skill'])) echo $_POST['skill']; print'" /> </p>'; } } if ($_SERVER['REQUEST_METHOD'] == 'POST') { $newskills = mysqli_real_escape_string($dbc, trim($_POST[skill'])); $q = "UPDATE table1 INNER JOIN table2 USING (some_id) SET skill='$newskills' WHERE user_id = {$_SESSION['user_id']} ";
  20. I missed that! I guess 'doh!' is appropriate. It's finally working. Thank you very much for the great service. Hope you enjoy the weekend.
  21. I just get the following error: Parse error: syntax error, unexpected '{' in C:\wamp\www\testincludes\testupdates.php on line 75 Line 75 corresponds to if (isset($_POST['name']) {
  22. Thanks Larry, I have tried this but am receiving: Parse error: syntax error, unexpected '{' The { is the first curly brace after if (isset($_POST['name']) <?php $q = "SELECT name FROM table1 WHERE name = 'Homer Simpson' "; $r = @mysqli_query($dbc, $q); $num = mysqli_num_rows($r); if ($num > 0) { $row = mysqli_fetch_array($r, MYSQLI_ASSOC); print'<h1>Change your details</h1> <form action="" method="post"> <p>change name: <input type="text" name="name" size="20" maxlength="60" value="'; if (isset($_POST['name']) { echo $_POST['name']; } else { echo $row['name']; } print'" /> </p> <p><input type="submit" name="submit" value="Change Password" /></p> </form>'; } Thank you for your assistance!
×
×
  • Create New...