Jump to content

preg replace trouble


ShiVer

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
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.