Hi.
I'm writing a script which accesses a MySQL database and outputs information. To understand the problem you'll need a bit of information about how the system should work.
A user can add a publication to the database. This contains information on authors (multiple) as well as information on the publication such as the year it was made, who published it etc.
The user can set up a "style" which allows them to choose an order for the data to be outputted. So they can make one which first outputs authors surnames then the publication name. Or one which lets them output the Year, Title then authors surnames and first names.
I then have an output page which will use a style they've made as well as however many publications they've chosen and output all the publications in the order set by the style.
I am now writing the query to make this output, but am running into some problems.
First it seems far too complicated to try and order it as the information is retrieved from the databased, so instead I'm getting all the information and adding it to one big variable "$output".
I have made it so that the information that is selected is outputted to the variable. It is only outputting the information they ask for, so that's working good.
My problem is with ordering it. I'm not sure how to do two things.
1) Order the data in the way it was chosen when they created the style from the new variable $output
2) Place selected formatting they have chosen (such as a space, ", - etc.) between each piece of data in the output.
So effectivly I want a script which could turn
"HumphreysGeorgeThe Publication2007"
into
"George, Humphreys - The Publication (2007)"
I am not sure what I need to do to get this to work as my PHP knowledge is still fairly limited and it's hurting my head trying to get it working.
If someone could help me I would be incredibly grateful! I hope I have made the problem clear, it is quite tricky to explain exactly what I mean.
I will include the code I have made so far on the output (a few bits like connecting to the database and some parameters which are set are on a previous page but I don't think their necessary to include):
<?
//Query selected style
$stylequery=mysql_query("SELECT * FROM style WHERE styleid='$usestyle'");
//Retrieve information on selected style
$styledata=mysql_fetch_array($stylequery);
//Find number of fields in style
$fields=$styledata["fields"];
//Set a counter to loook through each field with
$counter=0;
//Start a loop to check each field requested
while($counter<$fields){
//Increase counter to move through loop
$counter++;
//Set each field
$field[$counter]=$styledata["field".$counter];
}
//Search through fields to find out if any author information is required
if(in_array("fa",$field,true)){
//Set a variable to state that a field is required
$title=true;
}
if(in_array("fb",$field,true)){
//Set a variable to state that a field is required
$year=true;
}
if(in_array("fc",$field,true)){
//Set a variable to state that a field is required
$surname=true;
}
if(in_array("fd",$field,true)){
//Set a variable to state that a field is required
$name=true;
}
if(in_array("fe",$field,true)){
//Set a variable to state that a field is required
$inital=true;
}
//Loop for each document
foreach($array as $docid){
//Select the authorids which match the current docid
$authidquery=mysql_query("SELECT authorid FROM authorship WHERE docid='$docid'");
//Count how many results were returned
$authidtotal=mysql_num_rows($authidquery);
//Set a counter to move through the rows with
$counter=0;
//Start a second loop to go through all of the author information
while($counter<$authidtotal){
//Get the current authid
$authid=mysql_result($authidquery,$counter);
//Run query to select information
$doquery=mysql_query("SELECT * FROM authors WHERE authorid='$authid'");
$get=mysql_fetch_array($doquery);
if($surname=="true"){
$output .= $get["surname"];
}
if($name=="true"){
$output .= $get["name"];
}
if($inital=+"true"){
$output .= $get["inital"];
}
//Clear the query for the next result
$query="";
//Increase the counter to move onto the next result
$counter++;
//Close authid loop
}
//Get the publication information requested
$docquery=mysql_query("SELECT * FROM docs WHERE docid='$docid'");
$docresult=mysql_fetch_array($docquery);
if($title=="true"){
$output .= $docresult["title"];
}
if($year=="true"){
$output .= $docresult["year"];
}
//Close docid loop
}
echo $output;
?>
Anyone who has the time to help me, it would be very much appreciated.
Thanks a lot!
Matt