ShiVer Posted May 24, 2006 Share Posted May 24, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/10374-preg-replace-trouble/ Share on other sites More sharing options...
ShiVer Posted May 25, 2006 Author Share Posted May 25, 2006 Any ideas or suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/10374-preg-replace-trouble/#findComment-38944 Share on other sites More sharing options...
ShiVer Posted May 25, 2006 Author Share Posted May 25, 2006 I solved it:[code]<?phppreg_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] Quote Link to comment https://forums.phpfreaks.com/topic/10374-preg-replace-trouble/#findComment-38958 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.