Pagination if statement - php

I'm sitting at record id 1 and hit the Previous link, it goes to record 0, then record -1, then -2, and so on. I'm trying to show just the 'Next' link if I'm on record id 1, else show both links.
<ul class="pager">
<?php if (href="?read=<?=htmlspecialchars($_GET['read'] = 1)) { ?>
<li class="previous">Next</li>
<?php } else { ?>
<li class="previous">Previous</li>
<li class="previous">Next</li>
</ul>

<?php
//get page number or set to 1 if not page is set
$read = isset($_GET['read']) ? (int)$_GET['read'] : 1;
$limit = 5;
?>
<ul class="pager">
<?php if ($read > 1): ?>
<li class="previous">Previous</li>
<?php endif ?>
<?php if ($read < $limit): ?>
<li class="previous">Next</li>
<?php endif ?>
</ul>
ps: for NEXT link there should probably class next not previous but you have previous in your code so I left it at it is

Change this line
<?php if (href="?read=<?=htmlspecialchars($_GET['read'] = 1)) { ?>
To this
<?php if (href="?read=<?=htmlspecialchars($_GET['read'] == 1)) { ?>
$_GET['read'] = 1 statement always sets $_GET['read'] variable to 1.

Related

PHP: split a mySQLi array in two divs?

I'm currently working on a page that calls a mySQL database to populate a list of members. However, I need that list to be split into two equal parts, so that half can float to the left and half to the right. However, I have beat this horse to death and still cannot figure out how to split the array.
Here's what I have currently:
<div class="holder">
<?php
$members = $db->query('SELECT * FROM tableContacts ORDER BY lastName ASC');
$rowcount = mysqli_num_rows($members);
if ( $rowcount > 0 ) { ?>
<div class="members-left">
<ul class="members">
<?php while ($row = $members->fetch_assoc()) { ?>
<li class="member">
//SOME CONTENT HERE
</li>
<?php } ?>
</ul>
</div>
<?php } ?>
</div>
However, I want the output to look something like this (let's say my table has 10 members):
<div class="holder">
<!--output first half of members from table: -->
<div class="members-left">
<ul class="members">
<li class="member">...</li>
<li class="member">...</li>
<li class="member">...</li>
<li class="member">...</li>
<li class="member">...</li>
</ul>
</div>
<!--output second half of members from table: -->
<div class="members-right">
<ul class="members">
<li class="member">...</li>
<li class="member">...</li>
<li class="member">...</li>
<li class="member">...</li>
<li class="member">...</li>
</ul>
</div>
</div>
I've tried setting counters and using things like if($i <= $rowcount/2), but to no avail. Any help would be greatly appreciated—I'm very new to mySQL and have limited knowledge of PHP, so this one has me stumped.
add a control variable like "$i". add +1 to $i every loop. and check if $i reached half of number of rows. if reached close ul and div and open new div and ul and set $i to 0
<div class="holder">
<?php
$members = $db->query('SELECT * FROM tableContacts ORDER BY lastName ASC');
$rowcount = mysqli_num_rows($members);
$i=0;
if ( $rowcount > 0 ) { ?>
<div class="members-right">
<ul class="members">
<?php while ($row = $members->fetch_assoc()) { ?>
<li class="member">
//SOME CONTENT HERE
</li>
<?php
$i++;
if($i>=($rowcount/2)){
echo '</ul></div>
<div class="members-right">
<ul class="members">';
$i=0;
}
} ?>
</ul>
</div>
<?php } ?>
</div>
You could use a for loop for the first half and just finish the second half with a while loop that goes until there are no more results.
First half:
for($i = 0; $i <= $rowcount/2 && $row = $members->fetch_assoc(); $i++)
Second half
while ($row = $members->fetch_assoc())
Example with HTML
<div class="members-left">
<ul class="members">
<?php for($i = 0; $i <= $rowcount/2 && $row = $members->fetch_assoc(); $i++) { ?>
<li class="member">
// do something with $row
</li>
<?php } ?>
</ul>
</div>
<div class="members-right">
<ul class="members">
<?php while ($row = $members->fetch_assoc()) { ?>
<li class="member">
// do something with $row
</li>
<?php } ?>
</ul>
</div>
I would recommend you to make 2 arrays of your members and then print them out. The advantage is you will have more control of the members and your code will be more readable for later use;
<?php
$i =0;
while ($row = $members->fetch_assoc()) {
if($i % 2 == 0)
{
$right_members[] = $row;
}
else
{
$left_members[] = $row;
$i++;
}
echo '
<div class="members-right">
<ul class="members">';
foreach($right members as $r_member){
echo '<li class="member">...</li>';
}
echo '
</ul>
</div>';
//SAME WITH LEFT MEMBERS
?>
Having a counter was an idea, I will not debate on having model logic in view ;)
well, a solution would be :
<div class="holder">
<div class="members-column">
<ul class="members">
<?php
$members = $db->query('SELECT * FROM tableContacts ORDER BY lastName ASC');
$rowcount = mysqli_num_rows($members);
$i = 0;
while ($row = $members->fetch_assoc()) {
?>
<li class="member">
//SOME CONTENT HERE
</li>
<?
// Check if current row is the 'middle count', creating a new column
if ($i === ceil($rowcount / 2)) : ?>
</ul>
</div>
<div class="members-column">
<ul class="members">
<?php endif; ?>
<?php
$i++;
}
?>
</ul>
</div>
</div>

Do something Inside a PHP foreach Loop Only Once

I am using a foreach loop to create a brands page for our Magento store. In the loop every 5 items are put into an un-ordered list. They are also split up by letters of the alphabet (A - D, E - H, I - L, M - P, Q - U, V - Z). I need to insert a header before each set of lists. For example:
<h2>A - D</h2>
<ul>
<li>Brand 1</li>
<li>Brand 2</li>
<li>Brand 3</li>
<li>Brand 4</li>
<li>Brand 5</li>
</ul>
This is how I am getting the brands and splitting them up:
<?php foreach($brands as $brand) : ?>
<?php $name = $brand->getName(); ?>
<?php if((substr($name, 0, 1) == 'A') || (substr($name, 0, 1) == 'B') ||
(substr($name, 0, 1) == 'C') || (substr($name, 0, 1) == 'D')) : ?>
<?php if($count % 5 == 0) : ?>
</ul><ul>
<?php endif; ?>
<li><a href="<?php echo $brand->getUrl(); ?>">
<?php
$id = $brand->getId();
$imageUrl = Mage::getModel('catalog/category')->load($id)->getThumbnailUrl();
?>
<img src="<?php echo $imageUrl; ?>" alt="<?php echo $name; ?>">
</a></li>
<?php $count++; ?>
<?php endif; ?>
<?php endforeach; ?>
I thought I could insert the header into the right place by counting how many brands there are for each set of 4 or 5 letters then doing the same as what I have done to put in the 's. But it doesn't work.
Is there any way I can maybe tell the header to insert only on the first run of the loop?
Try this
$ascci_val = ord( strtolower(sub_str($name, 0, 1)) );
if( ($ascci_val >= 97) && ($ascci_val <= 100) ){
// brand name stats with a letter between A-D / a-d
}

create new ul after every 13 li elements

First of all, I don't know how to count in php, maybe someone could recommend me a good source to read;
Second, I'm not asking to solve this for me, but I just want a hint or simpler explanation that would make sense;
Here is my function:
<ul>
<?php foreach ($categories as $category) { ?>
<li>
<p><?php echo $category['name']; ?></p>
<?php if ($category['children']) { ?>
<div>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['children'][$i])) { ?>
<ul>
<li class="none"><h1><?php echo $category['children'][$i]['name']; ?></h1></li>
<?php if ($category['children'][$i]['children_level2']) { ?>
<?php for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) { ?>
<li>
<?php echo $category['children'][$i]['children_level2'][$wi]['name']; ?>
</li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
I want this part, every 13 li elements to create a new .ul. ./ul. tags
<?php if (isset($category['children'][$i])) { ?>
<ul>
<li class="none"><h1><?php echo $category['children'][$i]['name']; ?></h1></li>
<?php if ($category['children'][$i]['children_level2']) { ?>
<?php for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) { ?>
<li>
<?php echo $category['children'][$i]['children_level2'][$wi]['name']; ?>
</li>
<?php } ?>
<?php } ?>
</ul>
<ul><li>maximum of 13 elements</li></ul>
after 13 <li></li> elements create new <ul></ul> tags and put the 14 <li></li> element into the new <ul></ul> tag
I hope I explained what I want to do, for now I'll be waiting for your answers,
p.s. this is more for my learning skills then actual work, so thanks in advice
It's simple. Use condition
if ($wi && $wi % 13 == 0) {
print '</ul><ul>';
}
In order to count 13 elements, you can use the modulo operator %. The idea is that ($iw % 13) is equal to 0 if the content of $iw is a multiple of 13. (see more about the modulo operation)
With an if statement you can insert </ul><ul> when you need in your for loop.
There's two possibilities I can think of:
you could have a separate counter wich resets to 1 and outputs a <ul> every time it reaches 13
or you could check each time if $wi id divisible by 13:
<?php
for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) {
if ($wi && $wi % 13 == 0) {
// output <ul>
}
}
?>
If you're unsure what % means, here's the appropriate PHP manual page

how to change values between loop

I extracted the values of the array on the foreach loop, but I want when it retrieves a certain 'br_title' changed href link to <a href="http://noorresults.moe.sa..how to do that with simple code?..
<ul class="riMenu">
<?php foreach ($last_branches as $last) { ?>
<li> <?php if (!empty($last['br_title'])) echo $last['br_title'] ?></li>
<?php }; ?>
</ul>
</div>
</div><!-- Menu E
You can change your code as below
<?php
$total_to_show = 5; // take offset
$count = 1; //counter until it reach to offset
foreach ($last_branches as $last) { if($count > $total_to_show){?>
<li> <?php if(!empty($last['br_title'])) echo $last['br_title'] ?></li>
<? }else{?>
<li> <?php if(!empty($last['br_title'])) echo $last['br_title'] ?></li>
<?php }
$count++;
} ?>

Pagination with Page numbers?

I have a page which get's populated dynamically from a Mysql database.
To prevent to much information been displayed at once, I have added pagination to page.
Using this code.
$currentPage = $_SERVER["PHP_SELF"];
$maxRows_atoz = 10; $page= 0;
if (isset($_GET['page'])) {$page= $_GET['page'];}
$startRow_atoz = $page* $maxRows_atoz;
mysql_select_db($database_main, $main);
$query_atoz = "SELECT * FROM main WHERE title LIKE 'A%'";
$query_limit_atoz = sprintf("%s LIMIT %d, %d", $query_atoz, $startRow_atoz, $maxRows_atoz);
$atoz = mysql_query($query_limit_atoz, $main) or die(mysql_error());
$row_atoz = mysql_fetch_assoc($atoz);
if (isset($_GET['totalRows_atoz'])) {$totalRows_atoz = $_GET['totalRows_atoz'];} else {$all_atoz = mysql_query($query_atoz); $totalRows_atoz = mysql_num_rows($all_atoz);}
$totalPages_atoz = ceil($totalRows_atoz/$maxRows_atoz)-1;
$queryString_atoz = ""; if (!empty($_SERVER['QUERY_STRING'])) {$params = explode("&", $_SERVER['QUERY_STRING']); $newParams = array(); foreach ($params as $param) {
if (stristr($param, "pageNum_atoz") == false && stristr($param, "totalRows_atoz") == false) {array_push($newParams, $param);}}
if (count($newParams) != 0) {$queryString_atoz = "&" . htmlentities(implode("&", $newParams));}}
$queryString_atoz = sprintf("&totalRows_atoz=%d%s", $totalRows_atoz, $queryString_atoz);
?>
<ul>
<li class="previous"> <?php if ($page > 0) { // Show if not first page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, max(0, $page - 1), $queryString_atoz); ?>">
<img src="/files/previous.png" /></a>
<?php } // Show if not first page ?></li>
<li class="next"> <?php if ($page < $totalPages_atoz) { // Show if not last page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, min($totalPages_atoz, $page + 1), $queryString_atoz); ?>">
<img src="/files/next.png" /></a>
<?php } // Show if not last page ?></li>
</ul>
The code works in paginaing the results, but has a limitation.
It currently only display's Next and Previous buttons.
I would like to add page numbers. This way a user can skip to 4th or 5th page in the result set rather than having to press the next button 4 or 5 times.
So, can some please tell me how I would go about doing this?
Thanks
I must say, I havent gone through the entire code of yours. You must indent your code to help others. If I get your question right, the following may help you. I haven't much taken care of finer details. This is just to give you an idea. :-)
<ul>
<li class="previous">
<?php if ($page > 0) { // Show if not first page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, max(0, $page - 1),$queryString_atoz); ?>">
<img src="/files/previous.png" /></a>
<?php } // Show if not first page ?></li>
<!--MODIFY_BEGIN-->
<li>
<?php for ($i=0;$i<$page; $i++){
if ($currentPage==$i)
echo ($currentPage+1);
else
echo "<a title='Page $i' href='?page=$i'>Page $i</a>";
} ?>
</li>
<!--MODIFY_END-->
<li class="next"> <?php if ($page < $totalPages_atoz) { // Show if not last page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, min($totalPages_atoz, $page + 1), $queryString_atoz); ?>">
<img src="/files/next.png" /></a>
<?php } // Show if not last page ?></li>
</ul>

Categories