Divide list in two columns - php

I have list of values that I want to fetch from my database. So List is long I want to fetch list in two columns. How can I break it in 2 columns. Eg..
taking list of aphabets
A | B
C | D
E | F
G | H
I | J
K | L
IN this way. I used following approach...
<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$total_rows = mysql_num_rows($sql);
$i = 1;
$j = ceil($total_rows/2);
while ( $i <= $j ) {
$result = mysql_fetch_assoc($sql);
?>
<tr>
<? if($i%2 == 0) { ?>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
<? } elseif($i%2 != 0) { ?>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
<? } ?>
</tr>
<?php $i++; } ?>
Its giving me output in this way..
A | A
B | B
C | C
NOTE - I wish to do it using PHP not using any jquery or javascript.

It seems you are resetting your result every time you loop. Set the $results outside the while. The more accepted way to do this is:
while ($row = mysql_fetch_assoc($sql)){
}
Then test $i to see if it is divisible by 2 (meaning you have 2 columns and need to add the end of row and beginning of new row tags).
if($i%2 == 0){
echo '</tr><tr>';
}

try:
<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$i = 0;
$total_rows = mysql_num_rows($sql);
while ( $result = mysql_fetch_assoc($sql) ) {
$i++;
?>
<? if($i&1 == 1) { ?>
<tr>
<td class="navigation"><?php echo $result["pid"]; ?></td>
<? } elseif($i&1 == 0) { ?>
<td class="navigation"><?php echo $result["pid"]; ?></td>
</tr>
<? }
if($i == $total_rows && $total_rows&1 == 1){ echo "</tr>"; } ?>
<?php } ?>
Edited: Bug fixes..
i think Colmn rows this will work:
<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$total_rows = mysql_num_rows($sql);
$iOne = 1;
$iTwo = floor($totalRows/2);
foreach($iOne=1,$iTwo=ceil($totalRows/2); $iTwo < $total_rows; $iOne++,$iTwo++){
?>
<tr>
<td class="navigation"><?php echo mysql_result($sql,$iOne,'pid') ?></td>
<td class="navigation"><?php echo mysql_result($sql,$iTwo,'pid') ?></td>
</tr>
<?php
}
?>

I would suggest jQuery columnizer as a solution to your problem http://welcome.totheinter.net/columnizer-jquery-plugin/

Finally I succeeded with code.
<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$total_rows = mysql_num_rows($sql);
$i = 1;
while ( $result = mysql_fetch_assoc($sql) ) { ?>
<?php if($i%2 == '1') { ?>
<tr>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
<?php } elseif($i%2 == '0') { ?>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
</tr>
<?php } ?>
<?php $i++; } ?>

Related

How to display the numbers of each rows in PHP

I am having problem to display the numbers of each rows. I am displaying a table with LIMIT of up to 20 rows. I can display the numbers in the while loop but when user click for next rows it will display the number back to 1. How can I display it as a continue number example, 1-20, 21-40, etc? Below are my codes:
<?php
//SQL AND LIMIT codes here
$x = '1';
if(mysqli_num_rows($result) > 0){
while($row=mysqli_fetch_assoc($result)){
$pr_id=$row['pr_id'];
?>
<tr>
<td align="center"><?php echo $x; //display the number ?></td>
<td><?php echo html_entity_decode($row['title']); ?></td>
<td align="center"><?php echo date("d/m/y",strtotime($row['submit_date'])); ?></td>
<td><?php echo $row['purchase_type']; ?></td>
</tr>
<?php
$x++;
}
}
mysqli_free_result($result);
mysqli_close($conn);
echo 'Next';
?>
As you mentioned $next is page number:
One solution is you can do:
$x = 1 + (($next -1 ) * 20);
So...
When $next = 1 (first page) $x = 1 + (0*20) = 1
When $next = 2 (second page) $x = 1 + (1*20) = 21
When $next = 3 (third page) $x = 1 + (2*20) = 41
So on...
Can you please use limit in your sql query and you use below code for getting limit.
$start = 1 + ((page number)*20);
$end = $start + 19;
Now you can set it in your sql query
$sql = "select * from table name where limit".$start.", ".$end;
try like this
<?php
if(isset($_REQUEST['pn'])){
$pn = $_REQUEST['pn'];
} else {
$pn = '1';
}
$x = $pn * 20; //place your no. of rows instead of 20
if(mysqli_num_rows($result) > 0){
while($row=mysqli_fetch_assoc($result)){
$pr_id=$row['pr_id'];
?>
<tr>
<td align="center"><?php echo $x; //display the number ?></td>
<td><?php echo html_entity_decode($row['title']); ?></td>
<td align="center"><?php echo date("d/m/y",strtotime($row['submit_date'])); ?></td>
<td><?php echo $row['purchase_type']; ?></td>
</tr>
<?php
$x++;
}
}
mysqli_free_result($result);
mysqli_close($conn);
echo 'Next';
?>

