Skip to content
Index Help & Support Faucet Owners Programming

Random Banner Code ( PHP + MYSQL )

Posted by freek.in 1 year ago
1 1148
Random Banner Code ( PHP + MYSQL )
Posted 1 year ago, last edited 1 year ago
STEP 1 First make a table in your database by running this code. CREATE TABLE `banners` (   `id` int(50) NOT NULL AUTO_INCREMENT,   `code` text NOT NULL,   `status` int(5) NOT NULL,   `size` varchar(150) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; STEP 2 Now create form in ur admin panel , or where ever u want to use. Make styling as per your requirement it is just a form code. <!-- FORM TO ADD BANNER --> <form action="" method="post"> <input type="text" name="adsize" placeholder="Size of the Banner" required /><br /> <textarea name="adcode" placeholder="Advertisement Code" required></textarea><br /> <input type="radio" name="status" value="1"/>Enable | <input type="radio" name="status" value="2"/>Disable <br /> <input type="submit" value="Submit" name="bannerform" /> </form> <!-- FORM TO ADD BANNER --> STEP 3 Add this code in same page with php tag to submit the form value to get register in database. <?php #ADD BANNER TO DATABASE# if(isset($_POST)){ $advertsize = trim($_POST); $advertcode = trim($_POST); $status = trim($_POST); if(empty($_POST) OR empty($_POST)) { echo 'Sorry required field left empty'; } else { $advertcode = base64_encode($advertcode); $sql = "INSERT into `banners` (code,status,size) VALUES ('$advertcode','$status','$advertsize')"; $result = $dbcon -> query($sql); if(!empty($result)) { echo 'Advertisement successfully registerd'; } else { echo 'Failed to register'; } } } #ADD BANNER TO DATABASE# ?> STEP 4 make a php page which u can add to the page where u want to show random rotator banner. Or add this code in your previous function.php  #FUNCTION FOR BANNER ROTATOR# function bannerRot($size){ global $dbcon; $sql = "SELECT `code` FROM `banners` WHERE `size` = '$size' AND `status` = 1 ORDER BY RAND() LIMIT 1"; $result = $dbcon -> query($sql); if(mysqli_num_rows($result) > 0){ $row = $result -> fetch_assoc(); return base64_decode($row); } } #FUNCTION FOR BANNER ROTATOR# INSTRUCTIONS TO FOLLOW IS OVER You change " $dbcon " with your existing connection variable. or if you are not using database then make a connection by using this code  #CONNECTION WITH MYSQL# $dbcon = new mysqli('localhost','root','','test_rotator'); #CONNECTION WITH MYSQL# change details as per your server specs.  localhost -> almost every server is having this value as it is or may be 127.0.0.1 root -> will change to your database username blank after root will be filled with your database password test_rotator -> will change with database name
RE: Random Banner Code ( PHP + MYSQL )
Posted 1 year ago, last edited 1 year ago
Like to add one important thing.  You can use this <?php echo bannerRot('468x60'); ?> anywhere u want to display 468x60 size banner For example if ur size is 300x250 , so while registering banner give size name 300x250 and then use like this  <?php echo bannerRot('300x250'); ?> where u want to display 300x250 size banner.  More banners with same size will rotate automatic