954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do you only display 8 rows per page? PHP/MYSQL

In my last topic i quess i was asking to much so no one would help me, so this time ill take it one step at a time, heres my first problem.

<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM tutorials WHERE tut_program='Photoshop' AND tut_category='Text Effects' ";
$result=mysql_query($query);

$num=mysql_numrows($result); 

mysql_close();

echo "<b><center>Database Output</center></b>";

?>
<link href="file:///C|/Users/Scott/SlyGraphics/style.css" rel="stylesheet" type="text/css" />

<title>SlyGraffix: Database Test</title><table width="287" border="0" align="center" cellpadding="2" cellspacing="0">
             
<?
$i=0;
while ($i < $num) {
$tut_name=mysql_result($result,$i,"tut_name");
$tut_link=mysql_result($result,$i,"tut_link");
$tut_program=mysql_result($result,$i,"tut_program");
$tut_category=mysql_result($result,$i,"tut_category");
$tut_video=mysql_result($result,$i,"tut_video");
$tut_download=mysql_result($result,$i,"tut_download");
$tut_image=mysql_result($result,$i,"tut_image");
?>

 <tr>
                <td width="140"><a href="<? "$tut_link"; ?>"><img src="images/tutorials/fire_reflection.jpg" alt="d" width="140" height="120" border="0" /></a></td>
                <td width="139" valign="top"><p align="left"><a href="<? "$tut_link"; ?>"><? echo "$tut_name"; ?> </a> </p>
                    <p align="left"> Video: <? echo "$tut_video"; ?>
                      Download: <? echo "$tut_download"; ?>
                      External Link: Yes 
                      
                      &gt;
                      <? echo "$tut_program"; ?></p>
                    </td>
              </tr>
<?
++$i;
} 
echo "</table>";


?>


In this code i need it to only display 8 rows per page and also it so that after more is added to the mysql database it automatically creates a new page to display the new rows on.

Scottmandoo
Junior Poster in Training
61 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

Search for pagination. You can make use of 'limit' to display only 8 records at a time.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

ok thanks i will try that, do you know how to do the page thing, to make it automatically add new pages?

eg. 1st page - display rows 1 - 8, 2nd page - display rows 9 - 16 etc.

or will this pagination thing do it all?

Scottmandoo
Junior Poster in Training
61 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

The trick is to make a count of the total records you want to eventually display. Divide this number by the number of items to display per page (you might want to make sure that if there is a remainder that you add an extra page).

Then you build your call so that it determines which page is being called and then determines where to start the data and how many items to display.

LIMIT in MySQL allows you to limit the data. It is used like this [inlinecode=sql]SELECT * FROM tutorials WHERE tut_program = 'PHOTOSHOP' LIMIT 16, 8[/inlinecode]

The first number in the LIMIT says where to start looking and the second number says how many to show.

JRSofty
Junior Poster in Training
69 posts since Dec 2007
Reputation Points: 16
Solved Threads: 10
 

ok heres my code again i think i did it wrong because nothing displays, i feel like such a noob, lol.

<?
if (isset($_GET['pageno'])) {
   $pageno = $_GET['pageno'];
} else {
   $pageno = 1;
} // if
?>

<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM tutorials WHERE tut_program='Photoshop' AND tut_category='Text Effects' ";
$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];
$rows_per_page = 8;
$lastpage      = ceil($numrows/$rows_per_page);

$pageno = (int)$pageno;
if ($pageno < 1) {
   $pageno = 1;
} elseif ($pageno > $lastpage) {
   $pageno = $lastpage;
} // if

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

$query = "SELECT * FROM table $limit";
$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
... process contents of $result ...

if ($pageno == 1) {
   echo " FIRST PREV ";
} else {
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
   $prevpage = $pageno-1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
} // if

echo " ( Page $pageno of $lastpage ) ";

if ($pageno == $lastpage) {
   echo " NEXT LAST ";
} else {
   $nextpage = $pageno+1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
} // if

mysql_close();

echo "<b><center>Database Output</center></b>";

?>
<link href="file:///C|/Users/Scott/SlyGraphics/style.css" rel="stylesheet" type="text/css" />

<title>SlyGraffix: Database Test</title><table width="287" border="0" align="center" cellpadding="2" cellspacing="0">
             
<?
$i=0;
while ($i < $num) {
$tut_name=mysql_result($result,$i,"tut_name");
$tut_link=mysql_result($result,$i,"tut_link");
$tut_program=mysql_result($result,$i,"tut_program");
$tut_category=mysql_result($result,$i,"tut_category");
$tut_video=mysql_result($result,$i,"tut_video");
$tut_download=mysql_result($result,$i,"tut_download");
$tut_image=mysql_result($result,$i,"tut_image");
?>

 <tr>
                <td width="140"><a href="<? "$tut_link"; ?>"><img src="images/tutorials/fire_reflection.jpg" alt="d" width="140" height="120" border="0" /></a></td>
                <td width="139" valign="top"><p align="left"><a href="<? "$tut_link"; ?>"><? echo "$tut_name"; ?> </a> </p>
                    <p align="left"> Video: <? echo "$tut_video"; ?>
                      Download: <? echo "$tut_download"; ?>
                      External Link: Yes 
                      
                      &gt;
                      <? echo "$tut_program"; ?></p>
                    </td>
              </tr>
<?
++$i;
} 
echo "</table>";


?>
Scottmandoo
Junior Poster in Training
61 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

