How to display numbers vertically 1 to 20 using php - php

this is the sample logic i want to use.
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
5 10 15 20
this is my sample code. i'm just getting the logic from numbers so i can use to to what i want to display. thank you in advance to those who can help me.
<?php
$counter = 0;
$columnctr = 0;
for ($x = 0; $x <= 4; $x++) :?>
<tr>
<?php for ($j = 0; $j <= 17; $j++) :?>
<?php if($counter == $x) : ?>
<?php
$i = 0;
?>
<?php foreach ($iterator as $val) :?>
<?php if($x == $i) : ?>
<td>
<?php if($val == "meron") : ?>
<span class="circle-blue">
</span>
<?php $counter++;?>
<?php else : ?>
<span class="circle-red">
</span>
<?php $counter++;?>
<?php endif; ?>
</td>
<?php endif; ?>
<?php $i++;?>
<?php endforeach;?>
<?php else : ?>
<td>
</td>
<?php endif; ?>
<?php endfor;?>
</tr>

$total = 20;
$totalRow = 5;
$totalCol = ceil($total/$totalRow);
echo '<pre>';
for($x = 1; $x <= $totalRow; $x++){
for($y=1; $y <= $totalCol; $y++){
$number = $x + (($y -1) * $totalRow);
// don't print extra number for last column, check for $total = 21 or 22
if($number <= $total)
echo $number. ' ';
}
echo PHP_EOL;
}

for($i=1;$i<=5;$i++):
$f = 0;
for($j=$i;$j < ($i+4) ; $j++ ):
if($f == 0):
echo $j." ";
$p = $j;
else:
echo $p = ($p + 5)." ";
endif;
$f = 1;
endfor;
echo "<br/>";
endfor;

Related

Fill html table with php rand() numbers

I need to fill a 10x10 html table using php code with a random number from 1-10 rand(1,10), then depending on the result paint the cell red if <5 or green if >5. i manually created 10 rows with 10 td each and inside I put echo(rand(1,10)) for each one of them but my syntax is wrong and it looks gross
<table>
<tbody>
<?php for ($i = 0; $i < 10; $i++) : ?>
<tr>
<?php for ($k = 0; $k < 10; $k++) : ?>
<?php $num = rand(1, 10); ?>
<td style="color: <?= $num < 5 ? 'red' : 'green'; ?>"><?= $num; ?></td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</tbody>
</table>

how to create pagination with next and previous button?

code:
<table>
<tr style="background: white url(bg.gif) repeat-x left top;color: #fff;">
<th>College Id</th>
<th>College Name</th>
<th>Website</th>
<th>Field</th>
<th>City</th>
<th>Action</th>
</tr>
<?php
$per_page=10;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else {
$page=1;
}
$start_from = ($page-1) * $per_page;
$sql="select * from all_colleges LIMIT $start_from, $per_page";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['college_id']; ?></td>
<td><?php echo $row['college_name']; ?></td>
<td><?php echo $row['website']; ?></td>
<td><?php echo $row['field']; ?></td>
<td><?php echo $row['city']; ?></td>
<td>
<a class='view' title='view' href='view.php?id=<?php echo $row['college_id']; ?>'>
<img src='gridview/view.png' alt='view' />
</a>
<a class='update' title='Update' href='update.php?id=<?php echo $row['college_id']; ?>'>
<img src='gridview/update.png' alt='Update' />
</a>
<a class='delete' title='delete' href='delete.php?ad_id=<?php echo $row['college_id']; ?>'>
<img src='gridview/delete.png' alt='delete' />
</a>
</td>
</tr>
<?php
}
?>
</table>
<div style="padding: 10px;">
<?php
$query = "select * from all_colleges";
$result = mysqli_query($link, $query);
$total_records = mysqli_num_rows($result);
$total_pages = ceil($total_records / $per_page);
echo "<center><a href='admin.php?page=1' style='padding:5px;'>".'previous'."</a>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='admin.php?page=".$i."'>".$i."</a>";
}
echo "<a href='admin.php?page=$total_pages'>".'next'."</a></center>";
?>
</div>
In this code I want to create a pagination with next and previous button here this code is working properly but it show diffrent pagination i.e. if the number of results is 200 then it will display like
previous 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 next
but I want like
previous 1 2 3 4 5 ....8 9 next
How can I do this please help ?
Thank You
You can check each iteration of your loop if you want to display the number.
In the following code, only:
the first two pages
the last two pages
the current page
each two pages to the left and to the right of the current page
are displayed.
$skipped = false;
for ($i = 1; $i <= $total_pages; $i++) {
if ($i < 3 || $total_pages- $i < 3 || abs($page - $i) < 3) {
if ($skipped)
echo '<span> ... </span>';
$skipped = false;
echo "<a href='admin.php?page=" . $i . "'>" . $i . "</a>";
} else {
$skipped = true;
}
}
If you have a lot of pages, the big loop is unnecessary.
Instead, you could use three different loops:
$done = [];
for ($i = 1; $i < 3; $i++) {
$done[$i] = true;
//echo
}
if ($page > 3)
echo '<span> ... </span>';
for ($i = $page - 2; $i < $page + 3; $i++) {
if (isset($done[$i])
continue;
$done[$i] = true;
//echo
}
if ($page < $total_pages - 3)
echo '<span> ... </span>';
for ($i = $total_pages- 2; $i < $total_pages; $i++) {
if (isset($done[$i])
continue;
$done[$i] = true;
//echo
}
To fix your two other buttons (previous and next), link to $page - 1 and$page + 1 instead of 1 and $total_pages.

I just want to print pyramid only using for loop in php as like as the give image

My code is
<center>
<?php
for($i = 0; $i < 7 ; $i++) {
for($j = 0; $j < $i; $j++) {
echo "*";
}
echo "<br/>";
}
?>
<center>
But I want is pyramid with useing <center></center> html tags.
You're looking for something like this:
<div class="text-center"><!-- or however you choose to center this -->
<?php
$w = 7;
for ($n = 0; $n < $w; $n++) {
echo str_repeat("-", $n) . "<br>";
}
?>
</div>
<?php
int k =0;
for($i=1;$i<=20;$i++){
for(a=1;a<=20;$a++){
echo " ";
}
while(k!=(2*i-1){
echo "* ";
k++;
}
k=0;
echo "\n";
}
?>

showing 2nd page in a paginated table in a primary content div in php

Good Evening/morning,
I have a table which is paginated. The first page shows up in a primarycontent div, which is where i want it..however when clicking on page 2, 3..etch it opens up in a different link, I want it to open up in the primary content div like the first page, but can't figure out how to do it. Also when clicking the previous button it goes to page 1, instead of the page before the current page..ex on page 50, goes to page 1. Here is my code.
<?php
require_once ('mysqli_connect.php');
$display = 30;
if (isset($_GET['p']) && is_numeric ($_GET['p']))
{
$pages = $_GET['p'];
} else {
$q = "SELECT COUNT(NewCustomerID) FROM customer";
$r = #mysqli_query($dbc, $q);
$row = #mysqli_fetch_array ($r, MYSQLI_NUM);
$records = $row[0];
if ($records > $display){//more than 1 page.
$pages = ceil($records/$display);
} else {
$pages = 1;
}
} // end of p IF
if (isset($_GET['s']) && is_numeric ($_GET['s']))
{
$start = $_GET['s'];
} else {
$start = 0;
}
$q = "SELECT(NewCustomerID) AS customerid,
(OldCustomerID) AS oldcustomerid,
(FirstName) AS FirstName,
(MiddleName) AS MiddleName,
(LastName) AS LastName,
(UserName) AS UserName,
(CarID) AS CarID,
(CarColorID) AS CarColorID,
(ComputerID) AS ComputerID,
(IsLaptop) AS LaptopID,
(RaceID) AS RaceID,
(ResidenceID) AS ResidenceID,
(BirthMonthID) AS BirthMonthID
FROM customer ORDER BY LastName ASC LIMIT $start, $display";
$r = #mysqli_query($dbc, $q); if(!$r){die(mysqli_error($dbc));}
Echo '<table>
<tr>
<td><b>NewCustomerID </b></td>
<td><b>OldCustomerID </b></td>
<td><b>FirstName </b></td>
<td><b>MiddleName </b></td>
<td><b>LastName </b></td>
<td><b>UserName </b></td>
<td><b>CarID </b></td>
<td><b>CarColorID </b></td>
<td><b>ComputerID </b></td>
<td><b>IsLaptop </b></td>
<td><b>RaceID </b></td>
<td><b>ResidenceID </b></td>
<td><b>BirthMonthID </b></td>
</tr>';
$bg = '#eeeeee'; // set initial back ground color
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$bg = ($bg =='#eeeeee' ? '#ffffff' : '#eeeeee'); // switch the background color.
echo '<tr bgcolor="' . $bg . '">
<td>' . $row['customerid'] . '</td>
<td>' . $row['oldcustomerid']. '</td>
<td>' . $row['FirstName']. '</td>
<td>' . $row['MiddleName']. '</td>
<td>' . $row['LastName']. '</td>
<td>' . $row['UserName'].'</td>
<td>' . $row['CarID'].'</td>
<td>' . $row['CarColorID'].'</td>
<td>' . $row['BirthMonthID'].'</td>
<td>' . $row['ComputerID'].'</td>
<td>' . $row['LaptopID'].'</td>
<td>' . $row['RaceID'].'</td>
<td>' . $row['ResidenceID'].'</td>
</tr>';
} // end of while loop
echo '</table>';
mysqli_free_result($r);
mysqli_close($dbc);
// make the links to the other pages if necessary
if ($pages >1) {
// add some spaces and start a paragraph
echo '<br /> <p>';
// determine what page the script is on:
$current_page = ($start/$display)+1;
// if it's not the first page, make a previous link;
if ($current_page !=1) {
echo 'Previous ';
}
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
echo '<a href="#" ' . (($display * ($i - 1))) . '&p=' . $pages . '">'
. $i . '</a> ';
}else{
echo $i . ' ';
}
}// end of FOR loop
// if it is not the last page, make a next button
if ($current_page != $pages) {
echo 'Next';
}
echo '</p>'; // close the paragraph
} // endo of links section
?>
Save yourself a ton of time and use a pre-engineered grid. My grid of choice is DataTables It's got all the bugs and browser inconsistencies taken care of already, and paging is as simple as one line of code. If you want to get really simple, you could output your entire table and let datatables sort out the paging, as well as sorting, filtering, etc. If you're like me and have to deal with a lot of results, you can also load data via ajax and have datatables actually assemble the dom. Either way, it's quicker than writing your own.
First of all, you should pass in GET/POST parameter which will carry the page to show. After you grab it from there, fro example in variable $page, you can use it SQL query with keyword OFFSET and LIMIT, like LIMIT 10 OFFSET 2, that gives you exactly rows from 11 to 20 items numbers.
Hope that's exactly what you need.
EDITED
<?php
$route = '/index.php?p='; // where to go on click page
$total = ($records % $display) != 0 ? floor($records / $display) + 1 : $records / $display; // total pages count
$current = isset($_GET['p']) ? $_GET['p'] : 1; // current page number
$last = 1; // init value of the last page
?>
<p class="pager">
<?php if ($total <= 9): ?>
<?php for ($i = 1; $i <= $total; $i++): ?>
<a href="<?php echo $route, $i; ?>" <?php if ($current == $i) echo 'class="acitve"'; ?>><?php echo $i; ?></a>
<?php endfor; ?>
<?php else: ?>
<?php
$rbound = $current + 1;
$lbound = $current - 1;
if ($lbound < 1)
$lbound = 1;
if ($rbound > $total)
$rbound = $total;
for ($i = 1; $i <= 3; $i++):
?>
<a href="<?php echo $route, $i; ?>" <?php if ($current == $i) echo 'class="acitve"'; ?>><?php echo $i; ?></a>
<?php $last = $i; ?>
<?php endfor; ?>
<?php if ($lbound <= $last): ?>
<?php for ($i; $i <= $rbound; $i++): ?>
<a href="<?php echo $route, $i; ?>" <?php if ($current == $i) echo 'class="acitve"'; ?>><?php echo $i; ?></a>
<?php $last = $i; ?>
<?php endfor; ?>
<?php else: ?>
<?php echo $lbound == $last + 1 ? '' : '...'; ?>
<?php endif; ?>
<?php if ($lbound > 3 && $rbound < $total - 2): ?>
<?php for ($i = $lbound; $i <= $rbound; $i++): ?>
<a href="<?php echo $route, $i; ?>" <?php if ($current == $i) echo 'class="acitve"'; ?>><?php echo $i; ?></a>
<?php $last = $i; ?>
<?php endfor; ?>
<?php endif; ?>
<?php if ($rbound >= $total - 2): ?>
<?php for ($i = $lbound; $i < $total - 2; $i++): ?>
<a href="<?php echo $route, $i; ?>" <?php if ($current == $i) echo 'class="acitve"'; ?>><?php echo $i; ?></a>
<?php $last = $i; ?>
<?php endfor; ?>
<?php $last = $last > $total - 4 ? $last : $total - 2; ?>
<?php else: ?>
<?php echo $rbound == $total - 3 ? '' : '...'; ?>
<?php $last = $total - 2; ?>
<?php endif; ?>
<?php for ($i = $total - 2; $i <= $total; $i++): ?>
<a href="<?php echo $route, $i; ?>" <?php if ($current == $i) echo 'class="acitve"'; ?>><?php echo $i; ?></a>
<?php endfor; ?>
<?php endif; ?>
</p>
That's all in above gives you pager rendered as: 1 2 3 ... 12 13 14 ... 18 19 20, here current page is 13, so it shows first 3 pages number, current page +/- 1 page and last 3 page numbers. So for the first page it would be: 1 2 3 ... 18 19 20, same for the last. Also adds CSS class active to the current page link.

how to limit foreach loop to three loops

how to limit this loop ..just thee loops..thanks for helping
<?php
foreach($section['Article'] as $article) :
?>
<tr>
<td>
<?php
if ($article['status'] == 1) {
echo $article['title'];
}
?>
</td>
<td>
<?php
if($article['status']== 1) {
echo ' '.$html->link('View', '/articles/view/'.$article['id']);
}
?>
</td>
</tr>
<?php
endforeach;
?>
Slice the array.
foreach(array_slice($section['Article'], 0, 3) as $article ):
first, prepare your data
$i = 1;
$data = array();
foreach($section['Article'] as $article ) {
if($article['status']== 1) {
$article['link'] = $html->link('View', '/articles/view/'.$article['id']);
$data[] = $article;
if ($i++ == 3) break;
}
}
$section['Article'] = $data;
then display it
<?php foreach($section['Article'] as $article ): ?>
<tr>
<td><?php echo $article['title'] ?></td>
<td> <?php echo $article['link']?></td>
</tr>
<?php endforeach ?>
This will help if your array is numerically indexed
foreach($section['Article'] as $i => $article ):
if ($i > 3) break;
Otherwise - manually increment the counter:
$i = 0;
foreach($section['Article'] as $article ):
if ($i++ > 3) break;
It'd be easier to use a for() loop to do this, but to answer the question:
<?
$i = 0;
foreach ($section['Article'] AS $article):
if ($i == 3) { break; }
?>
...
<?
$i++;
endforeach
?>
A foreach loop wouldn't be the best if you need to limit it. Try using a for loop.
<?php
for(i=1; i<=3; i++)
{
$article = $section['Article'];
?>
<tr>
<td><?php if($article['status']== 1){echo $article['title'];} ?></td>
<td><?php if($article['status']== 1){echo ' '.$html->link('View', '/articles/view/'.$article['id']);}?></td>
</tr>
<?php } ?>
This code will make the text loop 3 times.
Awesome one must try this one
<?php $count = 0; $pages = get_pages('child_of=1119&sort_column=post_date&sort_order=desc'); foreach($pages as $page) {
$count++;
if ( $count < 50) { // only process 10 ?>
<div class="main_post_listing"> <?php echo $page->post_title ?><br /></div>
<?php
} } ?>

Categories