Jump to content

Archived

This topic is now archived and is closed to further replies.

digitalgod

pagination question

Recommended Posts

hey guys,

I was wondering if it's possible to print info in an html table and if there are more than 48 cells it starts a new column?

something like this

-------------------------------------
|                  |
|   INFO 1      |    INFO 49
|                  |
-------------------------------------
|                  |
|   INFO 2      |   etc
|                  |
-------------------------------------
|                  |
|up to info 48 |
|                  |
-------------------------------------

and if there's more than 96 it will create a new page. Any ideas how to do that?

Share this post


Link to post
Share on other sites
try[code]
<?php
mysql_connect("", "", "");
mysql_select_db("");
$result = mysql_query('Select count(*) from your_table');
$num_records = mysql_result($result,0,0);
$max_num_rows = 48;
$max_num_columns = 2;
$per_page = $max_num_columns * $max_num_rows;
$total_pages = ceil($num_records / $per_page);
if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1;
$start = ($page - 1) * $per_page;
$result = mysql_query("select info from your_table limit $start, $per_page");
$num_columns = ceil(mysql_num_rows($result)/$max_num_rows);
$num_rows = min($max_num_rows,mysql_num_rows($result));
echo "<table border=\"2\">\n";
for ($r = 0; $r < $num_rows; $r++){
echo "<tr>\n";
for ($c = 0; $c < $num_columns; $c++){
$x = $c * $num_rows + $r;
if ($x < $num_records) $y = mysql_result($result, $x, 'info'); else $y = '&nbsp;';
echo "<td>$y</td>";
}
echo "</tr>\n";
}
echo "</table>\n";
for ($i=1;$i <= $total_pages;$i++) {
if ($i == $page) echo " $i "; else echo " <a href=\"?page=$i\">$i</a> ";
}
?>
[/code]

Share this post


Link to post
Share on other sites
ok right now this is way beyond me!!...

can you please explain your code... I replaced the queries with my own and it's not working... all I get is a blank page and I really don't even know where to start...

the queries need to be dynamic so I'm using $_GET and this is what I have with your code

