Jump to content

Remove comma from last result


lbaxterl

Recommended Posts

I have the following script which outputs different results based on weather a result is odd or even, pretty simple. The prblem lies with the very last result ouputted, as you can see from the code it will have a comma trailing it, which causes IE to not display the gallery.

 

Can Anyone advise me on how to remove this last comma/echo the last result differently.

 

Thank you

 

<?php
include "config.inc.php";
$num = 1;
function checkNum($num){
  return ($num%2) ? TRUE : FALSE;
}


	$result = mysql_query( "SELECT photo_id, photo_filename FROM gallery_photos WHERE photo_category= 6 AND promo= 1 ");
	$nr = mysql_num_rows( $result );

		$NumOfRecords = mysql_num_rows($result);

while($row = mysql_fetch_array($result)) {


  
  $id = $row['photo_id'];
  $img = $row['photo_filename'];
  
  
  if(checkNum($num) === TRUE){
   echo "
   
  { src: 'admin/photos/".$img."', dir: 'left'   },";
  
  $num++;

  }
  
elseif ($num == $NumOfRecords) {
      echo "  { src: 'admin/photos/".$img."', dir: 'left'   }";
   }


  
  else{
  echo  "
   
  { src: 'admin/photos/".$img."', dir: 'right'   },";
  $num++;
}

}

      ?>

Link to comment
https://forums.phpfreaks.com/topic/227359-remove-comma-from-last-result/
Share on other sites

Sorry i posted code i was messing around with there here is the working code (in firefox at least), it still outputs the last record having a comma after it.

 


<?php
include "config.inc.php";
$num = 1;
function checkNum($num){
  return ($num%2) ? TRUE : FALSE;
}


	$result = mysql_query( "SELECT photo_id, photo_filename FROM gallery_photos WHERE photo_category= 6 AND promo= 1 ");
	$nr = mysql_num_rows( $result );

		$NumOfRecords = mysql_num_rows($result);

while($row = mysql_fetch_array($result)) {


  
  $id = $row['photo_id'];
  $img = $row['photo_filename'];
  
  
  if(checkNum($num) === TRUE){
   echo "
   
  { src: 'admin/photos/".$img."', dir: 'left'   },";
  
  $num++;

  }
  
  else{
  echo  "
   
  { src: 'admin/photos/".$img."', dir: 'right'   },";
  $num++;
}

}

      ?>

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.