Jump to content

Recommended Posts

Here's a line.  I want to add 100 more of same lines and increment the page numbers as shown below.  I know I can just manually add 100 lines but Is there a way to do this with PHP that would make it easy? Or is this more of a javascript thing?


// original
$page1 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=1">1</a>';   


// new
$page1 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=1">1</a>';
$page2 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=2">2</a>';   
$page3 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=3">3</a>';  
$page4 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=4">4</a>';  
$page5 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=5">5</a>';   
...etc                                                                                                                                       

Use a loop and an array to store the results.

 

$pages = [];
for ($pageNumber=1; $pageNumber <= 100; $pageNumber++){
    $pages[] = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=' . $pageNumber . '">' . $pageNumber . '</a>';
}

Use a loop and an array to store the results.

 

$pages = [];
for ($pageNumber=1; $pageNumber <= 100; $pageNumber++){
    $pages[] = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=' . $pageNumber . '">' . $pageNumber . '</a>';
}

 

That's great.  Now my next question is, how do I echo all 100 of those a href links outside the for loop?

Edited by imgrooot

That's great.  Now my next question is, how do I echo all 100 of those a href links outside the for loop?

 

You could run another for() loop, or a foreach() loop. For instance:

<div class='links_aggregate'>
<?php
foreach($pages as $p){
	print($p);
}
?>
</div>

Or, if you're gathering the data at the point in the page where you need to output the links, simply print() or echo() them there instead of assigning them to an array.

 

Heck, if you don't care about source readability, you could simply output an implode() on the array

echo implode(' ', $pages);

That last one hasn't been tested and is the product of a couple beers, so take it with a grain of salt.

You could run another for() loop, or a foreach() loop. For instance:

<div class='links_aggregate'>
<?php
foreach($pages as $p){
	print($p);
}
?>
</div>

Or, if you're gathering the data at the point in the page where you need to output the links, simply print() or echo() them there instead of assigning them to an array.

 

Heck, if you don't care about source readability, you could simply output an implode() on the array

echo implode(' ', $pages);

That last one hasn't been tested and is the product of a couple beers, so take it with a grain of salt.

 

That's great.  How do I get the "$pageNumber" from the first loop to show up in the second loop? Should I just put another foreach loop inside the second foreach loop above?  Essentially I am trying to get both the a href link with incremented pages and the incremented page numbers individually.

Allow me to get beyond your "problem". What exactly is it that you are doing that you need to generate 100 links?

 

Alright so here's the full picture.  I am trying to get a pagination working.  I am trying to modify the script I already had to match the new look.  The new pagination looks roughly like this (<) 1 2 3 4 5  (>).   I have it working but I find there has to be a more efficient method out there to get the same results. 

 

Here is the full process code for one of the pages. I didn't update it with the new code mentioned in the above comments.

<?php 
$type_id 	  = (!empty($_GET['type']) ? $_GET['type'] : null);
$price_filter 	  = (!empty($_GET['price']) ? $_GET['price'] : null);
$get_page         = (!empty($_GET['page']) ? $_GET['page'] : null);
?>
<div id="breadcrumb">
	<div id="breadcrumb-child">
		<h2>Shop</h2>
		<div id="price-filter">
			Price:
			<a <?php if($price_filter == 'low'){ ?>class="filter-highlight"<?php } ?> href="shop?type=<?php echo $type_id; ?>&name=<?php echo $get_type_3_name_slug; ?>&price=low">Low</a> |
			<a <?php if($price_filter == 'high'){ ?>class="filter-highlight"<?php } ?> href="shop?type=<?php echo $type_id; ?>&name=<?php echo $get_type_3_name_slug; ?>&price=high">High</a>
		</div>
	</div>
</div>
<div class="collection-list">
<?php

$count_records = $db->prepare("SELECT COUNT(*) FROM items WHERE type_3 = :type_3");
$count_records->bindParam(':type_3', $type_id);
$count_records->execute();
$total = $count_records->fetchColumn();

