How to display a banner message using PHP and SQL

Home > Search > How-to
  by

Let's say you want to display a banner on your website when a certain event occurs. For example, if there is some outage, you want to present a message to your visitors letting them know an outage is occurring.

Create a table in SQL with the following columns:

  • banner
  • message1
  • message2
  • message3

Add this PHP to the Web page where you want the banner to display.

When you want to display the banner, set banner to "true" in SQL, and type the message you want displayed. When you no longer need the banner to appear, set banner to "false" in SQL.

<?php 
$serverName = "example";
$connectionInfo = array( "Database"=>"example", "UID"=>"example", "PWD"=>"example");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$banner = "select * from banner";
$banner_query = sqlsrv_query( $conn, $banner );
while( $row = sqlsrv_fetch_array( $banner_query, SQLSRV_FETCH_ASSOC)) {		
	$banner = $row['banner'];
}

if ($banner == "true") {
	$banner = "select * from banner";
	$banner_query = sqlsrv_query( $conn, $banner );
	while( $row = sqlsrv_fetch_array( $banner_query, SQLSRV_FETCH_ASSOC)) {
		echo "<div style='border: 1px solid black; padding-top: 15px, padding-bottom: 15px; text-align: center; font-size: 14pt; color: white; background-color: black;'>";
		echo "<font color='red'>"; echo $row['message1']; echo "</font><br />";
		echo $row['message2']; echo "<br />";
		echo $row['message3'];	
		echo "</div>";
	}
}	
?>

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments