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 Quote Link to comment 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 Quote Link to comment 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 -? Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
timmah1 Posted September 10, 2009 Author Share Posted September 10, 2009 Thanks so much Stuie! Works perfect 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.