Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting columns 1

Status
Not open for further replies.

pookie62

Technical User
Oct 22, 2004
139
GB
Hi,
jpadie wrote me this script for outputting scores from different races. (For which I'm thankfull eternally)
It does work nice, but there's one little thing I'd like enhanced. the sorting of the columns.
I've looked into the sort() function but I don't get it working the way I want.
All races have their own date, which is stored in the table "wedstrijd" field name is "Datum".
I'd like to have the races displayed chronological (is this correct written?).
This is the script:
Code:
<?php
include("../../Loginsys/include/session.php");
if($session->logged_in){
?>
<style type="text/css">
<!--
body {
	background-image: url(../../images/bg_grad.jpg);
	margin-top: 30px;
}
.style1 {
	color: #FFFFFF;
	font-weight: bold;
	font-size: 14pt;
}
-->
</style>
<?php
 require_once('../../Connections/ASN.php'); ?>
<?php
error_reporting(E_ALL); 
mysql_select_db($database_ASN, $ASN);

$sql = "SELECT `wedstrijd`.`Naam` AS `wedstrijd`,`klasse`.`Klasse`,`overzicht`.`Voornaam` AS `Voornaam`,`deelnemer`.`Naam` AS `Achternaam` , `overzicht`.`Percentage` AS `Perc` FROM `overzicht` INNER JOIN `deelnemer` ON (`overzicht`.`DeelnemerId` = `deelnemer`.`Id`) INNER JOIN `wedstrijd` ON (`overzicht`.`WedstrijdId` = `wedstrijd`.`Id`) INNER JOIN `klasse` ON (`overzicht`.`KlasseId` = `klasse`.`Id`) WHERE (`klasseID` = 1) ORDER BY Voornaam";

$result = mysql_query ($sql) or die ("Query failed ".mysql_error());

// printing HTML result
//read every row into arrays

$athletes = array();
$races = array();


while ($row=mysql_fetch_assoc($result)):
    
    //first the race
    if (!in_array($row['wedstrijd'],$races)):    //this builds an array of race names for later use
        $races[] = $row['wedstrijd'];
    endif;
    $athletes[$row['Voornaam']." ".$row['Achternaam']][$row['wedstrijd']] = $row['Perc'];
endwhile;


//we now have two nicely formed arrays
//alphabetise the races 
//sort($races);
//start the table plot
?>
<CAPTION>
<div align="center" class="style1">Overzicht behaalde percentages
</div>
<table border="1">
<tr>
<th><u>Wedstrijd <br>
</u><br>
  Deelnemer</th>
<?
foreach ($races as $val):
?> <th><?=$val?></th> <?
endforeach;
?>
</tr> 
<?
//now the header rows are down
foreach ($athletes as $key=>$val):    //athletes names are stored in the key
?>
<tr><td nowrap><?=$key?></td>
<?
    foreach($races as $race):
		$perc = isset($val[$race]) ? $val[$race] : "--"; //is did not compete
        $tmpperc = ($perc)*100;
		$totperc = round($tmpperc, 3);
		?><td><?=$totperc?></td><?        
    endforeach;
?></tr><?
endforeach;
?>
</table>
<?php
}
if(!$session->logged_in){
echo "Je bent niet ingelogd, dus geen toegang tot deze pagina.";
}
?>
 
this brings back memories (although bits of it look a bit different to my recollection...)

i can't see that you are retrieving the date in the sql. if you are then can you point it out? you can do the sort. it may be possible to do it in sql but i suspect we'll have to do it programmatically.

when you post back can you include a link to the previous post so i can refresh my memory of your table structure etc?
 
Code:
$sql = "SELECT `wedstrijd`.`Datum`
  FROM `wedstrijd` Where Year(Datum)=2006";

but how do you link the date to the other sql query?
 
That's my problem ! ;-)
The table "wedstrijd" has the Id field and "overzicht" has the field wedstrijdId. Maybe there's a connection to build.. ?
Query would be then:
SELECT `wedstrijd`.`Datum`
FROM `wedstrijd`
INNER JOIN `overzicht` ON (`wedstrijd`.`Id` = `overzicht`.`WedstrijdId`)
WHERE
(Year(`Datum`) = 2006)
ORDER BY
`Datum`
 
could you send me the output of this (filling in the blank variables first!):
Code:
$host = "";
$user = "";
$pwd = "";
$db ="";
mysql_connect($host,$user, $pwd);
mysql_select_db($db);
$result = mysql_query("show tables from $db");
$create ="";
while ($row=mysql_fetch_array($result)):
	$tables = mysql_query("show create table ".$row[0]);
	$tablecreate = mysql_fetch_assoc($tables);
	$create . = $tablecreate["Create Table"] . "<br/>";
endwhile;
echo $create;
 
Getting a parse error on line 9:
$create . = $tablecreate["Create Table"] . "<br/>";

What is it that you wish to get ?
 
pls remove the space between the dot and the equals sign.

this will provide me with your table structure (no data). i can then model the structure here and see what the query should look like. sorry - should have made that clear in the earlier post.
 
Oke, here you go...

CREATE TABLE `active_guests` ( `ip` varchar(15) NOT NULL default '', `timestamp` int(11) unsigned NOT NULL default '0', PRIMARY KEY (`ip`) ) TYPE=MyISAM
CREATE TABLE `active_users` ( `username` varchar(30) NOT NULL default '', `timestamp` int(11) unsigned NOT NULL default '0', PRIMARY KEY (`username`) ) TYPE=MyISAM
CREATE TABLE `banned_users` ( `username` varchar(30) NOT NULL default '', `timestamp` int(11) unsigned NOT NULL default '0', PRIMARY KEY (`username`) ) TYPE=MyISAM
CREATE TABLE `deelnemer` ( `Id` bigint(20) NOT NULL auto_increment, `Naam` varchar(255) default NULL, `Voornaam` varchar(255) default NULL, `ASNNR` bigint(20) default NULL, `Datum_lid` datetime default NULL, `URL` varchar(255) default NULL, `Vereniging` varchar(255) default NULL, `Adres` varchar(255) default NULL, `Postcode` varchar(255) default NULL, `Woonplaats` varchar(255) default NULL, `Telnr` varchar(255) default NULL, `Email` varchar(255) default NULL, `Oud_Sefnr` varchar(255) default NULL, PRIMARY KEY (`Id`), KEY `Id` (`Id`) ) TYPE=MyISAM
CREATE TABLE `deelnemerklasselevel` ( `DeelnemerId` bigint(20) default NULL, `KlasseId` bigint(20) default NULL, `LevelId` char(1) default NULL ) TYPE=MyISAM
CREATE TABLE `deelnemerklasselevel_04` ( `DeelnemerId` bigint(20) default NULL, `KlasseId` bigint(20) default NULL, `LevelId` char(1) default NULL ) TYPE=MyISAM
CREATE TABLE `deelnemerklasselevel_05` ( `DeelnemerId` bigint(20) default NULL, `KlasseId` bigint(20) default NULL, `LevelId` char(1) default NULL ) TYPE=MyISAM
CREATE TABLE `inschrijving` ( `Id` bigint(20) default NULL, `DeelnemerId` bigint(20) default NULL, `WedstrijdId` bigint(20) default NULL, `KlasseId` bigint(20) default NULL, `Dag` char(2) default NULL, `Uur` bigint(20) default NULL ) TYPE=MyISAM
CREATE TABLE `inschrijving_04` ( `Id` bigint(20) default NULL, `DeelnemerId` bigint(20) default NULL, `WedstrijdId` bigint(20) default NULL, `KlasseId` bigint(20) default NULL, `Dag` char(2) default NULL, `Uur` bigint(20) default NULL ) TYPE=MyISAM
CREATE TABLE `inschrijving_05` ( `Id` bigint(20) default NULL, `DeelnemerId` bigint(20) default NULL, `WedstrijdId` bigint(20) default NULL, `KlasseId` bigint(20) default NULL, `Dag` char(2) default NULL, `Uur` bigint(20) default NULL ) TYPE=MyISAM
CREATE TABLE `klasse` ( `Id` bigint(20) default NULL, `Klasse` varchar(50) default NULL ) TYPE=MyISAM
CREATE TABLE `klassement` ( `KlasseId` bigint(20) default NULL, `LevelId` char(1) default NULL, `DeelnemerId` bigint(20) default NULL, `Tot_percentage` decimal(4,3) default NULL, `Gem_percentage` decimal(4,3) default NULL ) TYPE=MyISAM
CREATE TABLE `klassement_04` ( `KlasseId` bigint(20) default NULL, `LevelId` char(1) default NULL, `DeelnemerId` bigint(20) default NULL, `Totaal percentage` double default NULL, `Gemiddelde percentage` double default NULL ) TYPE=MyISAM
CREATE TABLE `klassement_05` ( `KlasseId` bigint(20) default NULL, `LevelId` char(1) default NULL, `DeelnemerId` bigint(20) default NULL, `Tot_percentage` decimal(4,3) default NULL, `Gem_percentage` decimal(4,3) default NULL ) TYPE=MyISAM
CREATE TABLE `kopie_deelnemer` ( `Id` bigint(20) NOT NULL auto_increment, `Naam` varchar(255) default NULL, `Voornaam` varchar(255) default NULL, `ASNNR` bigint(20) default NULL, `Datum_lid` datetime default NULL, `URL` varchar(255) default NULL, `Vereniging` varchar(255) default NULL, `Adres` varchar(255) default NULL, `Postcode` varchar(255) default NULL, `Woonplaats` varchar(255) default NULL, `Telnr` varchar(255) default NULL, `Email` varchar(255) default NULL, `Oud_Sefnr` varchar(255) default NULL, PRIMARY KEY (`Id`), KEY `Id` (`Id`) ) TYPE=MyISAM
CREATE TABLE `kopie_deelnemerklasselevel` ( `DeelnemerId` bigint(20) default NULL, `KlasseId` bigint(20) default NULL, `LevelId` char(1) default NULL ) TYPE=MyISAM
CREATE TABLE `overzicht` ( `Id` decimal(10,0) unsigned zerofill NOT NULL default '0000000000', `WedstrijdId` decimal(10,0) NOT NULL default '0', `Voornaam` varchar(255) NOT NULL default '', `DeelnemerId` bigint(20) NOT NULL default '0', `KlasseId` varchar(10) NOT NULL default '0', `Percentage` decimal(5,4) NOT NULL default '0.0000' ) TYPE=MyISAM
CREATE TABLE `score` ( `Id` bigint(20) default NULL, `InschrijvingId` bigint(20) default NULL, `SerieNr` smallint(6) default NULL, `Tijd` decimal(10,2) NOT NULL default '999.00', `Punten` smallint(6) default NULL ) TYPE=MyISAM
CREATE TABLE `score_04` ( `Id` bigint(20) default NULL, `InschrijvingId` bigint(20) default NULL, `SerieNr` smallint(6) default NULL, `Tijd` decimal(10,2) default NULL, `Punten` smallint(6) default NULL ) TYPE=MyISAM
CREATE TABLE `score_05` ( `Id` bigint(20) default NULL, `InschrijvingId` bigint(20) default NULL, `SerieNr` smallint(6) default NULL, `Tijd` decimal(10,2) NOT NULL default '999.00', `Punten` smallint(6) default NULL ) TYPE=MyISAM
CREATE TABLE `users` ( `username` varchar(30) NOT NULL default '', `password` varchar(32) default NULL, `userid` varchar(32) default NULL, `userlevel` tinyint(1) unsigned NOT NULL default '0', `email` varchar(50) default NULL, `timestamp` int(11) unsigned NOT NULL default '0', PRIMARY KEY (`username`) ) TYPE=MyISAM
CREATE TABLE `wedstrijd` ( `Id` bigint(20) NOT NULL default '0', `Naam` varchar(50) default NULL, `Datum` datetime default NULL, `Plaats` varchar(50) default NULL, PRIMARY KEY (`Id`) ) TYPE=MyISAM

Note: Following tables are not relevant for our purpose:
active_guests
active_users
banned_users
users
all tables with _xx ( year extension)

 
we are concerned with

deelnemer
overzicht
klasse
wedstrijd

can you describe the data in each table, please (i don't speak dutch too well).

also can you confirm the primary-foreign key relationships between each table.

i note, for example, that the westrijdid primary key is a 20 char int but its foreign key relation in overzicht is a decimal (10). this is generally a bad idea if these are a pair match
 
Oke,
deelnemer is the table which holds name(first and last are seperate),adress,membershipnumber emailadress etc.

overzicht is created for displaying all scores in all matches for each 'deelnemer' (remember the previous post)

klasse holds four categories in which everyone can choose to perform or not.
One klasse is of course required.

wedstrijd holds the details for every match, like Name, place and date

Hope this helps a bit.. if not please ask..
and thanks for your time again !!
 
Oopsie... too busy with other things..
Just returned from weekly squash evening (23.00 my time)

Primary/Foreign keys:
deelnemer.Id = PK
overzicht.deelnemerID = FK (PK field is not used)
klasse.Id = PK
wedstrijd.Id = PK which relates to overzicht.wedstrijdId
I'm running MySQL here on a testserver at home, using Navicat.
When I run this Query:

SELECT DISTINCT `wedstrijd`.`Datum`
FROM `wedstrijd`
INNER JOIN `overzicht` ON (`wedstrijd`.`Id` = `overzicht`.`WedstrijdId`)
WHERE
(Year(`Datum`) = 2006)
ORDER BY
`Datum`

output is as expected: I get the dates of the matches which are in the table overzicht
28-1-2006
4-2-2006
18-2-2006
4-3-2006
in chronological order.
Does this help ?
 
right. something like this query should return:
for every event (together with event data)
every participant
in each class
with their scores
ordered by the date of the event

is that the kind of shape you are after? we can play with remodelling the shape to fit into your display when we have the basic info right. sql query would look like:

Code:
SELECT 
  wedstrijd.Id, 
  wedstrijd.Naam, 
  wedstrijd.Datum, 
  wedstrijd.Plaats, 
  deelnemer.Voornaam, 
  deelnemer.Naam, 
  klasse.Klasse, 
  overzicht.Percentage
FROM 
  deelnemer 
  INNER JOIN 
      ((overzicht 
        INNER JOIN 
        klasse 
         ON 
         overzicht.KlasseId = klasse.Id) 
        INNER JOIN 
        wedstrijd 
         ON 
         overzicht.WedstrijdId = wedstrijd.Id) 
      ON deelnemer.Id = overzicht.DeelnemerId
ORDER BY
   wedstrijd.Datum;
 
Yes, this is the way !
I altered the Order By to:
ORDER BY
wedstrijd.Datum,klasse.Klasse,deelnemer.Voornaam;
 
is the date actually stored in the database as dd-mm-yyyy? or is it stored as a unix timestamp or in the traditionaly mysql format of yyyy-mm-dd?
 
Copied the content of a date field from table wedstrijd: 2005-10-01 00:00:00
 
here you go. you will appreciate that it has got more complex by the addition of multiple classes. previously you had limited the classes just to "1" so we didn't have to worry about three dimensionality.

i can't test the code without data in the db and i haven't dummied any yet.

hope this is what you were after!
Code:
<?
include("../../Loginsys/include/session.php");
if($session->logged_in){
?>
<style type="text/css">
<!--
body {
    background-image: url(../../images/bg_grad.jpg);
    margin-top: 30px;
}
.style1 {
    color: #FFFFFF;
    font-weight: bold;
    font-size: 14pt;
}
-->
</style>
<?php
 require_once('../../Connections/ASN.php'); ?>
<?php
error_reporting(E_ALL); 
mysql_select_db($database_ASN, $ASN);

$sql = 
"SELECT 
  wedstrijd.Id, 
  wedstrijd.Naam AS wedstrijd, 
  wedstrijd.Datum as datum, 
  wedstrijd.Plaats as plaats, 
  deelnemer.Voornaam as voornaam, 
  deelnemer.Naam as surname, 
  overzicht.KlasseId as klasseID,
  klasse.Klasse as klasse, 
  overzicht.Percentage as perc
FROM 
  deelnemer 
  INNER JOIN 
      ((overzicht 
        INNER JOIN 
        klasse 
         ON 
         overzicht.KlasseId = klasse.Id) 
        INNER JOIN 
        wedstrijd 
         ON 
         overzicht.WedstrijdId = wedstrijd.Id) 
      ON deelnemer.Id = overzicht.DeelnemerId
ORDER BY
	wedstrijd.Datum,klasse.Klasse,deelnemer.Voornaam
";

$result = mysql_query ($sql) or die ("Query failed ".mysql_error());

// printing HTML result
//read every row into arrays

$athletes = array();
$races = array();


while ($row=mysql_fetch_assoc($result)):
    
    //first the race
    if (!in_array($row['wedstrijd'],$races)):    //this builds an array of race names and dates for later use
        $races[] = array("wedstrijd"=>$row['wedstrijd'], "date"=>$row['datum'], "location" => $row['plaats']);
    endif;
    $athletes[$row['Voornaam']." ".$row['Achternaam']][$row['wedstrijd']][$row['klasse']] = $row['Perc'];	//we have now reshaped this array
endwhile;


//we now have two nicely formed arrays
//alphabetise the races 
//sort($races);
//start the table plot
?>
<CAPTION>
<div align="center" class="style1">Overzicht behaalde percentages
</div>
<table border="1">
<tr>
<th><u>Wedstrijd <br>
</u><br>
  Deelnemer</th>
<?
//this has been reshaped to show the date and the location
foreach ($races as $val):
?> <th align="center"><? echo $val['wedstrijd']."<br/><span style=\"font-size:smaller\">".$val['wedstrijd']." (".date("d-M-Y", strtotime($val['datum'])).")</span>";?></th> <?
endforeach;
?>
</tr> 
<?
//now the header rows are down
foreach ($athletes as $key=>$val):    //athletes names are stored in the key
?>
<tr><td nowrap><?=$key?></td>
<?
//shape [name][race][klasse][perc]
    foreach($races as $race):
				if (isset($val[$race])):
					//did compete and will have done so by classes
					$perc = "";
					foreach ($val[$race] as $klass=>$percentage):
						$perc .= round(($percentage*100), 3) . "($klass)<br/>";
					endforeach;			
				else:
					$perc = "--"; //did not compete
				endif;
				?><td><?=$perc?></td><?
    endforeach;
?></tr><?
endforeach;
?>
</table>
<?php
}
if(!$session->logged_in){
echo "Je bent niet ingelogd, dus geen toegang tot deze pagina.";
}
?>
 
Getting several errors :-(
Warning: Illegal offset type in C:\Inetpub\ on line 69
Line 69 = if (isset($val[$race])):

And the races column is repeated many time and has this text in it:
Notice: strtotime(): Called with empty time parameter in C:\Inetpub\ on line 57
Classic Speed Shoot
Classic Speed Shoot (09-Mar-2006) (=Today's date !) ??
Actual race date is 28-01-2006

Line 57 :
?> <th align="center"><? echo $val['wedstrijd']."<br/><span style=\"font-size:smaller\">".$val['wedstrijd']." (".date("d-M-Y", strtotime($val['datum'])).")</span>";?></th> <?

The athletes column looks oke

Hope you aren't fed up with me yet...
 
Forgot to mention there are no scores (perc) present either
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top