timmah1 Posted September 10, 2009 Share Posted September 10, 2009 I need to be able to show each date separated by a dash This shows all the dates it is supposed to, but it puts them all together, with no space or anything. How can I put a dash between the dates if there are more than 1, and not show the dash if there is only one? Here is my code <?php include("../connect.php"); $query5 = "SELECT * FROM picks WHERE week = '$current_week' GROUP BY game"; $w5 = mysql_query($query5) or die(mysql_error()); while ($b5 = mysql_fetch_array($w5)) { $week = $b5['week']; $current_game .= date("F j, Y", strtotime($b5['game'])); } echo "<h2 align='center'>FREE FOOTBALLL PICKS FOR WEEK $week - $current_game</h2>"; ?> Right now, it shows it like this FREE FOOTBALLL PICKS FOR WEEK 1 - September 12, 2009September 13, 2009 I'd like to be able to show it like this FREE FOOTBALLL PICKS FOR WEEK 1 - September 12, 2009 - September 13, 2009 Thanks in advance Link to comment https://forums.phpfreaks.com/topic/173783-solved-separate-items-by-dash-for-more-than-1/ Share on other sites More sharing options...
Stuie_b Posted September 10, 2009 Share Posted September 10, 2009 the easiest way is to count the number of rows returned and if it is greater than 1 add a dash to the string. the following should work fine <?php include("../connect.php"); $query5 = "SELECT * FROM picks WHERE week = '$current_week' GROUP BY game"; $w5 = mysql_query($query5) or die(mysql_error()); $count = mysql_num_rows($w5); while ($b5 = mysql_fetch_array($w5)) { $week = $b5['week']; $current_game .= date("F j, Y", strtotime($b5['game'])); if($count > 1){ $current_game .=" - "; } } echo "<h2 align='center'>FREE FOOTBALLL PICKS FOR WEEK $week - $current_game</h2>"; ?> Stuie Link to comment https://forums.phpfreaks.com/topic/173783-solved-separate-items-by-dash-for-more-than-1/#findComment-916061 Share on other sites More sharing options...
timmah1 Posted September 10, 2009 Author Share Posted September 10, 2009 Stuie, That works fine, thank you. But how to I keep it from displaying a - after the last result? Meaning, right now it shows like this September 12, 2009 - September 13, 2009 - Is there a way with that code to eliminate the last -? Link to comment https://forums.phpfreaks.com/topic/173783-solved-separate-items-by-dash-for-more-than-1/#findComment-916064 Share on other sites More sharing options...
Stuie_b Posted September 10, 2009 Share Posted September 10, 2009 Try the following <?php include("../connect.php"); $i=0; $query5 = "SELECT * FROM picks WHERE week = '$current_week' GROUP BY game"; $w5 = mysql_query($query5) or die(mysql_error()); $count = mysql_num_rows($w5); while ($b5 = mysql_fetch_array($w5)) { $i++; $week = $b5['week']; $current_game .= date("F j, Y", strtotime($b5['game'])); if($count > 1 AND $i !== $count){ $current_game .=" - "; } } echo "<h2 align='center'>FREE FOOTBALLL PICKS FOR WEEK $week - $current_game</h2>"; ?> Basically what's happening is we increase $i everytime we run through the results, then we check that it's not the last value returned if it's not we add a dash otherwise do nothing.. it's not the best solution but it should give you a starting point.. Stuie Link to comment https://forums.phpfreaks.com/topic/173783-solved-separate-items-by-dash-for-more-than-1/#findComment-916074 Share on other sites More sharing options...
timmah1 Posted September 10, 2009 Author Share Posted September 10, 2009 Thanks so much Stuie! Works perfect Link to comment https://forums.phpfreaks.com/topic/173783-solved-separate-items-by-dash-for-more-than-1/#findComment-916083 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.