I have a problem coding the numbers of pages of a pagination.
I will be as simple as possible in detailing what I need to do.
There are N numbers of pages (keep track as $listPages);
I need to show a maximum of 5 pages numbers generated as $listCount "1(current),2,3,4,5 + Next + Last" and keep the pattern with increasing pages (example for page 33 "First + Prev + 31,32,33 (current),34,35 + Next + Last");
$list keep track of the current page;
I written this code for the moment but it doesn't work correctly giving as output (example on page 7 "1,2,3,4,5,6,7(current),8,9 + Next + Last) it works going up, but it keep previous numbers. I hope some could help coding it, thanks!
function ShowPageNumbers($pageUrl, $listPages, $list) {
global $listPagesCount;
foreach (range(1, $listPages) as $listCount) {
//Check if current page
if($listCount == ($list - 1)) {
echo "<li class='current' style='margin:3px;'>";
echo "<span class='pages-nav-item'>" . $listCount . "</span>";
echo "</li>";
$listPagesCount++;
} else {
if (($list - 1) == 1) {
//null
} else {
//echo "<li class='the-prev-page' style='margin:3px;'>";
//echo "<a class='pages-nav-item' href=" . $pageUrl . ($listCount + 4) . " title=" . ($listCount + 4) . ">«</a>";
//echo "</li>";
}
echo "<li style='margin:3px;'>";
echo "<a class='pages-nav-item' href=" . $pageUrl . $listCount . " title=" . $listCount . ">" . $listCount . "</a>";
echo "</li>";
$listPagesCount++;
if($listPagesCount >= 3 && $listPagesCount == $list + 1) {
echo "<li class='the-next-page' style='margin:3px;'>";
echo "<a class='pages-nav-item' href=" . $pageUrl . $listCount . " title=" . $listCount . ">»</a>";
echo "</li>";
echo "<li class='extend' style='margin:3px;'>";
echo "<span class='pages-nav-item'>...</span>";
echo "</li>";
echo "<li class='last-page first-last-pages' style='margin:3px;'>";
echo "<a class='pages-nav-item' href=" . $pageUrl . $listPages . " title='Ultimo'>Ultimo</a>";
echo "</li>";
break;
}
}
}
}
Related
I need after the end of a loop in a recursive function to return the $build variable
This is my code:
$traverse = function ($tree,$build = '') use (&$traverse) {
foreach ($tree as $key=>$menu) {
if (count($menu->children) > 0) {
$build .= "<li ><a href='" . $menu->url . "'>" . $menu->text . "</a><ul>";
$traverse( $menu->children,$build);
$build .= "</ul></li>";
} else {
$build .= "<li ><a href='" . $menu->url . "'>" . $menu->text . "</a></li>";
}
}
};
$traverse($tree );
Regarding my comment u should have:
$traverse = function ($tree) use (&$traverse) {
$build = '';
if (count($menu->children) > 0) {
$build .= "<li ><a href='" . $menu->url . "'>" . $menu->text . "</a><ul>";
$build .= $traverse($menu->children);
$build .= "</ul></li>";
} else {
$build .= "<li ><a href='" . $menu->url . "'>" . $menu->text . "</a></li>";
}
return $build;
};
As u can see u also don't need to pass and use $build as argument to the function.
Also u should check the html code for being valid at the end. Because of it won`t be.
I'm trying to re-write this code so that it goes with the columns that are going to be user-defined. With this, my challenge consists of
a) Needs to select random starting item from array
b) Select the next random color from the original array that is not equivalent to the most recent items selected based the number: $intNotesColumn + 1
I was thinking a do-while statement nested inside another was appropriate for this but am unsure how to go about this. Here is my code so far:
$metroUIcolors = array( "#A30061", "#8200CC", "008987", "#A05000", "#B85A93", "#C07807", "#E51400", "#297A29" );
$metroUIcolorsLength = count($metroUIcolors);
$intNotesColumn = 3; // This will be user-defined later
// Now I query the SQL database to get my base-code
if ($result->num_rows > 0) {
// output data of each row
echo '<table border=0 valign=top>'
. '<tr>'
. '<td colspan=' . $intNotesColumn . '>' . '<h1>header</h1>' . '</td>'
. '</tr>'
. '<tr>';
$counterRank = 1;
while($row = $result->fetch_assoc()) {
echo "<td bgcolor=" . $metroUIcolors[rand(0, $metroUIcolorsLength - 1)]. ">"
. "<h2>" . $row["username"] . '</h2><br />'
. "<p class='notes'>" . $row["notes"] . "</p>"
. "<p class='footnotes'>"
. "<br />Last Reset: " . $row["lastReset"]
. '<br />Last Update: ' . $row['lastUpdate']
. '<br />SessionID: ' . $row["sessionID"]
. "<br />Counter = " . $counterRank . "</td>". '</p>';
if ($counterRank % $intNotesColumn == 0)
{
echo '</tr><tr>';
}
$counterRank++;
}
echo '</tr></table>';
} else{
echo "No Notes Found in databases";
}
Then, why don't you do it order picking one color at a time. You could use shuffle() so that there will be a different starting color everytime.
<?php
$counterRank = 1;
// shuffle the array
shuffle($metroUIcolors);
while($row = $result->fetch_assoc()) {
$rand_color = $counterRank % $metroUIcolorsLength;
echo "<td bgcolor=" . $metroUIcolors[$rand_color]. ">";
// everything else
$counterRank++;
}
?>
If you insist on doing the way you said, you may create an array $colorCount which has color codes as keys and count as values.
<?php
$counterRank = 1;
$colorCount = array_fill_keys($metroUIcolors, 0);
while($row = $result->fetch_assoc()) {
do {
$rand_color = $metroUIcolors[rand(0, $metroUIcolorsLength - 1)];
} while ($colorCount[$rand_color] > 5);
echo "<td bgcolor=" . $rand_color. ">";
// everything else
$counterRank++;
$colorCount[$rand_color] += 1;
}
?>
While making a photo gallery I encountered a problem. With every photo I try to show how many comments it has, however if a photo has 0 comments it will give me an 'undefined offset' error. I have no idea what I am doing wrong because it does show that there are 0 comments.
This is the code of what is relevant to the problem:
(The problem occurres in the line: if($reacties[$i]==0){)
if((isset($_GET['vanafFoto'])) AND (intval($_GET['vanafFoto']>=0)) AND (intval($_GET['vanafFoto'] < $countFotos))){
$begin = intval($_GET['vanafFoto']);
if(($begin + $aantalFotos) <= $countFotos){
$eind = ($begin + $aantalFotos);
} // end if
else {
$eind = $countFotos;
} // end else
} // end if
else {
$begin = 0;
$eind = $aantalFotos;
} // end else
$countFotos = count($fotoArray);
// path naar echte foto
} // end else
echo "<table border='0' cellpadding='0' cellspacing='2'><tr><td ><b>" . $pathspatie . "</b> <small>(" . $count . ")</small>
<br><br><center><small>Pictures " . ($begin + 1) . " - " . $eind . "</small></center></td></tr></table>";
if(($begin - $aantalFotos) >= 0){
$navigation = "<a href='" . $_SERVER['PHP_SELF'] . "?page=album&boek=" . $originalPath . "&vanafFoto=" . ($begin - $aantalFotos) . "'><</a> " . $navigation;
} // end if
if(($begin + $aantalFotos) < $count){
$navigation .= " <a href='" . $_SERVER['PHP_SELF'] . "?page=album&boek=" . $originalPath . "&vanafFoto=" . ($begin + $aantalFotos) . "'>></a>";
} // end if
echo $navigation . "<br><br>";
echo "</td></tr><tr>";
$fotonr = 1;
for($i=$begin; $i < $eind; $i++){
$thumb = str_replace($path2, $thumbPath, $fotoArray[$i]);
echo "<td align='center'><a href='" . $_SERVER['PHP_SELF'] . "?page=album&boek=" . $originalPath . "&fotoID=" . $i . "'><img border='0' src='" . $thumb . "' height='100'><br>";
echo "<small>reacties (";
if($reacties[$i]==0){ // error occurres here.
echo "0";
} // end if
else {
echo $reacties[$i];
} // end else
echo ")</small>";
echo "</a></td>";
$fotonr++;
if($fotonr == ($clm + 1)){
echo "</tr>\n<tr>";
$fotonr = 1;
} // end if
} // end for
If anyone can see what the problem is it would be great!
I did not understand you exact goal but maybe it is better to write one more check:
if(!isset($reacties[$i]) || $reacties[$i]==0){
echo "0";
}
I'm trying to create a dynamic navigation menu.
All allowed possibilities are defined in an array.
I'm trying to count the array, split it in half and print each half of the array into seperate div's. One on the left side of the screen and the other on the right side.
this is what i've got:
function menu_main() {
$count = count($GLOBALS['menu']);
$count = round($count / 2);
$menu_1 = array_slice($GLOBALS['menu'], 0, $count);
$menu_2 = array_slice($GLOBALS['menu'], $count++);
echo ' <div id="menu_1">' . "\n";
foreach($menu_1 as $page) {
echo ' <div class="menu_items">' . "\n";
echo ' <a class="menu" href="index.php?page=' . $page . '">' . $page . '</a> .' . "\n"; //Create menu left
echo ' </div>'. "\n";
}
echo ' </div>'. "\n";
echo ' <div id="menu_2">' . "\n";
foreach($menu_2 as $page) {
echo ' <div class="menu_items">' . "\n";
echo ' <a class="menu" href="index.php?page=' . $page . '">' . $page . '</a> .' . "\n"; //Create menu right
echo ' </div>'. "\n";
}
echo ' </div>'. "\n";
}
Is there a way to do this with itteration?
This works but it looks way to messy.
Ok, a couple of things:
$GLOBALS['menu'];
Why use $GLOBALS, or global $menu, for that matter? Why not pass what you need to the function?
function menuMain(array $menu)
{
$count = floor(count($menu)/2);
Would make a lot more sense. Also:
$menu_1 = array_slice($GLOBALS['menu'], 0, $count);
$menu_2 = array_slice($GLOBALS['menu'], $count++);//<== why the increment?
Why are you incrementing $count, only to then not use it anymore? You're post-incrementing the value, which means that if $count is 10, then the code above is evaluated to:
$menu_1 = array_slice($GLOBALS['menu'], 0, 10);
$menu_2 = array_slice($GLOBALS['menu'], 10);//10+1 happens AFTER the array_slice call is made
Also don't use "\n", because line-feeds are system dependent, PHP has a predefine constant for this reason: PHP_EOL. Use it (that's an order).
And don't concatenate what you echo, just comma-separate it:
echo 'foo', 'bar', PHP_EOL;
echo 'foo'.'bar'.PHP_EOL;
both may produce the same time, but the first version is faster, because the strings are pushed to the output, without first concatenating them into a new string.
Not that it matters here, because functions should return something, not echo it.
Anyway, your question: is there a way to do this with iteration? Yes, and you're already iterating (foreach iterates an array/object).
An alternative approach would be:
function mainMenu (array $menu)
{
$return =' <div id="menu_1">' . PHP_EOL;//functions shouldn't echo
//start with div 1
for($i = 0, $j= count($menu), $count = floor($j/2); $i<$j;++$i)
{
if ($i == $count)
{//reached half-way marker, close div 1, open div 2
$return .= '</div>'.PHP_EOL.'<div id="menu_2">'.PHP_EOL;
}
$return .= ' <div class="menu_items">' . PHP_EOL
.' <a class="menu" href="index.php?page=' . $menu[$i] . '">' . $menu[$i]
. '</a> .' .PHP_EOL;
}
return $return.'</div>';//close div2 and return
}
That should give you the exact same output as before, only it only uses a single loop, doesn't rely on globals, and doesn't echo things, so you call it like this:
$menu = array('your', 'global', 'array');
echo mainMenu($menu);//pass the array, echo what it returns
My solution if it helps.
$menu = array ("link1","link2","link3","link4","link5");
function menu_main()
{
global $menu;
$count = count($menu);
$menu_items = round($count / 2);
//initialize the divs for menus
$output_menu[0] = ' <div id="menu_1">' . "\n";
$output_menu[1] = ' <div id="menu_2">' . "\n";
$menu_count = 0;
foreach($menu as $link)
{
if ($menu_count < $menu_items)
{
//what enters in first menu
$output_menu[0] .= ' <div class="menu_items">' . "\n";
$output_menu[0] .= ' <a class="menu" href="index.php?page=' . $link . '">' . ucfirst($link) . '</a> .' . "\n"; //Create menu left
$output_menu[0] .= ' </div>'. "\n";
}
else
{
//what enters in second menu
$output_menu[1] .= ' <div class="menu_items">' . "\n";
$output_menu[1] .= ' <a class="menu" href="index.php?page=' . $link . '">' . ucfirst($link) . '</a> .' . "\n"; //Create menu left
$output_menu[1] .= ' </div>'. "\n";
}
$menu_count++;
}
//close the divs for menus
$output_menu[0] .= ' </div>'. "\n";
$output_menu[1] .= ' </div>'. "\n";
//return array with content for menus
return $output_menu;
}
$menu_main = menu_main(); // get content in variable
print_r($menu_main[0]); //print 1st menu
print_r($menu_main[1]); //print 2nd menu
Please forgive me if i make some mistake in english its not my native language.
To the point. This is my pagination code:
if (!isset($_GET["page"])) $_GET["page"] = 1;
if (sizeof($dirs) + sizeof($files) > $thumbs_pr_page)
{
$page_navigation .= "$label_page ";
for ($i=1; $i <= ceil((sizeof($files) + sizeof($dirs)) / $thumbs_pr_page); $i++)
{
if ($_GET["page"] == $i)
$page_navigation .= "$i";
else
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i) . "'>" . $i . "</a>";
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i-1) . "'> Previous </a>";
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i+1) . "'> Next </a>";
if ($i != ceil((sizeof($files) + sizeof($dirs)) / $thumbs_pr_page)) $page_navigation .= " | ";
}
}
Any ideas where is the problem ? The next/ previous button is mutliplied. But it should be only on the left side should be previous and on the right side next. I am only learning php now and its hard for me. Here you can see it how it looks: http://tinypic.com/view.php?pic=2djl3m&s=5 and it should be like this: http://tinypic.com/view.php?pic=2eeli54&s=5
You need to move the next and previous button out of your for-loop. Previous at front, next after it. Next and Previous dont need to depend on $i - they should depend on the currently loaded page, only.
if (!isset($_GET["page"])) $_GET["page"] = 1;
if (sizeof($dirs) + sizeof($files) > $thumbs_pr_page)
{
$page_navigation .= "$label_page ";
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($_GET["page"]-1) . "'> Previous </a>";
for ($i=1; $i <= ceil((sizeof($files) + sizeof($dirs)) / $thumbs_pr_page); $i++)
{
if ($_GET["page"] == $i)
$page_navigation .= "$i";
else
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i) . "'>" . $i . "</a>";
if ($i != ceil((sizeof($files) + sizeof($dirs)) / $thumbs_pr_page)) $page_navigation .= " | ";
}
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($_GET["page"]+1) . "'> Next </a>";
}
But you should also "catch" 0 and n+1 pages (then no Previous / Next Button is required)
Problem is in your for loop. You are checking if $_GET['page'] == $i and if not you are concatenating links to $page_navigation
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i) . "'>" . $i . "</a>";
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i-1) . "'> Previous </a>";
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i+1) . "'> Next </a>";
This will generate Next / Previous link for each iteration where $_GET['page'] == $i is not true (which is always except once). Since you need them only once (relative to current $_GET['page']) you need to move them outside the loop.
First of all, please try and put brackets in the after the else, because, the last two statements in the else will not be a part of the else clause, which will cause the Previous and Next to be a part of the for(;;;) instead of the else, and that is causing it to maybe repeat the Previous and Next.
if ($_GET["page"] == $i)
$page_navigation .= "$i";
else{
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i) . "'>" . $i . "</a>";
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i-1) . "'> Previous </a>";
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i+1) . "'> Next </a>";
}