Jump to content


Photo

pagination question


  • Please log in to reply
19 replies to this topic

#1 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 24 August 2006 - 07:11 PM

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?

#2 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 25 August 2006 - 07:51 AM

try
<?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> ";
}
?>


#3 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 29 August 2006 - 09:46 PM

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

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> ";
}

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:

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


#4 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 30 August 2006 - 11:14 PM

anyone?

#5 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 30 August 2006 - 11:18 PM

Here is a good tutorial for pagaination and there is also a forum for asking questions about the code

http://www.allsyntax...-with-PHP/1.php
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#6 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 30 August 2006 - 11:31 PM

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

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?



#7 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 31 August 2006 - 03:33 PM

change line
$result = mysql_query("SELECT COUNT(*) FROM listarchive WHERE date = ".$_GET['date']." AND club = ".$_GET['club']." ");
to
$result = mysql_query("SELECT COUNT(*) FROM listarchive WHERE date = '".$_GET['date']."' AND club = '".$_GET['club']."' ");
add some '

#8 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 01 September 2006 - 02:33 AM

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?

#9 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 01 September 2006 - 06:36 PM

ok here's what I got so far

$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> ";
}

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?

#10 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 01 September 2006 - 07:28 PM

change to
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>";


#11 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 01 September 2006 - 08:41 PM

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

#12 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 01 September 2006 - 08:52 PM

try
...
$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";


#13 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 01 September 2006 - 09:00 PM

gives me the same result as what I did before... unless I'm doing something wrong

$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> ";
}


#14 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 01 September 2006 - 09:07 PM

gives me the same result as what I did before... unless I'm doing something wrong

$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> ";
}

move <tr> tag from line 4 to line 2
look previous post

#15 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 05 September 2006 - 05:44 PM

thanks it worked perfectly!!

#16 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 14 September 2006 - 08:55 PM

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?

#17 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 18 September 2006 - 05:39 PM

anyone??

#18 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 18 September 2006 - 09:49 PM

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
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#19 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 18 September 2006 - 11:59 PM

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

#20 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 September 2006 - 07:10 AM

You need to remove the pagination that you just put in...

It shouldn't be too difficult to achieve.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users