Jump to content

hympert

New Members
  • Posts

    2
  • Joined

  • Last visited

hympert's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thank you all for your comments, I thought switch statement can be used in place of if statement to make the code looks nicer and shorter. So how can I rewrite my code so it works? I change from switch to if-else statement and it works but I can't add new category into database. Either the category name is already exist or not (also pass through other checks), it always shows "This category name is already exist." Any thoughts ? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Welcome to my blog</title> <link href="css/style.css" type="text/css" rel="stylesheet" /> </head> <body> <?php require("config.php"); ?> <div id="main"> <h1>Hympert's blog</h1> <div id="left"> <div id="categories"> <h3>Categories:</h3> <select> <option selected="selected"></option> <?php //dropdown menu to select a category $categories = mysql_query("SELECT * FROM categories ORDER BY name ASC"); while($category = mysql_fetch_array($categories)){ echo "<option>" . $category['name'] . "</option>"; } ?> </select> <br /><br /> <form method="get"> <input name="new_cat" type="submit" value="Create new category" /><br /><br /> </form> <?php $submit = $_GET['new_cat']; if(isset($submit)){ ?> <!-- create category form --> <form method="get" action=""> Category name:<input name="cat_name" type="text" value="" /><br /><br /> <input name="submit" type="submit" value="Create" /><br /> </form> <?php } $cat_name = $_GET['cat_name']; //check category name to see if it is valid or not //check the length, existed, blank of input category's nname if(isset($cat_name)){ if(trim($cat_name) == ""){ create_cat_form(); echo "Please input a category name."; }elseif(strlen($cat_name) > 50){ create_cat_form(); echo "Category name cannot exceed 50 characters."; }elseif($cat_name){ $r = mysql_query("SELECT name FROM categories"); while($result = mysql_fetch_array($r)){ $cat_name = $result; } create_cat_form(); echo "This category name is already exist."; }else{ mysql_query("INSERT INTO categories (name) VALUES ({$cat_name})"); echo "New category has been added succesfully."; } } function create_cat_form(){ echo "<form method=\"get\"> Category name:<input name=\"cat_name\" type=\"text\"/><br /><br /> <input name=\"submit\" type=\"submit\" value=\"Create\" /><br /> </form>"; } ?> </div><!-- end div #categories --> <div id="posts"> Posts </div><!-- end div #posts --> </div><!-- end div #left --> <div id="contents"> Recently posted </div><!-- end div #contents --> </div><!-- end div #main --> </body> </html>
  2. hympert

    Hello

    Hi everyone, I'm a newbie here. I learn php mainly by myself so it's sometimes difficult to understand every concept. I hope I can learn more experience and get the help from everyone here. Sincerely.
  3. Hi everyone, I'm trying to create a blog for myself and I'm having trouble right of the bat. I'm a beginner by the way. I'm trying to code a create new category page and check what information is put in by the user. It doesn't work somehow. Here're my code. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Welcome to my blog</title> <link href="css/style.css" type="text/css" rel="stylesheet" /> </head> <body> <?php require("config.php"); ?> <div id="main"> <h1>Hympert's blog</h1> <div id="left"> <div id="categories"> <h3>Categories:</h3> <select> <option selected="selected"></option> <?php //dropdown menu to select a category $categories = mysql_query("SELECT * FROM categories ORDER BY name ASC"); while($category = mysql_fetch_array($categories)){ echo "<option>" . $category['name'] . "</option>"; } ?> </select> <br /><br /> <form method="get"> <input name="new_cat" type="submit" value="Create new category" /><br /><br /> </form> <?php $submit = $_GET['new_cat']; if(isset($submit)){ ?> <!-- create category form --> <form method="get" action=""> Category name:<input name="cat_name" type="text" value="" /><br /><br /> <input name="submit" type="submit" value="Create" /><br /> </form> <?php } $cat_name = $_GET['cat_name']; //check category name to see if it is valid or not //check the length, existed, blank of input category's nname switch ($cat_name){ case trim($cat_name) == "": echo "Please input a category name."; break; case strlen($cat_name) > 50: echo "The length of a category name cannot exceed 50 characters."; break; case $r = mysql_query("SELECT name FROM categories"): while($result = mysql_fetch_array($r)){ $cat_name = $result; } echo "Category name is already exist."; break; default: mysql_query("INSERT INTO categories (name) VALUES ({$cat_name})"); } function create_cat_form(){ echo "<form method=\"get\"> Category name:<input name=\"cat_name\" type=\"text\"/><br /><br /> <input name=\"submit\" type=\"submit\" value=\"Create\" /><br /> </form>"; } ?> </div><!-- end div #categories --> <div id="posts"> Posts </div><!-- end div #posts --> </div><!-- end div #left --> <div id="contents"> Recently posted </div><!-- end div #contents --> </div><!-- end div #main --> </body> </html> <?php //database configuartion $db_host = "localhost"; $db_user = "root"; $db_pass = "hoanghiepp"; $db_name = "blog"; //database connection $connect = mysql_connect($db_host, $db_user, $db_pass) or die("Cannot connect to the server"); $select_db = mysql_select_db($db_name) or die("Cannot connect to the database"); ?> The CSS page /*reset the window format */ * {margin: 0; padding: 0;} body { background-color: azure; } h1 { text-align: center; margin-top: 50px; padding: 50px; font-size: 50px; color: violet; } /* style for the main div */ #main { width: 800px; height: 700px; background-color: aqua; margin: auto; } /* style for left div */ #left { width: 250px; height: auto; float: left; margin-right: 50px; background-color: yellow; } /* style for categories div */ #categories { width: 250px; height: auto; margin-bottom: 50px; } /* style for posts div */ #posts { width: 250px; height: auto; background-color: antiquewhite; margin-top: 50px; } /* style for contents div */ #contents { width: 500px; height: auto; background-color: beige; float: right; } a { text-decoration: none; } Hope anyone can tell me the problem with the switch statement. I use if statement before and it works perfectly fine, but the code seems too long and repeated. Thank you very much for helping me out. config.php index.php style.css
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.