Jump to content

Archived

This topic is now archived and is closed to further replies.

ShiVer

preg replace trouble

Recommended Posts

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: [b][ad-pos:left-col1][/b] :so it looks like this:

[code]<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>[/code]

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

Share this post


Link to post
Share on other sites
I solved it:

[code]<?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);
        }
    }
}
?>[/code]

Share this post


Link to post
Share on other sites

×

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.