<?php
$DB_SERVER = "localhost";
 $DB_USER = "root";
 $DB_PASS = "";
 $DB_NAME = "test";

//$con = mysql_connect($DB_SERVER, $DB_USER, $DB_PASS);
//mysql_select_db($DB_NAME);


$getNoOftitlesQuery = mysql_query("select count(*) from  David_Menu", $con);
$no_of_titles = mysql_result($getNoOftitlesQuery, 0);

?>

<!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>
<title>Menu Editor :: Menu Items:<?=$no_of_titles?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12pt;
	color: #333333;
}
body {
	background-color: #E0DFE3;
}
a {
	font-weight: bold;
}
-->
</style>
</head>
<body>

<?php

if ($submit) {
    echo "<h1>Submit:</h1>";
    $title = mysql_escape_string($title);
    $url = mysql_escape_string($url);

    $sql = "UPDATE David_Menu SET id = " . $id . ",url = '" . $url . "',title = '" . $title . "', description = '" . $description . "'  WHERE id = '" . $oldid . "'";
    echo $sql;

    $result = mysql_query($sql, $con);



    if ($result == 1) {
        echo "<h1>Change made!<br><br><a href=\"editmenu.php\">Go Back</a></h1>";
    } else {
        echo "<h1>Error on Change!<br><br><a href=\"editmenu.php\">Go Back</a></h1>";
    }
} elseif ($insert) {
    echo "<h1>Insert:</h1>";
    $url = mysql_escape_string($url);
    $title = mysql_escape_string($title);
    $description = mysql_escape_string($description);
    $result = mysql_query("INSERT INTO `David_Menu` (id,title,url,description) VALUES ('" . $id . "','" . $title . "','" . $url . "','" . $description . "')", $con);



    echo "INSERT INTO `David_Menu` ( `id` , `title`, `url` , `description` ) VALUES (`" . $id . "`, `" . $title . "`,`" . $url . "`, `" . $description . "`)";
    if ($result == 1) {
        echo "<h1>Insert made!";
        echo "<br><br><a href=\"editmenu.php\">Go Back</a></h1>";
    } else {
        echo "Error on insert!";
    }

    echo $result;
} elseif ($delete) {
    echo "<h1>Delete:</h1>";

    if ($id) {
        $result = mysql_query("DELETE FROM David_Menu WHERE id =" . $id, $con);
    }

    echo "DELETE FROM David_Menu WHERE id =" . $id;

    if ($result == 1) {
        echo "<h1>Delete made!";
        echo "<br><br><a href=\"editmenu.php\">Go Back</a></h1>";
    } else {
        echo "Error on delete!";
    }

    echo $result;
} elseif ($id) { // editing
    $result = mysql_query("SELECT * FROM David_Menu WHERE id = " . $id, $con);

    ?>
<form action="editmenu.php?submit=1" method="post" name="form" id="form">
  <table width="80" border="0" cellspacing="1" cellpadding="1">
    <tr>
     <th>ID</th>
      <th>Title</th>
      <th>URL</th>
      <th>Description</th>
    </tr>
    <?php
    while ($myrow = mysql_fetch_array($result)) {

        ?>
    <tr>
      <td><input name="id" type="text" id="id" size="5" value="<?= $myrow["id"]?>" /></td>
      <td><input name="title" type="text" id="title" value="<?= $myrow["title"]?>" /></td>
      <td><input name="url" type="text" id="url" value="<?= $myrow["url"]?>" /></td>
      <td><input name="description" type="text" id="description" value="<?= $myrow["description"]?>" /></td>
      <input name="oldid" type="hidden" value="<?=$myrow["id"]?>" />
    </tr>
  <?php
    }
    loop;

    ?>

  </table>


     <input type="submit" name="Submit" value="Submit" />
  </p>
</form>
<?php

} else {

    ?>
<h1>New:</h1>
<form action="editmenu.php?insert=1" method="post" name="form" id="form">
  <p>ID:
    <input name="id" type="text" id="id" size="5" />
    Title:
    <input name="title" type="text" id="title" />
    URL:
    <input name="url" type="text" id="url" />
Description:
    <input name="description" type="text" id="description" />
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit" />
  </p>
</form>
<?php
    echo "<h1> David_Menu:</h1>";
    $result = mysql_query("SELECT * FROM David_Menu ORDER BY id", $con);
    if ($myrow = mysql_fetch_array($result)) {

        ?>

<table border=1 align=left>
 <tr>
       <th>ID</th>
      <th>Title</th>
      <th>URL</th>
      <th>Description</th>
    </tr>
<?php

        do {
            echo "<tr class=\"navbox\">";

            printf("<td><a href=\"editmenu.php?id=%s\">%s</td>", $myrow["id"], $myrow["id"]);
            printf("<td><span align=left>%s</span></td>\n", $myrow["title"]);
            printf("<td><span align=left>%s</span></td>\n", $myrow["url"]);
            printf("<td><span align=left>%s</span></td>\n", $myrow["description"]);
            printf("<td><a href=\"editmenu.php?delete=1&id=%s\" \">Delete</td>", $myrow["id"]);

            echo "</td></tr>";
        } while ($myrow = mysql_fetch_array($result));

        echo "</table>";
    }
}

mysql_close($con);

?>
</p>
</body>
</html>
