Page 1 of 1

Table with PHP and Pagination

Posted: Sat Dec 10, 2005 7:13 pm
by Maluendaster
ok, i finally did it (the pagination), i got an example from http://www.boohahaonline.com/pagination%20example.php and worked ok.

The problem now is that it creates a table like this :
----------------------------------------------------------------------------
| NAME | URL | IMAGE |
----------------------------------------------------------------------------

You can see it here : http://www.planetbabes.net/pagination/index.php

And I want to be like this :

----------------------------------------------------------------------------
| NAME |
----------------------------------------------------------------------------
| IMAGE (The actual image, not the path) |
----------------------------------------------------------------------------
| VIEW GALLERY (url) |
----------------------------------------------------------------------------

Here's the code :

Code: Select all

<?php
//connect to the database
$connect = mysql_connect("localhost", "metallica", "rulez") or
die ("check your server connection.");

// Select the database to use
mysql_select_db ("master_of_puppets") or die (mysql_error());

$limit = 20; 
// Sets how many results shown per page 

$query_count = "SELECT * FROM galeria ORDER BY ID_Prueba DESC"; 
// Sets what we want to pull from the database 
// count(*) is better for large databases (thanks Greg!) 

$result_count = mysql_query($query_count); ; 
// Pulls what we want from the database 

$totalrows = mysql_num_rows($result_count); 
// This counts the number of users 

//**********beginning of pagination************
$page = $_GET['page'];
//********check and see what page we're on*********

if(empty($page)){ // Checks if the $page variable is empty (not set) 
$page = 1; // If it is empty, we're on page 1 
} 

$limitvalue = $page * $limit - ($limit); 
// Ex: (2 * 25) - 25 = 25 <- data starts at 25 

$query = "SELECT * FROM galeria ORDER BY ID_Prueba DESC LIMIT $limitvalue, $limit"; 
$result = mysql_query($query) or die("Error: " . mysql_error()); 
// Selects all the data from table. 
// mysql_error() will print an error if one occurs. 

/* Tip: The MySQL LIMIT value syntax is as follows: 
LIMIT $row_to_start_at, $how_many_rows_to_return
*/ 

//***create a table to display data*****

$bgcolor = "#FFFFFF"; 
/* Sets up one of the background colors. The color stated here will be 
displayed second */ 

echo("<table>"); 
// Just a simple table 

while($row = mysql_fetch_array($result)){ 
/* This starts the loop (a while loop). What this does is returns a row of data 
to the $row array. Each time the loop restarts, the next row of data is used. 
So, each pass of the loop returns a different row of data. */ 

// Start The Background Check 
if ($bgcolor == "#FFFFFF"){ 
$bgcolor = "#FFFFFF"; 
}else{ 
$bgcolor = "#FFFFFF"; 
} 
/* Tip: The color you want to appear first on the list should be where the 
#FFFFFF is listed. What this script does is checks to see if #E0E0E0 was used 
last; if it was, then it'll use #FFFFFF. All you have to do is replace the 
colors of your choice. */

echo("<tr bgcolor='.$bgcolor.'><td>"); 
// Here we start table row & table data 
// Make sure your bgcolor is the $bgcolor variable 

echo($row["Nombre"]); 
//Here we have the name of the recipe field 

echo("</td><td>"); 
// Here we end the one section of table data, and start another. 

echo($row["Url"]); 
// Prints the author id field 

echo("</td><td>"); 
// Here we end the one section of table data, and start another.

echo("<img src=$row[imagen]>"); 
// Here we make the link to the full description of the recipe.


echo("</td></tr>"); 
// Here we end the table data & table row 

} /* This closes the loop. Anything after this bracket will display after the 
data you've pulled from the database. */ 

echo("</table>"); 
/* While we're at it, let's close the table tag--we don't want other data 
inside this table */ 

//****make the previous button******** 
if ($page != 1){ 
$pageprev = $page-1; 

echo("<a href=\"{$_SERVER['PHP_SELF']}?page=$pageprev\">PREVIOUS"."</a>&nbsp;"); 
}else{ 
echo("PREV"); 
} 


//*****list the page numbers in link format********


$numofpages = $totalrows / $limit; 

for ($i = 1; $i <= $numofpages; $i++){ 
if ($i == $page){ 
echo($i."&nbsp;"); 
}else{ 
echo("<a href=\"{$_SERVER['PHP_SELF']}?page=$i\">$i</a>&nbsp;"); 
} 
} 


//*****check for remaing rows******


if (($totalrows % $limit) != 0){ 
if ($i == $page){ 
echo($i."&nbsp;"); 
}else{ 
echo("<a href=\"{$_SERVER['PHP_SELF']}?page=$i\">$i</a>&nbsp;"); 
} 
} 


//****make the next button*****


if (($totalrows - ($limit * $page)) > 0){ 
$pagenext = $page+1; 

echo("<a href=\"{$_SERVER['PHP_SELF']}?page=$pagenext\">&nbsp;NEXT"."</a>"); 
}else{ 
echo("NEXT"); 
} 

mysql_free_result($result);
?>
anyone??? thank you in advance.

Posted: Sat Dec 10, 2005 7:18 pm
by trukfixer
change your html to use rows like:

Code: Select all

cho("<tr bgcolor='.$bgcolor.'><td>");
// Here we start table row & table data
// Make sure your bgcolor is the $bgcolor variable

echo($row["Nombre"]);
//Here we have the name of the recipe field

echo("</td></tr><tr><td>");
// Here we end the one section of table data, and start another.

echo($row["Url"]);
// Prints the author id field

echo("</td></tr><tr><td>");
// Here we end the one section of table data, and start another.

echo("<img src=$row[imagen]>");
// Here we make the link to the full description of the recipe.


echo("</td></tr>");
// Here we end the table data & table row

} /* This closes the loop. Anything after this bracket will display after the
data you've pulled from the database. */

echo("</table>");

Posted: Sat Dec 10, 2005 8:26 pm
by Maluendaster
thanks, worked perfect!.. althoug i now have another problem.. I can't make a link out of the Url Row, here's what i have... you can check it here ( http://www.planetbabes.net/pagination/show_gals.php ) to see the progress...

Code: Select all

$bgcolor = "#FFFFFF"; 
/* Sets up one of the background colors. The color stated here will be 
displayed second */ 

echo("<table>"); 
// Just a simple table 

while($row = mysql_fetch_array($result)){ 
/* This starts the loop (a while loop). What this does is returns a row of data 
to the $row array. Each time the loop restarts, the next row of data is used. 
So, each pass of the loop returns a different row of data. */ 

// Start The Background Check 
if ($bgcolor == "#FFFFFF"){ 
$bgcolor = "#FFFFFF"; 
}else{ 
$bgcolor = "#FFFFFF"; 
} 
/* Tip: The color you want to appear first on the list should be where the 
#FFFFFF is listed. What this script does is checks to see if #E0E0E0 was used 
last; if it was, then it'll use #FFFFFF. All you have to do is replace the 
 colors of your choice. */

echo("<tr bgcolor=333333><td><center><b><font color=white size=2 face=verdana>"); 
// Here we start table row & table data 
// Make sure your bgcolor is the $bgcolor variable 

echo($row["Nombre"]); 
//Here we have the name of the recipe field 
echo("</b></center></font></td></tr><tr><td><center><img src="); 
// Here we end the one section of table data, and start another. 

echo($row["Imagen"]); 
// Prints the author id field 

echo("></center></td></tr><tr><td><center><img src=../common/view_gallery.gif><a href=out.php?dire="); 
// Here we end the one section of table data, and start another. 

echo($row["Url"]); 
// Here we make the link to the full description of the recipe. 


echo("></a></center></td></tr>"); 
// Here we end the table data & table row 

} /* This closes the loop. Anything after this bracket will display after the 
data you've pulled from the database. */ 

echo("</table>");
/* While we're at it, let's close the table tag--we don't want other data 
inside this table */ 

//****make the previous button******** 
if ($page != 1){ 
$pageprev = $page-1; 

echo("<a href=\"{$_SERVER['PHP_SELF']}?page=$pageprev\">PREVIOUS"."</a>&nbsp;"); 
}else{ 
echo("PREV"); 
}

Posted: Sat Dec 10, 2005 8:53 pm
by Maluendaster
jaja, i'm talking alone, i solved it, it was just a stupid thing.. But now that i included it to the index.php , it looks like this ( http://www.planetbabes.net ) (ADULT CONTENT)

why??? here's the code for the index.php (you can check the source code)