Jump to content

[SOLVED] remove last two characters of an array


eagleweb

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

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.