tags:

views:

129

answers:

2

I am working on designing an IT Asset database. Here I am working on a page used to view details on a specific asset determined by an asset id.

Here I grab $id from $_GET["id"];

When $id is null, the page does not load. When $id does not match any entry within the database, the page loads but no asset table is printed.

In both these cases, I would like to display a message like "There is no database entry for that Asset ID"

How would this be handled? Thank you.

<?php

/* 
*  View Asset
*
*/

# include functions script
include "functions.php";

$id = $_GET["id"];
ConnectDB();
$type = GetAssetType($id);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Wagman IT Asset</title>
</head>

<body>
    <div id="page">
                <div id="header">
                  <img src="images/logo.png" />
                </div>

                </div>

                <div id="content">
                    <div id="container">

                        <div id="main">
                        <div id="menu">
                            <ul>
                                <table width="100%" border="0">
                                <tr>
                                <td width="15%"></td>
                                <td width="30%%"><li><a href="index.php">Search Assets</a></li></td>
                                <td width="30%"><li><a href="addAsset.php">Add Asset</a></li></td>
                                <td width="25%"></td>
                                </tr>
                                </table>
                          </ul>
                        </div>
                        <div id="text">
                        <ul>
                        <li>
                        <h1>View Asset</h1>
                        </li>
                        </ul>
                        <br />
                        <?php
                        switch ($type){
                        case "Server":
                        $result = QueryServer($id);
                        $ServerArray = GetServerData($result);
                        PrintServerTable($ServerArray);
                        break;
                        case "Desktop";

                        break;
                        case "Laptop";

                        break;
                        }
                        ?>


                        </div>

                        </div>
                </div>
                <div class="clear"></div>
                <div id="footer" align="center">
                    <p>&nbsp;</p>
                </div>
                </div>
                <div id="tagline">
                Wagman Construction - Bridging Generations since 1902
                </div>


</body>
</html>
+2  A: 
<?php if (empty($type)): ?>
    <!-- print your message -->
<?php else: ?>
    <!-- show asset stuff -->
<?php endif; ?>
webbiedave
Thanks. This works if the id does not match anything in the database but still does not prevent the entire page from not loading if $id is null.
CT
You could throw in a `if (empty($_GET['id']))` in front of these.
webbiedave
A: 

If $id is null then you shouldn't connect to/query the database at all. Also, another style option using a ternary to check for empty.

$id = !empty($_GET["id"]) ? $_GET["id"] : '';
if ($id) {
   ConnectDB();
   $type = GetAssetType($id);
}

then, same as webbidave's solution in the HTML to show message

jigglee