Jump to content

[SOLVED] Separate items by dash for more than 1


timmah1

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.