I am building a shop taking products from a txt file and I want to create pagination for the product listing pages.
The code I am using to pull the products and display them is:
<?php
// get local file
$p = file_get_contents('mytxtfile.txt');
// turn file into an array, line by line
$ar = explode("
",$p);
// setup count for how many products there are
$found = 0;
// loop through products
for($i = 0; $i < count($ar); $i++){
$product = explode("|",$ar[$i]);
if(
(strpos(slug($product[9]),$_GET['slug']) !== false ) ||
(strpos(slug($product[10]),$_GET['slug']) !== false ) ||
(strpos(slug($product[21]),$_GET['slug']) !== false )
){
// the product numbers below correspond to the column number in the CSV
$found++; // increment the number of files found
echo '<div class="item">';
echo '<a href="/'.slug($product[21]).'/'.slug($product[1]).'.htm"><img src="'.$product[5].'" /></a>';
echo '<h3><a href="/'.slug($product[21]).'/'.slug($product[1]).'.htm">'.substr($product[1], 0, 70).'</a></h3>';
if (slug($product[8]) == slug($product[7])){
echo '<p class="price">Only $'.($product[7]).'</p>';
}
else {
echo '<p class="price"><del>Was $'.($product[8]).'</del> NOW Only $'.($product[7]).'</p>';
}
echo '</div>';
if($found > 101){
break;
}
}
}
?>
This will display my products upto the break but I want to show all products. Some categories have a lot of products and I want to keep the page load time down.
Use the jquery plugin datatables with mysql. What is stated above by glasseye is valid.
Managed to get it to work
<?php
// get local file
$p = file_get_contents('mytxtfile.txt');
// turn file into an array, line by line
$ar = explode("
",$p);
// setup count for how many products there are
$found = 0;
if ($_POST['variable'] == '')
{
$perpage = '22';
}
else
{
$perpage = $_POST['variable'] ;
}
$current = 0;
$page = (isset($_REQUEST['p']) ? $_REQUEST['p'] : 0 );
// loop through products
for($i = 0; $i < count($ar); $i++){
$product = explode("|",$ar[$i]);
// the product numbers below correspond to the column number in the CSV
if(strpos($product[19],'BRAND') !== false){
$found++; // increment the number of files found
$start = ($page * $perpage);
$end = $start + $perpage;
if( ($found > $start) && ($found < $end) ){
echo '<div class="item">';
echo '<a href="/'.slug($product[21]).'/'.slug($product[1]).'.htm"><img src="'.$product[5].'" /></a>';
echo '<h3><a href="/'.slug($product[21]).'/'.slug($product[1]).'.htm">'.substr($product[1], 0, 70).'</a></h3>';
if (slug($product[13]) == ''){
}
else {
echo '<p>Size = '.$product[13].'</p>';
}
if (slug($product[8]) == slug($product[7])){
echo '<p class="price">Only $'.($product[7]).'</p>';
}
else {
echo '<p class="price"><del>Was $'.($product[8]).'</del> NOW Only $'.($product[7]).'</p>';
}
echo '</div>';
}
}
}
echo '<div class="pagi">'.$found.' Results » ';
?>
<script>
choose: function() {
option = this.getElementsByTagName("option");
for(d = 0; d < option.length; d++) {
if(option[d].selected == true) {
document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
}
} if(this.form.name=='perpage') {this.form.submit();}
}
}
</script>
Show per page
<form name="perpage" action="index.php" method="post">
<select name="variable" onchange="this.form.submit()" />
<option disabled selected>22</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="200">200</option>
</select>
</form>
Page:
<?php
$totalpages = ($found / $perpage) + 1;
for($i = 1; $i < $totalpages; $i++){
echo '<a href="index.php?p='.$i.'">'.$i.'</a> ';
}
?>
</div>
I think databasing would be easier. Thanks for your advice guys!