tempi Posted August 9, 2007 Share Posted August 9, 2007 Hi there, I'm working with php in combination with mysql and couldn't figure out how to solve the following problem/task: I have an event-table with fields like: Title, Location, Time, Name, Startdate1, Startdate2, Startdate3 So the thing is, that I like to print out one event for all the different startdates. Let's say one eventrecord looks like ('Fishing','Lake Bana', '14:00', 'Michael Scofield', '2007-08-12', '2007-08-27', '2007-09-03' Then the output should be like 'Fishing','Lake Bana', '14:00', 'Michael Scofield', '2007-08-12' 'Fishing','Lake Bana', '14:00', 'Michael Scofield', '2007-08-27' 'Fishing','Lake Bana', '14:00', 'Michael Scofield', '2007-09-03' The problem is, that each event has these different startdates and in the end, all the events shoul be sorted by the date. So startdate3 from record1 could come after startdate1 from record2. Can I get these chronologically right? I hope you understand what I mean. Is this solvable? Thanks for any help! Tempi Quote Link to comment Share on other sites More sharing options...
tempi Posted August 10, 2007 Author Share Posted August 10, 2007 /push :-\ I tought about sorting the records in the php script itself. Plz, give me a hint. Greez Tempi Quote Link to comment Share on other sites More sharing options...
gurroa Posted August 10, 2007 Share Posted August 10, 2007 // this function sort double array by given key name function SortArrayByValue($ar, $keyname, $desc = false) { if (!is_array($ar) || empty($keyname)) return $ar; $arsort = array(); reset($ar); while(list($key, $rad) = each($ar)) { $val = $rad[$keyname]; $v = 1; while(isset($arsort[$val])) $val = $rad[$keyname].' '.$v++; $arsort[$val] = $key; } if ($desc) krsort($arsort); else ksort($arsort); reset($arsort); $retar = array(); while(list(,$key) = each($arsort)) $retar[$key] = $ar[$key]; return $retar; } $arevents = array(); while($row = mysql_fetch_array($result)) { $startdays = array($row['Startdate1'], $row['Startdate2'], $row['Startdate3']); if (!empty($startdays[0])) { $row['Startdate'] = $startdays[0]; $row['Sortway'] = $row['Startdate'].' '.$row['Time']; $arevents[] = $row; } if (!empty($startdays[1])) { $row['Startdate'] = $startdays[1]; $row['Sortway'] = $row['Startdate'].' '.$row['Time']; $arevents[] = $row; } if (!empty($startdays[2])) { $row['Startdate'] = $startdays[2]; $row['Sortway'] = $row['Startdate'].' '.$row['Time']; $arevents[] = $row; } } if (count($arevents) > 0) $arevents = SortArrayByValue($arevents, 'Sortway', false); //listevents reset($arevents); while(list(,$row) = each($arevents)) echo $row['Startdate'].' '.$row['Title'].'<br />'; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.