Pagination with part in php - php

I try to implement a pagination to my website in php
This is what I want
If I have 42 Pages and I am on the page 6
[1][2][3][4][5][6][7][8][9][10][20][30][40][42]
or I am on the page 23
[1][10][20][21][22][23][24][25][26][27][28][29][30][40][42]
This what I did
$nbPage = 42;
$actualPage = 23;
$min_part_pagination = floor($actualPage / 10)*10;
$max_part_pagination = ceil($actualPage / 10 )*10;
$last_part_pagination = floor($nbPage /10 )*10;
$first_part_pagination = 10;
$count = 1;
for($j = 1 ; $j <= $nbPage ; $j++){
if($j % 10){
if($pageActuelle == $count){
$max_part_pagination = ($max_part_pagination < $nbPage) ? $max_part_pagination : $nbPage+1;
for($k = $min_part_pagination+1 ; $k < $max_part_pagination ; $k++ ){
if($k == 1){
echo "<a href=http://dev.blablabl.fr/public_html/>".$k."</a> ";
}else{
echo "".$k." ";
}
}
}
elseif($count == $nbPage && $pageActuelle < $last_tranche_pagination ){
echo "".$count." ";
}elseif($count == 1 && $pageActuelle >= $first_tranche_pagination ){
echo "<a href=http://dev.blablabl.fr/public_html/>1</a> ";
}
}else{
if($pageActuelle == $count){
if($max_part_pagination == $max_part_pagination ){
$max_part_pagination += 10;
}else{
$max_part_pagination = $last_tranche_pagination;
}
echo 'min_part_pagination > '.$min_part_pagination.'<br />';
echo 'max_part_pagination > '.$max_part_pagination.'<br />';
for($k = $min_part_pagination ; $k < $max_part_pagination ; $k++){
echo "".$k." ";
}
}else{
echo "".$count." ";
}
}
My problem is If I am on the page 41 It show [40][41][42][43][44][45][46][47][48][49][50]
It doesn't stop at 42

Related

Creating two triangle using php

Someone please help me to create a two triangle patter using PHP. I'm already code but the output didn't as expected below.
expected output
My code:
function generatePattern($num) {
for ($id1 = 0; $id1 <= $num; $id1 = $id1 + 1) {
for ($id2 = $num; $id2 >= $id1; $id2 = $id2 - 1) {
print(' ');
}
for ($id3 = 1; $id3 <= $id1; $id3 = $id3 + 1) {
if ($id3 % 4 == 3) {
echo "o";
} else if ($id3 % 2 == 1) {
echo "x";
} else {
echo " ";
}
}
echo "\n";
}
for ($id1 = 0; $id1 <= $num-1; $id1 = $id1 + 1) {
echo str_repeat(' ', $num - 1);
for($id3 = $num-1; $id3 >= $id1; $id3 = $id3 - 1){
if ($id3 % 4 == 3) {
echo "o";
} else if ($id3 % 2 == 1) {
if ($id1 % 4 == 3) {
echo "o";
} else if ($id1 % 2 == 0) {
echo " ";
} else if ($id1 % 2 == 1) {
echo "x";
} else {
echo "x";
}
} else if ($id3 == $id1){
echo "x";
} else {
echo " ";
}
}
echo "\n";
}
}
generatePattern(4);
And my current output like this (the bottom triangle still messed up)
output
Do the required changes for space between o and x
function generatePattern($num) {
if($num % 2 == 0)
{
$num1 = $num + 1;
}else{
$num1 = $num;
$num = $num - 1;
}
for ($id1 = 1; $id1 <= $num; $id1++) {
for ($id2 = $num; $id2 >= $id1; $id2--) {
print(' ');
}
for ($id3 = 1; $id3 <= $id1; $id3++) {
if ($id3 % 4 == 3) {
echo "o";
} else if ($id3 % 2 == 1) {
echo "x";
} else {
echo " ";
}
}
echo "\n";
}
$str = str_repeat('x o ', ceil(($num1*2)/4));
echo substr($str, 0, $num1*2);
echo "\n";
$j = $num;
for($id1 = $num; $id1 >=1; $id1 = $id1 - 2)
{
for($id2 = 2; $id2 >= 1; $id2--)
{
if($j % 2 == 0)
{
$pattern = [' ', 'x', ' ', 'o',];
}else{
$pattern = [' ', 'o', ' ', 'x',];
}
echo str_repeat(' ', ($id2%2 == 0) ? $num: $num - 1);
$design = implode('', $pattern);
do{
$design .= implode('', $pattern);
}while(strlen($design) < $id1);
echo substr($design, 0, $id1);
echo "\n";
}
$j--;
}
}
generatePattern(14);

MySQL/PHP - page number pagination Only show 10 pages at times

I am having difficulties with my MYSQL / PHP dashboard. - Currently i am having 50 pages, but currently they are all showing on the same page.
http://imgur.com/wDfTWUa - as you can see in the attached file. - I only want 10 pages to be shown, and be able to click through the rest of the pages without seeing 4 rows of pages.
Exsampel <- 2 3 4 5 6 7 8 9 10 -> when you are on ?page=1, if you are on page ?page=10 <- 11 12 13 14 15 16 17 18 19->
Hope you can help me.
Code:
<?php
include 'config.php';
$sidenr = $_GET['page'];
$sidenr2 = ($sidenr -1) * 10;
echo $sidenr2;
echo "<br><br>";
$query100 = mysqli_query($conn, "SELECT * FROM `test` LIMIT $sidenr2,10") or die(mysqli_error($conn));
while($row = mysqli_fetch_array($query100))
{
echo $row['id']."<br>";
}
$result = mysqli_query($conn, "SELECT * FROM test");
$num_rows = mysqli_num_rows($result);
$sideantal = $num_rows / 10;
echo "Der skal være antal sider: ". $sideantal;
echo "<br><br>antal rækker ". $num_rows . "<br><br>";
?>
<br><br>
<?php
for ($number = 1; $number <= $sideantal; $number++) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
?>
function getPageRange($current, $max, $total_pages = 10) {
$desired_pages = $max < $total_pages ? $max : $total_pages;
$middle = ceil($desired_pages/2);
if ($current <= $middle){
return [1, $desired_pages];
}
if ($current > $middle && $current <= ($max - $middle)) {
return [
$current - $middle,
$current + $middle
];
}
if ($current <= $max ) {
return [
$current - ($desired_pages - 1),
$max
];
}
}
list($min,$max) = getPageRange($sidenr, $sideantal);
foreach (range($min, $max) as $number) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
try to change this:
for ($number = 1; $number <= $sideantal; $number++) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
</li>";
}
to this:
for ($number = 1; $number <= $sideantal; $number++) {
if (($number > $_GET['page']) && ($number <= $_GET['page'] + 10)) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
</li>";
}
}
You can try the following code:
for ($number = 1; $number <= $sideantal; $number++) {
/** If the loop count is greater than the current page but less than current page plus 10 */
if ( ($number > $_GET['page'] && ($number < ($_GET['page'] + 10)))) $is_valid = true;
/** If the loop count is less than the current page but greater than current page -10 and the current page is the last page */
if ($number < $_GET['page'] && $_GET['page'] == $sideantal && $number > ($_GET['page'] - 10)) $is_valid = true;
else $is_valid = false;
if ($is_valid) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
}

Why isn't count counting anything?

I'm having trouble making a simple count work. Right now 0 is constantly displayed when I run the code and I know it's because I set count to 0. But it should be displaying the number of times "Fizz" is displayed.
I'm sure it's simple but I just can't see what!
public function __construct($firstParam, $secondParam, $firstSound = "Fizz", $secondSound = "Buzz", $numbers = 100) {
$this->firstParam = $firstParam;
$this->secondParam = $secondParam;
$this->firstSound = $firstSound;
$this->secondSound = $secondSound;
$this->numbers = $numbers;
$this->numsArray = $numsArray;
}
public function __toString() {
$count = 0;
for ($i = 0; $i < count($this->numsArray); $i++){
$val = $this->numsArray[$i];
if ($val == $this->firstSound) {
$count++;
}
}
$print = "Number of Fizzes: ".$count;
return $print;
}
public function execute() {
$this->numsArray = array();
if ($this->secondParam > $this->firstParam) {
for ($i = 1; $i <= $this->numbers; $i++){
if ($i % $this->firstParam == 0 && $i % $this->secondParam == 0) {
$this->numsArray[] = "\n".$this->firstSound.$this->secondSound."\n";
} elseif ($i % $this->firstParam == 0) {
$this->numsArray[] = "\n".$this->firstSound."\n";
} elseif ($i % $this->secondParam == 0) {
$this->numsArray[] = "\n".$this->secondSound."\n";
} else {
$this->numsArray[] = "\n".$i."\n";
}
echo $this->numsArray[$i-1];
}
} else {
echo "\n".' First Number Bigger Than Second '."\n";
}
}
In your execute you are not assigning the values to numsArray[i] also you inject new line characters that will not match the equality you just when checking $val. Also I notice you use zero index to check them and index 1 to load it. Change execute to:
for ($i = 0; $i < $this->numbers; $i++){
if ($i % $this->firstParam == 0 && $i % $this->secondParam == 0) {
$this->numsArray[i] = $this->firstSound.$this->secondSound;
} elseif ($i % $this->firstParam == 0) {
$this->numsArray[i] = $this->firstSound;
} elseif ($i % $this->secondParam == 0) {
$this->numsArray[i] = $this->secondSound;
} else {
$this->numsArray[i] = $i;
}
echo $this->numsArray[$i];
This is a better binary string comparison for php...
if (strcmp($val, $this->firstSound) == 0)

Displaying retrieve data in different table

Okay, what I want is to display retrieved data in a table form database, I want my table to be limit in 5 data per table. I need the tables to be display horizontally.
Like this:
while($row1 = sqlFetchArray($row))
{ $ctr = 0;
$ctr+1;
if($ctr=1 || $ctr<=5)
{
$html .='<table width="100px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
if($ctr=6 || $ctr<=10)
{
$html .='<table width="110px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
if($ctr=11 || $ctr<=15)
{
$html .='<table width="120px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
}
Output:
1 6
2 7
3 8
4 9
5 10
This should work for you:
(But the function sqlFetchArray as to work like mysql_fetch_array)
<?php
$limit = 15;
for($numberCounter = 0; $numberCounter < $numberCount = mysql_num_rows($row); $numberCounter++) {
$count = 0;
if($numberCounter >= mysql_num_rows($row))
break;
if ($count == 0 || $count % $limit == 0)
echo "<table width='100px' style='float:left;'>";
while ($row1 = sqlFetchArray($row) && $count < $limit) {
if($numberCounter >= mysql_num_rows($row))
break;
echo "<tr><td>" . $row1[$numberCounter] . "</td></tr>";
$count++;
$numberCounter++;
}
if($count == 0 || $count % $limit == 0)
echo "</table>";
}
?>
As an example:
<?php
$test = range(1, 43);
$limit = 15;
for($numberCounter = 0; $numberCounter < $numberCount = count($test); $numberCounter++) {
$count = 0;
if($numberCounter >= count($test))
break;
if ($count == 0 || $count % $limit == 0)
echo "<table width='100px' style='float:left;'>";
while ($count < $limit) {
if($numberCounter >= count($test))
break;
echo "<tr><td>" . $test[$numberCounter] . "</td></tr>";
$count++;
$numberCounter++;
}
if($count == 0 || $count % $limit == 0)
echo "</table>";
}
?>

variable counter in php

Good day, I was tasked to make a variable counter in php.
Im stuck at the last stage. And that is to display the total number of positive values. I used the count() in php, but it's not giving me the total number. is there a way that i can get the summation of the value that is is count()?
if ($start < $end)
{
for($ctr = $start; $ctr<=$end; $ctr++)
{
if ($ctr == 0)
{
echo " <br/ >there is a zero <br/ >";
}
else if ($ctr <=0)
{
echo count($ctr) . " negative value" ;
}
else if($ctr >=0)
{
echo count($ctr) . " positive value ";
}
}
$zero = $pos = $neg = 0;
for($ctr = $start; $ctr<=$end; $ctr++)
{
switch(true){
case $ctr==0: $zero++; break;
case $ctr<0: $neg++; break;
case $ctr>0: $pos++; break;
}
}
if($zero)
echo "there is a zero<br/>";
if($pos)
echo "$pos positive values<br/>" ;
if($neg)
echo "$neg negative values<br/>" ;
Better way is without any loop:
function test($start, $end) {
echo "Test of [$start, $end]:\n";
if ($isThereZero = $start * $end <= 0) {
echo "There is zero\n";
}
if ($start < 0) {
$negatives = $isThereZero ? -$start : -$start+$end;
echo "$negatives negative values\n";
}
if ($end > 0) {
$positives = $isThereZero ? $end : $end - $start;
echo ($positives)." positive values\n";
}
}
test(-12, 5);
test(-12, -5);
test(5, 12);
test(0, 0);
Try that :
$zer = 0;
$pos = 0;
$neg = 0;
if ($start < $end)
{
for($ctr = $start; $ctr<=$end; $ctr++)
{
if ($ctr == 0)
{
$zer++;
}
else if ($ctr < 0)
{
$neg++;
}
else if($ctr > 0)
{
$pos++;
}
}
echo $zer . " zero value" ;
echo $neg . " negative value" ;
echo $pos . " positive value ";
Just for fun, this could be done without a loop. This is a bit tricky and also a bit far from the initial question :
Demo
$start = -3;
$end = 5;
echo ($start <= 0 && $end >= 0 ? "1" : "0")." zero value\n";
echo ($start < 0 && $start <= $end ? (-$start-($end > -1 ? 0 : -$end-1)) : "0"). " negative value\n";
echo ($end > 0 && $start <= $end ? ($end-($start < 1 ? 0 : $start-1)) : "0"). " positive value\n";
else if($ctr >=0)
{
$cnt++; //or $cnt+=1;
}
Then use $cnt++ for no of count for positive vales

Categories