Print out the query and test it in phpmyadmin/mysql. Check if the query returns any value.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

nope nothing

also changed
$query = "SELECT * FROM table $limit";
to $query = "SELECT * FROM tutorials $limit";

Scottmandoo
Junior Poster in Training
61 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

No. Print the query.Execute it.
$query = "select * from tutorials $limit";
echo $query;

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

I dont mean to sound so dumb but i got no idea what you mean, sorry for being like this but i've only learnt how to use php/mysql in the last couple of days and am still trying to get the grips of it.

So could you please explain it in 'dumb' terms.

btw i really appreciate this.

Scottmandoo
Junior Poster in Training
61 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

Ok! In simple terms, You have this query.
$query = "SELECT * FROM table $limit";
Just add another line, echo $query after it. It will print something on the screen. Copy it and paste it in phpmyadmin or mysql console. If your query is correct, you will get an output. :)

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

so what your pretty much saying is where it said.

$query = "SELECT * FROM tutorials $limit";
$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
... process contents of $result ...

make it say $query = "SELECT * FROM tutorials $limit";
echo $query
$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
... process contents of $result ...

if this is right i feel so stupid, also again it displays nothing, btw the code is on a php page

Scottmandoo
Junior Poster in Training
61 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

here,follow this tutorial on how to do it:

http://www.php-mysql-tutorial.com/php-mysql-paging.php

ryan_vietnow
Posting Pro
578 posts since Aug 2007
Reputation Points: 28
Solved Threads: 71
 

Alright i finally got a code that works, though there are still a few problems with it.

- it makes the rows align next to each other not underneath, if you could make it go 2 next to each other and then the next 2 under them so 4 rows of 2 in total per page would be perfect.
- How do you change code, everytime i change the table or font etc. it make nothing appear on the page at all.

Anyways heres the code:

<?php
include("dbinfo.inc.php");
mysql_connect($host,$user,$pass) or die("ERROR:".mysql_error());
mysql_select_db($db) or die("ERROR DB:".mysql_error()); 
$max = 8; //amount of articles per page. change to what to want
$p = $_GET['p'];
if(empty($p))
{
$p = 1;
}
$limits = ($p - 1) * $max; 
//view the tutorials article!
if(isset($_GET['act']) && $_GET['act'] == "view")
{
$tut_id = $_GET['tut_id'];
$sql = mysql_query("SELECT * FROM tutorials WHERE tut_id = '$id'");
while($r = mysql_fetch_array($sql))
{
$tut_name = $r['tut_name'];
$story = $r['story'];
$tut_link = $r['tut_link'];
}
}else{
//view all the tutorials articles in rows
$sql = mysql_query("SELECT * FROM tutorials LIMIT ".$limits.",$max") or die(mysql_error());
//the total rows in the table
$totalres = mysql_result(mysql_query("SELECT COUNT(tut_id) AS tot FROM tutorials"),0);	
//the total number of pages (calculated result), math stuff...
$totalpages = ceil($totalres / $max); 
//the table
echo "<center><table><tr><td></td><td></td></tr><tr>";
while($r = mysql_fetch_array($sql))
{
$tut_id = $r['tut_id'];
$tut_name = $r['tut_name'];
$tut_link = $r['tut_link'];
echo "<td><a href='$tut_link'>$tut_name</a></td><td></td>";
}
//close up the table
echo "</tr></table></center>";
for($i = 1; $i <= $totalpages; $i++){ 
//this is the pagination link
echo "<center>Page: <a href='tutorials.php?p=$i'>$i</a> | </center>";
}
}

?>


Check out my page here: Click Here

Scottmandoo
Junior Poster in Training
61 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

do you need a pagination script? i have a few i developed 6 months ago you can use.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

Thanks to kkeith29 i now have a perfect working pagination script.

Scottmandoo
Junior Poster in Training
61 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

<?PHP include "./Ads.php"?>     hotel.jpgrestaurant2.jpgvandrarhem.jpgspa.jpglog.jpg

Sweden.png United Kingdom(Great Britain).png Home Upload Ads Login Register Upload Ads Company Type HotellRestaurangVandrarhenPubarDiskotekCaféerSpaKonferens kommun upArrow <?

// connect to the database and query it to show the titles of all the threads,
// and the date when the thread was last posted
$kommun=$_POST['kommun'];
$companytype=$_POST['companytype'];
if(!$kommun)
{
echo "No selection was made";
exit;
}

$rowsPerPage =20;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
$query = "SELECT companytype,companyname,address,telephone FROM addreg where kommun like '$kommun' AND companytype like '$companytype' LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');

// print the random numbers while($row = mysql_fetch_array($result)) { echo "Companytype : " .$row['companytype'] . '
'; echo "Companyname : " .$row['companyname'] . '
'; echo "Address : " .$row['address'] . '
'; echo "Telephone : " .$row['telephone'] . '
'; echo "divv.jpg"."
"; } echo '
';

// how many rows we have in database
$query = "SELECT COUNT(companyname) AS numrows FROM addreg where kommun like'$kommun' AND companytype like '$companytype'";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " $page ";
}
}

// creating previous and next link
// plus the link to go straight to
// the first and last page

if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " [Prev] ";

$first = " [First Page] ";
}
else
{
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}

if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " [Next] ";

$last = " [Last Page] ";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}

// print the navigation link
echo $first . $prev . $nav . $next . $last;

// and close the database connection
include '../library/closedb.php';
?>





footer


prethum
Newbie Poster
21 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You