Jump to content

Displaying Mysql Contents in 4x2 Table


jamesjmann

Recommended Posts

I want to display my pictures I stored in Mysql in a 4 column, 2 row table WITH pagination.

 

Here's the code I use to display the data currently:

//your username 
$username = "username"; 
//your password 
$password = "password"; 
//your mySQL server 
$host = "host"; 
//The name of the database your table is in 
$database = "database"; 

//connect, but if there is a problem, display an error message telling why there is a problem 
$conn = mysql_connect($host,$username,$password) or die("Error connecting to Database!<br>" . mysql_error()); 
//Choose the database from your mySQL server, but if there is a problem, display an error telling why 
$db = mysql_select_db($database) or die("Cannot select database!<br>" . mysql_error()); 

// find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM myphotos";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 4;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db 
$sql = "SELECT * FROM myphotos ORDER BY id ASC LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo "<span class=\"pagination\"><a href='{$_SERVER['PHP_SELF']}?currentpage=1'>First</a></span> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <span class=\"pagination\"><a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Previous</a></span> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " <span class=\"paginationDown\"><b>$x</b></span> ";
// if not current page...
} else {
// make it a link
echo " <span class=\"pagination\"><a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a></span> ";
} // end else
} // end if 
} // end for

// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page 
echo " <span class=\"pagination\"><a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next</a></span> ";
// echo forward link for lastpage
echo " <span class=\"pagination\"><a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last</a></span><br> ";
} // end if
/****** end build pagination links ******/

// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result))
{
extract ($list);
// echo data
$url = $list['url'];;
$title = $list['title'];
$description = $list['description'];

echo("$title<br><a rel=\"example_group\" title=\"$description\" href=\"$url\"><img src=\"$url\" alt=\"\" width=\"\" height=\"\" class=\"gallery_images\" /></a>"); 
} // end while

/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo "<span class=\"pagination\"><a href='{$_SERVER['PHP_SELF']}?currentpage=1'>First</a></span> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <span class=\"pagination\"><a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Previous</a></span> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " <span class=\"paginationDown\"><b>$x</b></span> ";
// if not current page...
} else {
// make it a link
echo " <span class=\"pagination\"><a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a></span> ";
} // end else
} // end if 
} // end for

// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page 
echo " <span class=\"pagination\"><a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next</a></span> ";
// echo forward link for lastpage
echo " <span class=\"pagination\"><a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last</a></span><br>";
} // end if
/****** end build pagination links ******/

 

The above code just displays the pictures vertically. Here's this code live:

http://www.djsmiley.net/gallery/albums/my_photos.php

(you can't view the page in IE)

 

Now how would I make it so that the first 8 images in the database display in a 4x2 table, etc.?

 

Link to comment
https://forums.phpfreaks.com/topic/228630-displaying-mysql-contents-in-4x2-table/
Share on other sites

Archived

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

×
×
  • Create New...

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.