Trailing Comma Variation

I need to delete not a trailing comma but this character - |

This is what my display code looks like, where $Name is a list of names of national parks…


echo <<<EOD
   $Name |
EOD;

So the actual display looks something like this: Denali | Everglades | Yellowstone | Yosemite |

I’d like to delete that trailing character after Yosemite.

So I modified my code slightly:


$Name = ''.$Name.' |';

echo <<<EOD
   $Name
EOD;

Then I added the following script, replacing the comma with an |…


function deleteTrailingCommas($str)
{
    return trim(preg_replace("/(.*?)((,|\\s)*)$/m", "$1", $str));
}

echo <<<EOD
   $Name
EOD;

But I assumed this was tripping it up…


(||\\s)

However, I then discovered that even the original code (searching for a comma) triggers a “cannot redeclare” error. Do I get an error because I’m using the script inside a while loop?

Anyway, can anyone tell me how to zap that last character in my display? Thanks.

It would be easier if you put the multiple names in to an array.


$names = array( ... );
echo join( $names, ' | ' );

How do you put values from a DB table in an array? I tried this…


Name = $row=['Name'];
$Names = array($Name);
echo join( $Names, ' | ' ); 

echo <<<EOD
   $Names
EOD;

It displays the names, but each name is followed by “array.”

Thanks.

$names[] = $row['name'];

echo join( $names, ' | ' );

Leave off that second echo. And put the echo out side of the while loop. Collect all the information you need from the database and then use it outside of the while loop. You can use arrays for that.

You can do this:


$Names = "Denali | Everglades | Yellowstone | Yosemite |";
$Names = rtrim($Names, " |");

This rtrim() will strip all spaces and | characters from the end of the string.

Wow, that’s pretty cool; I didn’t know there was a way to escape while loops.

One weird problem, though. For some reason, it seems to ignore my database query constraints and suck everything out of the table it can. This is what it should display:

Big Cypress National Preserve | Biscayne National Park | Canaveral National Seashore | Dry Tortugas National Park | Everglades National Park | Gulf Islands National Seashore | Timucuan Ecological and Historic Preserve

But this is the actual display:

Big Cypress National Preserve | Biscayne National Park | Canaveral National Seashore | Dry Tortugas National Park | Everglades National Park | Gulf Islands National Seashore | Timucuan Ecological and Historic Preserve | Archie Carr National Wildlife Refuge // Osceola National Forest | |Osceola National Forest | | Arthur R. Marshall Loxahatchee National Wildlife Refuge // [etc.] // Osceola National Forest | |Array

I don’t know where these come from… | |, //

I checked my code in phpMyAdmin, and it seems to work just fine, displaying only Florida national parks…


$result = mysql_query('select count(*) from gw_protected_areas_usa');
if (($result) && (mysql_result ($result , 0) > 0)) {
} else {
die('Invalid query: ' . mysql_error());
}
{
  $res = mysql_query ("SELECT GWP.ID, GWP.IDArea, GWP.Name, GWP.URL, GWP.Category, GWP.Type
   FROM gw_protected_areas_usa GWP
   WHERE GWP.IDArea = '$MyID'
   AND GWP.Category != 'Cul' AND GWP.Parent = 'NPS'") or die (mysql_error());

echo '<p class="pflat"><b>National Parks, Monuments, etc.</b> &#8211; ';
{
while ($row = mysql_fetch_array ($res)) {
$ID = $row['ID'];
$IDArea = $row['IDArea'];

$Names[] = $row['Name'];
echo join( $Names, ' | ' ); 

$URL = $row['URL'];
$Cat = $row['Category'];
$Type = $row['Type'];

}
}
}

echo $Names;
echo '</p>';

Any idea what’s going on?

P.S. Lemon Juice, I tried your solution, too, but it isn’t working for me yet. I’ll play with it some more.

Thanks for all the tips.

WHERE GWP.IDArea = ‘$MyID’

Where is $MyID defined?

Also… why are there extra {}'s in your code?

Higher up the food chain on another include. $MyID simply identifies the state. For example, if the URL is MySite/World/Arizona, then that matches Arizona’s code - us-az - in the database. So if $MyID = us-az, then the page displays information associated with Arizona.

Hmmm…that’s a good question. I put my queries together long ago with a lot of help and have gradually refined them over the years, but I still don’t know how to write good queries. I’ve thought about taking a PHP or database class at a local university if I can find the time and money.

Have you checked to make sure that $MyID contains what you THINK it contains? If it contained, say, ‘%’, you’d select everything…

I just replaced $MyID with an actual value (us-nm), and the results are the same.

I discovered one thing, though. When Logic Earth said put the SECOND echo outside the while loop, I thought he meant $Names. I just put the join echo outside the while loop, and it works a little better.

This is what my code looks like now:


$result = mysql_query('select count(*) from gw_protected_areas_usa');
if (($result) && (mysql_result ($result , 0) > 0)) {
} else {
die('Invalid query: ' . mysql_error());
}
{
  $res = mysql_query ("SELECT GWP.ID, GWP.IDArea, GWP.Name, GWP.URL, GWP.Category, GWP.Type
   FROM gw_protected_areas_usa GWP
   WHERE GWP.IDArea = '$MyID'
   AND GWP.Category != 'Cul' AND GWP.Parent = 'NPS'") or die (mysql_error());

echo '<p class="pflat"><b>National Parks, Monuments, etc.</b> &#8211; ';
{
//<!--BeginDynamicTable-->
while ($row = mysql_fetch_array ($res)) {
$ID = $row['ID'];
$IDArea = $row['IDArea'];

$Names[] = $row['Name'];

}
}
}
echo join( $Names, ' | ' );
echo $Names;
echo '</p>';
?>

And this is what it now displays:

// New_Mexico // New_Mexico // New_Mexico // New_Mexico // New_MexicoBandelier National Monument | Capulin Volcano National Monument | Carlsbad Caverns National Park | El Malpais National Monument | White Sands National MonumentArray

I highlighted all the extra code I don’t want to display. I tried removing it with str_replace, but that doesn’t work.

Thanks.

Okay, lets clean this up a little bit cause it’s doing my head in to try and follow the curly braces…


[COLOR="#FF0000"]$result = mysql_query('select count(*) from gw_protected_areas_usa');
if (($result) && (mysql_result ($result , 0) > 0)) {
} else {
die('Invalid query: ' . mysql_error());
}
{[/COLOR]
  $res = mysql_query ("SELECT GWP.ID, GWP.IDArea, GWP.Name, GWP.URL, GWP.Category, GWP.Type
   FROM gw_protected_areas_usa GWP
   WHERE GWP.IDArea = '$MyID'
   AND GWP.Category != 'Cul' AND GWP.Parent = 'NPS'") or die (mysql_error());

[COLOR="#0000FF"]echo "SELECT GWP.ID, GWP.IDArea, GWP.Name, GWP.URL, GWP.Category, GWP.Type FROM gw_protected_areas_usa GWP WHERE GWP.IDArea = '$MyID' AND GWP.Category != 'Cul' AND GWP.Parent = 'NPS'";
[/COLOR]
echo '<p class="pflat"><b>National Parks, Monuments, etc.</b> &#8211; ';
[COLOR="#FF0000"]{[/COLOR]
//<!--BeginDynamicTable-->
while ($row = mysql_fetch_array ($res)) {
[COLOR="#FF0000"]$ID = $row['ID'];
$IDArea = $row['IDArea'];[/COLOR]

$Names[] = $row['Name'];

}
[COLOR="#FF0000"]}
}[/COLOR]
echo join( $Names, ' | ' );
[COLOR="#FF0000"]echo $Names;[/COLOR]
echo '</p>';
?>

#1: mysql_ is being deprecated; time to switch to mysqli or PDO. But, for the moment i’ll leave that intact.
#2: Anything red, get rid of.
#3: Anything blue, add.

This will spit out your query as-is, so you should be able to see what $MyID is carrying into the query.

Run that query in phpMyAdmin and verify you do NOT see the extra rows.

Wow, that’s awesome! The code you gave me works perfect as is!

Someone told me about PDO in another thread, but I haven’t had time to check it out yet. I have dozens of DB queries that I’m going to have to upgrade first, using the code you gave me as a guide. I never did understand the need for two queries - $result and $res. It’s just something I picked up when I was learning PHP/MySQL. I thought the $result script somehow initiated the second query, or made it run smoother.

I’ll have to check out mysqli, too.

In the meantime, I even figured out how to insert URL’s…


$res = mysql_query ("SELECT GWP.ID, GWP.IDArea, GWP.Name, GWP.URL, GWP.Category, GWP.Type
   FROM gw_protected_areas_usa GWP
   WHERE GWP.IDArea = '$MyID'
   AND GWP.Category != 'Cul' AND GWP.Parent = 'NPS'") or die (mysql_error());

echo '<p class="pflat"><b>National Parks, Monuments, etc.</b> &#8211; ';
{
while ($row = mysql_fetch_array ($res)) {

$URL = $row['URL'];
$URL = '<a href="'.$URL.'" title="">';
$Name = $row['Name'];
$Name = ''.$Name.'</a>';
$Names[] = $URL.$Name;

}
}
echo join( $Names, ' | ' );
echo '</p>';

Thanks again. All the tips on this discussion have given me a HUGE boost. The only drawback is that it’s going to take me a few days to rewire all my queries. :wink: