Jump to content

Recommended Posts

I have an array which ends with a comma and a space. I want to remove the comma and space at the end, but not between the rest of the words in the array. Below is what I have done.

 

I have a list of floor types and a list of people who can have multiple floor types.

 

I have two tables;

table1: floortypes: id (int) and type (varchar): id 1 = carpet, id 2 =linoleum, id 3 = tile, id 4 = wood.

table 2: listings: id (int) and floortype (varchar): id 1 = 1, 3, 4

<?php

mysql_select_db($database_mysql_connect, $mysql_connect);

$query = "SELECT * FROM listings WHERE id = '".$_GET['ID']."'";

$results = mysql_query($query, $mysql_connect) or die(mysql_error());

$row = mysql_fetch_assoc($results);

$floorchoices = explode(", ",$row['floortype']);

 

query2 = "SELECT * FROM floortype";

$results2 = mysql_query($query2, $mysql_connect) or die(mysql_error());

$row2 = mysql_fetch_assoc($results2);

?>

 

The Floor Types are:

<?php do { ?>

<?php if (in_array($row2['id'], $floorchoices)) {echo "".$row2['type'].", ";} ?>

<?php

} while ($row2 = mysql_fetch_assoc($floor));

?>           

 

This is the output: The Floor Types are: Carpet, Tile, Wood,

 

I need to remove that last comma and space.

 

I tried using combinations of rtrim and strlen and substr but can't seem to get it right.

Got any ideas on how to remove that comma and space at the end?

Change this:

<?php do { ?>
<?php if (in_array($row2['id'], $floorchoices)) {echo "".$row2['type'].", ";} ?>
<?php
} while ($row2 = mysql_fetch_assoc($floor));
?>

to

<?php
$tmp = array();
while ($row2 = mysql_fetch_assoc($floor))
   if (in_array($row2['id'], $floorchoices))
     $tmp[] = $row2['type'];
echo implode(', ',$tmp);
?>

 

Note: you shouldn't enter/leave PHP on each line.

 

Ken

I made a slight error when copying the original code to this forum, but it was affecting it.

The only code is this:

<?php
mysql_select_db($database_mysql_connect, $mysql_connect);
$query = "SELECT * FROM listings WHERE id = '".$_GET['ID']."'";
$results = mysql_query($query, $mysql_connect) or die(mysql_error());
$row = mysql_fetch_assoc($results);
$floorchoices = explode(", ",$row['floortype']);

query2 = "SELECT * FROM floortype";
$results2 = mysql_query($query2, $mysql_connect) or die(mysql_error());
$row2 = mysql_fetch_assoc($results2);
?>

Then there is this:

The Floor Types:

<?php
$tmp = array();
while ($row2 = mysql_fetch_assoc($results2))
   if (in_array($row2['id'], $floorchoices))
     $tmp[] = $row2['type'];
echo implode(', ',$tmp);
?>

Change

<?php
$results2 = mysql_query($query2, $mysql_connect) or die(mysql_error());
$row2 = mysql_fetch_assoc($results2);
?>

to

<?php
$results2 = mysql_query($query2, $mysql_connect) or die(mysql_error());
?>

i.e. remove the line "$row2 = mysql_fetch_assoc($results2);" since that's now done in the while loop.

 

Ken

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.