Jump to content

mkmac

New Members
  • Posts

    7
  • Joined

  • Last visited

Everything posted by mkmac

  1. To start fresh here's the code where the pagination will go. I have the pagination in HTML only at the bottom. I want you to see where I'm coming from since I'm passing the ID to display the records on this page. If you can do it without the ID more power to ya. For example, to get to the page below, I'm clicking on an image which passes an id in order for the content to display on the page below. Let me know how you would construct this without using an id to pass the data since currently the page is constructed to display "all" results based off an ID. Thanks <?php include("head.php"); ?> <body class=""> <?php include("navigation.php"); ?> <?php if(ctype_digit($_GET['id'])){ $the_id = $_GET['id']; } else { $the_id = 0; } include('connection.php'); $sql="SELECT * FROM the-table WHERE the_id = $the_id"; $result = mysqli_query($con,$sql) or die(mysqli_error($con)); $row = mysqli_fetch_array($result); ?> <section class="container"> <div class="containerDetail"> <h1>title <?php echo $row['the_title'] ?></h1> <ul> <li>subject: <?php echo $row['the_subject'] ?></li> <li> summary: <?php echo $row['summary'] ?> </li> <?php if(isset($row['view']) && !empty($row['view'])){ echo "<br><li><a href='$row[view]'>View</a></dd>"; ?> <?php }else{ } ?> </ul> </div> <img src="<?php echo $row['Image']?>" /> <div class="pagination"> <div> <div id="paglinks"><a href="#">prev</a> | <a href="#">next</a></div> </div> </div> </section> </body> </html>
  2. Anyone else want to try? Important points. !. The page is one page .. for example .. thepage.php. The page handles all the data. On the previous page an image is clicked on passing a variable which is the id from the db and displays it as thepage.php?id=1. The second image thepage.php?id=2 etc. 2. In my mind the pagination.. Prev and Next will need to be populated with thepage.php?id=1 or thepage.php?id=2 or thepage.php?id=3 depending on which page you're on. 3. At this time I have 5 rows of data. Instead of stopping on - thepage.php?id=5 (the last record) .. it keeps going to thepage.php?id=6, thepage.php?id=7, thepage.php?id=8 etc. I want the next to disappear when on the the last record with only the prev displaying. Then the same for Prev when on the first record (thepage.php?id=1) Prev should disappear. Let me know if someone can help with this. Thanks
  3. I read it but that's how I'm displaying the data on the page with the ids. I thought if I just pass those ids then it will display the content needed which it does. You mentioned "PAGE" number... I don't know what you're referring to. I don't have page numbers. I made one page which displays all the info and when clicked say on next then it will be the same page let's call it thepage.php?... click next ... it goes to thepage.php?id=2..... If you could so I understand what you're saying, post what this would be then - //ADD CODE TO DISPLAY THE RECORD(S) If I have that maybe it will make more sense on what you're saying.
  4. UPDATE: I got it to work far as the id in the link. But still having he issue of: 1. It's displaying rows that do not exist like for example id=7 that row doesn't exist. 2. What should happen is, after the last row id=6 instead of going to id=7 next should be grayed out. 3. The same for prev after no records are found then it should not be linked to go to a record that doesn't exist.
  5. I added the prev and next links but not getting the id in the link like I was previously. It only displays id 1 when clicking on next and -1 when clicking on prev. In my previous code, I was getting all the ids and displaying the records. The issue was displaying records that wasn't there. This was due to the code picking ids that didn't exist - Maybe because I was using SELECT * ? Let me add I need all the records for the id that appears in the next and previous links. For example, the next link has id 1, I need everything in id 1 to display. This is the reason I'm using the id and using get to display the id in my code above. <?php $recordsPerPage = 1; if(ctype_digit($_GET['id'])){ $the_id = $_GET['id']; } else { $the_id = 0; } include('connection.php'); //Get the current page to display $currentPage = isset($_GET['page']) ? intval($_GET['page']) : 1; //Calculate the limit start value $limitStart = ($currentPage-1) * $recordsPerPage; //Get the current page records ## NOTE the SQL_CALC_FOUND_ROWS allows you to use a query to get the ## current page records and calculate the total records (w/o the limit) $query = "SELECT SQL_CALC_FOUND_ROWS the_id FROM thetable ORDER BY the_id LIMIT {$limitStart}, {$recordsPerPage}"; $result = mysqli_query($con, $query); //Run second query to get the total records calculated from 1st query $query = "SELECT FOUND_ROWS()"; $result_count = mysqli_query($con, $query); $rowResult = mysqli_fetch_array($result_count); $totalRecords = $rowResult[0]; //Calculate total pages $totalPages = ceil($totalRecords / $recordsPerPage); //Create pagination controls $paginationControls = ''; //Previous page if($currentPage > 1) { $prevPage = $page - 1; $paginationControls .= "<a href=\"./details.php?page={$prevPage}\">« Previous </a>"; } else { $paginationControls .= "« Previous"; } //Next page if($currentPage < $totalPages) { $nextPage = $page + 1; $paginationControls .= "<a href=\"./details.php?page{$nextPage}\">Next »</a>"; } else { $paginationControls .= "Next »";; } if($currentPage == 0){ echo "Previous «"; }else{ echo "<a href=\"./details.php?id=" . ($currentPage - 1) . "\">Previous «</a>"; } echo " "; if($start == $query-1){ echo "Next »"; }else{ echo "<a href=\"./details.php?id=" . ($currentPage + 1) . "\">Next »</a>"; } mysqli_close($con); ?>
  6. I'm aware of the sql injection. I was trying to get something working so I can understand it. Then I go back and fix what needs to be fixed. I'll try this and let you know. Thank you,
  7. Hi, I'm trying to populate the previous and next links with an id from mysql. The code below works but also displays ids that do not exist in the database. I want the code to only show me the rows that exist not the ones that do not exist. For example, at this time I have 5 rows. The ids of the rows are 1, 2, 3, 4, 6 when I get to say, id 6 and click on next it displays id 7 instead of going back to 1 or greying out next - meaning there's no more to view. Can someone help? <?php include('connection.php'); if(isset($_GET['id'])){ $start = $_GET['id']; }else{ $start = 0; } $sql = mysql_num_rows(mysql_query("SELECT * FROM thetable")); $result = mysqli_query($con,$sql); $rows = mysql_fetch_array($result); echo $rows['thetable']; if($start == 0){ echo "Previous «"; }else{ echo "<a href=\"./thepage.php?id=" . ($start - 1) . "\">« Previous </a>"; } if($start == $sql-1){ echo "Next »"; }else{ echo "<a href=\"./thepage.php?id=" . ($start + 1) . "\">Next »</a>"; } ?> <?php // End while loop. mysqli_close($con); ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.