Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-19-2012, 11:17 PM   PM User | #1
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
Exclamation trying to search a string for certain strings and replace them with a new string

Hi All,

Ok i am doing some work for my local football team, and i have a db table with players names and another with fixture details.

Now in the fixture details i have a field for match report which is a long text field type.

What i am trying to do is create a function which replaces all the instances of each player name found in the report with the players name but hyper linked to his profile page.

so for example if my name was in the report the function would produce the following
Luke Jackson => <a href='staff_profile.php?id=10929'>Luke Jackson</a>

So i tried this
PHP Code:
function FindNames($report)
{
    
//GET LIST OF NAMES TO SEARCH FOR
    
$sql mysql_query("SELECT staffID, staffName FROM tbl_staff");
    while(
$row=mysql_fetch_array($sql))
    {
        
$id $row['staffID'];
        
$name $row['staffName'];
        
$addLink str_replace($name"<a href='staff_profile.php?id=$id'>$name</a>"$report);    
    }
    return 
$report;

and tried echoing the match report out like so
PHP Code:
<?php echo FindNames($report)?>
but it doesnt replace any of the names found in the report, nothing changes any ideas what i doing wrong?

many thanks
Luke
__________________
Kernow Connect: Online Shopping, Price Comparison, Maximum Savings On Top UK Stores
Follow Us On: Twitter | Facebook

Last edited by LJackson; 08-19-2012 at 11:32 PM..
LJackson is offline   Reply With Quote
Old 08-19-2012, 11:51 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
As you are specifically treating the returned data as an associative array then you should use mysql_fetch_assoc. Or, if you insist,

PHP Code:
while ($row=mysql_fetch_array($sqlMYSQL_ASSOC)) 
The replacements are performed left-to-right. So replacing $name with more text that also includes $name may be causing an issue as well.

[If this is an issue then one way around this that occurs to me is to replace $name with XXXXXX, then, immediately after, to replace XXXXXX with $name.]

BTW mysqli is recommended and use of mysql is discouraged. But you are by no means alone in side-stepping this advice?!
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 08-19-2012, 11:55 PM   PM User | #3
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Also, if

PHP Code:
<?php echo FindNames($report)?>
echo's $report in full, but completely unaltered, then this suggests to me that the space between the first and last-name is an issue (as it doesn't find the name(s) at all).
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 08-20-2012, 12:00 AM   PM User | #4
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
thanks mate, i also just noticed that im returning

return $report;

but the new text with all the replacements is called

$addLink = str_replace($name, "<a href='staff_profile.php?id=$id'>$name</a>", $report);

so i renamed $addLink to $report and it works a treat thanks for your help
__________________
Kernow Connect: Online Shopping, Price Comparison, Maximum Savings On Top UK Stores
Follow Us On: Twitter | Facebook
LJackson is offline   Reply With Quote
Old 08-20-2012, 12:15 AM   PM User | #5
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
@LJackson
That was the first thing I noticed (he, he) but I made the incorrect assumption that it was making the replacements to $replace, so assumed that this wasn't the issue. Anyway..

BTW I still think using mysql_fetch_assoc is more sensible
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:00 PM.


Advertisement
Log in to turn off these ads.