ankur0101 Posted May 3, 2012 Share Posted May 3, 2012 Hi, I have 2 pages, config.php and category.php config.php is class CategoryWork { public $form = ""; public $error = ""; public $add_form = "<br /><p><strong>Add New Category</strong></p><form id=\"form1\" name=\"form1\" method=\"post\" action=\"category.php?action=add\"> <table width=\"550\" height=\"170\" border=\"0\"> <tr> <td width=\"153\">Name :</td> <td colspan=\"2\"><label for=\"cat_name\"></label> <input name=\"cat_name\" type=\"text\" id=\"cat_name\" size=\"50\" maxlength=\"50\" /></td> </tr> <tr> <td>Slug :</td> <td colspan=\"2\"><label for=\"cat_slug\"></label> <input name=\"cat_slug\" type=\"text\" id=\"cat_slug\" size=\"50\" maxlength=\"50\" /></td> </tr> <tr> <td>Description</td> <td colspan=\"2\"><label for=\"cat_desc\"></label> <textarea name=\"cat_desc\" id=\"cat_desc\" cols=\"48\" rows=\"10\"></textarea></td> </tr> <tr> <td> </td> <td width=\"97\"><input type=\"submit\" name=\"button\" id=\"button\" value=\"Submit\" /></td> <td width=\"286\"><input type=\"reset\" name=\"button2\" id=\"button2\" value=\"Reset\" /></td> </tr> <tr> <td> </td> <td colspan=\"2\"></td> </tr> </table> </form>"; public $edit_form1_first = "<br /><p><strong>Edit Category</strong></p><form id=\"form1\" name=\"form1\" method=\"post\" action=\"category.php?action=edit\"> <table width=\"454\" height=\"79\" border=\"0\"> <tr> <td width=\"110\">Category :</td> <td width=\"334\"><label for=\"select\"></label> <select name=\"select_cat\" id=\"select\">"; public $edit_form1_last = "</select> </td> </tr> <tr> <td> </td> <td><input type=\"submit\" name=\"button\" id=\"button\" value=\"Submit\" /></td> </tr> </table> </form>"; public $invalid_get_error = "<br /><br /><p><font color=\"#990000\"><strong><em>ERROR : Invalid Option.</em></strong></font></p>"; public function CheckAction($action){ if(!isset($action)) { header("Location:dashboard.php"); } else { $action = $action; if($action == "add") { $this->form = NULL; $this->form = $this->add_form; } else if ($action == "edit") { $this->form = NULL; $this->form = $this->GenerateDropDown(); } else if ($action == "del") { $this->form = "del"; } else { $this->form = $this->invalid_get_error; } // if else .. else if ends here }// if else ends here } // Function CheckAction ends public function ValidateAddForm($name, $slug, $desc){ if(isset($name) || isset($slug) || isset($desc)) { if($name == NULL || $slug == NULL || $desc == NULL) { $this->error = "<br /><br /><p><font color=\"#990000\"><strong><em>ERROR : All 3 fields are required.</em></strong></font></p>"; } else { // Data Insert in database $name = $name; $slug = $slug; $desc = $desc; $check_category_query = "SELECT * FROM category where slug = '$slug'"; $check_category_result = mysql_query($check_category_query); $check_category_count = mysql_num_rows($check_category_result); if($check_category_count == 1) { $this->error = "<br /><br /><p><font color=\"#990000\"><strong><em>ERROR : Slug Already exists.</em></strong></font></p>"; } else if ($check_category_count == 0) { // Do insertion in database $categoryinsert_query = "INSERT INTO category (name, slug, description) VALUES ('$name', '$slug', '$desc')"; $categoryinsert_result = mysql_query($categoryinsert_query); $this->error = "<br /><br /><font color=\"#009900\"><p><em><strong>Success, Category added.</strong></em></p></font>"; } } } } // function ends here public function GenerateDropDown(){ $dropdown_query = "SELECT * FROM category ORDER BY name"; $dropdown_result = mysql_query($dropdown_query); echo $this->edit_form1_first; while($row = mysql_fetch_array($dropdown_result)) { echo "<option value=".$row['id'].">".$row['name']."</option>"; } echo $this->edit_form1_last; } // end of GenerateDropDown function } Here is the code of category.php page <?php include('config.php'); $system = new SystemAdmin; $categorywork = new CategoryWork; $system->ValidateToken($_COOKIE['token']); $categorywork->CheckAction($_GET['action']); $categorywork->ValidateAddForm($_POST['cat_name'], $_POST['cat_slug'], $_POST['cat_desc']); ?> <!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Welcome <?php echo $system->username; ?></title> <?php echo $system->metalines; ?> <?php echo $system->slug_javascript; ?> </head> <body> <h1>Welcome <?php echo $system->username; ?>,</h1> <?php echo $system->mainmenu; ?> <?php echo $system->spryfunctions; ?> <?php echo $categorywork->error; ?> <?php echo $categorywork->form; ?> </body> </html> Problem is that when I go to /category.php?action=edit, it shows dropdown at the top of page and not at the position of <?php echo $categorywork->form; ?> I have attached a screenshot, please go through it. How can I solve this ? Second problem is how can I hide undefined index notice ? Link to comment https://forums.phpfreaks.com/topic/261994-function-not-working-at-right-position/ Share on other sites More sharing options...
creata.physics Posted May 3, 2012 Share Posted May 3, 2012 Really I don't see how this is an issue. If you truly are understanding the code you're writing then you'd know exactly where to place what. Here's a suggestion that would help you figure out what your issue is: take out all the html code from your class variables. There is no reason that I can see why you are having your html in config.php, it all can be put into category.php without taking away any functionality. Link to comment https://forums.phpfreaks.com/topic/261994-function-not-working-at-right-position/#findComment-1342548 Share on other sites More sharing options...
ankur0101 Posted May 4, 2012 Author Share Posted May 4, 2012 I am generating a drop down menu i.e. <select> and its values are driven from database. In Simple language, what I want to do is : I have class called CategorWork class CategoryWork{ public $form = ""; public $edit_form1_first = "<br /><p><strong>Edit Category</strong></p><form id=\"form1\" name=\"form1\" method=\"post\" action=\"category.php?action=edit\"> <table width=\"454\" height=\"79\" border=\"0\"> <tr> <td width=\"110\">Category :</td> <td width=\"334\"><label for=\"select\"></label> <select name=\"select_cat\" id=\"select\">"; public $edit_form1_last = "</select> </td> </tr> <tr> <td> </td> <td><input type=\"submit\" name=\"button\" id=\"button\" value=\"Submit\" /></td> </tr> </table> </form>"; } Now you can see that starting part i.e. <select> is in $edit_form1_first Ending part i.e. </select> is in $edit_form1_last Now middle part <option value=""></option> is generated by a function as follows : class CategoryWork{ // above code here public function GenerateDropDown(){ $dropdown_query = "SELECT * FROM category ORDER BY name"; $dropdown_result = mysql_query($dropdown_query); echo $this->edit_form1_first; // Here First part is called while($row = mysql_fetch_array($dropdown_result)) { echo "<option value=".$row['id'].">".$row['name']."</option>"; // Here middle options are displayed from database } echo $this->edit_form1_last; // and last part is called } // end of GenerateDropDown function } Now I am putting this function in an object $this->form = $this->GenerateDropDown(); Now lets go to main page which will display data i.e. index.php <?php include('config.php'); $categorywork = new CategoryWork; ?> <!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>Untitled Document</title> </head> <body> <p>Wazzupp world</p> <p>Here is my second line</p> <p>Below this line, I want that function</p> <p><?php echo $categorywork->form; ?></p> </body> </html> Problem : index.php should show data in following order : Wazzupp world Here is my second line Below this line, I want that function <<< Now that drop down should be here >>> [/color]BUT It is showing in different way as, Drop Down Wazzupp world Here is my second line Below this line, I want that function My question is WHY drop down is getting displayed at top and not at its given location ??? Link to comment https://forums.phpfreaks.com/topic/261994-function-not-working-at-right-position/#findComment-1343143 Share on other sites More sharing options...
ankur0101 Posted May 4, 2012 Author Share Posted May 4, 2012 I am using $categorywork->form because I am performing multiple actions under a page category.php such as /category.php?action=add /category.php?action=edit /category.php?action=del add will display addition form and that works. edit will display a drop down list which is not working Link to comment https://forums.phpfreaks.com/topic/261994-function-not-working-at-right-position/#findComment-1343147 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.