Solved

How to display dynamic data based on MySQL entries?

Posted on 2009-07-13
6
253 Views
Last Modified: 2013-12-13
Hi all,
I am attempting to create an alert display monitor. The admin interface allows me to add the following fields to mysql:
 id  date  time  location  description  severity  publish

Publish is meant as an on/off switch to turn the display on or off.
Here is a dump of mysql table
----------------------
--
-- Table structure for table `outages`
--

CREATE TABLE IF NOT EXISTS `outages` (
  `id` int(11) NOT NULL auto_increment,
  `date` date NOT NULL,
  `time` time NOT NULL,
  `location` varchar(50) NOT NULL,
  `description` text NOT NULL,
  `severity` varchar(50) NOT NULL,
  `publish` varchar(5) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

--
-- Dumping data for table `outages`
--

INSERT INTO `outages` (`id`, `date`, `time`, `location`, `description`, `severity`, `publish`) VALUES
(1, '2009-07-10', '09:41:00', 'Charlotte', 'test 1', 'Low', 'N'),
(2, '2009-07-13', '08:00:59', 'Jacksonville', 'Test Red', 'Urgent', 'N'),
(3, '2009-07-13', '08:01:16', 'Jacksonville', 'Test Orange', 'High', 'Y'),
(4, '2009-07-13', '08:01:33', 'Jacksonville', 'Test Yellow', 'Medium', 'Y'),
(5, '2009-07-13', '08:01:47', 'Jacksonville', 'Test Blue', 'Low', 'Y'),
(6, '2009-07-13', '08:02:00', 'Jacksonville', 'Test Green', 'No Issues', 'Y');
----------------------
and in the code I have put my test query page.
What I want, is to do a loop through the records and print the last active alert. What is the best way to accomplish this? The way the code is right now, all it echos is the first record, even if I change the publish parameter.

Thanks for the help
<?php require_once('Connections/conUDS.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 

{

  if (PHP_VERSION < 6) {

    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  }
 

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;    

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}
 

mysql_select_db($database_conUDS, $conUDS);

$query_rsBulletins = "SELECT * FROM outages";

$rsBulletins = mysql_query($query_rsBulletins, $conUDS) or die(mysql_error());

$row_rsBulletins = mysql_fetch_assoc($rsBulletins);

$totalRows_rsBulletins = mysql_num_rows($rsBulletins);

?>

<style type="text/css">

<!--

#urgent {

	font-weight: bold;

	color: #C00;

}

#high {

	font-weight: bold;

	color: #F60;

}

#medium {

	font-weight: bold;

	color: #FF0;

}

#low {

	font-weight: bold;

	color: #0CF;

}

#allgreen {

	font-weight: bold;

	color: #093;

}

-->

</style>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Testing Alert Query</title>

<link href="css/helpdesk.css" rel="stylesheet" type="text/css" />

</head>
 

<body>
 

<p>This page is for testing the alert query. </p>
 
 

<code>

	<?php

        if ($row_rsBulletins['publish'] = "Y") {

            if ($row_rsBulletins['severity'] = "Urgent") {

                echo "<div id=\"urgent\">I am a red alert!</div>";

            }

            else if ($row_rsBulletins['severity'] = "High") {

                echo "<div id=\"high\">I am a orange alert!</div>";

            }

            else if ($row_rsBulletins['severity'] = "Medium") {

                echo "<div id=\"medium\">I am a yellow alert!</div>";

            }

            else if ($row_rsBulletins['severity'] = "Low") {

                echo "<div id=\"low\">I am a blue alert!</div>";

            }

        } else if ($row_rsBulletins['publish'] = "N") {

            echo "<div id=\"allgreen\">There are no reported outages.</div>";

        }

    ?>

</code>
 
 
 

</body>

</html>

Open in new window

0
Comment
Question by:udsfsg
  • 4
  • 2
6 Comments
 
LVL 14

Expert Comment

by:flob9
ID: 24840562
Try this.
<?php require_once('Connections/conUDS.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 

{

  if (PHP_VERSION < 6) {

    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  }

 

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

 

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;    

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

 
 

?>

<style type="text/css">

<!--

#urgent {

        font-weight: bold;

        color: #C00;

}

#high {

        font-weight: bold;

        color: #F60;

}

#medium {

        font-weight: bold;

        color: #FF0;

}

#low {

        font-weight: bold;

        color: #0CF;

}

#allgreen {

        font-weight: bold;

        color: #093;

}

-->

</style>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Testing Alert Query</title>

<link href="css/helpdesk.css" rel="stylesheet" type="text/css" />

</head>

 

<body>

 

<p>This page is for testing the alert query. </p>

 

 

<code>

        <?php
 

mysql_select_db($database_conUDS, $conUDS);

$query_rsBulletins = "SELECT * FROM outages WHERE publish = 'Y' ORDER BY date DESC LIMIT 10"; /* 10 last published, for example */ 

$rsBulletins = mysql_query($query_rsBulletins, $conUDS) or die(mysql_error());
 

$totalRows_rsBulletins = mysql_num_rows($rsBulletins);
 

while($row_rsBulletins = mysql_fetch_assoc($rsBulletins))

{

        

        if ($row_rsBulletins['publish'] = "Y") {

            if ($row_rsBulletins['severity'] = "Urgent") {

                echo "<div id=\"urgent\">I am a red alert!</div>";

            }

            else if ($row_rsBulletins['severity'] = "High") {

                echo "<div id=\"high\">I am a orange alert!</div>";

            }

            else if ($row_rsBulletins['severity'] = "Medium") {

                echo "<div id=\"medium\">I am a yellow alert!</div>";

            }

            else if ($row_rsBulletins['severity'] = "Low") {

                echo "<div id=\"low\">I am a blue alert!</div>";

            }

        } else if ($row_rsBulletins['publish'] = "N") {

            echo "<div id=\"allgreen\">There are no reported outages.</div>";

        }
 

}

    ?>

</code>

 

 

 

</body>

</html>

Open in new window

0
 
LVL 2

Author Comment

by:udsfsg
ID: 24841420
ok, that displays the 4 records of 6 in the database maked as publish="Y", but it doesn't differential between the different typs of alerts. (Urgent, High, Medium, Low)
Here is a picture of the output.

repeats4times.gif
0
 
LVL 2

Author Comment

by:udsfsg
ID: 24841835
If I change the code like this, I get al of them to post.
<?php require_once('Connections/conUDS.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 

{

  if (PHP_VERSION < 6) {

    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  }

 

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

 

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;    

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

 

 

?>
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Testing Alert Query</title>

<link href="css/helpdesk.css" rel="stylesheet" type="text/css" />

<style type="text/css">

<!--

#urgent {

        font-weight: bold;

        color: #C00;

}

#high {

        font-weight: bold;

        color: #F60;

}

#medium {

        font-weight: bold;

        color: #FF0;

}

#low {

        font-weight: bold;

        color: #0CF;

}

#allgreen {

        font-weight: bold;

        color: #093;

}

-->

</style>

</head>

 

<body>

 

<h1>This page is for testing the alert query. </h1>

 

 

<code>

<?php

mysql_select_db($database_conUDS, $conUDS);

$query_rsBulletins = "SELECT * FROM outages ORDER BY date DESC LIMIT 10"; /* 10 last published, for example */ 

$rsBulletins = mysql_query($query_rsBulletins, $conUDS) or die(mysql_error());

 

$totalRows_rsBulletins = mysql_num_rows($rsBulletins);

 

while($row_rsBulletins = mysql_fetch_assoc($rsBulletins))

{

	if ($row_rsBulletins['publish'] == "Y") {

		if ($row_rsBulletins['severity'] == "Urgent") {

        	echo "<div id=\"urgent\">I am a red alert!</div>";

		}

    	else if ($row_rsBulletins['severity'] == "High") {

        	echo "<div id=\"high\">I am a orange alert!</div>";

    	}

    	else if ($row_rsBulletins['severity'] == "Medium") {

       		echo "<div id=\"medium\">I am a yellow alert!</div>";

     	}

   		else if ($row_rsBulletins['severity'] == "Low") {

     		echo "<div id=\"low\">I am a blue alert!</div>";

		}

		else {

			echo "<div id=\"allgreen\">There are no reported outages.</div>";

		}

	} else {

		echo "<div id=\"allgreen\">There are no published alerts.</div>";

	}

}

?>

</code>

 

 

 

</body>

</html>

Open in new window

0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 2

Author Comment

by:udsfsg
ID: 24841873
All colors outputted.
allcolors.gif
0
 
LVL 14

Accepted Solution

by:
flob9 earned 500 total points
ID: 24844799
Yes, didn't check the wrong single "=" compare statements, sorry.

the line "if ($row_rsBulletins['publish'] == "Y")" is also useless, since this is already filtered by the SQL query.

To have the "There are no published alerts." you need to do something like this :

$query_rsBulletins = "SELECT * FROM outages ORDER BY date DESC LIMIT 10"; /* 10 last published, for example */ 

$rsBulletins = mysql_query($query_rsBulletins, $conUDS) or die(mysql_error());

 

$totalRows_rsBulletins = mysql_num_rows($rsBulletins);
 

if($totalRows_rsBulletins > 0)

{

	while($row_rsBulletins = mysql_fetch_assoc($rsBulletins))

	{

		if ($row_rsBulletins['severity'] == "Urgent") {

        	echo "<div id=\"urgent\">I am a red alert!</div>";

		}

    	else if ($row_rsBulletins['severity'] == "High") {

        	echo "<div id=\"high\">I am a orange alert!</div>";

    	}

    	else if ($row_rsBulletins['severity'] == "Medium") {

       		echo "<div id=\"medium\">I am a yellow alert!</div>";

     	}

   		else if ($row_rsBulletins['severity'] == "Low") {

     		echo "<div id=\"low\">I am a blue alert!</div>";

		}

		else {

			echo "<div id=\"allgreen\">There are no reported outages.</div>";

		}

	}

}

else

{

	echo "<div id=\"allgreen\">There are no published alerts.</div>";

}

Open in new window

0
 
LVL 2

Author Comment

by:udsfsg
ID: 24850411
I'd say you above response is close. I just made a small change to add the WHERE statement to the query, and I pulled the 1st else statement.
This seems to be giving me the desired look.
Thanks Flob9!

<?php

mysql_select_db($database_conUDS, $conUDS);

$query_rsBulletins = "SELECT * FROM outages WHERE publish = 'Y' ORDER BY date DESC LIMIT 10"; /* 10 last published, for example */ 

$rsBulletins = mysql_query($query_rsBulletins, $conUDS) or die(mysql_error());

 

$totalRows_rsBulletins = mysql_num_rows($rsBulletins);

 

if($totalRows_rsBulletins > 0)

{

	while($row_rsBulletins = mysql_fetch_assoc($rsBulletins))

	{

		if ($row_rsBulletins['severity'] == "Urgent") {

        	echo "<div id=\"urgent\">I am a red alert!</div>";

		}

    	else if ($row_rsBulletins['severity'] == "High") {

        	echo "<div id=\"high\">I am a orange alert!</div>";

    	}

    	else if ($row_rsBulletins['severity'] == "Medium") {

       		echo "<div id=\"medium\">I am a yellow alert!</div>";

     	}

   		else if ($row_rsBulletins['severity'] == "Low") {

     		echo "<div id=\"low\">I am a blue alert!</div>";

		}

		

	}

} else {

	echo "<div id=\"allgreen\">There are no published alerts.</div>";

}

?>

Open in new window

0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now