Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Sorting by columns in PHP

Sorting by columns in PHP
Thread Tools
Senior User
Join Date: Oct 2000
Location: Earth
Status: Offline
Reply With Quote
Aug 17, 2004, 05:42 AM
 
Hey everyone,
I hope you don't mind if I ask a simple PHP question. I have built a script that just displays all my table content. However, I want to be able to sort the table by clicking on the column headers. I sent my script to a friend of mine and he sent me back the following. But this doesn't work and he has no idea why. This is the script:


<html>
<body>
<br>

<?php
$script_name = basename($_SERVER['PHP_SELF']);


$db = mysql_connect() or exit("Keine Verbindung");
mysql_select_db("testdb",$db);

$sqlbef = "SELECT * FROM sprachen ORDER BY German";
switch($_REQUEST['spalte'])
{
case 1:
$lang = "English";
break;
case 2:
$lang = "French";
break;
case 3:
$lang = "Italian";
break;
case 4:
$lang = "Spanish";
break;
default:
$lang = "German";
break;
}

switch($_REQUEST['sort'])
{
case "D":
$direction = "DESC";
break;
default:
$direction = "";
break;
}

$sqlbef .= " ORDER BY $lang $direction";
$sqlerg = mysql_query($sqlbef,$db);
$anz = mysql_num_rows($sqlerg);
mysql_close($db);

echo "<table border width=90% align='center'>";
echo "<tr>";
echo "<td width=20%><a href=\"$script_name?spalte=0&sort=" .
($_REQUEST['sort'] != "D" && $_REQUEST['spalte'] == 0 ? "D" : "A") .
"\">German</a></td>";
echo "<td width=20%><a href=\"$script_name?spalte=1&sort=" .
($_REQUEST['sort'] != "D" && $_REQUEST['spalte'] == 1 ? "D" : "A") .
"\">English</a></td>";
echo "<td width=20%><a href=\"$script_name?spalte=2&sort=" .
($_REQUEST['sort'] != "D" && $_REQUEST['spalte'] == 2 ? "D" : "A") .
"\">French</a></td>";
echo "<td width=20%><a href=\"$script_name?spalte=3&sort=" .
($_REQUEST['sort'] != "D" && $_REQUEST['spalte'] == 3 ? "D" : "A") .
"\">Italian</a></td>";
echo "<td width=20%><a href=\"$script_name?spalte=4&sort=" .
($_REQUEST['sort'] != "D" && $_REQUEST['spalte'] == 4 ? "D" : "A") .
"\">Spanish</a></td>";
echo "</tr>";


for ($i=0;$i<$anz;$i=$i+1)
{
$a = mysql_result($sqlerg,$i,"German");
$b = mysql_result($sqlerg,$i,"English");
$c = mysql_result($sqlerg,$i,"French");
$d = mysql_result($sqlerg,$i,"Italian");
$e = mysql_result($sqlerg,$i,"Spanish");
echo "<tr>";
echo "<td>$a</td><td>$b</td><td>$c</td><td>$d</td><td>$e</td>";
echo "</tr>";
}
echo "</table>";
?>

</body>
</html>


Apache tells me:
[Tue Aug 17 12:41:52 2004] [error] PHP Notice: Undefined index: spalte in /Users/ingmar/Sites/try11 Web/showall.php on line 9
[Tue Aug 17 12:41:52 2004] [error] PHP Notice: Undefined index: sort in /Users/ingmar/Sites/try11 Web/showall.php on line 25
[Tue Aug 17 12:41:52 2004] [error] PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /Users/ingmar/Sites/try11 Web/showall.php on line 32
[Tue Aug 17 12:41:52 2004] [error] PHP Notice: Undefined index: sort in /Users/ingmar/Sites/try11 Web/showall.php on line 34
[Tue Aug 17 12:41:52 2004] [error] PHP Notice: Undefined index: spalte in /Users/ingmar/Sites/try11 Web/showall.php on line 34
[Tue Aug 17 12:41:52 2004] [error] PHP Notice: Undefined index: sort in /Users/ingmar/Sites/try11 Web/showall.php on line 36
[Tue Aug 17 12:41:52 2004] [error] PHP Notice: Undefined index: spalte in /Users/ingmar/Sites/try11 Web/showall.php on line 36
[Tue Aug 17 12:41:52 2004] [error] PHP Notice: Undefined index: sort in /Users/ingmar/Sites/try11 Web/showall.php on line 38
[Tue Aug 17 12:41:52 2004] [error] PHP Notice: Undefined index: spalte in /Users/ingmar/Sites/try11 Web/showall.php on line 38
[Tue Aug 17 12:41:52 2004] [error] PHP Notice: Undefined index: sort in /Users/ingmar/Sites/try11 Web/showall.php on line 40
[Tue Aug 17 12:41:52 2004] [error] PHP Notice: Undefined index: spalte in /Users/ingmar/Sites/try11 Web/showall.php on line 40
[Tue Aug 17 12:41:52 2004] [error] PHP Notice: Undefined index: sort in /Users/ingmar/Sites/try11 Web/showall.php on line 42
[Tue Aug 17 12:41:52 2004] [error] PHP Notice: Undefined index: spalte in /Users/ingmar/Sites/try11 Web/showall.php on line 42

