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

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

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 -?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.