[code=php:0]
if (isset($_GET['club']) && isset($_GET['date'])) {
$result = mysql_query("SELECT COUNT(*) FROM listarchive WHERE date = ".$_GET['date']." AND club = ".$_GET['club']." ");
$num_records = mysql_result($result,0,0);
$max_num_rows = 48;
$max_num_columns = 2;
$per_page = $max_num_columns * $max_num_rows;
$total_pages = ceil($num_records / $per_page);
if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1;
$start = ($page - 1) * $per_page;
$result = mysql_query("SELECT * FROM listarchive LIMIT $start, $per_page WHERE date = ".$_GET['date']." AND club = ".$_GET['club']."");
$num_columns = ceil(mysql_num_rows($result)/$max_num_rows);
$num_rows = min($max_num_rows,mysql_num_rows($result));
echo "<table border=\"2\">\n";
for ($r = 0; $r < $num_rows; $r++){
echo "<tr>\n";
for ($c = 0; $c < $num_columns; $c++){
$x = $c * $num_rows + $r;
if ($x < $num_records) $y = mysql_result($result, $x, 'info'); else $y = '&nbsp;';
echo "<td>$y</td>";
}
echo "</tr>\n";
}
echo "</table>\n";
for ($i=1;$i <= $total_pages;$i++) {
if ($i == $page) echo " $i "; else echo " <a href=\"?page=$i\">$i</a> ";
}
[/code]

date looks like 0000-00-00

*edit*

found out why it wasn't displaying anything... Now I get an error saying Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
for lines 279 and 280 which are:

[code]
<?php
$num_columns = ceil(mysql_num_rows($result)/$max_num_rows);
$num_rows = min($max_num_rows,mysql_num_rows($result));
?>
[/code]

Share this post


Link to post
Share on other sites
Here is a good tutorial for pagaination and there is also a forum for asking questions about the code

http://www.allsyntax.com/tutorials/PHP/23/Pagination-with-PHP/1.php

Share this post


Link to post
Share on other sites
that's really not what I'm looking for, I know how to do pagination, what I don't know how to do is display information like I stated in my original post

[quote author=digitalgod link=topic=105519.msg421543#msg421543 date=1156446671]
hey guys,

I was wondering if it's possible to print info in an html table and if there are more than 48 cells it starts a new column?

something like this

-------------------------------------
|                  |
|   INFO 1      |    INFO 49
|                  |
-------------------------------------
|                  |
|   INFO 2      |   etc
|                  |
-------------------------------------
|                  |
|up to info 48 |
|                  |
-------------------------------------

and if there's more than 96 it will create a new page. Any ideas how to do that?
[/quote]

Share this post


Link to post
Share on other sites
change line[code]$result = mysql_query("SELECT COUNT(*) FROM listarchive WHERE date = ".$_GET['date']." AND club = ".$_GET['club']." ");[/code]to[code]$result = mysql_query("SELECT COUNT(*) FROM listarchive WHERE date = '".$_GET['date']."' AND club = '".$_GET['club']."' ");[/code]add some '

Share this post


Link to post
Share on other sites
yeah I did do that before and it worked well after that. I have another problem now, I need it to create empty cells if there isn't enough info. So if there's only 15 names it will still create 96 cells but only 15 of them will have info.. is that doable?

Share this post


Link to post
Share on other sites
ok here's what I got so far

[code=php:0]
$total_result = mysql_query("SELECT COUNT(*) FROM listarchive WHERE date = '".$_GET['date']."' AND club = '".$_GET['club']."' ");
$num_records = mysql_result($total_result,0,0);
$max_num_rows = 48;
$max_num_columns = 2;
$per_page = $max_num_columns * $max_num_rows;
$total_pages = ceil($num_records / $per_page);
if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1;
$start = ($page - 1) * $per_page;
$list_result = mysql_query("SELECT * FROM listarchive WHERE date = '".$_GET['date']."' AND club = '".$_GET['club']."' ORDER BY name LIMIT $start, $per_page");
$num_columns = ceil(mysql_num_rows($list_result)/$max_num_rows);
$num_rows = min($max_num_rows,mysql_num_rows($list_result));
echo "<table border=\"1\">\n";
echo "<tr><td width=197><div align='center'><strong>name/nom + #</strong></div></td><td height=17 width=21><strong>Girl</strong></td><td height=17 width=21><strong>Guy</strong></td></tr>\n";
for ($r = 0; $r < $max_num_rows; $r++){
echo "<tr>\n";
for ($c = 0; $c < $max_num_columns; $c++){
$x = $c * $num_rows + $r;
if ($x < $num_records) {
$y = mysql_result($list_result, $x, 'name');
$z= "+" . mysql_result($list_result, $x, 'number');
} else {
$y = '&nbsp;';
$z = '&nbsp;';
}
echo "<td>$y $z</td>";
echo "<td>&nbsp;</td>";
echo "<td>&nbsp;</td>";

}
echo "</tr>\n";

}
echo "</table>\n";
for ($i=1;$i <= $total_pages;$i++) {
if ($i == $page) echo "Page $i "; else echo " <a href=\"?page=$i\">$i</a> ";
}
[/code]

so that fills up everything that doesn't have info with empty spaces but I just need it to do
name/nom + #  | Girl  | Guy|

again when it starts the second column... any ideas no how to do that?

Share this post


Link to post
Share on other sites
change to [code]if ($x < $num_records) {
  $y = mysql_result($list_result, $x, 'name');
  $z= "+" . mysql_result($list_result, $x, 'number');
  $col2 = '&nbsp;';
  $col3 = '&nbsp;';
} else {
  $y = '&nbsp;';
  $z = '&nbsp;';
  $col2 = 'Girl';
  $col3 = 'Guy';
}
echo "<td>$y $z</td>";
echo "<td>$col2</td>";
echo "<td>$col3</td>";[/code]

Share this post


Link to post
Share on other sites
thanks but that's not what I meant. I need the top part of the tables to display what each column is

so I have

-------------------------------------------------------------------
| name/nom + #    |  Guy  |  Girl  | name/nom + #    |  Guy  |  Girl  |
--------------------------------------------------------------------
| INFO 1              |  &nbsp;|&nbsp;|    INFO 49          |&nbsp;| &nbsp;|
----------------------------------------------------------------------

etc

sorry for the way the table looks lol, but I hope you understand what I mean.

I really appreciate your help

Share this post


Link to post
Share on other sites
try[code]...
$num_rows = min($max_num_rows,mysql_num_rows($list_result));
echo "<table border=\"1\">\n<tr>";
for ($i = 0 ; $i < $max_num_columns; $i++)
echo "<td width=197><div align='center'><strong>name/nom + #</strong></div></td><td height=17 width=21><strong>Girl</strong></td><td height=17 width=21><strong>Guy</strong></td>";
echo "</tr>\n";[/code]

Share this post


Link to post
Share on other sites
gives me the same result as what I did before... unless I'm doing something wrong

[code=php:0]
$num_rows = min($max_num_rows,mysql_num_rows($list_result));
echo "<table class='guestlist'>\n";
for ($i = 0 ; $i < $max_num_columns; $i++)
echo "<tr><td width=197><div align='center'><strong>name/nom + #</strong></div></td><td height=17 width=21><img width=17 height=17 src='images/boy.jpg'></td><td height=17 width=21><img width=15 height=17 src='images/girl.jpg'></td></tr>\n";
echo "</tr>\n";

for ($r = 0; $r < $max_num_rows; $r++){
echo "<tr>\n";
for ($c = 0; $c < $max_num_columns; $c++){
$x = $c * $num_rows + $r;
if ($x < $num_records) {
$y = mysql_result($list_result, $x, 'name');
$z= "+" . mysql_result($list_result, $x, 'number');
/*$promo =  mysql_result($list_result, $x, 'promo_code');
if ($promo != "") {
$promo = "(".$promo.")";
}*/
} else {
$y = '&nbsp;';
$z = '&nbsp;';
//$promo = '&nbsp;';
}
echo "<td>$y $z </td>";
echo "<td>&nbsp;</td>";
echo "<td>&nbsp;</td>";

}
echo "</tr>\n";

}
echo "</table>\n";
for ($i=1;$i <= $total_pages;$i++) {
if ($i == $page) echo "Page $i "; else echo " <a href=\"?page=$i\">$i</a> ";
}
[/code]

Share this post


Link to post
Share on other sites
[quote author=digitalgod link=topic=105519.msg426200#msg426200 date=1157144406]
gives me the same result as what I did before... unless I'm doing something wrong

[code=php:0]
$num_rows = min($max_num_rows,mysql_num_rows($list_result));
echo "<table class='guestlist'>\n";
for ($i = 0 ; $i < $max_num_columns; $i++)
echo "<tr><td width=197><div align='center'><strong>name/nom + #</strong></div></td><td height=17 width=21><img width=17 height=17 src='images/boy.jpg'></td><td height=17 width=21><img width=15 height=17 src='images/girl.jpg'></td></tr>\n";
echo "</tr>\n";

for ($r = 0; $r < $max_num_rows; $r++){
echo "<tr>\n";
for ($c = 0; $c < $max_num_columns; $c++){
$x = $c * $num_rows + $r;
if ($x < $num_records) {
$y = mysql_result($list_result, $x, 'name');
$z= "+" . mysql_result($list_result, $x, 'number');
/*$promo =  mysql_result($list_result, $x, 'promo_code');
if ($promo != "") {
$promo = "(".$promo.")";
}*/
} else {
$y = '&nbsp;';
$z = '&nbsp;';
//$promo = '&nbsp;';
}
echo "<td>$y $z </td>";
echo "<td>&nbsp;</td>";
echo "<td>&nbsp;</td>";

}
echo "</tr>\n";

}
echo "</table>\n";
for ($i=1;$i <= $total_pages;$i++) {
if ($i == $page) echo "Page $i "; else echo " <a href=\"?page=$i\">$i</a> ";
}
[/code]
[/quote]move <tr> tag from line 4 to line 2
look previous post

Share this post


Link to post
Share on other sites
got another question, would there be any way to change the code so that instead of having to click on Page 2, Page 3 etc they're all displayed at the same time and there's just a page break between them?

Share this post


Link to post
Share on other sites
I'm guessing this is for printing purposes, otherwise, why would you need a page break.  If so, then the answer is no, you'd have to work out how many rows you have that fit on a page and pad out with <br/> tags accordingly.

Regards
Huggie

Share this post


Link to post
Share on other sites
well right now everything appears on different pages, if I can have them all appear on the same page that would be great but I"m not sure how to do it

Share this post


Link to post
Share on other sites
You need to remove the pagination that you just put in...

It shouldn't be too difficult to achieve.

Regards
Huggie

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.