Pagination with Sorting & Links

Okay, it’s all working now. I need to get the columns fixed now - any ideas as to how to edit this set:

[php]
//START TABLE AND TABLE HEADER
echo “

\n”;
$array = mysql_fetch_assoc($result);
foreach ($array as $key=>$value) {
if($config[‘nicefields’]){
$field = str_replace("_"," ",$key);
$field = ucwords($field);
}
$field = columnSortArrows($key,$field,$orderby,$sort);
echo “\n”;
}
echo “\n”;
[/php]

We need to get it to not display namelink and advertiserlink as columns, so the rest line up correctly.

” . $field . “

Oh, I would also like to be able to “title” the column headers myself, instead of the automatic database naming scheme.

Thanks again!!

Sorry, I have been ALL-OUT and crazy today, but, did finally get some sleep this morning!
Please repost again with the current fixed code and tell me which line is 156 and I will get you all fixed up.
(Running to store right now, but, plan to be back and working online most of the night!)

We can fix this up tonight for sure!

www.aiowebservices.com/tabledev

Need to modify so that I can enter in custom headers for the columns. Thanks!!!

CURRENT CODE:

[php]

Database Table View <?php

//DATABASE SETTINGS
$config[‘host’] = “REMOVED”;
$config[‘user’] = “REMOVED”;
$config[‘pass’] = “REMOVED”;
$config[‘database’] = “REMOVED”;
$config[‘table’] = “entries”;
$config[‘nicefields’] = true; //true or false | “Field Name” or “field_name”
$config[‘perpage’] = 20;
$config[‘showpagenumbers’] = true; //true or false
$config[‘showprevnext’] = true; //true or false

/******************************************/
//SHOULDN’T HAVE TO TOUCH ANYTHING BELOW…
//except maybe the html echos for pagination and arrow image file near end of file.

include ‘./Pagination.php’;
$Pagination = new Pagination();

//CONNECT
mysql_connect($config[‘host’], $config[‘user’], $config[‘pass’]);
mysql_select_db($config[‘database’]);

//get total rows
$totalrows = mysql_fetch_array(mysql_query("SELECT count(*) as total FROM ".$config['table'].""));

//limit per page, what is current page, define first record for page
$limit = $config[‘perpage’];
if(isset($_GET[‘page’]) && is_numeric(trim($_GET[‘page’]))){$page = mysql_real_escape_string($_GET[‘page’]);}else{$page = 1;}
$startrow = $Pagination->getStartRow($page,$limit);

//create page links
if($config[‘showpagenumbers’] == true){
$pagination_links = $Pagination->showPageNumbers($totalrows[‘total’],$page,$limit);
}else{$pagination_links=null;}

if($config[‘showprevnext’] == true){
$prev_link = $Pagination->showPrev($totalrows[‘total’],$page,$limit);
$next_link = $Pagination->showNext($totalrows[‘total’],$page,$limit);
}else{$prev_link=null;$next_link=null;}

//IF ORDERBY NOT SET, SET DEFAULT
if(!isset($_GET[‘orderby’]) OR trim($_GET[‘orderby’]) == “”){
//GET FIRST FIELD IN TABLE TO BE DEFAULT SORT
$sql = “SELECT * FROM ".$config['table']." LIMIT 1”;
$result = mysql_query($sql) or die(mysql_error());
$array = mysql_fetch_assoc($result);
//first field
$i = 0;
foreach($array as $key=>$value){
if($i > 0){break;}else{
$orderby=$key;}
$i++;
}
//default sort
$sort=“ASC”;
}else{
$orderby=mysql_real_escape_string($_GET[‘orderby’]);
}

//IF SORT NOT SET OR VALID, SET DEFAULT
if(!isset($_GET[‘sort’]) OR ($_GET[‘sort’] != “ASC” AND $_GET[‘sort’] != “DESC”)){
//default sort
$sort=“ASC”;
}else{
$sort=mysql_real_escape_string($_GET[‘sort’]);
}

//GET DATA
$sql = “SELECT length,name,namelink,year,cost,advertiser,advertiserlink FROM ".$config['table']." ORDER BY $orderby $sort LIMIT $startrow,$limit”;
$result = mysql_query($sql) or die(mysql_error());

//START TABLE AND TABLE HEADER
echo “

\n”;
$array = mysql_fetch_assoc($result);
foreach ($array as $key=>$value) {
if($config[‘nicefields’]){
$field = str_replace("_"," ",$key);
$field = ucwords($field);
}
$field = columnSortArrows($key,$field,$orderby,$sort);
echo “\n”;
}
echo “\n”;

//reset result pointer
mysql_data_seek($result,0);

//start first row style
$tr_class = “class=‘odd’”;

//LOOP TABLE ROWS
while($row = mysql_fetch_assoc($result)){

echo “<tr “.$tr_class.”>\n”;

$i=0; //used to count fields…
foreach ($row as $field=>$value) {
if($i==1){
$namelink=$value; // Save name for use in next field…
}elseif($i==2){
echo “

”;
}elseif($i==5){
$advertiserlink=$value; // Save name for use in next field…
}elseif($i==6){
echo “”;
}else{
echo "";
}
$i=$i+1;
}
echo “\n”;
//switch row style
if($tr_class == "class='odd'"){
	$tr_class = "class='even'";
}else{
	$tr_class = "class='odd'";
}

}

//END TABLE
echo “

” . $field . “
” . $namelink . “” . $advertiserlink . “ $value
\n”;

if(!($prev_link==null && $next_link==null && $pagination_links==null)){
echo ‘

’."\n";
echo $prev_link;
echo $pagination_links;
echo $next_link;
echo ‘
’."\n";
echo “
\n”;
}

/FUNCTIONS/

function columnSortArrows($field,$text,$currentfield=null,$currentsort=null){
//defaults all field links to SORT ASC
//if field link is current ORDERBY then make arrow and opposite current SORT

$sortquery = "sort=ASC";
$orderquery = "orderby=".$field;

if($currentsort == "ASC"){
	$sortquery = "sort=DESC";
	$sortarrow = '<img src="arrow_up.png" />';
}

if($currentsort == "DESC"){
	$sortquery = "sort=ASC";
	$sortarrow = '<img src="arrow_down.png" />';
}

if($currentfield == $field){
	$orderquery = "orderby=".$field;
}else{	
	$sortarrow = null;
}

return '<a href="?'.$orderquery.'&'.$sortquery.'">'.$text.'</a> '. $sortarrow;	

}

?>

[/php]

Very Nice, NotALoafer… Get’n there finally. It is hard to do this long distance…

I realized what you are needing now… You just have to drop the correct cols from the title hearders.

The old code was:

//START TABLE AND TABLE HEADER
echo “

\n”;
$array = mysql_fetch_assoc($result);
foreach ($array as $key=>$value) {
if($config[‘nicefields’]){
$field = str_replace("_"," ",$key);
$field = ucwords($field);
}
$field = columnSortArrows($key,$field,$orderby,$sort);
echo “\n”;
}
echo “\n”;

Change that to something like this:
[php]

//START TABLE AND TABLE HEADER
echo “

” . $field . “
\n”;
$array = mysql_fetch_assoc($result);
$i = 0;
foreach ($array as $key=>$value) {
if($config[‘nicefields’]){
$field = str_replace("_"," ",$key);
$field = ucwords($field);
}
$field = columnSortArrows($key,$field,$orderby,$sort);
if($i !=3 && $i!=5)
echo “\n”;
$i = $i +1;
}
echo “\n”;
[/php]
This would skip cols #3 and #5… Not sure if these are the correct ones. I see that my earlier example messed you up because I started counting at 0. Very sorry for that. All my comments for you were after very long days with sick relatives an a death in the family… Sorry for the mix ups… Hope you understand the logic… By the way, if the table headers are always the same, you do not have to do it with a program. You could just echo"
” . $field . “
header1 header2 etc…
… Etc… Well, good luck with it all…

Awesome man. Thanks again so much for your help. Are you sure you don’t want anything for your time? You were a great help and I got it all figured out now/launched. I’d be more then happy to send something your way via PayPal or the likes.

Thanks again!

No problem! If someone asks you a question here you can help them now!

$$$ always lures people… LOL… But, we are here to help. Just sorry I didn’t realize your questions earlier on… Very glad it is working for you.

Please come back with the next issue… Always seems one solved causes another one…

CYA in the bitstream…

Sponsor our Newsletter | Privacy Policy | Terms of Service