Forum Moderators: coopster
/* set the allowed order by columns */
$default_sort = 'id';
$allowed_order = array ('id', 'name','PCODEGROUP','FULLGROUP');
/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset ($_GET['order']) ¦¦
!in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}
/* connect to db */
mysql_connect ('localhost','root','');
mysql_select_db ('nhla_cases');
/* construct and run our query */
$query = "SELECT * FROM problem ORDER BY $order";
$result = mysql_query ($query);
/* make sure data was retrieved */
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
echo "No data to display!";
exit;
}
/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);
echo "<TABLE border=1>\n";
echo "<TR>\n";
foreach ($row as $heading=>$column) {
/* check if the heading is in our allowed_order
* array. If it is, hyperlink it so that we can
* order by this column */
echo "<TD><b>";
if (in_array ($heading, $allowed_order)) {
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>";
} else {
echo $heading;
}
echo "</b></TD>\n";
}
echo "</TR>\n";
/* reset the $result set back to the first row and
* display the data */
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";
?>
$count = 1;
$c1 = '336699';
$c2 = 'ffffff';
while ($row = mysql_fetch_assoc ($result)) {
echo "<TR bgcolor='#" . ${'c' . $count} . "'>\n";
foreach ($row as $column) {
echo "<TD>$column</TD>\n";
}
echo "</TR>\n";
$count==1?2:1;
}
bgcolor in the tr is kinda hacky too ;) I would use classes and assign those but, oh well
I really appreciate the suggestions! But being an idiotic newby to Php I have no idea where to plug this into my code! Could I prevail upon you to show me where it might fit in my existing code below and a brief explanation as to how it works. I am struggling to learn some of the more sophisticated coding now.
[edited by: jatar_k at 5:11 pm (utc) on June 8, 2005]
[edit reason] removed code - was the same as msg1 [/edit]
/* reset the $result set back to the first row and
* display the data */
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";
so that is all you need to change to do the colours. In my original fix (which works for me, sry) I edited this code exactly as did mcibor in msg 9.
you should be able to replace some lines with ours and get one of the two to work.
/* set the allowed order by columns */
$default_sort = 'CLNAME';
$allowed_order = array ('CLIENTNUM','CASENUM','CLNAME','CFNAME','PCODE','COPEN');
//Added to attempt to create alternating colors
$color = array("#87ECF8", "#CFCFCF");
$i = 0;
/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset ($_GET['order']) ¦¦
!in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}
/* connect to db */
mysql_connect ('localhost','root','');
mysql_select_db ('nhla_cases');
/* construct and run our query */
$query = "SELECT CLIENTNUM,CASENUM,CFNAME,CLNAME,PCODE,COPEN FROM _clientsw_cases ORDER BY $order LIMIT 25";
$result = mysql_query ($query);
/* make sure data was retrieved */
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
echo "No data to display!";
exit;
}
/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);
echo "<TABLE border=0>\n";
echo "<TR>\n";
foreach ($row as $heading=>$column) {
/* check if the heading is in our allowed_order
* array. If it is, hyperlink it so that we can
* order by this column */
echo "<TD><b>";
if (in_array ($heading, $allowed_order)) {
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>";
} else {
echo $heading;
}
echo "</b></TD>\n";
}
echo "</TR>\n";
/* reset the $result set back to the first row and
* display the data */
mysql_data_seek ($result, 0);
//while ($row = mysql_fetch_assoc ($result))
while ($row = mysql_fetch_assoc ($result))
//
{
echo "<TR bgcolor=\"".$color[$i]."\">\n";
$i = 1 - $i;
echo '<td>'.$row['CFNAME'].'</td><td>'.$row['CLNAME'].'</td><td>'.$row['PCODE'].'</td><td>'.$row['COPEN'].'</td><td><a
href="money.php? CLIENTNUM='.$row['CLIENTNUM'].'">eligibility</a> ¦<a
href="caseinfo3.php? CASENUM='.$row['CASENUM'].'">case</a> ¦<a
href="caseinfoc.php? CASENUM='.$row['CASENUM'].'">close case</a> ¦<a
href="clientinfo3.php? CLIENTNUM='.$row['CLIENTNUM'].'">client</a> ¦ <TD><a href="T12.php?CASENUM='.$row['CASENUM'].','.$row['CFNAME'].','.$row['CLNAME'].'">Add Time</a></td><td><a href="case_events.php? CASENUM='.$row['CASENUM'].'">Case Hours</a></td></tr>';
//{
//echo "<TR>\n";
foreach ($row as $column) {
echo "<TD>$column</TD>\n";
}
echo "</TR>\n";
}
echo "</TABLE>\n";
?>
Thanks a million!