Jump to content

[SOLVED] Displaying the text that is after the last delimiter in a field


jaxdevil

Recommended Posts

I have a field called `cats` and in it it has all of the categories and subcategories, seperated by the delimiter '|' and they are in hierarchical order, the main category is first, then the subcategory, then the sub-subcategory, etc. I need to display the final subcategory in a row display, I can break up the data with explode but I do not know how to select the last text, the final subcategory after the last |. Here is an example of how the categories field  (called `cats` in the database) looks for one example. The problem is some of the fields have a category, and a subcategory, and some have category, subcategory, and sub-subcategory, etc. So I can't just display say the 4th  array, I have to make it display what is after the last delimiter. here is an exmaple of the categories field:

 

consumer electronics|portable audio|mp3 and mp4 players|ipod

 

And here is the entire code I have now:

 

<?php
mysql_connect('localhost','xxx','xxx');
mysql_select_db('xxx') or die(mysql_error());
?>
<&#157;xml version="1.0" encoding="UTF-8"&#157;>
<rss version ="2.0" xmlns:g="http://base.google.com/ns/1.0">
<channel>
<title>GS Wholesale</title>
<description>Your Consumer Electronics and More Store!</description>
<link>http://GSWholesale.com</link>
<?php
$sql = "SELECT * FROM products WHERE `active`='Yes' ORDER BY `cats` LIMIT 5" or die ( "Query failed due to: ".mysql_error());
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
    $cats = explode("|",$row['cats']);
?>
<item><title><?=$row['man']?> <?=$row['mod']?> <?=$row['name']?></title>

<g:brand><?=$row['mod']?></g:brand>
<g:condition>new</g:condition>
<g:description><?=$row['description']?> <?=$row['details']?></g:description>
<g:model_number><?=$row['mod']?></g:model_number>
<g:id><?=$row['mod']?></g:id>
<g:image_link><?=$row['img_url']?></g:image_link>
<g:pickup>true</g:pickup>
<g:link>http://GSWholesale.com/product.php?id=<?=$row['mod']?></g:link>
<g:mpn><?=$row['mod']?></g:mpn>
<g:price><?=$row['price']?></g:price>
<g:product_type><?=$cats?></g:product_type>
<g:upc><?=$row['upc']?></g:upc>
<g:quantity>1000</g:quantity>
<g:payment_accepted>Google Checkout</g:payment_accepted>
<g:payment_accepted>PayPal</g:payment_accepted>
<g:payment_accepted>Visa</g:payment_accepted>
<g:payment_accepted>Mastercard</g:payment_accepted>
<g:payment_accepted>American Express</g:payment_accepted>
<g:payment_accepted>Discover</g:payment_accepted>
<g:payment_accepted>Cash</g:payment_accepted>
<g:payment_accepted>Checks</g:payment_accepted>
<g:payment_accepted>Cashiers Checks</g:payment_accepted>
<g:payment_accepted>Money Orders</g:payment_accepted>
<g:payment_accepted>Wire Transfer</g:payment_accepted>
               </item>
</channel></rss>
<?
}
?>

 

Any help is greatly appreciated!

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.