PHP Pagination for user?

I've tried to make a pagination with PHP, but it seems to not work. I want to limit my data which showing in each page, I have this code and it uses PHP code:
showUsers.php
<?php
$query = mysql_query("select * from users");
while ($data = mysql_fetch_array($query)) {
?>
<td><?php echo $data['no_peg']; ?></td>
<td>
<?php
echo $data['username'];
//previlage admin
if ($_SESSION['role'] == 'admin') {
?>
<div class="row-actions">
Edit
<?php if ($data['role'] != 'admin') {?>
| Delete
<?php } ?>
</div>
<?php } ?>
</td>
<td><?php echo $data['fullname']; ?></td>
<td><?php echo $data['telephone']; ?></td>
<td><?php echo $data['email']; ?></td>
I want to show just 10 names per page, to avoid long scrolling, but how can it work?
Hi,if you want to make a pagination, you should add in a LIMIT clause into your queries, like this:
SELECT* FROM users LIMIT 0, 10
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return.
So, what's the next?
You need to add a parameter to your query url specifies the number of page when you list the users.
Such as:
showUsers.php?page=1
Then, in your program, you can get parameter by this:
$page = isset($_GET['page']) ? $_GET['page'] : 1;
I hope it will help you, I'm new here.
<?php
$sqlCount = "select count(id_user) from users";
$rsCount = mysql_fetch_array(mysql_query($sqlCount));
$totalData = $rsCount[0];
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$limit = 10;
$start_from = $limit * ($page - 1);
$sql_limit = "SELECT * FROM users limit $start_from, $limit";
$result = mysql_query($sql_limit);
while ($data = mysql_fetch_array($result)) {
?>
<td><?php echo $data['no_peg']; ?></td>
<td>
<?php
echo $data['username'];
//previlage admin
if ($_SESSION['role'] == 'admin') {
?>
<div class="row-actions">
Edit
<?php if ($data['role'] != 'admin') {?>
| Delete
<?php } ?>
</div>
<?php } ?>
</td>
<td><?php echo $data['fullname']; ?></td>
<td><?php echo $data['telephone']; ?></td>
<td><?php echo $data['email']; ?></td>
</tr>
<?php
}
?>
<?php
$totalPage = ceil($totalData / $limit);
echo 'Page : ';
for($i = 1; $i <= $totalPage; $i++){
if($page != $i){
echo '['.$i.'] ';
}else{
echo "[$i] ";
}
}
?>
Where is your pagination code? Your code just shows this query:
$query = mysql_query("select * from users");
But for basic pagination you need to set a LIMIT like so:
$query = mysql_query("select * from users LIMIT 0,10");
So that would only grab the first 10 items. And then—let’s say, on page 2 you could do this:
$query = mysql_query("select * from users LIMIT 11,10");
That would grab the next 10 items starting from item 11.
That’s the basic concept. But you have to code the logic for passing along pagination values & such.
<?php
if(is_int($_GET('pageNo'))) // getting the page number from the URL i.e script.php?pageNo=2
{
$query = mysql_query("select * from users limit ".$_GET['pageNo']." ,10");
while ($data = mysql_fetch_array($query)) {
?>
<td><?php echo $data['no_peg']; ?></td>
<td>
<?php
echo $data['username'];
//previlage admin
if ($_SESSION['role'] == 'admin') {
?>
<div class="row-actions">
Edit
<?php if ($data['role'] != 'admin') {?>
| Delete
<?php } ?>
</div>
<?php } ?>
</td>
<td><?php echo $data['fullname']; ?></td>
<td><?php echo $data['telephone']; ?></td>
<td><?php echo $data['email']; ?></td>
</tr>
<?php
}
} // not sure where the closing if should be you figure it out :P
?>

Go to next row when HTML table is full

I am using an HTML table to display data from a MySQL table using PHP. I need it so once the table has 10 columns, it will move on to the next row.
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr width="100%">';
while($row = mysqli_fetch_array($result))
{
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";
mysqli_close($con);
?>
How can this be done?
Untested but something like this should work or get you started in a good direction:
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr width="100%">';
$x=0;
while($row = mysqli_fetch_array($result))
{
if($x==0){
echo "<tr>\n";
}elseif($x%10){
echo"</tr><tr>\n";
}
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
$x++;
}
echo "</tr></table>";
mysqli_close($con);
?>
Add a counter to your loop starting at one.
Each time through the loop if the remainder after dividing the counter value by 10 is 1
add the <tr>. If the remainder is 0 then add a </tr> Then after the loop a </tr> if the remainder is not evenly divisible by 10.
<?php
echo '<table width="100%" border="1px"><tr width="100%">';
$i = 0;
while($row = mysqli_fetch_array($result))
{
$i++;
?>
<?php if ($i%10 ==1): ?><tr><?php endif; ?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php if ($i%10 ==0): ?></tr><?php endif; ?>
<?php
}
if ($i%10 != 0) echo "</tr>";
echo "</tr></table>";
Using modulo (%)
After each 10th cell, if a new cell is added, the current row is closed and a new row is opened first, before outputting the cell.
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr>';
$cell = 0;
while($row = mysqli_fetch_array($result))
{
if ($cell++ % 10 == 0 && $cell > 1)
{
?>
</tr><tr>
<?php
}
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";
mysqli_close($con);
?>
The extra condition && $cell > 1 seems to be a little odd, but without it, you will get an empty row to start with. Eliminating it by putting ++ before $cell will cause the first row to be 9 cells instead of 10. Putting $cell > 0 && in front of the modulo will cause cell never to be incremented, because the first part of the expression is always false. Moving the if to execute it after outputting the cell, would cause the risk of ending with an empty row. It could be solved using a do..while loop, but you'd have to check up front if you have one row at least.
Long story short: use the code above. :)
Using a simple counter and reset it after each row
I think it's even more readable without the modulo, though you'd have to initialize $cell to -1 to prevent the first row to be 9 cells. Nevertheless, I think this is cleaner:
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr>';
$cell = -1;
while($row = mysqli_fetch_array($result))
{
if (++$cell == 10)
{
$cell = 0;
?>
</tr><tr>
<?php
}
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";
mysqli_close($con);
?>
<table>
<tr>
<?php
$endRow = 0;
$columns = 10; // number of columns
$hloopRow1 = 0;
do {
if($endRow == 0 && $hloopRow1++ != 0) echo "<tr>";
?>
<td>
<?php echo $row['Name']; ?>
</td>
<?php $endRow++; if($endRow >= $columns) { ?>
</tr>
<?php $endRow = 0; }
} while ($row = mysql_fetch_assoc($result));
if($endRow != 0) {
while ($endRow < $columns) {
echo("<td> </td>");
$endRow++;
}
echo("</tr>");
}
?>
</table>
This should work fine. Hope this helps.

Fetch mysql result in columns

I wish to divide my mysql fetched list in 2 columns. Eg.
A | D
B | E
C | F
I want to do it using table instead of < ul>< li>
<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$total_rows = mysql_num_rows($sql);
while ( $result = mysql_fetch_assoc($sql) ) { ?>
<tr>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
</tr>
<?php } ?>
I have this structure.
NOTE - I wish to perform my solutions using PHP not by anysort of jquery and javascript.
<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$total_rows = mysql_num_rows($sql);
$resultSet = array();
while ( $row = mysql_fetch_assoc($sql) ) {
$resultSet[] = $row;
}
$halfLength = count($resultSet) / 2;
for ($i = 0; $i < $halfLength; $i++) {
?>
<tr>
<td class="navigation"><?php echo fetch_table_poet( $resultSet[$i]["pid"], 1, 3); ?></td>
<td class="navigation"><?php echo fetch_table_poet( $resultSet[$halfLength + $i]["pid"], 1, 3); ?></td>
</tr>
<?php } ?>

PHP: End and start a new <tr> after 6 rows

I want to make a table, and then for each 6 rows should have a tr, and then the rows are inside td.
So example:
<tr>
<td><?php echo $show[id]; ?></td> // 1
<td><?php echo $show[id]; ?></td> // 2
<td><?php echo $show[id]; ?></td> // 3
<td><?php echo $show[id]; ?></td> // 4
<td><?php echo $show[id]; ?></td> // 5
<td><?php echo $show[id]; ?></td> // 6
</tr> <tr> // start new tr after 6 rows
...repeat the tds
How can i do something like this?
I have tried myself doing
<tr>
<?php
while ($show == mysql_fetch_array($query)){ ?>
<td><?php echo $show[id]; ?></td>
<?php } ?>
</tr>
But as you can see this just inserts everything in one tr..
Thank you
<tr>
<?php
$c = 0; // Our counter
$n = 6; // Each Nth iteration would be a new table row
while ($show = mysql_fetch_array($query))
{
if($c % $n == 0 && $c != 0) // If $c is divisible by $n...
{
// New table row
echo '</tr><tr>';
}
$c++;
?>
<td><?php echo $show[id]; ?></td>
<?php
} ?>
</tr>
Related links:
Modulus operator (%) on php.net
Count the number of lines if the modulo 6 is null then echo the </tr><tr>
<tr>
<?php
$i=0;
while ($show == mysql_fetch_array($query)){ ?>
<td><?php echo $show[id]; ?></td>
<?php if(++$i%6 == 0) echo '</tr><tr>'; ?>
<?php } ?>
</tr>
You can do:
<tr>
<?php
$count = 0;
while ($show = mysql_fetch_array($query)){
if($count == 6) {
$count = 0;
echo "</tr> <tr>";
}
echo "<td>".$show[id]."</td>";
$count++;
}
</tr>
You’re accidentally using the comparison operator == instead of an assignment operator =.
And to always put six cells into each row, I’d do this:
$perRow = 6;
$counter = 0;
echo '<tr>';
while ($show = mysql_fetch_array($query)) {
if ($counter % $perRow === 0 && $counter !== 0) {
echo '</tr><tr>';
}
echo '<td>', $show['id'], '</td>';
$counter++;
}
while ($counter++ % $perRow !== 0) {
echo '<td></td>';
}
echo '</tr>';
This will ensure that each row is properly filled with six cells.
<?php
$countRows = 0;
while ($show == mysql_fetch_array($query)){
if($countRows == 0) echo '<tr>';
?>
<td><?php echo $show[id]; ?></td>
<?php
$countRows++;
if($countRows == 6){
$countRows = 0;
echo '</tr>';
}
?>
<?php } ?>
<?php if($countRows < 6) echo '</tr>'; ?>

Categories