In the following codes, its coming numbers 1, 2, 3, 4, 5 .....and so on but i need that number in 5, 4, 3, 2,1 like that
<?php
//$events = $db->select("select * from main_menu where mm_menu='4' and mm_status='1' and mm_project='1' order by mm_order asc limit 0,10");
$events = $db->select("select * from main_menu where mm_menu='4' and mm_status='1' and mm_project='1' order by mm_id desc");
if (count($events) > 0) {
$i = 1;
foreach ($events as $event_info) {
$clsIn = ($i==1) ? "in" : "";
$clsOut = ($i==1) ? "" : "collapsed";
?>
<tr>
<td><?php echo $i ?></td>
<td><?php echo $event_info['mm_title'];?></td>
<td><?php echo html_entity_decode($event_info['mm_content']);?></td>
</tr>
<?php
$i++;
}
}?>
If you want to count down, set $i to the total number of events, and then decrement it:
First:
$i = count($events);
and then at the end of the loop:
$i--;
Related
I am going to create a pagination then
$num_row = mysqli_num_rows($res);
total I have 240 rows then:
if($num_row>5){
$count=0;
$index=1;
while($count < $num_row && $index < 5){ ?>
<button id="<?php echo $count; ?>" class="index_b"><?php echo $index; ?></button>
<?php
$count = $count + 5;
$index++;
}
}
I want first 1 2 3 4 5 then next button so I tried
<div class="next_index_b">
<?php if($num_row > 50){?>
<button class="next_membd_index_button" name="50" ><img src="images/arrow.png" /></button>
<?php } ?>
Now my issue is after 4th pagination number, no number is working.
Don't use mysqli_num_rows()! Ideally, you should pull ONLY the data that you're looking for, and then run 2 separate queries. One to fetch the full count, and one to fetch the items per page:
$db = new PDO(...);
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$offset = ($page-1) * 50;
$count = $db->query("SELECT COUNT(*) FROM table")->fetchColumn();
$trans = $db->prepare("SELECT * FROM table LIMIT :offset, 50");
$trans->bindValue(':offset', $offset, PDO::PARAM_INT);
$trans->execute();
while ($row = $trans->fetchRow()) {
// echo data
}
//Add footer here. Use $page variable and $count to determine how many pages are available.
while($count < $num_row && $index < 5){ ?>
This condition means that when $index equals 5, the loop is stopped
Therefore, the button with 5 will never be shown.
Try using less or equal than instead of less than
if($num_row>5){
$count=0;
$index=1;
while($count < $num_row && $index <= 5){ ?>
<button id="<?php echo $count; ?>" class="index_b"><?php echo $index; ?></button>
<?php
$count = $count + 5;
$index++;
}
}
Or even better, switch to #FrankerZ's solution
Simple question of course But I still could not figure it till now :) Im pulling names out of an sql and I want to be like 1 2 3 4,then next Line like 5, 6, 7, 8 Like So
<a>Bob</a>, <a>John</a>, <a>Bob</a>, <a>John</a> <BR>
<a>Bob</a>, <a>Ion</a>, <a>Bob</a>, <a>Pon</a> <BR>
Well thats What I want it To look like But For Now Its One Row Endless with no spaces is what I acheved so far Like So :)
<?PHP
require('connect.php');
$sql="SELECT * FROM profile";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
$name =$row['username'];
$power =$row['power'];
?>
<span class='user-group-<?PHP echo $power; ?>'><?PHP echo $name; ?></span> ,
<?PHP } ?>
Now What Do I need to do to have every forth result + that space :?
Use an $i counter:
require('connect.php');
$sql = "select * from profile";
$result = mysql_query($sql);
// Start at 1
$i = 1;
while($row = mysql_fetch_array($result)){
$name = $row['username'];
$power = $row['power']; ?>
<span class='user-group-<?php echo $power; ?>'><?PHP echo $name; ?></span>
<?php
// When hit 4, add <br />
if($i == 4) {
echo '<br />';
// Reset to 0. $i will then auto-increment
// to 1 when hits $i++
$i = 0;
}
else
// Add comma if not equal to 4.
echo ',';
$i++;
}
I use the following to print a 15-row, two-column table from an associative arrray in PHP:
<table id="freq-table" class="table-list" cellspacing="0" cellpadding="10">
<tbody>
<?
$rowCount = 0;
foreach($word_frequency as $word => $frequency) if ($rowCount++ < 15) {?>
<tr>
<td><? echo $word; ?></td>
<td<? echo $frequency; ?></td>
</tr>
<?}?>
</tbody>
</table>
It works fine but takes up too much vertical space on the page. How can I format this into three side-by-side tables of five rows and two columns each with the first group having array items 1-5, the second group 6-10, and the last group 11-15? (refer to following illustration):
key1 value1 | key6 value6 | key11 value11
key2 value2 | key7 value7 | key12 value12
...
...
key5 value5 | key10 value10 | key15 value15
I've tried various table, div, container, and multiple loop experiments with very mixed (and unsuitable) results. Thank you in advance.
Since you can also use multiple columns to achieve this visual effect, you're going to probably want to format the data ahead of time to make the code of generating the table a little cleaner.
<?php
// Format the array data so each row contains all of the columns needed
$rows = array();
$max_per_column = 5;
$max_words = 15;
$rows = array_pad($rows, $max_per_column, array());
$count = 0;
foreach ($word_frequency as $word => $frequency) {
if ($count >= $max_words) {
break;
}
array_push($rows[$count % $max_per_column], $word, $frequency);
$count++;
}
?>
<table id="freq-table" class="table-list" cellspacing="0" cellpadding="10">
<tbody>
<?php
foreach ($rows as $cols) {
echo '<tr><td>' . implode('</td><td>', $cols) . '</td></tr>';
}
?>
</tbody>
</table>
Try some thing like that.
<table width="100%">
<tr>
<?php
$rowCount = 0;
foreach($word_frequency as $word => $frequency) if ($rowCount < 15) { ?>
<?php if($rowCount % 5 == 0) { ?>
<td><table border=1>
<?php } ?>
<tr>
<td><?php echo $word; ?></td>
<td><?php echo $frequency; ?></td>
</tr>
<?php if($rowCount % 5 == 4) { ?>
</table></td>
<?php } ?>
<?php $rowCount++; } ?>
</tr>
</table>
The only way to do this is to use a numeric key. For example
$word_frequency[0] = array();
$word_frequency[0]['word'] = "some word";
$word_frequency[0]['frequency'] = 10;
...
$word_frequency[15] = array();
$word_frequency[15]['word'] = "some other word";
$word_frequency[15]['frequency'] = 14;
Once you have your array, you could repeat the loop as follows
<table id="freq-table" class="table-list" cellspacing="0" cellpadding="10">
<tbody>
<?
$rowCount = 0;
for($i = 0; $i <= 5; $i++) {
$word[0] = $word_frequency[$i]["word"];
$frequency[0] = $word_frequency[$i]["frequency"];
$word[1] = $word_frequency[$i + 5]["word"];
$frequency[1] = $word_frequency[$i + 5]["frequency"];
$word[2] = $word_frequency[$i + 10]["word"];
$frequency[2] = $word_frequency[$i + 10]["frequency"];
?>
<tr>
<?
for($x = 0; $x <= 2; $x++ ){
?>
<td><? echo $word[$x]; ?></td>
<td<? echo $frequency[$x]; ?></td>
<?
}
?>
</tr>
<?
}
?>
</tbody>
</table>
Every $i loop creates the row, while the $x loop creates the columns. This assumes, of course, that you have at least 15 items, that you're going to create only 5 rows and three columns of key/value pairs.
Well, if I were doing it, I'd probably use a ul with a css columns :D
http://davidwalsh.name/css-columns
However, that's not a very "programmerly" thing to do.
It might help that you don't have to go through the array in order.
If you look at what you have, there is a pattern to the indices:
i + 0x, i + 1x, i + 2x, ...
Where x is your number of items divided by the number of columns.
and you stop iterating when i = total / x
Sorry to be too tired to code this for you but the gist is this:
$myarray = [1 ... 15];
$columns = 3;
$arrayKeys = array_keys($myarray);
$total = count($array);
$x = floor $total / $column;
for ($i = 0; $i > $x; $i += $x + 1 ){
echo $arrayKeys[$i] . " - " . $myarray($arrayKeys[$i]);
echo $arrayKeys[$i + $x] . " - " . $myarray($arrayKeys[$i + $x]);
echo $arrayKeys[$i + ($x * 2) ] . " - " . $myarray($arrayKeys[$i + ($x * 2)]);
}
Or something like that... that's off the top of my head and I think it has some issues, but you should be able to beat that into something that works.
<table><tr>
<?php
for($i=0;$i<15;$i++) {
if($i%5 == 0) {echo '</tr> <tr>';}
?><td><?php echo $i ?></td>
<?php
}?>
</tr>
</table>
this generate:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
how can I make:
0 3 6 9 12
1 4 7 10 13
2 5 8 11 14
?
You need a nested loop
<table>
<?php
$rows = 3;
for($i=0 ; $i < $rows ; $i++ ) {
echo "<tr>";
for( $j = 0 ; $j < 5 ; $j++ ) {
echo "<td>" . ($j * $rows + $i) . "</td>";
}
echo "</tr>";
}
?>
</table>
I tried to make the variable names descriptive:
<table>
<?php
// Set the number of rows, cols, and starting number here:
$number_rows = 3;
$number_cols = 5;
$starting_num = 0;
// You can use foreach w arrays... much easier
$rows = range(0,$number_rows - 1);
$cols = range(0,$number_cols - 1);
foreach($rows as $one_row) {
?>
<tr>
<?php
foreach($cols as $one_col) {
// Do the calculation
echo "<td>" .
($starting_num + $one_col + ( max($rows) * $one_col ) + $one_row) .
"</td>";
}
?>
</tr>
<?php
}
?>
</table>
Working example like in the OP.
Now let's say you want to go from (1300-1431), then you start at 1300 and want a 4 x 8.
$number_rows = 8;
$number_cols = 4;
$starting_num = 1300;
Like this.
There are two "tricks."
The first is using range() to quickly define an array integers. I find arrays more pleasant to work with than raw numbers, since arrays can be worked on with the intuitive construct of foreach().
The second is figuring out how to know what number to print if you have the column, row, and starting, number. It's simplest to figure out the numbers for the first row and go from there. Let's number the rows and cols from 0. So col:0 row:0 will be the starting number. Col:1 Row:0, the number to the right, will just be the starting number + one (the column number) + the number of rows less one (easiest to see by looking at the matrix of number), and the number of rows less one is the maximum number in the rows array. So for the first row we have:
$starting_num + $one_col + ( max($rows) * $one_col )
then all we just add the row number to take that into account, and we've got it all:
$starting_num + $one_col + ( max($rows) * $one_col ) + $one_row
Tested example with given start and end:
<table>
<?php
$start = 1300;
$end = 1432;
$n = $end - $start + 1;
$cols = 5;
$rows = ceil($n / $cols);
for($i=0 ; $i < $rows ; $i++ ) {
echo "<tr>";
for( $j = 0 ; $j < $cols ; $j++ ) {
$val = $j * $rows + $i;
echo "<td>";
echo ($val < $n) ? $val + $start : ' ';
echo "</td>";
}
echo "</tr>";
}
?>
</table>
I am realy stuck with part of my php code! I have a range of numbers from 1 to 40 say that I pull from a table in my database and out put onto the screen using a while loop! with these numbers I am using a submit button which i will replace with a image button later on! Just now i can only get them in one line using a table but I want to get them into groups of say 5 or so colums and then go to next line print the next 5 or so colums! I have been trying for loops but they print out 1111, 2222, 3333, 4444, etc. in diffrent lines which is not what i want! I want,
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
etc
Please help me i have been baffeld with this for ages this is my code so far!
<?
$q3 = "SELECT * FROM tblgame";
$r3 = mysql_query($q3);
while($row2 = mysql_fetch_array($r3))
{
$game_cost = $row2['game_cost'];
echo "<p>Game ID: ".$row2['game_id'];
echo "<br>Game Day: ".$game_day;
echo "<br>Next Game Date: ".$row2['game_date_day']."/".$row2['game_date_month']."/".$row2['game_date_year'];
$fyear = $row2['game_date_year'];
$fmonth = $row2['game_date_month'];
$fday = $row2['game_date_day'];
$tmonth = $date_month;
$tyear = $date_year;
$tday = $date_day;
$days_between = abs((mktime ( 0, 0, 0, $fmonth, $fday, $fyear) - mktime ( 0, 0, 0, $tmonth, $tday, $tyear))/(60*60*24));
echo "<br>Days till next draw: ". $days_between;
echo "<br />Game Cost: £".$row2['game_cost']."</p>";
?>
<table>
<tr>
<?
$q4 = "SELECT * FROM tblnewgameitem WHERE game_id = $row2[game_id] ORDER BY 'game_number' ";
$r4 = mysql_query($q4);
$n1 = mysql_num_rows($r4);
$i=0;
while($row3 = mysql_fetch_array($r4))
{
?>
<td>
<?
if($row3['user_email'] == "")
{
?>
<form action="buyanumber.php" method="POST">
<input type="hidden" name="game_id" value="<?echo $row2['game_id'];?>">
<input type="hidden" name="num" value="<? echo $row3['game_number']; ?>">
<input type="submit" value="<?echo $row3['game_number'];?>" name="submit">
</form>
<?
}
else
{
$n = $n + 1;
echo " ".$row3['game_number']." ";
}
?>
</td>
<?
}
?>
</tr>
</table>
Please help me i have been stuck on this problem for a good number of days and its driving me loopy lol!
Thank you
Stephen
nested for-loops is the key:
$list=array();
while($row2 = mysql_fetch_array($r3)) $list[] = $row2;
$countList = count($list);
$cols = 5;
$rows = ceil($countList / $maxPerRow);
for ($i=0; $i<$rows; $i++) {
echo 'opening stuff per row... <tr> or something';
for ($j=0; $j<$rows; $j++) {
echo 'your stuff per item... might be somthing like <td>s';
}
echo 'closing stuff per row... </tr> or something';
}
something like this
I'm assuming that the 'real' problem lays somewhere between <table> and </table> and that you want to put the values returned by the fourth query (for some reason mapped to $row3) in a table with 5 columns.
Using the PHP modulo operator ('%'), you could do something like this:
<?php
$r4 = <your mysql_query>
$i = 0;
while ($row3 = mysql_fetch_array($r4)) {
if ($i % 5 == 0) { // true for 0, 5, 10, ...
echo "<tr>";
}
echo "<td>";
// what you want to put between your <td> tags comes here
echo "</td>";
if (($i+1) % 5 == 0) { // true for 4, 9, 14, ...
echo "</tr>";
}
$i++;
}
// if the number of rows is not a multiple of 5, we must clean up after ourselves:
if ($i % 5 != 0) {
echo "<td colspan=\"" + (5-($i % 5)) + "\"> </td></tr>";
}
?>
I haven't tested the code myself.
For the sake of clearity, please consider using proper indentation and consistent variable naming in your code. Also note that using a proper title and less exclamation marks in your question would improve the clearity of it.