Hi. Can you please help me. I have created a pagination, taken from David Power's Book 'PHP Solutions'. When I click the link to navigate to the second page, it appears to me that the SQL query doesn't run for the second time.
Please forgive me for the vagueness of this question. I'm new to programming. below is my code:
Please forgive me for the vagueness of this question. I'm new to programming. below is my code:
Code:
<?php if(isset($_GET['search'])) { // set maximum of records to be retrieved at a time define('SHOWMAX', 4); require_once('../includes/connection.inc.php'); $conn = dbConnect('xxx','xxx'); $sql = "SELECT COUNT(*) FROM vacancies WHERE job_title LIKE :search"; $searchterm = '%' . $_GET['search'] . '%'; $stmt = $conn->prepare($sql); $stmt->bindParam(':search', $searchterm,PDO::PARAM_STR); $stmt->execute(); $totalVacancies = $stmt->fetchColumn(); $stmt->closeCursor(); if(isset($_GET['curPage'])) { $curPage = $_GET['curPage']; } else { $curPage = 0; } // calculate the start row of the subset $startRow = $curPage * SHOWMAX; // prepare SQL to retrieve subset of post details $sql = "SELECT vacancy_id, company_name, job_title, duties FROM vacancies WHERE job_title LIKE :search ORDER BY posted DESC LIMIT $startRow," . SHOWMAX; $searchterm = '%' . $_GET['search'] . '%'; $stmts = $conn->prepare($sql); $stmts->bindParam(':search', $searchterm,PDO::PARAM_STR); $stmts->bindColumn('vacancy_id',$vacancy_id); $stmts->bindColumn('company_name',$company_name); $stmts->bindColumn('job_title',$job_title); $stmts->bindColumn('duties', $duties); $stmts->execute(); } // if the search is empty, redirect the use to the home page /*if(empty($_GET['search'])) { header('Location: http://localhost/modernmedia-sa/careers/home.php'); }*/ ?> and the other page <div id="leftSide"> <div id="tab"> <h1>Listed Vacancies: Share a vacancy post. <a class="underlinedLink" title="List Vacancy" href="./vacancyform.php">CLICK HERE</a></h1> </div> <div id="vacancies"> <?php if(isset($totalVacancies)) { ?> <h1 style="color:#006f93;">Number of results for <b><?php echo htmlentities($_GET['search'], ENT_COMPAT, 'UTF-8'); ?></b>: <?php echo $totalVacancies; ?></h1> <?php if($totalVacancies) { ?> <?php foreach($stmts as $row) { ?> <h1 style="margin-bottom:6px; border-bottom:none;"><b><?php echo $company_name; ?><b></h1> <br/> <p style="color:black; line-height:18px;"><b>Vacancy:</b> <?php echo $job_title; ?> <br/><br/> <b>Description:</b> <?php echo $row['duties']; ?> <br/></br/> <div class="link"> <a title="fullpost" href="./vacanciespost.php?vacancy_id=<?php echo $vacancy_id; ?>">View Post</a> </div> </p> <br/><br /> <hr style="border-color:gray; border:1px solid #006f93;" /> <br/> <?php } ?> <?php } } ?> <p style="color:#0182ad" title="Page Scroll" href="">Displaying <?php echo $startRow+1; if($startRow+1 < $totalVacancies) { echo ' to '; if($startRow+SHOWMAX < $totalVacancies) { echo $startRow+SHOWMAX; } else { echo $totalVacancies; } } echo " of $totalVacancies"; ?> | <!-- Navigation Links --><?php // creat a back link if current page greater than 0 if($curPage > 0) { echo '<a style="color:#006f93;" href="' . $_SERVER['PHP_SELF'] . '?curPage=' .($curPage-1) . '"> < Prev</a>'; } else { // otherwise leave the cell empty echo ' '; } ?> | <?php // pad the final row with emtpy cells if more than 2 columms if(COLS-2 > 0) { for($i = 0; $i < COLS-2; $i++) { echo ' '; } } ?> <?php // create a foward link if more records exist if($startRow+SHOWMAX < $totalVacancies) { echo '<a style="color:#006f93;" href="' . $_SERVER['PHP_SELF'] . '?curPage=' . ($curPage+1) . '">Next ></a>'; } if(empty($totalVacancies)) { echo "Sorry. There are no records found for this section"; } ?> </div> <!-- End Vacancy --> </div> </div>
Comment