Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder harlequin2k5's Avatar
    Join Date
    Sep 2005
    Location
    Holiday, FL
    Posts
    635
    Thanks
    18
    Thanked 0 Times in 0 Posts

    str_replace not working properly

    what I'm trying to do is have "keywords" within content changed from text to links

    right now only one post is displaying and no keywords have been changed

    PHP Code:
    <?php
            error_reporting
    (E_ALL);

            require_once(
    "functionlist.php");

            
    $conn=doconnect();
            
    $query "SELECT * from keywords,affiliates where keywords.keyword=affiliates.keyword";
            
    $result mysql_query($query) or die("Error: " mysql_error());
            
    $row=mysql_fetch_array($result);
            
    $keyword = array();


            
    $i 0//i is going to be the index of our row
            
    while ($row=mysql_fetch_array($result))
            {
                
    $keyword[$i][0]=$row['keyword'];
                
    $keyword[$i][1]=$row['link'];
                
    $i++; //go to the next row
            
    }
            
    $keywordCount $i;

            
    $query "SELECT * from posts order by id";
            
    $result mysql_query($query) or die("Error: " mysql_error());
            
    $row=mysql_fetch_array($result);
            echo 
    "<div style=\"padding:10px;\">";
            while (
    $row mysql_fetch_array($result))
            {
            echo 
    "<h1>";
            echo 
    $row['title'];
            echo 
    "</h1><p>";

            
    //Replace content keywords with random affiliate links
            
    $string=$row['content'];
            
            for(
    $r 0$r $keywordCount$r++)
                
    $string=str_replace("$keyword[$r][0]","<a href=\"$keyword[$r][1]\" class=\"affiliatelink\">$keyword[$r][0]</a>",$string);
            echo 
    $string;
            echo 
    "</p>";
            }    
            echo 
    "</div>";
            
    ?>
    normally I would say that I'm sure I'm missing something stupid but this is my first time with an array so I'm utterly clueless

    any help is greatly appreciated!

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    PHP Code:
    $string=str_replace("$keyword[$r][0]","<a href=\"$keyword[$r][1]\" class=\"affiliatelink\">$keyword[$r][0]</a>",$string); 
    Those array variables won't be parsed properly, you must enclose them in {}'s or (better yet) don't put them inside quotes at all.
    PHP Code:
    $newString "<a href=\"".$keyword[$r][1]."\" class=\"affiliatelink\">".$keyword[$r][0]."</a>";
    $string str_replace($keyword[$r][0], $newString$string); 

  • Users who have thanked Fumigator for this post:

    harlequin2k5 (08-25-2007)

  • #3
    Regular Coder harlequin2k5's Avatar
    Join Date
    Sep 2005
    Location
    Holiday, FL
    Posts
    635
    Thanks
    18
    Thanked 0 Times in 0 Posts
    thank you fumi - that worked quite well...I thought it might have something to do with the quotes but like I said I don't know anything about arrays - between you and oracleguy I now have a much better understanding of how they work

    with that said...would you be able to tell me why only 1 record from "posts" is displaying?

  • #4
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    You're on the right track; just got to keep bashing ur head on array's till it sinks in.

    Try this version which uses a multidim array instead (untested):-

    PHP Code:

    error_reporting
    (E_ALL);

    require_once(
    "functionlist.php");

    $conn=doconnect();
    $query "SELECT * from keywords,affiliates where keywords.keyword=affiliates.keyword";
    $result mysql_query($query) or die("Error: " mysql_error());
    $row=mysql_fetch_array($result);
    $keyword = array();

    while (
    $row=mysql_fetch_array($result))
        {
            
    $keyword[] = array($row['keyword'],$row['link']);
        }
    $keywordCount count($keyword);

    $query "SELECT * from posts order by id";
    $result mysql_query($query) or die("Error: " mysql_error());
    $row=mysql_fetch_array($result);
    echo 
    "<div style=\"padding:10px;\">";
    while (
    $row mysql_fetch_array($result))
        {
            echo 
    "<h1>";
            echo 
    $row['title'];
            echo 
    "</h1><p>";
            
            
    //Replace content keywords with random affiliate links
            
    $string=$row['content'];
            foreach(
    $keyword as $k => $v)
                {
                               
    $string str_replace$v[0],'<a href="' $keyword[$v][1] .'" class="affiliatelink">' $keyword[$v][0] . '</a>'$string );
                     }
                    echo 
    $string;
            echo 
    "</p>";
        }    
    echo 
    "</div>"
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • Users who have thanked meth for this post:

    harlequin2k5 (08-25-2007)

  • #5
    Regular Coder harlequin2k5's Avatar
    Join Date
    Sep 2005
    Location
    Holiday, FL
    Posts
    635
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by meth View Post
    You're on the right track; just got to keep bashing ur head on array's till it sinks in.
    yep there's a hole in the wall *chuckles*

    you're suggestion did not work and believe it or not I was almost close to what you provided when I had started on this - and I believe what you've provided will make it easier for me to figure out how to randomize the affiliate link unfortunately I'm not yet familiar with all of these new errors I've been exposed to

    the error generated is:
    Code:
    Warning: Illegal offset type
    fumi and meth have been such great help; as with the many times that aero has helped me I feel like I'm actually learning rather than have a piece of code thrown at me - thanks to you both!

  • #6
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Gah, spotted what I'd say is the error:

    PHP Code:

    foreach($keyword as $k => $v)
                {
                      
    $string str_replace$v[0],'<a href="' $keyword[$v][1] .'" class="affiliatelink">' $keyword[$v][0] . '</a>'$string );
                } 
    Should be:

    PHP Code:
    foreach($keyword as $k => $v)
                {
                      
    $string str_replace$v[0],'<a href="' . [$v][1] .'" class="affiliatelink">' . [$v][0] . '</a>'$string );
                } 
    Sh!t happens when you cut corners with cntrl c, cntrl v
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #7
    Regular Coder harlequin2k5's Avatar
    Join Date
    Sep 2005
    Location
    Holiday, FL
    Posts
    635
    Thanks
    18
    Thanked 0 Times in 0 Posts
    I'm starting to think I'm not the only one that needs some coffee lol

    now I get an unexpected [ error

    edit: and if I can ask a stupid question? where does $v get declared?
    Last edited by harlequin2k5; 08-25-2007 at 04:40 PM.

  • #8
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    To randomize the link, you can use a php array function to select randomly from the $keyword array instead of a foreach loop.

    array_rand() will select a random key of the array.

    If you dont want to have the same link appearing more than once, you can delete the key from the array straignt after using it: unset($keyword[$v]);

    For eg:

    PHP Code:
    echo "<div style=\"padding:10px;\">";
    while (
    $row mysql_fetch_array($result))
        {
            echo 
    "<h1>";
            echo 
    $row['title'];
            echo 
    "</h1><p>";
            
            
    //Replace content keywords with random affiliate links
            
    $string=$row['content'];
            if( 
    is_array($keyword) && !empty($keyword) ) {
                
    $key array_rand($keyword);
                
    $string str_replace$keyword[$key][0],'<a href="' $keyword[$key][1] .'" class="affiliatelink">' $keyword[$key][0] . '</a>'$string );
                
    //optional unset the link for unique links only
                
    unset($keyword[$key]);
                
    //end option
            
    }
            
    //unset
            
    echo $string;
            echo 
    "</p>";
        }    
        
    echo 
    "</div>"
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #9
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    $v is declared in the foreach loop condition ($keyword as $k => $v). Whats the error?
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #10
    Regular Coder harlequin2k5's Avatar
    Join Date
    Sep 2005
    Location
    Holiday, FL
    Posts
    635
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Parse error: syntax error, unexpected '['

  • #11
    Regular Coder harlequin2k5's Avatar
    Join Date
    Sep 2005
    Location
    Holiday, FL
    Posts
    635
    Thanks
    18
    Thanked 0 Times in 0 Posts
    ok I think I'm over this...I've gotten lost again and now nothing is displaying
    edit: I must have copied something over wrong - it's working

    and of course I've left my book at work lol

    thanks for your help meth - I was so close but my brain has fogged over again

    edit: but it's still displaying only 1 record from posts - there are actually 2 in the table
    Last edited by harlequin2k5; 08-25-2007 at 04:55 PM.

  • #12
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Use a do while instead of a while for your resultset then. Check out the commented out code for an eg of it working:

    PHP Code:
    /* 
    //here's a dummy run with some array data instead of sql results
    //comment out your code and uncomment this code to see it work.
    $keyword[] = array('quick','www.quick.com');
    $keyword[] = array('brown','www.brown.com');
    $keyword[] = array('fox','www.fox.com');
    $keyword[] = array('jumped','www.jumped.com');
    $keyword[] = array('over','www.over.com');
    $keyword[] = array('lazy','www.lazy.com');
    $keyword[] = array('dog','www.dog.com');

    $string[] = 'The quick brown fox is on holidays. Please leave a message.';
    $string[] = 'The lazy dog could not be bothered; the fox can go jump.';
    $string[] = 'The quick fox went splat and the dog got over being lazy.';
    $string[] = 'The fox aint so quick any more and the dog married a fat brown cat.';

    echo '<div>';

    foreach($string as $k => $v)
        {
            echo "<h1>";
            echo 'Eg Title';
            echo "</h1><p>";
            
            //Replace content keywords with random affiliate links
            $string = $v;
            foreach( $keyword as $k2 => $v2 ) {
                $string = str_replace( $v2[0],'<a href="' . $v2[1] .'" class="affiliatelink">' . $v2[0] . '</a>', $string );
            }
            //unset
            echo $string;
            echo "</p>";
        }    
        
    echo "</div>";

    */

    error_reporting(E_ALL);

    require_once(
    "functionlist.php");

    $query "SELECT * from keywords,affiliates where keywords.keyword=affiliates.keyword";
    $result mysql_query($query) or die("Error: " mysql_error());
    $row mysql_fetch_array($result);
    $keyword = array();

    while (
    $row=mysql_fetch_array($result))
        {
            
    $keyword[] = array($row['keyword'],$row['link']);
        }
        
    $keywordCount count($keyword);

    $query "SELECT * from posts order by id";
    $result mysql_query($query) or die("Error: " mysql_error());
    $row=mysql_fetch_array($result);

    echo 
    "<div style=\"padding:10px;\">";
    do {
        echo 
    "<h1>";
        echo 
    $row['title'];
        echo 
    "</h1><p>";
        
        
    //Replace content keywords with random affiliate links
        
    $string=$row['content'];
        
        foreach(
    $keyword as $k => $v)
            {
                
    $string str_replace$v[0],'<a href="' $v[1] .'" class="affiliatelink">' $v[0] . '</a>'$string );
            }
            
        echo 
    $string;
        echo 
    "</p>";
        
    } while ( 
    $row mysql_fetch_array($result) );
           
    echo 
    "</div>"
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #13
    Regular Coder harlequin2k5's Avatar
    Join Date
    Sep 2005
    Location
    Holiday, FL
    Posts
    635
    Thanks
    18
    Thanked 0 Times in 0 Posts
    omg - I haven't tried it yet but that thought had entered my mind as I was trying to figure out how to correct the while statement - I knew that's where the hang up was!

    so simple - I will edit this post after I've tried it

  • #14
    Regular Coder harlequin2k5's Avatar
    Join Date
    Sep 2005
    Location
    Holiday, FL
    Posts
    635
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Thumbs up

    meth!!

    I still have quite a bit of studying to do with the solution you've provided but after stepping away from the computer for a few hours and not thinking about it, I was finally able to mostly understand your quick fox example and then I copied in your code...it works beautifully!

    thank you for your patience and your help!!!

    edit: I meant to show you the link... http://shiznit.no-ip.info/coasttocoast - there's still alot more work to be done with the content for this site but at least we're off to a great start!
    Last edited by harlequin2k5; 08-26-2007 at 02:47 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •