jarvis Posted August 31, 2009 Share Posted August 31, 2009 Hi, We've a cms which has the following pages: index.php - shows certain article headlines from a db (i.e. last 10 added) article.php shows the actual article by id, i.e. when you click the article title the link is article.php?id=1 this then grabs the info relating to the article with an id of 1 contact.php shows the contact info about.php shows the about info What I need to do is assign a banner ad (image and link) which I add via a CMS script. Question is, how can I assign it to a specific page (article, index, about, contact etc)? Is there a way of given a page a hidden value and when that page is loaded it loads the relevant banner? Hope that makes sense? Many thanks Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted August 31, 2009 Share Posted August 31, 2009 if ($_SERVER['PHP_SELF'] == "/index.php"){ //load the correct banner } Will get kind of clunky with more and more pages, but I would suggest putting the info in a database, or storing the relevant data into an array and getting it that way, like //database way $get = $_SERVER['PHP_SELF']; $sql = "SELECT src FROM bannerTable WHERE page='$get'"; $go = mysql_query($sql); $row = mysql_fetch_array($go); echo "<img src='".$row['src']"' id='banner' />"; Quote Link to comment Share on other sites More sharing options...
jarvis Posted September 3, 2009 Author Share Posted September 3, 2009 This is great thanks! I've got a query though, my table allows you to enter the file name or select it from a db (via a drop down of article titles from the cms) if it's a dynamic page. My DB table for this part therefore looks like: page_id page_name 1 10 2 21 3 index.php 4 3 5 contact_us.php 6 5 7 view_files.php 8 1 If I then use my code in the header file to get the image I want to display: $id = $_GET['aid']; $query = "SELECT page_id FROM pages WHERE page_name='$aid'"; This works to get the image for any page with an id number (dynamic page), however, it errors on those pages without, such as about_us.php, index.php and view_files.php. How can I get around this? Thanks so much! Quote Link to comment Share on other sites More sharing options...
jarvis Posted September 7, 2009 Author Share Posted September 7, 2009 Sorry to bump but any help is much appreciated! Thanks Quote Link to comment Share on other sites More sharing options...
dave_sticky Posted September 7, 2009 Share Posted September 7, 2009 If I've understood what mean then something like this will allow for differentiation between an ID and a filename: <?PHP if (!isset($_GET['aid']) { $get = $_SERVER['PHP_SELF']; // Whatever you want it to do with the file name } else { $aid = $_GET['aid']; // Your SQL } ?> Is that what you were after? Quote Link to comment Share on other sites More sharing options...
bundyxc Posted September 7, 2009 Share Posted September 7, 2009 if (isset($_GET['aid'])) { //If this there's a $_GET['aid'] in the URL, this page is dynamic. Consult database for the banner. $sql = "SELECT `page_id` FROM `pages` WHERE `page_name`='" . $_GET['aid'] . "'"; $query = mysql_query($sql); $fetch = mysql_fetch_assoc($query); $img = $fetch['page_id']; } else { //Otherwise, this page isn't dynamic. if ($_SERVER['PHP_SELF'] == '/about_us.php') { $img = '/images/about_us_banner.bmp'; } elseif ($_SERVER['PHP_SELF'] == '/index.php') { $img = '/images/index_banner.jpg'; } elseif ($_SERVER['PHP_SELF'] == '/view_files.php') { $img = '/images/view_files_banner.png'; } } //The $img variable now holds your banner URL. Quote Link to comment Share on other sites More sharing options...
jarvis Posted September 7, 2009 Author Share Posted September 7, 2009 @dave_sticky- thanks I think this may work - will have a play & see! @bundyxc - Although I like that, the prob is if the number of pages extends, I need to add to the code & is perhaps a little clumsy? Also, the file name part wont work in that way. As when you add a banner to a page via the cms, it creates a dir with the id number of that entry into the pages table and then the filename remains the same. For example, if I select from my list of pages index.php and upload istock123.jpg, it then adds this to the pages table and an images table. A dir is created numbered 1 if it's the first entry and the image is uploaded. I wonder if something like: if (isset($_GET['aid'])) { //If this there's a $_GET['aid'] in the URL, this page is dynamic. Consult database for the banner. $sql = "SELECT `page_id` FROM `pages` WHERE `page_name`='" . $_GET['aid'] . "'"; $query = mysql_query($sql); $fetch = mysql_fetch_assoc($query); $img = $fetch['page_id']; } else { //Otherwise, this page isn't dynamic. $get = $_SERVER['PHP_SELF']; $sql = "SELECT `page_id` FROM `pages` WHERE `page_name`='" . $get = $_SERVER['PHP_SELF'] . "'"; $query = mysql_query($sql); $fetch = mysql_fetch_assoc($query); $img = $fetch['page_id']; } Would that work? Quote Link to comment Share on other sites More sharing options...
dave_sticky Posted September 7, 2009 Share Posted September 7, 2009 $sql = "SELECT `page_id` FROM `pages` WHERE `page_name`='" . $get = $_SERVER['PHP_SELF'] . "'"; I wouldn't do that! Could be a copy & paste error though. You've already declared $get, so just use: $sql = "SELECT `page_id` FROM `pages` WHERE `page_name`='" . $get . "'"; Quote Link to comment Share on other sites More sharing options...
bundyxc Posted September 7, 2009 Share Posted September 7, 2009 Well now you've got me all confused. I thought that $_GET['aid'] was the `page_id`. Is it the `page_name`? Quote Link to comment Share on other sites More sharing options...
jarvis Posted September 7, 2009 Author Share Posted September 7, 2009 That would be perhaps. I guess what I need to do is determine whether the url is an id or page_name, then from there, pass whichever it is into the sql query!? Would this work $sql = "SELECT `page_id` FROM `pages` WHERE `page_name`='"; if (!isset($_GET['aid']) { $get = $_SERVER['PHP_SELF']; } else { $aid = $_GET['aid']; } "'"; Quote Link to comment Share on other sites More sharing options...
bundyxc Posted September 7, 2009 Share Posted September 7, 2009 Once you have the page name/id, what do you do with it? Is there a seperate column on the database for the banner image, or how are you going to go about finding the banner URL? Quote Link to comment Share on other sites More sharing options...
jarvis Posted September 7, 2009 Author Share Posted September 7, 2009 Hi bundyxc, appreciate the time and effort on this one! Basically, all I need to do is grab either the id or page name and pass it to the query, the query will then return the page_id which I tie in with another query (see below), this is used for some code to display the relevant image. When you upload an image to a page, a directory is created and the image uploaded to that dir. So, with the above it would be an image path of something like www.domain.com/banners/1/istock123.jpg Where 1 is the dir created DROP TABLE IF EXISTS `pages`; CREATE TABLE IF NOT EXISTS `pages` ( `page_id` smallint(4) unsigned NOT NULL AUTO_INCREMENT, `page_name` varchar(255) NOT NULL, PRIMARY KEY (`page_id`) ) ENGINE=InnoDB; DROP TABLE IF EXISTS `banner_ads`; CREATE TABLE IF NOT EXISTS `banner_ads` ( `banner_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `page_id` int(10) unsigned NOT NULL DEFAULT '0', `file_name` varchar(255) NOT NULL, `file_size` int(6) unsigned NOT NULL, `file_type` varchar(30) NOT NULL, `image_description` varchar(255) DEFAULT NULL, `web` varchar(150) DEFAULT NULL, `date_entered` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`banner_id`), KEY `file_name` (`file_name`), KEY `date_entered` (`date_entered`) ) ENGINE=InnoDB; I hope that makes sense? Quote Link to comment Share on other sites More sharing options...
bundyxc Posted September 7, 2009 Share Posted September 7, 2009 Of course. I'm actually headed to bed after I post this. It's about 5am, and I need to be up by 8am. haha. I swear, I'm almost too passionate about PHP. Anyway, try this: <? if (isset($_GET['aid'])) { $id = $_GET['aid']; $fetch = mysql_fetch_assoc(mysql_query("SELECT `page_name` FROM `pages` WHERE `page_id`='" . $id . "'")); $name = $fetch['page_name']; } else { $name = $_SERVER['PHP_SELF']; $fetch = mysql_fetch_assoc(mysql_query("SELECT `page_id` FROM `pages` WHERE `page_name`='" . $name . "'")); $id = $fetch['page_id']; } print('Page ID: ' . $id . '<br />Page Name: ' . $name; ?> Quote Link to comment Share on other sites More sharing options...
jarvis Posted September 7, 2009 Author Share Posted September 7, 2009 I think that's it! Thank bundyxc. Just one last thing, how do I just get the file name as that returns the whole path. I've the following but it doesn't work, it just says page name is array rather than the page name! $url = $_SERVER["PHP_SELF"]; $name = Explode('/', $url); $name[count($name) - 1]; #$name = $_SERVER['PHP_SELF']; $query = "SELECT `page_id` FROM `pages` WHERE `page_name`='" . $name . "'"; If i echo $name it shows the correct file name. Am I being stupid? Quote Link to comment Share on other sites More sharing options...
dave_sticky Posted September 7, 2009 Share Posted September 7, 2009 $url = $_SERVER["PHP_SELF"]; $name = Explode('/', $url); $name[count($name) - 1]; #$name = $_SERVER['PHP_SELF']; $query = "SELECT `page_id` FROM `pages` WHERE `page_name`='" . $name . "'"; 2 ways of making this work... 1. Line 3, change to $name = $name[count($name) - 1]; 2. In your SQL Query, change $name to $name[count($name) -1]; Your choice really. Both should work (but not at the same time)! Quote Link to comment Share on other sites More sharing options...
jarvis Posted September 8, 2009 Author Share Posted September 8, 2009 Thanks dave_sticky! I think im now getting somewhere. One odd thing is that although the code works in sql if I run the query direct, it wont run in the script. if (isset($_GET['aid'])) { $id = $_GET['aid']; $query = "SELECT `page_name` FROM `pages` WHERE `page_id`='" . $id . "'"; #echo $query; $result = mysql_query($query); $name = $row['page_name']; while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) { $default_image_sql = 'SELECT image_description, banner_id, page_id, file_name, web FROM banner_ads WHERE page_id = '.$id.''; echo $default_image_sql; $default_image_rs = mysql_query($default_image_sql) or die(mysql_error()); $default_image_row = mysql_fetch_array($default_image_rs); $online_images_total_sql = 'SELECT count(file_name) AS total_images FROM banner_ads WHERE page_id = '.$id.''; echo $online_images_total_sql; $online_images_total_rs = mysql_query($online_images_total_sql) or die(mysql_error()); $total_images_uploaded = mysql_fetch_array($online_images_total_rs); $image_available = $total_images_uploaded['total_images']; echo $image_available; if ($image_available == 0) { }else{ // There is an image so display it $imgfile = 'banners/'.$default_image_row['page_id'].'/'.$default_image_row['file_name']; if(file_exists($imgfile) && $image = @getimagesize($imgfile)) { $t_imgfile='banners/'.$default_image_row['page_id'].'/t_'.$default_image_row['file_name']; $web = $default_image_row['web']; if ($web !=""){ echo '<a href="http://'.$web.'"><img src="'.$t_imgfile.'" alt="'.$default_image_row['image_description'].'" border="0" /></a>'; } else { echo '<img src="'.$t_imgfile.'" alt="'.$default_image_row['image_description'].'" border="0" />'; } } } } } else { $url = $_SERVER["PHP_SELF"]; $name = Explode('/', $url); $name[count($name) - 1]; $name = $name[count($name) - 1]; $query = "SELECT `page_id` FROM `pages` WHERE `page_name` = '" . $name . "'"; ####THIS LINE### echo $query.'<hr/>'; $result = mysql_query($query) or die(mysql_error()); $id = $row['page_id']; while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) { $default_image_sql = 'SELECT image_description, banner_id, page_id, file_name, web FROM banner_ads WHERE page_id = '.$name.'' ; #echo $default_image_sql; $default_image_rs = mysql_query($default_image_sql) or die(mysql_error()); $default_image_row = mysql_fetch_array($default_image_rs); $online_images_total_sql = 'SELECT count(file_name) AS total_images FROM banner_ads WHERE page_id = '.$name.''; #echo $online_images_total_sql; $online_images_total_rs = mysql_query($online_images_total_sql) or die(mysql_error()); $total_images_uploaded = mysql_fetch_array($online_images_total_rs); $image_available = $total_images_uploaded['total_images']; echo $image_available; if ($image_available == 0) { }else{ // There is an image so display it $imgfile = 'banners/'.$default_image_row['page_id'].'/'.$default_image_row['file_name']; if(file_exists($imgfile) && $image = @getimagesize($imgfile)) { $t_imgfile='banners/'.$default_image_row['page_id'].'/t_'.$default_image_row['file_name']; $web = $default_image_row['web']; if ($web !=""){ echo '<a href="http://'.$web.'"><img src="'.$t_imgfile.'" alt="'.$default_image_row['image_description'].'" border="0" /></a>'; } else { echo '<img src="'.$t_imgfile.'" alt="'.$default_image_row['image_description'].'" border="0" />'; } } } } } print 'Page ID: ' . $id . '<br />Page Name: ' . $name; I've marked the line which fails. If I browse to the page, it should return the page_id, browse to the dir with that number and then show the image. The query is fine in mysql but wont work above. have tried LIKE %% but that too fails. Why is that? Am at a loss! Thanks Quote Link to comment Share on other sites More sharing options...
dave_sticky Posted September 8, 2009 Share Posted September 8, 2009 Hmmm, Interesting... See Comments below... $url = $_SERVER["PHP_SELF"]; $name = Explode('/', $url); $name[count($name) - 1]; // Delete this line... Doesn't need to be there, and don't know if it will be causing problems $name = $name[count($name) - 1]; Other than that, when you say it doesn't work, what do you mean? Do you get an error message from the or die(mysql_error()); bit? Quote Link to comment 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.