// How many items to list per page
$limit = 3;

// How many pages will there be
$pages = ceil($total / $limit);

// What page are we currently on?
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array (
	'options' => array(
		'default'   => 1,
		'min_range' => 1,
	),
)));

// Calculate the offset for the query
$offset = ($page - 1)  * $limit;

// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total);

if($page > 0) {
	$offset = ($page - 1)  * $limit;
} else {
	$offset = 0;
}

if($price_filter == 'high') {

  $get_records = $db->prepare("SELECT items.*, type_1.*, type_3.* FROM items
  LEFT JOIN type_1 ON items.type_1 = type_1.type_1_id
  LEFT JOIN type_3 ON items.type_3 = type_3.type_3_id
  WHERE items.type_3 = :type_3 ORDER BY items.item_price DESC LIMIT {$limit} OFFSET ".$offset);
  $get_records->bindParam(':type_3', $type_id);
  $get_records->execute();
  $result_records = $get_records->fetchAll(PDO::FETCH_ASSOC);
  if(count($result_records) > 0){
    foreach($result_records as $row) {

      // There are other variables but I am only showing you the ones relevant to the pagination
      $get_type_1_id	 			=	intval($row['type_1']);
      $get_type_3_name_slug  		        =	trim($row['type_3_name_slug']);

      // This shows all the individual products on a page.
      include 'snippets/product-loop.php';
    }
  } else {

    echo '<div id="message-flash">Items coming soon.</div>';

  }

} else if($price_filter == 'low') {

  $get_records = $db->prepare("SELECT items.*, type_1.*, type_3.* FROM items
  LEFT JOIN type_1 ON items.type_1 = type_1.type_1_id
  LEFT JOIN type_3 ON items.type_3 = type_3.type_3_id
  WHERE items.type_3 = :type_3 ORDER BY items.item_price ASC LIMIT {$limit} OFFSET ".$offset);
  $get_records->bindParam(':type_3', $type_id);
  $get_records->execute();
  $result_records = $get_records->fetchAll(PDO::FETCH_ASSOC);
  if(count($result_records) > 0){
    foreach($result_records as $row) {

      // There are other variables but I am only showing you the ones relevant to the pagination
      $get_type_1_id	 			=	intval($row['type_1']);
      $get_type_3_name_slug  		        =	trim($row['type_3_name_slug']);

      // This shows all the individual products on a page.
      include 'snippets/product-loop.php';
    }
  } else {

    echo '<div id="message-flash">Items coming soon.</div>';

  }

} else {

  $get_records = $db->prepare("SELECT items.*, type_1.*, type_3.* FROM items
  LEFT JOIN type_1 ON items.type_1 = type_1.type_1_id
  LEFT JOIN type_3 ON items.type_3 = type_3.type_3_id
  WHERE items.type_3 = :type_3 ORDER BY items.item_id DESC LIMIT {$limit} OFFSET ".$offset);
  $get_records->bindParam(':type_3', $type_id);
  $get_records->execute();
  $result_records = $get_records->fetchAll(PDO::FETCH_ASSOC);
  if(count($result_records) > 0){
    foreach($result_records as $row) {

      // There are other variables but I am only showing you the ones relevant to the pagination
      $get_type_1_id	 		        =	intval($row['type_1']);
      $get_type_3_name_slug  		        =	trim($row['type_3_name_slug']);

      // This shows all the individual products on a page.
      include 'snippets/product-loop.php';
    }
  } else {

    echo '<div id="message-flash">Items coming soon.</div>';

  }

}

// Pagination starts here
// It looks like this format: (<) 1 2 3 4 5 (>)

if($price_filter == 'low') {

  $prevlink = ($page > 1) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">‹</span>';

  $page1 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=1">1</a>';
  $page2 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=2">2</a>';
  $page3 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=3">3</a>';
  $page4 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=4">4</a>';
  $page5 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=5">5</a>';
  $page6 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=6">6</a>';
  $page7 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=7">7</a>';
  $page8 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=8">8</a>';
  $page9 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=9">9</a>';
  $page10 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=10">10</a>';

  $nextlink = ($page < $pages) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=' . ($page + 1) . '" title="Next page">›</a>' : '<span class="disabled">›</span>';
	
} else if($price_filter == 'high') {

  $prevlink = ($page > 1) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">‹</span>';

  $page1 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=1">1</a>';
  $page2 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=2">2</a>';
  $page3 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=3">3</a>';
  $page4 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=4">4</a>';
  $page5 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=5">5</a>';
  $page6 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=6">6</a>';
  $page7 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=7">7</a>';
  $page8 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=8">8</a>';
  $page9 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=9">9</a>';
  $page10 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=10">10</a>';

  $nextlink = ($page < $pages) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=' . ($page + 1) . '" title="Next page">›</a>' : '<span class="disabled">›</span>';

} else {

  $prevlink = ($page > 1) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">‹</span>';

  $page1 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=1">1</a>';
  $page2 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=2">2</a>';
  $page3 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=3">3</a>';
  $page4 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=4">4</a>';
  $page5 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=5">5</a>';
  $page6 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=6">6</a>';
  $page7 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=7">7</a>';
  $page8 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=8">8</a>';
  $page9 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=9">9</a>';
  $page10 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=10">10</a>';

  $nextlink = ($page < $pages) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=' . ($page + 1) . '" title="Next page">›</a>' : '<span class="disabled">›</span>';

}

?>
  <div id="pagination">
    <ul>

      <li><?php echo $prevlink; ?></li>

      <?php if($page <= 5) {  ?>
      
      <li class="page-numbers <?php if($get_page == 1) { echo "active-page-number";} ?>"><?php echo $page1; ?></li>
      <li class="page-numbers <?php if($get_page == 2) { echo "active-page-number";} ?>"><?php echo $page2; ?></li>
      <li class="page-numbers <?php if($get_page == 3) { echo "active-page-number";} ?>"><?php echo $page3; ?></li>
      <li class="page-numbers <?php if($get_page == 4) { echo "active-page-number";} ?>"><?php echo $page4; ?></li>
      <li class="page-numbers <?php if($get_page == 5) { echo "active-page-number";} ?>"><?php echo $page5; ?></li>
      
      <?php } else if($page <= 10) { ?>
      
      <li class="page-numbers <?php if($get_page == 6) { echo "active-page-number";} ?>"><?php echo $page6; ?></li>
      <li class="page-numbers <?php if($get_page == 7) { echo "active-page-number";} ?>"><?php echo $page7; ?></li>
      <li class="page-numbers <?php if($get_page ==  { echo "active-page-number";} ?>"><?php echo $page8; ?></li>
      <li class="page-numbers <?php if($get_page == 9) { echo "active-page-number";} ?>"><?php echo $page9; ?></li>
      <li class="page-numbers <?php if($get_page == 10) { echo "active-page-number";} ?>"><?php echo $page10; ?></li>
      
      <?php } else ?>

      <li><?php echo $nextlink; ?></li>

    </ul>
  </div>
</div>
<?php
Edited by imgrooot

I had a feeling you were trying to do something with pagination. This is a classic XY Problem. http://xyproblem.info/

 

I am surprised the other experts didn't see through what you were asking. I am not inclined to get into this, but you are doing it all wrong. You have now provided enough information that the others here will now be able to properly help you with the "real' problem.

Edited by benanamen

I had a feeling you were trying to do something with pagination. This is a classic XY Problem. http://xyproblem.info/

 

I am surprised the other experts didn't see through what you were asking. I am not inclined to get into this, but you are doing it all wrong. You have now provided enough information that the others here will now be able to properly help you with the "real' problem.

 

Yes It's all about the pagination.  The way I have it set up now is more manual work for me.  The solution works but I am trying to find a way to improve the pagination code so that it's more efficient.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.