Show SOME text from a database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jannik
Forum Newbie
Posts: 4
Joined: Wed Dec 08, 2010 2:47 pm

Show SOME text from a database

Post by jannik »

Hi
when i write

Code: Select all

$data['download_cat_name'];
it says:
Cat - Subcat
I only want it to show
Subcat
How do I do this codewise? So it ONLY shows what is in the database field AFTER the
-
sign.

Really hope that someone understands my bad english, and can help me.

Thanx in advance.

Jannik
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Show SOME text from a database

Post by McInfo »

There are many was to do that. Which way is best depends on the expected inputs and outputs.

Here is one example.

Code: Select all

<?php
list ($cat, $sub) = explode(' - ', 'Cat - Subcat', 2);

var_dump($cat, $sub);
/*
string(3) "Cat"
string(6) "Subcat"
*/
jannik
Forum Newbie
Posts: 4
Joined: Wed Dec 08, 2010 2:47 pm

Re: Show SOME text from a database

Post by jannik »

Hmm dont think u understand what I need.... (my bad)

Perhaps this can help. Just found it by searching google...

opentable($locale['400'].": ".$cdata['download_cat_name'] = substring(download_cat_name, instr(download_cat_name, '-') + 1));

But it doesnt seem to work...
Dont know if I can add the "Substring" inside the Opentable function...

Can you see what i am doing wrong?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Show SOME text from a database

Post by McInfo »

You say you have a variable, $data['download_cat_name'], that holds a string. The string is something like "Fruit - Apple" where a category and subcategory are joined by the string " - ".

So,

Code: Select all

list ( , $data['download_cat_name']) = explode(' - ', $data['download_cat_name'], 2);
Does $data['download_cat_name'] not now hold just the subcategory?
jannik
Forum Newbie
Posts: 4
Joined: Wed Dec 08, 2010 2:47 pm

Re: Show SOME text from a database

Post by jannik »

My code now looks like this

Code: Select all

echo "<a href='downloads.php?cat_id=".$data['download_cat_id']."'>".$strip = strstr($data['download_cat_name'], '-')."</a>";
It works but it still shows the "- " in front. do you know hos to remove those two in this code???

If not I will then try your code tomorrow, will go to sleep now

Thanx in advance. :-)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Show SOME text from a database

Post by McInfo »

Use ltrim() or trim().
jannik
Forum Newbie
Posts: 4
Joined: Wed Dec 08, 2010 2:47 pm

Re: Show SOME text from a database

Post by jannik »

Hi. Could someone please help me with the code

I simply can't add the ltrim or trim function.... Pulling my hair off :evil:
Could someone please add the trim code to the above code of mine, so that "- " is being removed from the beginning.

Regards

Jannik
Post Reply