Jump to content


Photo

preg replace trouble


  • Please log in to reply
2 replies to this topic

#1 ShiVer

ShiVer
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationJoMo

Posted 24 May 2006 - 05:49 PM

Alright, I'm going to try to make this as least confussing as possible but it prolly won't work, so bare with.

My company has asked me to create an ad system script for a few websites that we own. It shouldn't be too difficult, I'm just having some troubles with it.

Right now I have a mysql table set up with the ad information such as it's id, where the image is located, the url the image is suppose to link to, and the position on the page where it's to be set.

I have it set up so there are tokens in an html template in this format: [ad-pos:left-col1] :so it looks like this:

<table border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td>
    [ad-pos:left-col1]<br />
    [ad-pos:left-col2]<br />
    [ad-pos:left-col3]<br />
    </td>
  </tr>
</table>

left-col1, left-col2, and left-col3 of course being actual positions themselves. So in the database the position field for an ad in [ad-pos:left-col1] token would read left-col1 and so on.

What I want it to do is this:
If there is no ad in the database with that posistion it simply won't show anything in replace of the token. But if there is one ad or more it will replace the token with one of ads randomly.

I thought it would be a simple proceedure but i can't get it work. I know there's is probably nothing to it and I'll hate myself when someone shows me like four lines of code that do it, lol.

Like I said I probably confussed the crap out of you, so if anyone has any questions feel free to ask, Thanks in advanced!

- Ryan


#2 ShiVer

ShiVer
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationJoMo

Posted 25 May 2006 - 03:48 PM

Any ideas or suggestions?

#3 ShiVer

ShiVer
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationJoMo

Posted 25 May 2006 - 05:26 PM

I solved it:

<?php
preg_match_all("/\[ad-pos:(.*?)\]/si", $leftcol, $token);
foreach ($token[1] as $pos) {
    $select = mysql_query("select * from adsys_ads where position = '$pos' order by RAND() limit 1");
    if (mysql_num_rows($select) <= 0) {
        $leftcol = preg_replace("/\[ad-pos:$pos\]/si", "", $leftcol);
    } else {
        while ($ads = mysql_fetch_array($select)) {
            $leftcol = preg_replace("/\[ad-pos:$pos\]/si", $ads['img_url'], $leftcol);
        }
    }
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users