Zaaka Posted August 14, 2009 Share Posted August 14, 2009 I have the following query string : $query_Recordset2 = sprintf("SELECT DISTINCT ctYear, o, F1, ctComp, ctpYear, ctLvl,ctpup, ctTrainers, ctNotes FROM mainsheet WHERE F1 = %s", GetSQLValueString($colname_Recordset2, "int")); produced by Dreamweaver CS3, but I cannot get it to do what I need. I have a list of dates eg 2008/9 ,and a list of months but dont want the year duplicated. This is what I have: YearMonth 2007/8September 07 2007/8October 07 2007/8November 07 2007/8January 08 2007/8August 08 2008/9January 09 This is what I need: YearMonth 2007/8September 07 [/td]October 07 November 07 January 08 2008/9January 09 But the DISTINCT command wont work Link to comment https://forums.phpfreaks.com/topic/170335-solved-distinct-can-i-use-to-do-this/ Share on other sites More sharing options...
roopurt18 Posted August 15, 2009 Share Posted August 15, 2009 DISTINCT selects distinct records; it has no bearing on how the records are displayed. You need to handle that yourself in the code used to display the results. Give me a moment and I'll work up an example. Link to comment https://forums.phpfreaks.com/topic/170335-solved-distinct-can-i-use-to-do-this/#findComment-898559 Share on other sites More sharing options...
roopurt18 Posted August 15, 2009 Share Posted August 15, 2009 <?php // Going to load up a list of dates $dates = array(); $base = strtotime( '1999-12-31' ); for( $i = 0; $i < 200; $i++ ) { $offset = rand( 1, 365 * rand( 0, 10 ) ); // day of year $offset = $offset * 24 * 60 * 60; // seconds into the year $offset = $offset + rand( 0, 24 * 60 * 60 ); // seconds into the day $dates[] = date( 'Y-m-d H:i:s', $base + $offset ); } sort( $dates ); $curYear = null; // keep track of which year we're displaying echo <<<TABLE Year | Rest TABLE; foreach( $dates as $d ) { $stamp = strtotime( $d ); $y = sprintf( "%-10s", date( 'Y', $stamp ) ); // We only display $y as the year IF its not the current year we're tracking if( $curYear === $y ) { // It's the one we're tracking, so zero it out $y = ' '; }else{ $curYear = $y; // Track the new year } $disp = date( 'F jS, g:i A', $stamp ); echo <<<TABLE {$y}| {$disp} TABLE; } ?> Link to comment https://forums.phpfreaks.com/topic/170335-solved-distinct-can-i-use-to-do-this/#findComment-898570 Share on other sites More sharing options...
Zaaka Posted August 15, 2009 Author Share Posted August 15, 2009 so how do i apply it to the following : <?php do { ?> <tr> <td width="50"><?php echo $row_Recordset1['ctYear']; ?></td> <<<< Problem Line <td><?php echo $row_Recordset1['ctComp']; ?></td> <td width="60"><div align="center"><?php echo $row_Recordset1['ctpYear']; ?></div></td> <td width="50"><div align="center"><?php echo $row_Recordset1['ctLvl']; ?></div></td> <td width="50"><div align="center"><?php echo $row_Recordset1['ctpup']; ?></div></td> <td width="40" align="right"> <a href="vctinfo.php?s=<?php echo $row_Recordset1['o']; ?>">VIEW </a></td> <td width="40">- <a href="ectinfo.php?s=<?php echo $row_Recordset1['o']; ?>">EDIT</a></td> <td width="40">- <a href="dctinfo.php?o=<?php echo $row_Recordset1['o']; ?>&s=<?php echo $row_Recordset1['F1']; ?>">DEL</a></td> </tr> <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?> Link to comment https://forums.phpfreaks.com/topic/170335-solved-distinct-can-i-use-to-do-this/#findComment-898604 Share on other sites More sharing options...
roopurt18 Posted August 15, 2009 Share Posted August 15, 2009 <?php $curYear = null; do { $dispYear = $row_Recordset1['ctYear']; if( $dispYear === null ) { $dispYear = ' '; }else{ $curYear = $dispYear; } ?> <tr> <td width="50"><?php echo $dispYear; ?></td> <<<< Problem Line <td><?php echo $row_Recordset1['ctComp']; ?></td> <td width="60"><div align="center"><?php echo $row_Recordset1['ctpYear']; ?></div></td> <td width="50"><div align="center"><?php echo $row_Recordset1['ctLvl']; ?></div></td> <td width="50"><div align="center"><?php echo $row_Recordset1['ctpup']; ?></div></td> <td width="40" align="right"> <a href="vctinfo.php?s=<?php echo $row_Recordset1['o']; ?>">VIEW </a></td> <td width="40">- <a href="ectinfo.php?s=<?php echo $row_Recordset1['o']; ?>">EDIT</a></td> <td width="40">- <a href="dctinfo.php?o=<?php echo $row_Recordset1['o']; ?>&s=<?php echo $row_Recordset1['F1']; ?>">DEL</a></td> </tr> <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?> Link to comment https://forums.phpfreaks.com/topic/170335-solved-distinct-can-i-use-to-do-this/#findComment-898634 Share on other sites More sharing options...
Zaaka Posted August 15, 2009 Author Share Posted August 15, 2009 Didnt work at first but changed : if( $dispYear === null ) { $dispYear = ' '; To if( $dispYear === $curYear ) { $dispYear = ' '; And it works Great, Thankyou for your help. Link to comment https://forums.phpfreaks.com/topic/170335-solved-distinct-can-i-use-to-do-this/#findComment-898764 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.