title: Link to an item on a paginated page in php
body: <p>I'm quite a novice coder in PHP / MySQL. I am developing a site at the moment and have found many answers to some of the questions I have had on this site - Great Resource! - but now I need to ask one of my own which I'm struggling with. Do bear with me...</p>
  
  <p>So on my site I have a side panel which shows testimonials from a database and randomly pulls in 1 result so that the testimonial showing changes every time the user navigates around the site. The side panel is an php include and is shown across the site. I am only showing a few lines of the testimonial and then including a read more link. </p>
  
  <p>On the testimonials page, I have 13 testimonials paginated on 1 per page (13 pages) as each testimonial is quite long - that's how the client wants it.</p>
  
  <p>What I am looking to do is to link from the 'Read More' link to the relevant testimonial - I assume using an html anchor is best? The issue I'm finding is that I can't seem to link to the relevant item on it's paginated page. If I use the testimonial ID I can link through to only page 1 of the testimonials.php and not to the specific page that the testimonial appears on. I hope this makes sense?</p>
  
  <p>I have included a copy of my code from the Side Panel and also on the Testimonial page. </p>
  
  <p>Any insight or help would be greatly appreciated.</p>
  
  <p>Thanks.</p>
  
  <p><strong>Sidebar.php</strong>
      
  
  <pre><code>$gettest = mysql_query ("SELECT * FROM testimonial ORDER BY RAND() LIMIT 1");
  
  while ($row=mysql_fetch_array($gettest)) 
  {
  $testimonial = $row['testimonial'];
  $shorttest = myTruncate($testimonial, 200, " ");
  ?&gt;
  
  &lt;blockquote&gt;&lt;span class="bqstart"&gt;&amp;#8220;&lt;/span&gt;&lt;p&gt;&lt;?PHP echo $shorttest;?&gt;&lt;/p&gt;&lt;span class="bqend"&gt;&amp;#8221;&lt;/span&gt;&lt;/blockquote&gt;      
  &lt;p&gt;&lt;a href="testimonials.php#&lt;?PHP echo '' . $row["id"] . '';?&gt;"&gt;&lt;?PHP echo '' . $row["name"] . '';?&gt;&lt;/a&gt;&lt;/p&gt;
  &lt;br clear='all' /&gt;
  
  &lt;?PHP
  }                           
  mysql_close($DB);
  ?&gt;
  </code></pre>
  
  <p><strong>Testimonials.php</strong></p>
  
  <pre><code>&lt;?php
  
  include('connectDB.php'); 
  
  $tableName="testimonial";
  $targetpage = "testimonials.php";
  $limit = 1; 
  
  $query = "SELECT COUNT(*) as num FROM testimonial";
  $total_pages = mysql_fetch_array(mysql_query($query));
  $total_pages = $total_pages[num];
  
  $stages = 3;
  $page = mysql_escape_string($_GET['page']);
  if($page){
  $start = ($page - 1) * $limit;
  }else{
  $start = 0;
  }   
  
  // Get page data
  $query1 = "SELECT * FROM testimonial LIMIT $start, $limit";
  $result = mysql_query($query1);
  
  // Initial page num setup
  if ($page == 0){$page = 1;}
  $prev = $page - 1;
  $next = $page + 1;
  $lastpage = ceil($total_pages/$limit);
  $LastPagem1 = $lastpage - 1;                    
  
  $paginate = '';
  if($lastpage &gt; 1)
  {   
  
  $paginate .= "&lt;div class='paginate'&gt;";
  // Previous
  if ($page &gt; 1){
  $paginate.= "&lt;a href='$targetpage?page=$prev'&gt;previous&lt;/a&gt;";
  }else{
  $paginate.= "&lt;span class='disabled'&gt;previous&lt;/span&gt;";   }
  
  // Pages
  if ($lastpage &lt; 7 + ($stages * 2))  // Not enough pages to breaking it up
  {
  for ($counter = 1; $counter &lt;= $lastpage; $counter++)
  {
  if ($counter == $page){
   $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;";
  }else{
   $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;";}
  }
  }
  elseif($lastpage &gt; 5 + ($stages * 2))   // Enough pages to hide a few?
  {
  
  // Beginning only hide later pages
  if($page &lt; 1 + ($stages * 2))
  {
  for ($counter = 1; $counter &lt; 4 + ($stages * 2); $counter++)
  {
   if ($counter == $page){
       $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;";
   }else{
       $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;";}
  }
  $paginate.= "...";
  $paginate.= "&lt;a href='$targetpage?page=$LastPagem1'&gt;$LastPagem1&lt;/a&gt;";
  $paginate.= "&lt;a href='$targetpage?page=$lastpage'&gt;$lastpage&lt;/a&gt;";
  }
  
  // Middle hide some front and some back
  elseif($lastpage - ($stages * 2) &gt; $page &amp;&amp; $page &gt; ($stages * 2))
  {
  $paginate.= "&lt;a href='$targetpage?page=1'&gt;1&lt;/a&gt;";
  $paginate.= "&lt;a href='$targetpage?page=2'&gt;2&lt;/a&gt;";
  $paginate.= "...";
  for ($counter = $page - $stages; $counter &lt;= $page + $stages; $counter++)
  {
   if ($counter == $page){
       $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;";
   }else{
       $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;";}
  }
  $paginate.= "...";
  $paginate.= "&lt;a href='$targetpage?page=$LastPagem1'&gt;$LastPagem1&lt;/a&gt;";
  $paginate.= "&lt;a href='$targetpage?page=$lastpage'&gt;$lastpage&lt;/a&gt;";
  }
  
  // End only hide early pages
  else
  {
  $paginate.= "&lt;a href='$targetpage?page=1'&gt;1&lt;/a&gt;";
  $paginate.= "&lt;a href='$targetpage?page=2'&gt;2&lt;/a&gt;";
  $paginate.= "...";
  for ($counter = $lastpage - (2 + ($stages * 2)); $counter &lt;= $lastpage; $counter++)
  {
   if ($counter == $page){
       $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;";
   }else{
       $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;";}
  }
  }           
  
  }
  
  // Next
  if ($page &lt; $counter - 1){
  $paginate.= "&lt;a href='$targetpage?page=$next'&gt;next&lt;/a&gt;";
  }else{
  $paginate.= "&lt;span class='disabled'&gt;next&lt;/span&gt;";
  }
  
  $paginate.= "&lt;/div&gt;";       
  
  }
  // pagination
  echo $paginate;
  
  ?&gt;
  
  
  
  &lt;?php 
  
  while($row = mysql_fetch_array($result))
  {
  
  
  
  ?&gt;
  &lt;a name="&lt;?PHP echo '' . $row["ID"] . '';?&gt;"&gt;&lt;/a&gt;
  
  &lt;blockquote&gt;&lt;span class="bqstart"&gt;&amp;#8220;&lt;/span&gt;&lt;p&gt;&lt;?PHP echo '' .nl2br($row["testimonial"]) . '';?&gt;&lt;/p&gt;&lt;span class="bqend"&gt;&amp;#8221;&lt;/span&gt;&lt;/blockquote&gt; 
  &lt;p class="highlight"&gt;&lt;?PHP echo '' . $row["name"] . '';?&gt;&lt;/p&gt;
  &lt;br clear='all' /&gt;
  
  &lt;?PHP
  }
  
  echo $paginate;
  
  mysql_close($DB);
  
  ?&gt;
  </code></pre>