Reversing page navigation on PHP - php

Can anyone help me with reversing this PHP page navigation?
Current script setting shows this format: [0] | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 ... 14 • Forward > • End >>>
But I really need it to reverse for this format: [14] | 13 | 12 | 11| 10 | 9 | 8 | 7 | 6 ... 0 • Back > • Start >>>
Here is the PHP code:
<?
$onpage = 10; // on page
function page(){
if(empty($_GET["page"])){
$page = 0;
} else {
if(!is_numeric($_GET["page"])) die("Bad page number!");
$page = $_GET["page"];
}
return $page;
}
function navigation($onpage, $page){
//----------------
$countt = 150;
$cnt=$countt; // total amount of entries
$rpp=$onpage; // total entries per page
$rad=4; // amount of links to show near current page (2 left + 2 right + current page = total 5)
$links=$rad*2+1;
$pages=ceil($cnt/$rpp);
if ($page>0) { echo "<<< Start <font color='#CCCCCC'>•</font> < Back <font color='#CCCCCC'>•</font>"; }
$start=$page-$rad;
if ($start>$pages-$links) { $start=$pages-$links; }
if ($start<0) { $start=0; }
$end=$start+$links;
if ($end>$pages) { $end=$pages; }
for ($i=$start; $i<$end; $i++) {
echo " ";
if ($i==$page) {
echo "[";
} else {
echo "<a href=\"?page=$i\">";
}
echo $i;
if ($i==$page) {
echo "]";
} else {
echo "</a>";
}
if ($i!=($end-1)) { echo " <font color='#CCCCCC'>|</font>"; }
}
if ($pages>$links&&$page<($pages-$rad-1)) { echo " ... ".($pages-1).""; }
if ($page<$pages-1) { echo " <font color='#CCCCCC'>•</font> Forward > <font color='#CCCCCC'>•</font> End >>>"; }
}
$page = page(); // detect page
$navigation = navigation($onpage, $page); // detect navigation
?>

for ($i=$end; $i>$start; $i--) {
echo " ";
if ($i==$page) {
echo "[";
} else {
echo "<a href=\"?page=$i\">";
}
//othercode
}

I found the solution for my question. Here is a link to reversed page navigation: php page navigation by serial number

Related

for loop if else Break php

I'd like to add page like this :
foreach($list->result() as $row){
if($no % 12 == 0 ) {
// Add Page Break Here
echo "<p>break</p>";
}
if($no % 6 == 0 ) {
//// $pdf->Ln();
echo "<br><br><br>";
}
echo $no;
echo "<br>";
$no++;
}
I want if this data > 12 page break , if this data < 12 not add page but if data > 6 add more space
My problem is, it's always like this :
1
2
3
4
5
space
space
space
6
7
8
9
10
11
break
space
space
space
12
This can be easily fixed by:
foreach ($list->result() as $row)
{
if ($row['SOMETHING??'] >= 12)
{
/// Add Page Break Here
echo "<p>break</p>";
}
if ($row['SOMETHING??'] >= 6 && $row['SOMETHING??'] < 12)
{
//// $pdf->Ln();
echo "<br><br><br>";
}
echo $no;
echo "<br>";
$no++;
}
Look carefully at the comparison operators which are being used.
Documentation:
http://php.net/manual/en/language.operators.comparison.php
You just need to re-arrange the order of what you are doing, you are outputting the breaks Before you output the line they relate to
foreach($list->result() as $row){
echo $no;
echo "<br>";
if ($no % 12 == 0 ) {
// Add Page Break Here
echo "<p>break</p>";
}
if ($no % 6 == 0 ) {
//// $pdf->Ln();
echo "<br><br><br>";
}
$no++;
}
You may also need to stop the %6 test doing something when the %12 line will have done something like so
foreach($list->result() as $row){
echo $no;
echo "<br>";
if ($no % 12 == 0 ) {
// Add Page Break Here
echo "<p>break</p>";
}
if ($no % 6 == 0 && $no % 12 != 0) {
//// $pdf->Ln();
echo "<br><br><br>";
}
$no++;
}

I want the following output using for loop in php

I want to display the output as like this
1 1
12 21
123 321
1234 4321
1234554321
Here is my php code
for($i=1;$i <= 5;$i++)
{
for($j=1;$j<=$i;$j++)
{
// print the result
echo "$j";
}
for($y=$i;$y<=$i;$y++)
{
echo ' ';
}
for($k=$i;$k>=1;$k--)
{
// print the result
echo " $k";}
echo "<br/>";
}
But I got the output like this
1 1
12 2 1
123 3 2 1
1234 4 3 2 1
12345 5 4 3 2 1
Please help me get an output like above.
try
for($i=1;$i <= 5;$i++) {
for($j=1;$j<=$i;$j++) {
echo "$j";
}
for($y=0;$y<(5-$i)*4;$y++) {
echo ' ';
}
for($l=$i;$l>0;$l--) {
echo "$l";
}
echo "<br/>";
}
output:-
1 1
12 21
123 321
1234 4321
1234554321
For browser view :- for($y=0;$y<(5-$i)*4;$y++) else correct way to going is for($y=0;$y<(5-$i)*2;$y++)
Did you ever here about the PHP functions str_repeat and range?
http://php.net/manual/en/function.str-repeat.php.
http://php.net/manual/en/function.range.php
With it you can print your character like this:
echo str_repeat(' ', (5 -$i) *4);
Complete code
$count = 5; // count oft rows and oft iterated numbers
for ($i = 1; $i <= $count; $i++) {
echo implode('', range(1, $i));
echo str_repeat(' ', ($count -$i) *4);
echo implode('', array_reverse(range(1, $i)));
echo '<br />';
}
How about this?
for($i=1;$i<=5;$i++){
echo substr("12345", 0, $i);
echo str_repeat(5-$i, ' ');
echo str_reverse(substr("12345", 0, $i));
echo '<br>';
}
Try this:
for($i=1;$i <= 5;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo "$j";
}
for($y=1;$y<=10-(2*$i);$y++)
{
echo ' ';
}
for($k=$i;$k>=1;$k--)
{
// print the result
echo "$k";
}
echo "<br/>";
}
OUTPUT:
1 1
12 21
123 321
1234 4321
1234554321
How about this with $y<= 2 * (5 - i),
for($i=1;$i <= 5;$i++) {
for($j=1;$j<=$i;$j++) {
echo "$j";
}
for($y=1;$y<= 2 * (5 - i); $y++) {
echo ' ';
}
for($k=$i;$k>=1;$k--) {
echo "$k";
}
echo "<br/>";
}
for ($counter=1;$counter<= 5;$conter++)
{
for ($j=1;$j<=$counter;$j++)
{
echo "$j";
}
for ($y=1;$y<=10-(2*$counter);$y++)
{
echo ' ';
}
for ($k=$counter;$k>=1;$k--)
{
echo "$k";
}
echo "<br/>";
}

Displaying Rows of HTML Elements in Vertical Rows

