...

View Full Version : Error I can't find!



wooowooo
12-28-2007, 03:16 PM
Hi please does anyone know why I am getting an encapsulated t string error on line 17 with this code? I cant find the problem :(



<?php

$default_sort = 'CustomerID';
$allowed_order = array ('CustomerID','Name', 'Address','Postcode','Telephone','Email');

if (!isset ($_GET['order']) ||
!in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}


mysql_connect ('localhost','user','pass');
mysql_select_db ('database');


$ad = (isset($_GET['ad']) && $_GET['ad'] == 'desc') 'desc' : 'asc';
$query = "SELECT * FROM table ORDER BY $order $ad";
$result = mysql_query ($query);


$numrows = mysql_num_rows($result);
if ($numrows == 0) {
echo "No data to display!";
exit;
}


$row = mysql_fetch_assoc ($result);
echo "<TABLE border=1>Click on a column heading to sort the column assendingly <p> \n";
echo "<TR>\n";
foreach ($row as $heading=>$column) {
echo "<TD>";
if (in_array ($heading, $allowed_order)) {
$tad = ($ad == 'asc' && $order == $heading) ? 'desc' : 'asc';
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&ad=$tad\">$heading[/url]";
} else {
echo $heading;
}
echo "</TD>\n";
}
echo "</TR>\n";

mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result)) {
echo "<TR>\n";
foreach ($row as $column) {
echo "<TD>$column</TD>\n";
}
echo "</TR>\n";
}
echo "</TABLE>\n";

?>

arnyinc
12-28-2007, 03:23 PM
I don't use the ternary operator very often, but I think the question mark is required.



$ad = (isset($_GET['ad']) && $_GET['ad'] == 'desc') ? 'desc' : 'asc';

wooowooo
12-28-2007, 03:42 PM
Spot on, quick one though - do you know why only the first letter from the heading is being displayed and not the whole heading as was happening before I edited the code to sort assending and desending?

Many thanks

arnyinc
12-28-2007, 04:01 PM
Nothing jumps out at me as something that would cause that specific problem. I would do a view source to verify the HTML is right or wrong. Then verify your SQL statement is returning exactly what you expect. Then you can limit it to your PHP causing the issue.

This line seems a bit off:


echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&ad=$tad\">$heading[/url]";


The closing url should probably be an </a>.

I don't use the curly brackets. That's not to say it won't work but that's something I can't verify off the top of my head either.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum