Pagination with Sorting & Links

Here is my dev page: http://aiowebservices.com/tabledev/

Everything is working great using a pre-made script that I found online. However I have two hidden tables that are behind the scenes that contain link data. One is a link for the name column, and the other is a link for the navigation column. I need these links to be made click-able by clicking on name or the advertiser. The fields are called namelink and advertiserlink, respectively.

What do I need to do to the current script to achieve this? Below I have posted the code for what I have so far.

Thanks for your help ahead of time!

[php]<?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,year,cost,advertiser 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 "<th>" . $field . "</th>\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";
foreach ($row as $field=>$value) {	
	echo "<td>" . $value . "</td>\n";
}
echo "</tr>\n";

//switch row style
if($tr_class == "class='odd'"){
	$tr_class = "class='even'";
}else{
	$tr_class = "class='odd'";
}

}

//END TABLE
echo “

\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]

Cant be done. A hidden field can not be clicked on because it is invisible.
Unless you meant something else we do not understand. Help us help you, clear up your meaning…

Just in case you are talking about the HREF that you are displaying at the bottom of the PHP code,
You basically use the HREF to point at another page passing whatever info you need to.
Or, place a button in it instead that calls a Javascript function…

Not really sure what you are asking help for. But, ask again and we will help!

My bad. I went ahead and showed the two tables that I hid originally.

The namelink needs to be linked to the name column, so each of the corresponding links can be clicked on when you click on the name. For example, Custom Bill Garden Design needs to have the href link right next to it.

Does that make more sense? The same goes for the advertiser/advertiserlink columns.

The two columns*** not tables. My mistake.

Okay, now that makes sense…

So, you print (display) the row currently, like this:

foreach ($row as $field=>$value) {
echo “

” . $value . “\n”;
}

That means that all the data is place side by side across the row in table-detail cells.
Simple and useful way to display the data. But, no control as is for each cell.
You would have to change this to add in a control for the fourth cell to alter it to add your HREF’s.

Something like this might work…
[php]
$i=0; //used to count fields…
foreach ($row as $field=>$value) {
if($i=4){
echo “

Press here to go there!”;
}else{
echo “” . $value. “”
}
$i=$i+1;
}
echo “\n”;
[/php]
I think that would work. Not tested, but… Basically, it is putting the HREF into the cell where needed. On a further note, HREF’s are set up so the visual name part does not have to be the link. So, in the cell, you do not need to use the actual address. I used a Press-Here note to show the sample… What you might want to do is change this to have the actual name as the link. That would involve a bit more coding as you would have to pull the name and the address and with the address first as the HREF. This would be the best way to do it as it would look great! A user would press the actual name and go that way… (No need for the second col for the address…)

Well, hope that helps…

That’s exactly what needs to be done - have the names and advertisers click-able using the link data columns without having the ugly link data next to them.

I’m just out of time on this project, and by no means am I a professional PHP coder. I’d definitely be willing to pay someone a bit to code it for me if necessary, since I am out of time to get this part of the project done. This is the last little bit of this web project that needs to get done.

I could use the money, not working right now… LOL… BUT, I already gave you the code…
Just need to change the name col… Here is the new version, try it and let me know if it works for you…

[php]
$i=0; //used to count fields…
foreach ($row as $field=>$value) {
if($i=3){
$tempname=$value; // Save name for use in next field…
if($i=4){
echo “

’” . $tempname . “”;
}else{
echo “” . $value. “”
}
$i=$i+1;
}
echo “\n”;
[/php]
OH, also remove the hyperlink title from the table… I think that is what you want to do…
Good luck.

SOrry Sorry, pressed send and forgot to fix the IF’s first… Use this version not the last!
[php]
$i=0; //used to count fields…
foreach ($row as $field=>$value) {
if($i=3){
$tempname=$value; // Save name for use in next field…
}elseif($i=4){
echo “

’” . $tempname . “”;
}else{
echo “” . $value. “”
}
$i=$i+1;
}
echo “\n”;
[/php]
So, at count #3 which is the name, it saves the name. Then, at count #4 which is the hyperlink, it create the link using the name as the text. So, the name becomes the link… Should work nicely for you…

I feel really dumb now - I tried plugging your code into my script a few different ways, mainly like this:

[php]

<?php /* Drale DBTableViewer v100123 Copyright 2009 http://www.drale.com Darrell is not responsible for any abnormal behavior of this program This file is part of Drale DBTableViewer. Drale DBTableViewer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Drale DBTableViewer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Drale DBTableViewer. If not, see . drale.com is not responsible for any abnormal behavior of this program version 100123: rebuilt columnSortArrows() included tableviewdemo dummy data version 100120: added mysql_real_escape_string only allow ASC and DESC in $_GET['sort'] version 100119: initial build */ ?> Database Table View <?php

//DATABASE SETTINGS
$config[‘host’] = “pacnwboat.com”;
$config[‘user’] = “pnwb_db1usr”;
$config[‘pass’] = “floatboatFloat”;
$config[‘database’] = “pnwb_db1”;
$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 * 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 "<th>" . $field . "</th>\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=3){
$tempname=$value; // Save name for use in next field…
}elseif($i=4){
echo “

”;
}else{
echo “”
}
$i=$i+1;
}
echo “\n”;
echo "</tr>\n";



