Jump to content

codeboyuk

Member
  • Posts

    12
  • Joined

  • Last visited

Everything posted by codeboyuk

  1. It sounds like you have had more luck with Zend than I did. The installation reacted badly with Vista on my system, so I had to remove it... I don't know if anyone else had this problem. So at the moment I cannot use Zend which clearly isn't ideal but I have to make do
  2. I've successfully created the remove_user function. I changed the original scripts somewhat. There was a lot of messy noise in it. I managed to find a good tutorial and used that to create this one successfully... Here are the files in case anybody wants to use the class or modify it. Clearly it can be improved, but it isn't too bad as a beginners attempt at a basic OO... remove_user.php session_start(); $pageTitle = "Remove User"; include("../includes/admin_header.php"); include("../classes/userAuthorisationFunctions.php"); echo '<div class="admin_main_body">'; echo '<div class ="admin_main_body" id="listing">'; echo '<br /><br />'; $userManagement = new userManagement(); <form name="userList" action="<?php htmlentities($_SERVER["PHP_SELF"]) ?>" method="post"> $userManagement->showUsers(); $userManagement->removeUsers(); echo '<div>'; echo '<div>'; include("../includes/admin_footer.php"); user_Authorisation.php class userManagement { function removeUsers() { ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "root", "")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE practicesite")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); if(isset($_POST['delete'])) // from button name="delete" { $checkbox = $_POST['checkbox']; //from name="checkbox[]" $countCheck = count($_POST['checkbox']); for($i=0;$i<$countCheck;$i++) { $del_id = $checkbox[$i]; $query = "DELETE FROM admin WHERE admin_id = $del_id"; $result = @mysqli_query($GLOBALS["___mysqli_ston"], $query); } if($result) { header('Location: admin_index.php'); } else { echo "Error: form not processed"; } } else { echo "The form could not be processed"; } } function showUsers() { ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "root", "")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE practicesite")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); $query = "SELECT admin_id, username, email FROM admin"; $result = @mysqli_query($GLOBALS["___mysqli_ston"], $query); if ($result) { echo "<table cellspacing='0' cellpadding='10'>"; echo "<tr><th colspan='4'>List of Current Users</th></tr>"; echo "<tr><th>Username</th><th>Email Address</th><th>Select Users</th></tr>"; while ($row = $result->fetch_object()) { $email = $row->email; $username = $row->username; $id = $row->admin_id; echo "<tr><td>$username</td><td>$email</td><td><input type='checkbox' name='checkbox[]' id='checkbox[]' value=$id /></td></tr>"; } echo "</table>"; echo "<p><input id='delete' type='submit' class='button' name='delete' value='Delete Selected Users'/></p></form>"; } else { echo "The user list could not be displayed due to a technical error. Please consult the administrator."; } } }
  3. I'm working on it....

  4. Hi Ben....thanks - you have given me great help here.... I was unsure exactly what the $_REQUEST action actually achieved. I came to the conclusion (clearly misguided) that it was subtly different to $_POST/$_GET in that it didn't need to correspond to a "name" attribute being set. I thought that instead it was just checking to see whether the form itself had been actioned (i.e. the "action" attribute had been called/REQUESTED). But it seems I was wrong. I've just seen that you have also replied to my other post relating to the confusion I was having regarding the action attribute and how I should set it in this instance.....It looks like that is the fundamental problem. But I reckon I can sort it now Yea - the logic isn't brilliant! It was never my strong point...I just try to battle on through until I get the desired result....I think that part of the problem is that I relied on a third party script as a framework to this script. So, if the script I was using to model this code on was flawed, then as a consequence, this script was going to be flawed.... I'm gonna go back through your pointers and implement the changes to the script tomorrow.... I'll keep you posted Thanks again!!! If you didn't live 5 thousand miles away, I'd buy you a pint!
  5. Hi Here's the SQL Schema: CREATE TABLE IF NOT EXISTS `admin` ( `admin_id` int(8) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(20) NOT NULL, `email` varchar(40) NOT NULL, `password` varchar(40) NOT NULL, PRIMARY KEY (`admin_id`), UNIQUE KEY `username` (`username`,`email`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=28 ; INSERT INTO `admin` (`admin_id`, `username`, `email`, `password`) VALUES (1, 'Testing', 'test@test.com', '7288edd0fc3ffcbe93a0cf06e3568e28521687bc'), (2, 'Test2', 'testing3@testing.com', 'dc724af18fbdd4e59189f5fe768a5f8311527050'), (3, 'Wilhelm', 'admin@blooter.com', 'dc724af18fbdd4e59189f5fe768a5f8311527050'), (4, 'TestAgain', 'Testing@testing3.com', 'dc724af18fbdd4e59189f5fe768a5f8311527050'), (5, 'WillyWonka', 'will@will.com', 'dc724af18fbdd4e59189f5fe768a5f8311527050'), (6, 'Spiderman', 'Spiderman@spidey.com', 'dc724af18fbdd4e59189f5fe768a5f8311527050'), (7, 'Superman', 'Clarke@Krypton.com', 'dc724af18fbdd4e59189f5fe768a5f8311527050'); I've included the class file again with comments to accompany the closing conditionals....Does it make any sense?? class userManagement { function getUsers() { ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "root", "")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE practicesite")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); $query = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM admin ORDER BY username ASC"); while ($user = mysqli_fetch_assoc($query)) { echo '<table><tr><th colspan="1">Username</th><th colspan="1">Email Address</th></tr>'; echo '<tr class="yellow"><td class="width">' . $user['username'] . '</td><td class="adjacent">' . $user['email'] . '</td></tr>'; echo '</table>'; } } function removeUsers() { ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "root", "")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE practicesite")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); //if (isset($_POST['submit'])){ if(!isset( $_REQUEST['action']) ) { $sql = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM admin ORDER BY username ASC"); while ($user = mysqli_fetch_assoc($sql)) { echo '<table>'; echo '<tr class="yellow"><td class="width">Username</td><td class="adjacent">Email Address</td><td class="adjacent"></td><td class="adjacent"></td></tr>'; echo '<tr><td>' . $user['username'] . '</td><td class="adjacent">' . $user['email'] . '</td><td class="adjacent"><input type="checkbox" name=' . $user['username'] . '/></td></tr>'; echo '</table>'; } echo '<input type="submit" value="Delete Selected User(s)"/>'; } else if(isset($_REQUEST['action'])) { if (count($_POST) > 0 ) { $userDelcount = 0; //Sets counter to 0....Each time a delete request is passed to the database, the value is incremented by 1. foreach($_POST as $name => $value) { //echo "$name"; $query = "DELETE * FROM admin WHERE where username = '$name'"; //$result = mysqli_query($GLOBALS["___mysqli_ston"], $query); if (@mysqli_query($GLOBALS["___mysqli_ston"], $query)); //If a query was received. { $userDelcount ++; //Increment the $userDelcount variable by 1. } } //closes foreach loop } //Closes if (count) condition. else if (count($_POST) != $userDelCount ) //If the number of users, passed by the form, does not equal the value of the counted users passed to the userDelcount variable, print error. { echo "<p>There was a problem processing your request.Please try again later.</p>"; } else //if users were successfully deleted.... { echo "<p>The user(s) you selected has/have been deleted.</p>"; unset($_REQUEST['action']); echo "<p>Please <a href = '../admin170976/remove_user.php'>click here to continue.</p>"; } } else //This line should display if $_REQUEST['action'] was not sent by the form when the delete button was activated, but no values were received i.e. no users were checked/selected.. echo "<p>You did not select any users.</p>"; //"<p>Please <a href = '../admin170976/remove_user.php'>click here to return.</p>"; } }
  6. Hi Ben Sorry about the missing tag! I edited the script slightly before posting it in order to make it more easily understood, but I seem to have removed the closing while loop bracket (this should have been placed just after the closing table tag). I notice that I failed to close the form tag and I'd also forgotten to remove the 'error checking' lines: if ($result) { echo $result; $userDelcount ++; } I made a few mistakes. Clearly I was having a bad day! I agree with you about tags. They add clarity to a script. If I've missed any it will usually be unintentional (unless I'm modifying someone else's script). I attach the new formats of the same scripts. They are slightly different to yesterday: I've moved the form elements out of removeUsers() and into remove_user.php. This is how I had it originally, and I think that it looks neater. I've also cleaned up the code - removing any needless/redundant lines. I'm still getting erros - but maybe you can see where the problem lies....Hopefully anyway! remove_user.php session_start(); $pageTitle = "Remove User"; include("../includes/admin_header.php"); include("../classes/userAuthorisationFunctions.php"); echo '<div class="admin_main_body">'; echo '<br /><br />'; $userManagement = new userManagement(); <form name="userList" action="<?php htmlentities($_SERVER["PHP_SELF"]) ?>" method="post"> $userManagement->removeUsers(); echo '<form>'; echo '<div>'; include("../includes/admin_footer.php"); userAuthorisationFunctions.php class userManagement { function getUsers() { ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "root", "")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE practicesite")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); $query = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM admin ORDER BY username ASC"); while ($user = mysqli_fetch_assoc($query)) { echo '<table><tr><th colspan="1">Username</th><th colspan="1">Email Address</th></tr>'; echo '<tr class="yellow"><td class="width">' . $user['username'] . '</td><td class="adjacent">' . $user['email'] . '</td></tr>'; echo '</table>'; } } function removeUsers() { ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "root", "")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE practicesite")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); if(!isset( $_REQUEST['action']) ) { $sql = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM admin ORDER BY username ASC"); while ($user = mysqli_fetch_assoc($sql)) { echo '<table>'; echo '<tr class="yellow"><td class="width">Username</td><td class="adjacent">Email Address</td><td class="adjacent"></td><td class="adjacent"></td></tr>'; echo '<tr><td>' . $user['username'] . '</td><td class="adjacent">' . $user['email'] . '</td><td class="adjacent"><input type="checkbox" name=' . $user['username'] . '/></td></tr>'; echo '</table>'; } echo '<input type="submit" value="Delete Selected User(s)"/>'; } else if(isset($_REQUEST['action'])) { if (count($_POST) > 0 ) { $userDelcount = 0; foreach($_POST as $name => $value) { $query = "DELETE * FROM admin WHERE where username = '$name'"; if (@mysqli_query($GLOBALS["___mysqli_ston"], $query)); { $userDelcount ++; } } } else if (count($_POST) != $userDelCount ) { echo "<p>There was a problem processing your request.Please try again later.</p>"; } else { echo "<p>The user(s) you selected has/have been deleted.</p>"; unset($_REQUEST['action']); echo "<p>Please <a href = '../admin170976/remove_user.php'>click here to continue.</p>"; } } else echo "<p>You did not select any users.</p>"; //"<p>Please <a href = '../admin170976/remove_user.php'>click here to return.</p>"; } } //$register = new Register(); Thanks a lot!!
  7. Hi, I posted earlier referring to this issue, but then went away to attempt to resolve it. Anyway - no joy. The problem I am having (I think), is getting the form to process once the user has clicked 'delete'. Basically - nothing happens. I have error reporting switched on, but I'm not receiving any notifications. I based this on a procedural approach, although I've modified it to fit into a class. That seems to be where the problems started - because I had to change the form action attribute (with the procedural approach, it was pointing to a file. With the oo approach I had to change to PHP_SELF). The basics of my approach are as follows: Files used: 1/remove_user.php - calls and initiates the methods and classes. 2/userAuthorisationFunctions.php - stores the classes and methods - removeUsers() is the method that I am using and calling to do the work here. 3/I'm using checkboxes and a while loop to run the SQL data through and populate a table. 4/There is one delete button that is designed to delete any number of users that the administrator has 'checked'. Because I only want one occurence of the button, I have placed it just outside of the loop. 5/The page is rendering correctly, showing a list of current system users. I originally stored the form opening and closing elements in remove_user.php. However, as I had been having trouble getting it to work, I moved both elements over to be stored within removeUsers()....I could move them back, but as I don't know where the error is, I have left them where they are. I think that the error is likely to be in one of two place: a/ The action attribute. b/ The loop syntax. Here are the files: remove_user.php session_start(); $pageTitle = "Remove User"; include("../includes/admin_header.php"); include("../classes/userAuthorisationFunctions.php"); echo '<div class="admin_main_body">'; echo '<br /><br />'; $userManagement = new userManagement(); $userManagement->removeUsers(); echo '<div>'; include("../includes/admin_footer.php"); userAuthorisationFunctions.php class userManagement { function getUsers() { //To display users. Probably redundant. ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "root", "")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE practicesite")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); $query = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM admin ORDER BY username ASC"); while ($user = mysqli_fetch_assoc($query)) { echo '<table><tr><th colspan="1">Username</th><th colspan="1">Email Address</th></tr>'; echo '<tr class="yellow"><td class="width">' . $user['username'] . '</td><td class="adjacent">' . $user['email'] . '</td></tr>'; echo '</table>'; } } function removeUsers() { //To display users and also allow an administrator to remove users. ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "root", "")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE practicesite")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); //if (isset($_POST['submit'])){ if(!isset( $_REQUEST['action']) ) { $sql = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM admin ORDER BY username ASC"); while ($user = mysqli_fetch_assoc($sql)) { //echo '<table><tr><th colspan="1">Username</th><th colspan="1">Email Address</th><th colspan="1">Delete User</th></tr>'; //echo '<table><tr>'; echo '<form id = "photo_del_form" method = "post" action = "htmlentities($_SERVER["PHP_SELF"])">'; echo '<table>'; echo '<tr class="yellow"><td class="width">Username</td><td class="adjacent">Email Address</td><td class="adjacent"></td><td class="adjacent"></td></tr>'; echo '<tr><td>' . $user['username'] . '</td><td class="adjacent">' . $user['email'] . '</td><td class="adjacent"><input type="checkbox" name=' . $user['username'] . '/></td></tr>'; echo '</table>'; echo '<form>'; echo '<input type="submit" value="Delete Selected User(s)"/>'; } else if(isset($_REQUEST['action'])) { if (count($_POST) > 0 ) { $userDelcount = 0; foreach($_POST as $name => $value) { $query = "DELETE * FROM admin WHERE where username = '$name'"; echo $query; $result = mysqli_query($GLOBALS["___mysqli_ston"], $query); if ($result) { echo $result; $userDelcount ++; } } } else if (count($_POST) != $userDelCount ) { echo "<p>There was a problem processing your request.Please try again later.</p>"; } else { echo "<p>The user(s) you selected has/have been deleted.</p>"; unset($_REQUEST['action']); echo "<p>Please <a href = '../admin170976/remove_user.php'>click here to continue.</p>"; } } else echo "<p>You did not select any users.</p>"; //"<p>Please <a href = '../admin170976/remove_user.php'>click here to return.</p>"; } //End of function. } } Any advice would be really appreciated. Cheers
  8. I think that you have misunderstood what I was saying. With respect to tables being search engine friendly or not: It's not as if Google/Yahoo etc publish their metric algorithms - so I will hold back on commenting. I'll leave that to the SEO salesmen. But as I don't use tables for layout (generally), this has never really been an issue. For me, tables are very useful. Combined with css AND forms, they can produce very well set out and structured elements. Having said that - I don't know anyone however who uses tables needlessly for layout. CSS is the standard approach. Clearly if you are just trying to produce a contact form or something similar, tables are not required. It would be silly to use one. However, for tabulated data - as Andrea said, they serve a purpose. But to approach the layout of a site with tables in mind, instead of CSS would be silly. "CSS layout really isn't that difficult, once one's gotten the hang of it, and is not as complicated as you make it sound". I am not saying it is complicated, but if it is tabulated data you are trying to present - you would surely go with a table. And cross browser issues are a concern. Especially now that CSS3 is here. Chrome and Safari have stepped up to the plate, whereas Firefox surprisingly is lagging behind with its interpretation of many of the new rules. Tables are therefore more reliable for certain tasks, in my opinion, because of these discrepencies. It depends entirely on what is being 'laid out'. Structured presentation of data: use tables. Standard layout of forms (in most cases): use CSS. Positioning of media: CSS. Text and other such content: CSS. This leads on nicely to my next post actually (which is the reason I logged in to here). I have produced tabulated data displaying an administrator list as part of a CMS. I can't get the form controlling it to process! Anyway, good discussion. I am no expert on any of these issues. In fact, I'm not an expert at anything. I would always use the approach that best suits the job at hand. And as my old programming lecturer taught me: keep it simple stupid. Don't make life needlessly difficult. I'd rather be in the pub!
  9. Hi I am building a class to manage site users (Via a CMS). In order to complete this I am building a delete/remove user function/method. Things are going ok (I think), but I am confused about what to put in the action tag of the form being used to process the data. Before coding my method, I referred to a script that someone showed me which does something similar in functionality: it deletes photos previously uploaded to a folder, and also removes the associated information from the database. I used this script as the framework for the code that I am working with. Eveything seems to be working fine - EXCEPT - when I click on the 'delete users' button, nothing happens. The first thing that I examined for error was the action attribute within my form. The value of the action attribute is: action = "<? php htmlentities($_SERVER["PHP_SELF"]) ?>" This format has worked for previous forms. I compared this to the action attribute within the script that I was using as a guide. It's value is set as follows: action = "remove_photo.php?<?php echo "action='godel' ";?> In this instance, remove_photo.php is the file that is being used to both process and display the form. The reference script works perfectly but it uses a procedural approach . For my application, I am using a more object oriented approach, so both my opening and closing form tag elements are stored in a seperate file to the class file that processes it. I am new to OO but relatively comfortable with the paradigm so far (my other methods etc are working fine). I therefore suspect that the reason for my script's inability to process is due to the action attribute setting (I cannot be 100% sure of course!). I am going to therefore apply the action attribute settings used successfully in the reference script, to the application that I am building. However, the trouble is, I don't understand it I don't like it when something works, but I can't see the logic behind why it does. I am not concerned about the form id or method attrbutes. I understand what is going on there - and why they are set as they are. However, the action attribute is very difficult to understand for the following reasons: 1/ There is no variable/method/entity/object/ANYTHING called 'godel' elsewhere in the reference script. For this reason I thought that it might not be needed, so removed the bit of code within PHP tags concerning it: <?php echo "action='godel' ";?> - When I did this - the script didn't work. In fact - nothing at all would process. This was a similar result to what was happening with the script I am building. 2/ I then put the PHP line back in, but replaced 'godel' with 'something'. I saved and processed the form. - it worked. 3/ I then tried it again replacing 'godel' with an empty string i.e: ' '. - it worked. Has anyone come across forms being processed in a similar way before? If so, please could you explain what is happening here. I can't find anything relating to this type of occurence online.... I am going to place the php tags in the file I am building, to see if it has the desired effect. If it does, I will obviously keep it. But not understanding what is happening, or why it is happening is very annoying!! Thanks Will p.s. I've just noticed that there are threads on here dealing with deleting data from a database etc. I'll have a read!
  10. Surely it depends on what the table is being used for. CSS is primarily used for layout of standard elements - headers, footers, content areas etc. But tables can still be very useful and supplementary - especially when combined with forms. Tables can be more reliable than CSS, as they are less affected by cross browser issues. So if you want, for example, form elements to be perfectly in line: a table can do the job well with less chopping and changing (and less head banging against a brick wall), which can be the case with CSS. CSS often needs to be adjusted, and then re-adjusted (ad infinitum), in order to get the perfect layout accross all browsers. This kind of overkill can make your life needlessly difficult - and mean that you are spending more time, and being less efficient. I would practice with CSS - working with different objects and elements, and then decide for yourself where it should be used. Tables therefore, in my opinion, will not be redundant anytime soon.
  11. Thanks for your advice Ben. I'm just feeling my way around with what is possible and what isn't possible with the OO approach. You mentioned that getters and setters should only be used if data is needed to be stored within the object. With a login process, user data entered into the form fields is stored within the relevant $_POST variables/attributes (for reconciling to the database). It is (technically speaking) taken from one file, and then stored within the class object for processing. The session id will also need to be set within the class object and then used by files outside of the class object. Does this mean that I should have used the getter/setter approach here? Clearly it worked without me using it - but I'd rather be using a conventional approach if I should be. Thanks for the tip about the class name! 'Auth' seems very appropriate for the functions that it will carry out. Thanks Will
  12. Just to let you know that I have opted for adding a new function to the existing class. I've also changed the class name to 'user'. So basically the class now contains two functions: admin_login and logout....I think 'user' makes more sense as the parent object. I've built it in such a way that when the user clicks the logout link, it directs him to logout.php, which immediately includes the site classes file and then calls the logout function as so: $user->logout(); Its working, although would still be interested to get some feedback on my previous post. Also, what is the difference between the following: user::admin_login($user); and, $user->admin_login(); They both appear to do the same thing or am I missing something? One final question: When the user clicks the link, I am using the traditional '<a href> filename.php </a>' approach. Is there a way of cutting that stage out altogether i.e. by using a variable instead: such as '<a href> $user->logout(); </a>' Thanks
  13. Hi I am a newbie to PHP OO. I have successfully built a class called login. Unsurprisingly this deals with the logic of logging in a user. However, I am now on with the logout logic, but am unsure where the best place to include this code would be. I was initially going to just add a new function to the login class (which I guess should also be referred to as the base class).But then I read about class inheritance, and wondered whether it would make better practice to extend the login class to cover a logout class i.e: 'class logout extends login'. Also, my base class does not use the setter or getter methods. It just didn't need them as far as I could see (I basically just modified the original procedural script - transferring the logic to a seperate class away from the content HTML/CSS). I suppose what I want to know is whether or not the setter and getter approach is a necessity for all objects, or do I just use it as and when I think I need them? Finally. If I was to extend the login class e.g. 'class logout extends login', would I need to instantiate the logout class or the login class, or both? (if I wanted to use the functions/variables contained in them). Currently I have adopted the approach of instantiating the class at the bottom of the class script (rather than instantiating it within a separate HTML file for example). These are probably stupid questions! So I apologise! I attach the class for you to look at at. I appreciate that it is quite simplistic! error_reporting (E_ALL); class login { function admin_login() { $message = NULL; if (isset($_POST['submit'])) { require_once ('../includes/connections/mysql_connect.php'); if (empty($_POST['username'])) { $u = FALSE; $message .= '<p> Please enter your username </p>'; } else { $u = escape_data($_POST['username']); } if (empty($_POST['password'])) { $p = FALSE; $message .= '<p>You forgot to enter your password </p>'; } else { $p = escape_data($_POST['password']); } if ($u && $p) { // If everything's OK. $query = "SELECT * FROM admin WHERE username= ('$u') AND password=('$p')"; //changed the table name from 'customer' to 'customers' to fit in with new database structure. $result = @mysqli_query($GLOBALS["___mysqli_ston"], $query); //echo $result; //var_dump "$result"; $row = mysqli_fetch_array($result, MYSQLI_BOTH); if ($row) { session_start(); $_SESSION['admin_id'] = $row[0]; header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/admin_index.php"); } else { $message = '<p> The username and password combination are incorrect.</p>'; } ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res); } else { $message .= '<p>Please try again.</p>'; } } if (isset($message)) { echo '<font color="red">', $message, '</font>'; } }//Closing function } //Closing class tag $login = new login(); //***Instantiating the login class. If I extend the class would I still call this from here? /*Extended login class starts here*/ class logout extends login { //Code would go here } $login = new login(); //***Or would I move it down here? Or do I need to instantiate the logout class instead?? Thanks Will
×
×
  • Create New...