I appreciate your help Johan.
Not really sure what you mean by schema, so here's everything that I can think of that could help...
First, here are the fields within the 'clanwars' db table that contain data that I would like to unserialize and move to other fields (except for 'cwID' and 'maps'):
[CODE]| cwID | maps | hometeam | homescore | oppscore |
| 40 | a:3:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";} | a:2:{i:0;s:2:"57";i:1;s:3:"217";} | a:3:{i:0;s:3:"140";i:1;s:3:"134";i:2;s:3:"110";} | a:3:{i:0;s:2:"86";i:1;s:3:"110";i:2;s:3:"134";} |[/CODE]
Notes:
- cwID is the index (why I included it).
- I do not need to keep 'maps' since I won't be using them in the future, but I included them here because they are linked to 'homescore' and 'oppscore' in the coding.
- 'hometeam' contains userID numbers to represent the players who played. They appear to be stored alphabetically according to the players nickname (for example 57 = 'Adam' and 217 = 'Bob') since other entries are not in numerical order but are alphabetical.
- 'homescore' and 'oppscore' contain 3 scores for each of the 3 maps that were played and make-up a match (or clanwar).
Here's the new fields that I've created, containing the unserialized data (to give you an idea of what I would like):
[CODE]| cwID | player1 | player2 | player3 | player4 | player5 | player6 | player7 | home_score1 | home_score2 | home_score3 | home_total | op_score1 | op_score2 | op_score3 | op_total | difference |
| 40 | 57 | 217 | | | | | | 140 | 134 | 110 | | 86 | 110 | 134 | | |[/CODE]
This is the code used which serializes/stores the data:
if(isset($_POST['hometeam'])) $hometeam = $_POST['hometeam'];
if(isset($_POST['map_name'])) $maplist = $_POST['map_name'];
if(isset($_POST['map_result_home'])) $homescr = $_POST['map_result_home'];
if(isset($_POST['map_result_opp'])) $oppscr = $_POST['map_result_opp'];
$maps = array();
if(!empty($maplist)) {
if(is_array($maplist)) {
foreach($maplist as $map) {
$maps[]=stripslashes($map);
}
}
}
$backup_theMaps = serialize($maps);
if(function_exists("mysql_real_escape_string")) {
$theMaps = mysql_real_escape_string($backup_theMaps);
}
else {
$theMaps = addslashes($backup_theMaps);
}
$scores = array();
if(!empty($homescr)) {
if(is_array($homescr)) {
foreach($homescr as $result) {
$scores[]=$result;
}
}
}
$theHomeScore = serialize($scores);
$results = array();
if(!empty($oppscr)) {
if(is_array($oppscr)) {
foreach($oppscr as $result) {
$results[]=$result;
}
}
}
$theOppScore = serialize($results);
$team=array();
if(is_array($hometeam)) {
foreach($hometeam as $player) {
if(!in_array($player, $team)) $team[]=$player;
}
}
$home_string = serialize($team);
safe_query("INSERT INTO ".PREFIX."clanwars ( maps, hometeam, homescore, oppscore)
VALUES( '".$theMaps."', '$home_string', '$theHomeScore', '$theOppScore' ) ");
$cwID=mysql_insert_id();
And the code used to unserialize/show it:
$ds=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."clanwars WHERE cwID='$cwID'"));
$maps="";
$hometeam="";
$score="";
$extendedresults="";
$homescr = array_sum(unserialize($ds['homescore']));
$oppscr = array_sum(unserialize($ds['oppscore']));
$theMaps = unserialize($ds['maps']);
if(is_array($theMaps)) {
$n=1;
foreach($theMaps as $map) {
if($n == 1) {
$maps.=$map;
} else {
if($map=='') {
$maps=$_language->module['no_maps'];
} else {
$maps.=', '.$map;
}
}
$n++;
}
}
if($homescr>$oppscr){
$results_1='<font color="'.$wincolor.'">'.$homescr.'</font>';
$results_2='<font color="'.$loosecolor.'">'.$oppscr.'</font>';
}
elseif($homescr<$oppscr){
$results_1='<font color="'.$loosecolor.'">'.$homescr.'</font>';
$results_2='<font color="'.$wincolor.'">'.$oppscr.'</font>';
}
else{
$results_1='<font color="'.$drawcolor.'">'.$homescr.'</font>';
$results_2='<font color="'.$drawcolor.'">'.$oppscr.'</font>';
}
if(!empty($ds['hometeam'])) {
$array=unserialize($ds['hometeam']);
$n=1;
foreach($array as $id) {
if(!empty($id)) {
if($n>1) $hometeam.=', <a href="index.php?site=profile&id='.$id.'">'.getnickname($id).'</a>';
else $hometeam.='<a href="index.php?site=profile&id='.$id.'">'.getnickname($id).'</a>';
$n++;
}
}
}
$scoreHome=unserialize($ds['homescore']);
$scoreOpp=unserialize($ds['oppscore']);
$homescr=array_sum($scoreHome);
$oppscr=array_sum($scoreOpp);
if($homescr>$oppscr) {
$result_map='[color='.$wincolor.']'.$homescr.'[/color] : [color='.$loosecolor.']'.$oppscr.'[/color]';
$result_map2='won';
}
elseif($homescr<$oppscr) {
$result_map='[color='.$loosecolor.']'.$homescr.'[/color] : [color='.$wincolor.']'.$oppscr.'[/color]';
$result_map2='lost';
}
else {
$result_map='[color='.$drawcolor.']'.$homescr.'[/color] : [color='.$drawcolor.']'.$oppscr.'[/b][/color]';
$result_map2='draw';
}
if(is_array($theMaps)) {
$d=0;
foreach($theMaps as $map) {
$score='';
if(($d+1)%2) { $bgone=BG_1; $bgtwo=BG_2; } else { $bgone=BG_3; $bgtwo=BG_4; }
if($scoreHome[$d] > $scoreOpp[$d]){
$score_1='<font color="'.$wincolor.'">'.$scoreHome[$d].'</font>';
$score_2='<font color="'.$loosecolor.'">'.$scoreOpp[$d].'</font>';
}
elseif($scoreHome[$d] < $scoreOpp[$d]){
$score_1='<font color="'.$loosecolor.'">'.$scoreHome[$d].'</font>';
$score_2='<font color="'.$wincolor.'">'.$scoreOpp[$d].'</font>';
}
else{
$score_1='<font color="'.$drawcolor.'">'.$scoreHome[$d].'</font>';
$score_2='<font color="'.$drawcolor.'">'.$scoreOpp[$d].'</font>';
}
eval ("\$clanwars_details_results = \"".gettemplate("clanwars_details_results")."\";");
$extendedresults.=$clanwars_details_results;
unset($score);
$d++;
}
} else $extendedresults='';
So far I have this as a pathetic attempt at a script:
<?php
// gets all data
$ergebnis=(safe_query("SELECT * FROM ".PREFIX."clanwars"));
if($ergebnis) {
while($ds=mysql_fetch_array($ergebnis)) {
// gets total scores
$homescore=array_sum(unserialize($ds['homescore']));
$oppscore=array_sum(unserialize($ds['oppscore']));
// gets difference
$difference = $homescore - $oppscore;
}
}
safe_query("UPDATE ".PREFIX."clanwars SET
home_total='$homescore',
op_total='$oppscore',
difference='$difference'
");
?>
Another question I have is would the above write the relevant data for the correct cwID, since it is selecting all data?
(sorry if this was not what you needed to see/wasted your time).