Show 20 comments in every 15 comments x 2 [on hold]
I have a challenge here.
Imagine a base function called "com15(page)" that returns 15 comments per
page, but i need to show 20 comments in order per page using a second
function called "com20(page)".
To show the 20 comments i need to call the com20(1) using com15(1) and
com15(2) then sort and show the results.
how would you code the function com20(page)?
This is what i got so far...
Page 1 ( 1 - 20 ) ok page 2 ( 21 - 40 ) ok page 3 the problem starts....
<?
error_reporting(E_ALL);
ini_set('display_errors', 1);
//-----------------> com15
function com15($page = 1) {
$result = array();
$max = 15 * $page;
$min = $max - 15;
for($i=$min;$i<$max;$i++) {
$n = $i + 1;
$comment = "<div style='margin:10px;''>Comment:
<strong>".$n."</strong></div>";
array_push($result, $comment);
}
return $result;
}
//-----------------> com20
function com20($page = 1) {
$result = array();
$append = array();
$batch1 = com15($page);
$batch2 = com15($page + 1);
$a = 0;
for($i=0;$i<20;$i++) {
if($page>1) {
if($i < 10) {
$b = $i + 5;
$comment = $batch1[$b];
array_push($result, $comment);
} else {
$comment = $batch2[$a];
array_push($result, $comment);
$a++;
}
} else {
if($i < 15) {
$comment = $batch1[$i];
array_push($result, $comment);
} else {
$comment = $batch2[$a];
array_push($result, $comment);
$a++;
}
}
}
foreach($result as $comment) {
echo $comment;
}
}
//-----------> Init Pagination
$p = 1;
extract($_GET);
com20($p);
echo "Page: ";
for($t = 1;$t<7;$t++) {
echo '<a style="margin:10px;" href="?p='.$t.'">'.$t.'</a>';
}
?>
No comments:
Post a Comment