Sorry ... Had the weekend away. Much needed.
Anyhow, I'm wondering why you are using the includes for the code segments? Generally, I would try to keep the code that sets the data up for viewing with the code that does the displaying.
That being said, I think TestA and TestC belong together. Likewise TestB interacts with and expects values from the main page, so they would be merged. Then once you were down to two files, you'd notice that TestA expects values from the main file as well. So you'd merge and it would all be on one page again. There are exceptions for this, but for a basic page like this the single page approach will win out for me every time.
Now for creating the basic SQL, well ...firstly, I'd set up some basic conversion values, from numbers to table fields. And while we're doing that, why not set up some display options, seeing as how we're keeping this all together.
Code: Select all
// Set the column parameters (displayed columns)
$column_list = array(0=>"ID", 1=>"Driver", 2=>"Races", 3=>"Wins", 4=>"Points");
$column_size = array(0=>100, 1=>300, 2=>150, 3=>150, 4=>150);
$column_default = 4;
So, we have the columns to be shown, the column widths for each, and a value holding the column that will be sorted by default. Next we need to capture the $_GET value and determine the action to be taken.
Code: Select all
// Search for corresponding column in $column list
if((isset($_GET['col'])) && (array_key_exists($_GET['col'], $column_list))) {
// Found the column, set some valaues to use lower
$sort_id = $_GET['col'];
$sort_col = $column_list[$sort_id];
} else {
// No values were found so set default view to sort on $column_default
$sort_id = $column_default;
$sort_col= $column_list[$sort_id];
}
// Set search order to DESC if anything is passed, otherwise assume ASC
if(isset($_GET['order'])) $sort_type = "DESC"; else $sort_type = "ASC";
// Compile and commit the query
$result = mysql_query("SELECT ID, Driver, Races, Wins, Points FROM Demo ORDER BY $sort_col $sort_type");
You'll notice that for normal variables, you can just add them amongst normal text if you use double quotation marks. This just makes it easier to see what is going to be inserted without using . 'this' . $annoying . 'syntax'
Anyhow, this next bit is probably debatable, but I try to keep HTML code out of PHP code. So I normally just break out of my <?php ?> tags and output HTML as needed, marking up as I go.
So ... for example, to display the table and draw the header row, and taking into account that I now have an array of columns to be shown, I'd use something like
Code: Select all
?>
<table width="<?php echo array_sum($column_size); ?>px" cellpadding="0" cellspacing="0">
<tr>
<?php
foreach($column_list as $col_id=>$column) {
// Set the base link
$sort_link = "sort_switch.php";
// For all columns that arent the default column ...
if ($col_id != $column_default) {
// Show descending link if this column is the current sort column and is sorted ascendingly
if(($col_id == $sort_id) && ($sort_type == "ASC")) $sort_link .= "?col=$col_id&order=desc";
// Or link to ascending (default) view for this column
else $sort_link .= "?col=$col_id";
// if default column is in ascending view, show descending link, otherwise show nothing and restore default view (COL 0 ASC)
} elseif(($col_id == $sort_id) && ($sort_type == "ASC")) $sort_link .= "?order=desc";
?>
<th width="<?php echo $column_size[$col_id]; ?>"<?php if($col_id == $sort_id) echo " class=\"highlight\""; ?>><a href="<?php echo $sort_link; ?>"><?php echo $column; ?></a>
<?php
}
?>
</tr>
<?php
As you can see, there's a little code in there looking more complex than it really is because of the comments. Basically, it just calculates what link needs to be shown. I've included some logic to remove the col number on the default columns, so sorting in the default way returns you to the base URL.
From there you just show the data ... the following should do it ...
Code: Select all
while($row = mysql_fetch_array($result))
{
?>
<tr>
<?php
foreach($column_list as $col_id=>$column) {
?>
<td<?php if($col_id == $sort_id) echo " class=\"highlight\""; ?>><?php echo $row[$column]; ?></td>
<?php
}
?>
</tr>
<?php
}
?>
</table>
You could also build the SQL using $column_list, but I thought it would be good to leave the option to return data you don't display (such as ID's to use in links etc).
So you just specify the columns to reutn in your SQL, set up the columns in $column list, and set up display widths accordingly, then set the default search column.
You'll want to set $sort_link to the name of your page, but apart from that, it should pretty much draw itself.
Let me know if you need more help implementing this (but it can pretty much just be pasted together from the above).
Cheers