//switch row style
if($tr_class == "class='odd'"){
	$tr_class = "class='even'";
}else{
	$tr_class = "class='odd'";
}

}

//END TABLE
echo “

’” . $tempname . “” . $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]

I get an error like this which I tried a few ways to fix: Parse error: syntax error, unexpected ‘}’, expecting ‘,’ or ‘;’ in index.php on line 151

Any ideas? Thanks again for your time!

Sorry! Me bad! for the IF’s you need == not = So if($i==3)… SORRY SORRY, was typing it fast for you so you could finish it up…

Just tried it in the same place above and am still getting the same error with the == instead of =

This is what I have:

[php]

//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==3){
$tempname=$value; // Save name for use in next field…
}elseif($i==4){
echo “

’” . $tempname . “”;
}else{
echo “” . $value. “”
}
$i=$i+1;
}
echo “\n”;

[/php]

Can you please post the code from

to
so I can look at it closer.
I think it is just a missing bracket! Thanks…

Original Code:

[php]
//GET DATA
$sql = “SELECT * 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 "<th>" . $field . "</th>\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";
foreach ($row as $field=>$value) {	
	echo "<td>" . $value . "</td>\n";
}
echo "</tr>\n";



//switch row style
if($tr_class == "class='odd'"){
	$tr_class = "class='even'";
}else{
	$tr_class = "class='odd'";
}

}

//END TABLE
echo “

\n”;
[/php]

Modified Code

[php]
//GET DATA
$sql = “SELECT * 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 "<th>" . $field . "</th>\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==3){
$tempname=$value; // Save name for use in next field…
}elseif($i==4){
echo “

”;
}else{
echo “”
}
$i=$i+1;
}
echo “\n”;
echo “\n”;
//switch row style
if($tr_class == "class='odd'"){
	$tr_class = "class='even'";
}else{
	$tr_class = "class='odd'";
}

}

//END TABLE
echo “

’” . $tempname . “” . $value. “
\n”;
[/php]

Okay, probably my typing again… SORRY!

This line:
echo “

’” . $tempname . “”;
in the if $i==4…

It has an extra single quote!!! It should be:
[php]
echo “

” . $tempname . “”;
[/php]
Try that while I read the rest of the code…

Tried that fix, still same error:

Parse error: syntax error, unexpected ‘}’, expecting ‘,’ or ‘;’ in /home/aiows/public_html/tabledev/index.php on line 156

Thanks again for all of your help! It is really appreciated.

Okay, still looking at it. Which line is 156??

If it would make it easier, I could PM you the FTP information… might be helpful since it’s hard to go back and forth with these little changes.

there is a missing semi-colon a couple lines down from that last correction… SORRY…

Too tired today… LOL… Again, my fault…

I’m still having a hard time with it.

Again, I’d be happy to provide you with FTP information - it might be easier then going back and forth on here. I would definitely be more then happy to give you something for your time via PayPal or the likes too.

I’m kinda down to the wire on this thing and need to get it done with.

Thanks

CLOSER!

Using this code, I got this: www.aiowebservices.com/tabledev

[php]

//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==2){
$tempname=$value; // Save name for use in next field…
}elseif($i==3){
echo “

” . $tempname . “”;
}else{
echo " $value ";
}
$i=$i+1;
}
echo “\n”;[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service