Pagination no database

Hi

im wondering how i could implement Pagination to my media
here is my function…

[php]function get_media($media_cat, $display_recent, $purifier, $media_id) {
if (!empty($media_id)) {
if ($media_id == $media_cat) {
$display_recent = null;
}
}
$files = array();
$files = glob(MEDIA_BASEDIR.$media_cat.’/*.txt’);
usort($files, function($x, $y) {
return $x < $y;
});
$recent_content = array_slice($files, 0, $display_recent);
foreach ($recent_content as $media) {
$line = file($media);
$media_title = ($purifier->purify($line[0]));
$date_time = basename($media);
$date_time = basename($media, ‘.txt’);
$content = file_get_contents($media);
$content = str_replace($media_title,’’,$content);
thememediatop($media_title, $media_cat, $media_id);
print_r ($purifier->purify($content));
thememediabottom($date_time);
}
}[/php]

i will probably make a new function for the media page itself for the pagination, this function can be placed on any page by telling it what directory to use and how many to display useing…

[php]get_media(‘Announcements’, 1, $purifier, $media_id);[/php]

any help would be great, thanks.

Okay, so what help do you need?

At the most basic level, pagination takes a total amount, returns a subset, and keeps track of it’s location.

Based on that information, what do think would be an idea of how to handle it for a files system?

I need help to paginate my media, this is my first attempt at one…

I have tried to implement this pagination from this site: http://www.devnetwork.net/viewtopic.php?f=1&t=128417 but i cant get it to work… i cant see where i have gone wrong…

here is what iv managed to do with it so far… but its not working

[php]function media_index($purifier, $media_id) {
$files = array();
$files = glob(MEDIA_BASEDIR.$media_id.’/*.txt’);

$records_per_page = 6;
$total_records = count($files);
$total_pages = ceil($total_records / $records_per_page);

if isset((int)$_GET['p']) {
	$current_page = (int)$_GET['p'];
}
if($current_page<1 || $current_page>$total_pages)
{
	$current_page = 1;
}

usort($files, function($x, $y) {
	return $x < $y;
	});
	
$media_content = array_splice($files, ($current_page-1)*$records_per_page, $records_per_page);

foreach ($media_content as $media) {
	for ($col=0; $col<$records_per_page; $col++) {
		
		$line = file($media);
		$media_title = ($purifier->purify($line[0]));
		$date_time = basename($media);
		$date_time = basename($media, '.txt');
		$content = file_get_contents($media);
		$content = str_replace($media_title,'',$content);
		
		thememediatop($media_title, $media_cat, $media_id);
		print_r ($purifier->purify($content));
		thememediabottom($date_time);
	}
}

$first = "First";
$prev  = "Prev";
$next  = "Next";
$last  = "Last";
if($current_page>1) {
	$prevPage = $current_page - 1;
	$first = "<a href='./?page=Media&id={$media_id}&p=1'>{$first}</a>\n";
	$prev  = "<a href='./?page=Media&id={$media_id}&p={$prevPage}'>{$prev}</a>\n";
}
if($current_page<$total_pages) {
	$nextPage = $current_page + 1;
	$next = "<a href='./?page=Media&id={$media_id}&p={$nextPage}'>{$next}</a>\n";
	$last = "<a href='./?page=Media&id={$media_id}&p={$total_pages}'>{$last}</a>\n";
}
 
print ("{$first} | {$prev} | {$next} | {$last}");

}[/php]

i have tried to update the code from that site best i can, im still learning.

thanks

Sorry i noticed i did not close the isset function…
i have tried to get this working and still getting no output and no error…

also nothing else runs after wherever the falt is in this function…

[code]function media_index($purifier, $media_id) {
$files = array();
$files = glob(MEDIA_BASEDIR.$media_id.’/*.txt’);

$records_per_page = 6;
$total_records = count($files);
$total_pages = ceil($total_records / $records_per_page);

if (isset($_GET['p'])) {
	$current_page = (int)$_GET['p'];
}
if ($current_page<1 || $current_page>$total_pages) {
	$current_page = 1;
}

usort($files, function($x, $y) {
	return $x < $y;
	});
	
$media_content = array_splice($files, ($current_page-1)*$records_per_page, $records_per_page);

for ($col=0; $col>$records_per_page; $col++) {
	foreach ($media_content as $media) {
		
		$line = file($media);
		$media_title = ($purifier->purify($line[0]));
		$date_time = basename($media);
		$date_time = basename($media, '.txt');
		$content = file_get_contents($media);
		$content = str_replace($media_title,'',$content);
		
		thememediatop($media_title, $media_cat, $media_id);
		print_r ($purifier->purify($content));
		thememediabottom($date_time);
	}
}[/code]

thanks

See what this does for you. You will have to modify it for your purpose.

[php]<?php
/**

//----------------------------------------------------------------------------
// Generate Sample Data
//----------------------------------------------------------------------------

foreach (range(1, 40) as $value)
{
$data[] = array(
‘Product’ => 'Product ’ . $value,
‘Price’ => rand(10, 100),
‘Quantity’ => rand(1, 25)
);
}

//----------------------------------------------------------------------------
// Number Of Chunks
//----------------------------------------------------------------------------

$number_of_chunks = 4;

//----------------------------------------------------------------------------
// Break Array Into Chunks
//----------------------------------------------------------------------------

$data = array_chunk($data, $number_of_chunks);

//----------------------------------------------------------------------------
// Get Page Count
//----------------------------------------------------------------------------

$pagecount = count($data);

//----------------------------------------------------------------------------
// Make sure page is set and is numeric
//----------------------------------------------------------------------------

if (isset($_GET[‘p’]) && (is_numeric($_GET[‘p’])))
{
if ($_GET[‘p’] > $pagecount)
{
die(‘Error: Page Does Not Exist’);
}
$i = $_GET[‘p’] - 1;
}
else
{
$i = 0;
}

//----------------------------------------------------------------------------
// Display array_chunk Data
//----------------------------------------------------------------------------

echo ‘

’;
print_r($data[$i]);
echo ‘
’;

//----------------------------------------------------------------------------
// Display array_chunk Pagination
//----------------------------------------------------------------------------

for ($i = 1; $i <= $pagecount; $i++)
{
echo "<a href=’{$_SERVER[‘SCRIPT_NAME’]}?p=$i’>$i | ";
}
?>[/php]

cool, i will post back when i get it all together and working!

Thanks :slight_smile:

Well its not going too well :frowning:

Once I see it working it will all make sence to me and i will learn more from it…

heres what i have done so far with no luck

[code]function media_index($purifier, $media_id) {

foreach (range(1, 40) as $value) {
	$data[] = glob(MEDIA_BASEDIR.$media_id.'/*.txt');
	$data[] = array($data => $data . $value);
	foreach ($recent_content as $media) {
		$line = file($media);
		$media_title = ($purifier->purify($line[0]));
		$date_time = basename($media);
		$date_time = basename($media, '.txt');
		$content = file_get_contents($media);
		$content = str_replace($media_title,'',$content);
		thememediatop($media_title, $media_cat, $media_id);
		$clean_data = ($purifier->purify($content));
		thememediabottom($date_time);
	}
}

usort($data, function($x, $y) {
	return $x < $y;
	});
	
$number_of_chunks = 4;
$data = array_chunk($data, $number_of_chunks);
$pagecount = count($data);

if (isset($_GET['p']) && (is_numeric($_GET['p']))) {
	if ($_GET['p'] > $pagecount) {
		die('<span style="color:#FF0000">Error: Page Does Not Exist</span>');
    }
	$i = $_GET['p'] - 1;
} else {
	$i = 0;
}

print_r ($clean_data);

for ($i = 1; $i <= $pagecount; $i++) {
	print "<a href='./?page=Media&id={$media_id}&p={$i}'>{$i}</a> | ";
}

}[/code]

i can see what its doing but i cant understand how to implement my array into this because my array is just a list of files…

any clue as to how i impement this?

[code] $files = array();
$files = glob(MEDIA_BASEDIR.$media_id.’/*.txt’);

usort($files, function($x, $y) {
	return $x < $y;
	});

foreach ($media_content as $media) {
		
		$line = file($media);
		$media_title = ($purifier->purify($line[0]));
		$date_time = basename($media);
		$date_time = basename($media, '.txt');
		$content = file_get_contents($media);
		$content = str_replace($media_title,'',$content);
		
		thememediatop($media_title, $media_cat, $media_id);
		print_r ($purifier->purify($content));
		thememediabottom($date_time);
}[/code]

Thanks

Hi i found one…

[code]$folders = glob("*", GLOB_ONLYDIR);
usort($folders, function ($a, $b) {
return filemtime($b) - filemtime($a);
});

$record_count = 20;
$total_pages = ceil(count($folders)/$record_count);
$page = $_REQUEST[‘page’]; ///make it dyanamic :: page num
$offset = ($page-1)*$record_count;
$folders_filter = array_slice($folders, $offset,$record_count);

foreach ($folders_filter as $folder) {
echo "$folder
";
}

if($total_pages > 1){
if($page != 1){
echo ‘Prev’;
}
if($page != $total_pages){
echo ‘Next’;
}
}[/code]

i had to update it best i can… here is what i have working so far :slight_smile:

[code]function media_index($purifier, $media_id) {
if (isset($_GET[‘p’]) && (is_numeric($_GET[‘p’]))) {
$p = htmlentities($_GET[‘p’], ENT_QUOTES | ENT_SUBSTITUTE, ‘UTF-8’);
} else {
$p = 1;
}
$media_files = array();
$media_files = glob(MEDIA_BASEDIR.$media_id.’/.txt’);
usort($media_files, function($x, $y) {
return $x < $y;
});
$row_count = 1;
$total_pages = ceil(count($media_files)/$row_count);
$offset = ($p-1)
$row_count;
$media_files = array_slice($media_files, $offset, $row_count);

foreach ($media_files as $media) {
	$line = file($media);
	$media_title = ($purifier->purify($line[0]));
	$date_time = basename($media);
	$date_time = basename($media, '.txt');
	$content = file_get_contents($media);
	$content = str_replace($media_title,'',$content);
	thememediatop($media_title, $media_id, $media_id);
	print_r ($purifier->purify($content));
	thememediabottom($date_time);
}
if($total_pages > 1) {
	print ("<div style='text-align:center;'><p>Currently viewing page {$p} of {$total_pages}</p>");
	if($p != 1) {
		print ("&loarr;&nbsp;<a href='./?page=Media&id={$media_id}&p=".($p-1)."'>Prev</a>&nbsp;&hoarr;");
	}
	if($p != $total_pages) {
		print ("&nbsp;<a href='./?page=Media&id={$media_id}&p=".($p+1)."'>Next</a>&nbsp;&roarr;");
	}
	print ("</div>");
}

}[/code]

how dose it look? i think the code was old so not sure if ive updated it enough

thanks

Hi sorry to spam befor i get my replys i have been cleaning it up

this is what i have now…

[php]
function media_index($purifier, $media_id) {
$media_files = array();
$media_files = glob(MEDIA_BASEDIR.$media_id.’/.txt’);
usort($media_files, function($x, $y) {
return $x < $y;
});
$row_count = 3;
$total_pages = ceil(count($media_files)/$row_count);
if (isset($_GET[‘p’]) && (is_numeric($_GET[‘p’]))) {
$p = htmlentities($_GET[‘p’], ENT_QUOTES | ENT_SUBSTITUTE, ‘UTF-8’);
if ($p > $total_pages) {
$p = 1;
} else if ($p < $total_pages) {
$p = 1;
}
} else {
$p = 1;
}
$offset = ($p-1)
$row_count;
$media_files = array_slice($media_files, $offset, $row_count);
foreach ($media_files as $media) {
$line = file($media);
$media_title = ($purifier->purify($line[0]));
$date_time = basename($media);
$date_time = basename($media, ‘.txt’);
$content = file_get_contents($media);
$content = str_replace($media_title,’’,$content);
thememediatop($media_title, $media_id, $media_id);
print_r ($purifier->purify($content));
thememediabottom($date_time);
}
if ($total_pages > 1) {
print (“

Currently viewing page {$p} of {$total_pages}

\n”);
if($p != 1) {
print (“⇽ Prev  \n”);
}
if($p != $total_pages) {
print ("  Next ⇾\n");
}
print ("
\n");
}
}
[/php]

ill be made up if you aprove this :slight_smile:
thanks

Sorry again, ive just made more changes

made it a propper pagination by adding the numbers so it looks like

prev 12345 next

here it is…

[php]function media_index($purifier, $media_id) {
$media_files = array();
$media_files = glob(MEDIA_BASEDIR.$media_id.’/.txt’);
usort($media_files, function($x, $y) {
return $x < $y;
});
$media_pages = $media_files;
$media_pages = array_combine(range(1, count($media_pages)), $media_pages);
$row_count = 1;
$total_pages = ceil(count($media_files)/$row_count);
if (isset($_GET[‘p’]) && (is_numeric($_GET[‘p’]))) {
$p = htmlentities($_GET[‘p’], ENT_QUOTES | ENT_SUBSTITUTE, ‘UTF-8’);
if ($p > $total_pages) {
$p = 1;
}
} else {
$p = 1;
}
$offset = ($p-1)
$row_count;
$media_files = array_slice($media_files, $offset, $row_count);
foreach ($media_files as $media) {
$line = file($media);
$media_title = ($purifier->purify($line[0]));
$date_time = basename($media);
$date_time = basename($media, ‘.txt’);
$content = file_get_contents($media);
$content = str_replace($media_title,’’,$content);
thememediatop($media_title, $media_id, $media_id);
print_r ($purifier->purify($content));
thememediabottom($date_time);
}
print ("

");
if ($total_pages > 1) {
print (“

Currently viewing page {$p} of {$total_pages}

\n”);
if ($p != 1) {
print (“⇽ Prev  \n”);
}
$i = 0;
foreach ($media_pages as $pages=>$page) {
if ($p != $pages) {
$link_name = $pages;
} else {
$link_name = ("{$pages}");
}
print (" {$link_name} \n");
$i++;
}
if ($p != $total_pages) {
print ("  Next ⇾\n");
}
}
print ("
\n");
}[/php]

thanks again !! and sorry for spamming haha

im a noob i found a error with the numbers i added so i made a new one that was actualy easyer to write and smaller…

[php]function media_index($purifier, $media_id) {
$media_files = array();
$media_files = glob(MEDIA_BASEDIR.$media_id.’/.txt’);
usort($media_files, function($x, $y) {
return $x < $y;
});
$row_count = 3;
$total_pages = ceil(count($media_files)/$row_count);
if (isset($_GET[‘p’]) && (is_numeric($_GET[‘p’]))) {
$p = htmlentities($_GET[‘p’], ENT_QUOTES | ENT_SUBSTITUTE, ‘UTF-8’);
if ($p > $total_pages) {
$p = 1;
}
} else {
$p = 1;
}
$offset = ($p-1)
$row_count;
$media_files = array_slice($media_files, $offset, $row_count);
foreach ($media_files as $media) {
$line = file($media);
$media_title = ($purifier->purify($line[0]));
$date_time = basename($media);
$date_time = basename($media, ‘.txt’);
$content = file_get_contents($media);
$content = str_replace($media_title,’’,$content);
thememediatop($media_title, $media_id, $media_id);
print_r ($purifier->purify($content));
thememediabottom($date_time);
}

print ("<div style='text-align:center;'>");
if ($total_pages > 1) {
	print ("<p>Currently&nbsp;viewing&nbsp;page&nbsp;{$p}&nbsp;of&nbsp;{$total_pages}</p>\n");
	if ($p != 1) {
		print ("&loarr;&nbsp;<a href='./?page=Media&amp;id={$media_id}&amp;p=".($p-1)."'>Prev</a>&nbsp;&nbsp;\n");
	}
	$media_pages = range(1, $total_pages);
	foreach ($media_pages as $pages) {
		if ($p != $pages) {
			$link_name = $pages;
		} else {
			$link_name = ("<strong>{$pages}</strong>");
		}
		print ("&nbsp;<a href='./?page=Media&amp;id={$media_id}&amp;p={$pages}'>{$link_name}</a>&nbsp;\n");
	}
	if ($p != $total_pages) {
		print ("&nbsp;&nbsp;<a href='./?page=Media&amp;id={$media_id}&amp;p=".($p+1)."'>Next</a>&nbsp;&roarr;\n");
	}
}
print ("</div>\n");

}[/php]

here is what i just wrote this…
[php]
$media_pages = range(1, $total_pages);
foreach ($media_pages as $pages) {
if ($p != $pages) {
$link_name = $pages;
} else {
$link_name = ("{$pages}");
}
print (" {$link_name} \n");
}
[/php]

to replace this…

[php]$media_pages = $media_files;
$media_pages = array_combine(range(1, count($media_pages)), $media_pages);

$i = 0;
foreach ($media_pages as $pages=>$page) {
if ($p != $pages) {
$link_name = $pages;
} else {
$link_name = ("{$pages}");
}
print (" {$link_name} \n");
$i++;
}[/php]

honestly ive been on this all day i wont blame you for going mad about the spam :slight_smile: cant help it :slight_smile:

Thanks.

Is there a question here?

Not anymore…

If anything is my last post safe and clean enough?

Thanks

This;

[php]function media_index($purifier, $media_id) {
$media_files = array();
$media_files = glob(MEDIA_BASEDIR.$media_id.’/.txt’);
usort($media_files, function($x, $y) {
return $x < $y;
});
$row_count = 3;
$total_pages = ceil(count($media_files)/$row_count);
if (isset($_GET[‘p’]) && (is_numeric($_GET[‘p’]))) {
$p = htmlentities($_GET[‘p’], ENT_QUOTES | ENT_SUBSTITUTE, ‘UTF-8’);
if ($p > $total_pages) {
$p = 1;
}
} else {
$p = 1;
}
$offset = ($p-1)
$row_count;
$media_files = array_slice($media_files, $offset, $row_count);
foreach ($media_files as $media) {
$line = file($media);
$media_title = ($purifier->purify($line[0]));
$date_time = basename($media);
$date_time = basename($media, ‘.txt’);
$content = file_get_contents($media);
$content = str_replace($media_title,’’,$content);
thememediatop($media_title, $media_id, $media_id);
print_r ($purifier->purify($content));
thememediabottom($date_time);
}

print ("<div style='text-align:center;'>");
if ($total_pages > 1) {
	print ("<p>Currently&nbsp;viewing&nbsp;page&nbsp;{$p}&nbsp;of&nbsp;{$total_pages}</p>\n");
	if ($p != 1) {
		print ("&loarr;&nbsp;<a href='./?page=Media&amp;id={$media_id}&amp;p=".($p-1)."'>Prev</a>&nbsp;&nbsp;\n");
	}
	$media_pages = range(1, $total_pages);
	foreach ($media_pages as $pages) {
		if ($p != $pages) {
			$link_name = $pages;
		} else {
			$link_name = ("<strong>{$pages}</strong>");
		}
		print ("&nbsp;<a href='./?page=Media&amp;id={$media_id}&amp;p={$pages}'>{$link_name}</a>&nbsp;\n");
	}
	if ($p != $total_pages) {
		print ("&nbsp;&nbsp;<a href='./?page=Media&amp;id={$media_id}&amp;p=".($p+1)."'>Next</a>&nbsp;&roarr;\n");
	}
}
print ("</div>\n");

}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service