I want to show a list of categories in my Virtuemart webshop vertically sorted the same way as shown in this demonstration:
http://www.inkplant.com/code/mysql-vertical-sort.php
So I borrowed the code:
<?php
$cols = 4; //number of columns, you can set this to any positive integer
$values = array();
$result = mysql_query("SELECT * FROM states ORDER BY name");
$numrows = mysql_num_rows($result);
$rows_per_col = ceil($numrows / $cols);
for ($c=1;$c<=$cols;$c++) { $values['col_'.$c] = array(); }
$c = 1;
$r = 1;
while ($row = mysql_fetch_assoc($result)) {
$values['col_'.$c][$r] = stripslashes($row['name']);
if ($r == $rows_per_col) { $c++; $r = 1; } else { $r++; }
}
echo "<table>" ;
for ($r=1;$r<=$rows_per_col;$r++) {
echo "<tr>" ;
for ($c=1;$c<=$cols;$c++) { echo "<td>".$values['col_'.$c][$r]."</td>" ; }
echo "</tr>" ;
}
echo "</table>" ;
unset($values);
?>
I then tried to modify it in my Virtuemart category template file with this result:
<?php
$cols = 3; //number of columns, you can set this to any positive integer
$values = array();
$numrows = $precounterdigit;
$rows_per_col = ceil($numrows / $cols);
for ($c=1;$c<=$cols;$c++) { $values['col_'.$c] = array(); }
$c = 1;
$r = 1;
foreach ( $this->category->children as $category ) {
$catname = $category->category_name;
$caturl = JRoute::_ ( 'index.php?option=com_virtuemart&view=category&virtuemart_category_id=' . $category->virtuemart_category_id );
$values['col_'.$c][$r] = '<div class="category floatleft'.$category_cellwidth.'">
<div class="spacer"><h2>
<a href="'.$caturl.'" title="'.$catname.'">
'.$catname.'<br /></a></h2>
</div></div>';
if ($r == $rows_per_col) { $c++; $r = 1; } else { $r++; }
}
echo '<div class="tablediv">' ;
for ($r=1;$r<=$rows_per_col;$r++) {
echo '<div class="row">' ;
for ($c=1;$c<=$cols;$c++) { echo $values['col_'.$c][$r]; }
echo '</div>' ;
}
echo '</div>' ;
unset($values);
?>
It actually shows perfectly in the category view if the number of categories are dividable by 3 or dividable by 3 -1. Meaning that it shows correctly if there are 3, 5, 6, 8, 9, 11, 12 etc... categories on the page.
If the number of categories equals a number that is dividable by 3 +1 then it shows in a weird way..
Here is an example of how it shows when there are 9 categories:
Cat1 | Cat4 | Cat7
Cat2 | Cat5 | Cat8
Cat3 | Cat6 | Cat9
Here is an example of how it shows when there are 8 categories:
Cat1 | Cat4 | Cat7
Cat2 | Cat5 | Cat8
Cat3 | Cat6 |
And here is an example of how it shows when there are 7 categories:
Cat1 | Cat4 | Cat7
Cat2 | Cat5 | Cat3
Cat6 |
I really cannot figure this one out, so I hope someone can help me a little bit here..
Just try this, Sure you can change cols and rows.
<?php //user210424
$cols = 3;
$rows = 3;
$j = 0;
$array = array("ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE");
for($i=1; $i<=$cols; $i++) {
echo "<div class='col' style='float:left;'>";
for($j; $j<$rows*$i; $j++) {
echo "<div class='row'>".$array[$j]."</div>";
}
echo "</div>";
}
?>

Divide list in three columns

I have list of values that I want to fetch from my database. List could be long so I want to divide into two columns if it has over 15 items and to three columns if it has over 30. How can I break it in 3 columns. Eg..
01 | 12 | 23
02 | 13 | 24
03 | 14 | 25
04 | 15 | 26
05 | 16 | 27
06 | 17 | 28
07 | 18 | 29
08 | 19 | 30
09 | 20 | 31
10 | 21 |
11 | 22 |
For now i'm using tables and it has huge if-nest at the beginning of every loop
$result = mysql_query("SELECT namecount FROM table WHERE name='$myvar'");
if (!$result)
echo 'MySQL Error: ' . mysql_error();
else
{
while ($row = mysql_fetch_array($result))
{
$namecount= $row['namecount'];
for($i=1;$i<=$namecount;$i++)
{
if($namecount>15)
{
if($i==1)
echo "\n<table><tr><td width=\"200px\">";
if($namecount%2==0)
{
if($i==$namecount/2)
echo "</td>\n<td>";
}
else
{
if($i==($sailiot+3)/2)
echo "</td>\n<td>";
}
}
//Print content here
if($namecount>15)
{
if($namecount%2!=0 && $i==$namecount)
echo "<h3> </h3><p> <br> </p>"; //if last item and count is odd, print empty item
if($i==$namecount)
echo "\n</td></tr></table>\n";
}
}
}
}
This (kinda) works with two columns, but what about three?
I would extract <tr>, </tr> out of the inner loop, this saves additional ifs. For splitting into multiple columns, you can calculate the number of items in a column and introduce a second intra column variable to keep track of:
while ($row = mysql_fetch_array($result)) {
$namecount = $row['namecount'];
// calculate items per column
$columns = 1;
if ($namecount > 15)
$columns = 2;
if ($namecount > 30)
$columns = 3;
$items = $namecount / $columns;
if ($namecount % $columns > 0)
++$items;
echo "\n<table><tr><td width=\"200px\">";
for ($i = 1, $j = 1; $i <= $namecount; $i++, $j++) {
if ($j > $items) {
echo "</td>\n<td>";
$j = 1; // reset for new column
}
// Print content here
}
if ($namecount % 2 != 0) {
// if count is odd, print empty item
echo "<h3> </h3><p> <br> </p>";
}
echo "\n</td></tr></table>\n";
}
you maybe can try this
$result = mysql_query("SELECT namecount FROM table WHERE name='$myvar'");
if (!$result)
echo 'MySQL Error: ' . mysql_error();
else
{
while ($row = mysql_fetch_array($result))
{
$namecount= $row['namecount'];
for($i=1;$i<=$namecount;$i++)
{
if($namecount<=15){
echo "<table><thead><tr><th>".$your_variable_data."</th></tr></thead>";}
else if ($namecount >15 and $namecount <=30){
echo "<thead><tr><th>".$your_variable_data."</th></tr></thead></table>";
}
}
}
its not tested but it should work for 3 columns if you want more just add else if .
here a demo how it will be
http://jsfiddle.net/TCJjj/107/
use css3 multi column property
<style>
.namecount {
-moz-column-count:3; /* Firefox */
-webkit-column-count:3; /* Safari and Chrome */
column-count:3;
}
</style>
<div class="namecount">
Long Text
</div>
I found that CSS3 multi-columns was horribly inconsistent between browsers, and very buggy (elements with float:left, white-space:nowrap, etc. cause it to break).
So I wrote the following brute-force collator that retains key=>value pairs.
// vars
$list = array('a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z');
$columns = 3;
// sort it
$count = count($list);
$base = ceil($count/$columns);
$keys = array_keys($list);
for ($i=0;$i<$columns;$i++) {
for ($j=0;$j<$base;$j++) {
if (!empty($keys)) {
$col[$i][] = array_shift( $keys );
}
}
}
$sorted = array();
for ($j=0;$j<$base;$j++) {
for ($i=0;$i<$columns;$i++) {
if (isset($col[$i][$j])) {
$sorted[$col[$i][$j]] = $list[$col[$i][$j]];
}
}
}
// check output
echo '<div style="float:left;margin-right:20px"><h3>ORIGINAL</h3>';
foreach ($list as $k=>$v) echo $k .' = '. $v.'<br>';
echo '</div>';
echo '<div style="float:left"><h3>SORTED</h3>';
foreach ($sorted as $k=>$v) echo $k .' = '. $v .'<br>';
echo '</div>';

PHP : Custom pagination in Wordpress

I use this code for my custom paging :
global $wpdb, $table_prefix, $current_user;
get_currentuserinfo();
$umail = $current_user->user_email;
$paged = $wpdb->get_results("SELECT * FROM {$table_prefix}comments WHERE comment_author_email = '$umail'");
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$pages = COUNT($paged);
$pages = ceil($pages / 2);
$querystring = "";
foreach ($_GET as $key => $value) {
if ($key != "page") $querystring .= "$key=$value&";
}
// Pagination
for ($i = 1; $i <= $pages; $i++) {
echo "<a " . ($i == $page ? "class=\"selected\" " : "");
echo "href=\"?{$querystring}page=$i";
echo "\">$i</a> ";
}
This code paginate my comments look like this : 1 2 3 4 5 6 7 8 9 10 11
How can change code to get paginate look like this: 1 2 3 ... 11
Thanks for any help.
Try the following loop:
$page = 8;//current page
$pages = 15;//count of all pages
$batch_middle = 5;//the approximate number of pages in the middle links to display. Current page will be in the middle
$batch_lr = 1;//number of pages in the left and right links
for($i = 1; $i <= $pages;$i++)
{
//display first links
if ($i <= $batch_lr)
{
echo $i . ' ';
continue;
}
//display last links
if ($i > $pages-$batch_lr)
{
echo $i . ' ';
continue;
}
//display middle links
if ($i>=($page-floor($batch_middle/2)) && $i<=($page+floor($batch_middle/2)))
{
echo $i . ' ';
continue;
}
//placeholder
echo ' ... ';
//move the pointer
$i = ($i < $page) ? ($page-floor($batch_middle/2)-1) : ($pages-$batch_lr) ;
}
//output example 1: 1 2 ... 14 15
//output example 2: 1 2 ... 7 8 9 ... 14 15
//output example 3: 1 2 3 4 5 ... 14 15
//output example 4: 1 ... 6 7 8 9 10 ... 15
Replace $page and $pages with your logic with getting a current page and count of total pages.
Replace the $batch_middle and $batch_lr to configure a number of links in the first, second and third batches of the links

Categories