Any idea? I don't get any further...

I hope you can help,
Steve
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Aug 17, 2004, 08:32 AM
 
Google for "PHP Notice: Undefined index:" gives http://forums.devshed.com/archive/t-121189


Basically you have too strict error checking set in your php configuration file.
You know it makes sense. ☼ ☼ ☼ Growl.
     
Senior User
Join Date: Feb 2003
Location: USA
Status: Offline
Reply With Quote
Aug 17, 2004, 08:40 AM
 
I don't know PHP too well but was wondering if it was a string concatenation problem. In PHP can you simply do this?

$sqlbef .= " ORDER BY $lang $direction";

Just curious, I know I need to RTFM...
MacBook 2.0 160/2GB/SuperDrive
Lots of older Macs
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Aug 17, 2004, 04:34 PM
 
Try this:
Code:
<html> <body> <br> <?php $script_name = basename($_SERVER['PHP_SELF']); if (!isset($_REQUEST['spalte'])) $_REQUEST['spalte'] = 0; if (!isset($_REQUEST['sort'])) $_REQUEST['sort'] = "A"; $db = mysql_connect("localhost", "testUser", "testPass") or die("Could not connect to database"); mysql_select_db("testdb",$db) or die("Database is not present"); $sqlbef = "SELECT * FROM sprachen"; switch($_REQUEST['spalte']) { case 1: $lang = "English"; break; case 2: $lang = "French"; break; case 3: $lang = "Italian"; break; case 4: $lang = "Spanish"; break; default: $lang = "German"; } switch($_REQUEST['sort']) { case "D": $direction = "DESC"; break; default: $direction = ""; } $sqlbef .= " ORDER BY $lang $direction"; $sqlerg = mysql_query($sqlbef,$db) or die(mysql_error() . "\n"); $anz = mysql_num_rows($sqlerg); mysql_close($db); function TableColumn($language) { global $script_name; echo "$script_name?spalte=$language&sort="; echo ($_REQUEST['sort'] != "D" && $_REQUEST['spalte'] == $language) ? "D" : "A"; } ?> <table border width="90%" align="center"> <tr> <td width="20%"><a href="<?php TableColumn(0); ?>">German</a></td> <td width="20%"><a href="<?php TableColumn(1); ?>">English</a></td> <td width="20%"><a href="<?php TableColumn(2); ?>">French</a></td> <td width="20%"><a href="<?php TableColumn(3); ?>">Italian</a></td> <td width="20%"><a href="<?php TableColumn(4); ?>">Spanish</a></td> </tr> <?php for ($i=0;$i<$anz;$i=$i+1) { $a = mysql_result($sqlerg, $i, "German"); $b = mysql_result($sqlerg, $i, "English"); $c = mysql_result($sqlerg, $i, "French"); $d = mysql_result($sqlerg, $i, "Italian"); $e = mysql_result($sqlerg, $i, "Spanish"); echo "\t<tr>\n"; echo "\t\t<td>$a</td><td>$b</td><td>$c</td><td>$d</td><td>$e</td>\n"; echo "\t</tr>\n"; } ?> </table> </body> </html>
You'll need to make sure you've got your database set up properly. Here's a test database you can import. The resulting database needs to be accessible to user "testUser" with password "testPass".

Code:
# phpMyAdmin MySQL-Dump # version 2.3.2 # http://www.phpmyadmin.net/ (download page) # # Host: localhost # Generation Time: Aug 17, 2004 at 02:33 PM # Server version: 4.00.17 # PHP Version: 4.3.4 # Database : `testdb` # # Dumping data for table `sprachen` # INSERT INTO `sprachen` (`German`, `English`, `French`, `Italian`, `Spanish`) VALUES ('Spriekenzie Deutch?', 'Do you talk the Englishes?', 'Parlez-vous Francais?', 'Habla Italiano?', 'Habla EspaÒol, joto?'); INSERT INTO `sprachen` (`German`, `English`, `French`, `Italian`, `Spanish`) VALUES ('Ein bierra, danke.', 'Gimme a beer, beyotch!', 'Un biere, c\'il vous plait.', 'Uno bierra, por favore.', 'Una cerveza muy grande, por favor.');
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:09 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2