Jump to content

gsingh85

Members
  • Posts

    21
  • Joined

  • Last visited

Everything posted by gsingh85

  1. Please see the following code: $colors = ["red hello","green this","blue lovely","yellow morning"]; foreach ($colors as $towns) { $hello = (explode(" ", $towns)); } If I do print_r($hello[1]); within the loop I get all the values of index [1] in the browser although not sure why there are no spaces between the words: HelloThisLovelyMorning However if I do the same print outside the loop I only get this displaying in the browser: Morning That's no good to me because I want all the values with the index of [1] so I can use them anywhere I want. Any help would be much appreciated.
  2. Sorry. Here is an example I have been working on: $fitters = \DB::select('address')->from('fitters')->execute()->as_array(); foreach ($fitters as $fitter) { $town = explode(',', $fitter['address']); if (isset($town[2])) { $town = $town[2]; $get_available_towns[] = $town; A new variable exists $get_available_towns to add some extra on the array but why are the [] brackets required?
  3. I have a variable and it contains a lot of strong data would I want to convert the string data into an array. What's the best way of doing because I know older versions of php will be slightly different?
  4. Oh I see. Do think I should read up on adding elements to arrays?
  5. Thanks very much for your reply. It does and it doesn't although your explanation is perfect. What does this actually mean? $get_available_towns[] = $town; Do you know a good article that could explain this is detail or a good text book with this section because I'm fairly new to this and although I have used arrays and loops I've never had to add value to arrays or create empty arrays.
  6. Hi Everyone Please checkout the below code: foreach ($joiners as $joiner) { $town = explode(',', $joiner['address']); if (isset($town[2])) { $town = $town[2]; $get_available_towns[] = $town; $get_available_towns = array(); } foreach ($joiners as $joiner) { $town = explode(',', $joiner['address']); if (isset($town[2])) { $town = $town[2]; $get_available_towns[] = $town; $get_available_towns = array(); } I don't really understand what is happening. I know the code and everything is correct and it works but I really don't understand how the loop, arrray and variable is working. I wanted to print_r the $get_available_towns outside the loop but it would not work without the square brackets but also I did not even know the square brackets were required. The point was to loop through the data and print all the values. I understand how loops, array's and variables work but I've not seen them used like this. This makes me think I am missing some basic knowledge but I have looked around and I can't see any examples of how loops and arrays are used like this. I have also checked the php manual but still no joy. Can you guys tell me where I could find the information that would have examples on how something like the above code would work?
  7. I've been at this now for a while but I'm struggling to understand this code. I understand the bigger picture of what the code is doing but I can't seem to graps the logic behind the code and the steps it's taking. Here is part of the code: class Validate{ private $_passed = false, $_errors = array(), $_db = null; if($rule === 'required' && empty($value)){ $this->addError("{$item} is required"); } else if(!empty($value)){ switch($rule){ case 'min': if(strlen($value) < $rule_value){ $this->addError("{$item} must be a minimum of {$rule_value}"); } private function addError($error){ $this->_errors[] = $error; } public function errors(){ return $this->_errors; } I'm struggling to understand how the error methods and properties are being used. I can't see what is being used within the class or outside of it. Here is the code outside the class: $validate = new Validate();{ $validate = new Validate(); $validation = $validate->check($_POST, array( 'username' => array( 'required' => true, 'min' => 2, 'max' => 20, 'unique' => 'users' ), 'password' => array( 'required' => true, 'min' => 6 ), 'password_again' => array( 'required'=> true, 'matches'=> 'password' ), 'name' => array( 'required' => true, 'min' => 2, 'max' => 50 ) )); if($validation->passed()){ echo 'passed'; } else{ foreach ($validation->errors() as $error){ echo $error, '<br>'; } Looking at the 2nd part I cannot see why the programmer has put errors() as $error. Why not just create a method so you don't need to say "as $error". Also looking at the first part: private function addError($error){ $this->_errors[] = $error; } public function errors(){ return $this->_errors; } I don't understand why he has created two methods. Why not just create one? How is the public errors method able to display the errors within the class because _errors doesn't look like it contains anything. Can someone please break this down and help me to understand this because I've been at this for a while and I'm not really getting anywhere. Your help would be much appreciated.
  8. That's great. Thanks so much for your help guys.
  9. I'm fairly new to this and I have looked around but I can't find much information on it. Basically I know what an array and a function is but I'm currently working through an oop login and register sytem and I am seeing this quite a lot: function myFunction($myVariable = array()){ $Firstvariable = $myVariable[0]; $Secondvariable = $myVariable[1]; //etc } This is really confusing me and I can't see any examples in any tutorial sites or books of an array and a function being used like this. Can someone just explain how something like this would work in simple terms? I have also checked the manual but I can't see an example an array being used like this. Any help would be much appreciated. Thanks.
  10. Well I should have explained at the start that I am knew to object-oriented programming. I didn't think methods from one class could create an instance of another class. I thought for each class you have to specifically create an object by using the "new" key word.
  11. I see. So we don't need to say: $obj = new PDOSTATEMENT; In order to use the methods from the PDOSTATMENT class. I though because there was now $x = new PDO STATEMENT; we couldn't use any methods from with the PDOSTATEMENT class.
  12. I'm using an example of someone else's code and it's not the complete code. Yes I agree with that but I thought first in order to use the query() method from the PDOStatement class you first need to instantiate the class such as: $object = new PDOSTATEMENT; $object->query() The only class that has been instantiated as far as I can see is the PDO class which is not the PDOSTATEMENT class.
  13. "The fetch() method is not a static method, it is a public method that belongs to the PDO Statement Object." I see what you mean but then how come we don't need to instantiate the PDOSTATEMENT class first in order to use its method?
  14. Hi. I am a little confused about PDO, its classes and the general oop aspects of it. If you at the below code: <?php try { $handler = new PDO('mysql:host=127.0.01;dbname=app', 'root',''); $handlser->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catc(PDOException $e) { echo $e->getMessage(): die(); } $query = $handler->query('SELECT * FROM table'); while($r = $query->fetch(PDO::FETCH_OBJ)) { echo $r->message; } What's confusing me is that the fetch method is a method within the PDOSTATEMENT class so how come an object hasn't been created before hand in order to use the method? Furthermore it appears the method is a static method within the PDOSTATEMENT class so how come the double semi colon is being used such as: PDOSTATEMENT::FETCH? Any help would be appreciated. Thanks.
  15. I won't go into the full details now but I would like someone to provide me some private training on php. Basically, I've been doing it now for several months through various online tutorials and videos but I'm not really getting anywhere. I will of course come to a financial agreement with you if your interested. Let me know and I'll discuss it with you properly if your interested in working with me. Please drop me a PM if you are. I live in the UK by the way.
  16. I have seen vacancies for 'php developers' on the jobs board quite a lot and nearly every time I see them requiring experience in one type of framework or another. Zend and Joomla seem to be quite popular. So I'm just thinking when web design agenices/studios are creating bespoke websites for consumers are they using a php framework to create for example a blog or a newsletter? Or are they creating php code from scratch and by hand each and everytime?
  17. I will go back to the basics too I just want to get this basic application complete so I have something to work with and play with. How shall I create the function to return the authors name?
  18. Ok I've literally been at this for about 2 hours. After some tweaks I've managed to get this error: Fatal error: Call to a member function getName() on a non-object in C:\xampp\htdocs\tutorials\basiccomment\index.php on line 34 This is because of this code: $comment->author->getName() and $comment->getContent(). If I change to this: $comment->author & $comment->comment Then I don't get the above "fatal error" instead it posts empty "name" and "commentcontent". If fact it posts ":;" on the html file. I think I'm starting to understand the theory and the logic behind it but I've not got it to work so I still haven't fully grasped it. I've been switching between recommended links, sites and tutorials but they do things slightly different from each other so it's hard to fit it into a real world application. I know I've done it wrong because it's not posting anything but I'm not sure what I need to do to put it right. I've been doing some changes here and there but I've still not got it to work. I think I missing a function somewhere but I'll post the code in full if anyone is kind enough to offer me any information. <?php class Comment { public $author; public $comment; } class user { } $User = new User; $comment = new Comment($User); // now to get the author of the comment, we'd echo $comment->author; if ($_POST) { // create our data pieces $user = new User($_POST ['name']); $commentContent = $_POST ['commentcontent']; $comment = new Comment($user, $commentContent); //not quite sue what this means /** * To answer this question, this is writing the data to a file called comments.html */ $handle = fopen("comments.html","a"); fwrite($handle, "<b>" . $comment->author . "</b>:<br/>" . $comment->comment . ",<br/>"); fclose($handle); } ?> <html> <body> <form action = "" method = "POST"> Comments: <textarea rows = "10" cols = "30" name = "commentcontent"></textarea><br/> name: <input type = "text" name = "name"<br/> <input type = "submit" value = "post!"><br/> </form> <?php include "comments.html"; ?> </body> </html>
  19. Hi Every. Over the last few days I've been working on my very basic comment box programme. I've purposly kept it basic so I can follow it. I managed to make the programme work using a tutorial without any kind of classes or objects but now I want to create the code in oop style. I've created a class which I think is right and within that class I have created 2 variables within it. I have also created 2 objects of that class however I don't think I've done it right. Can someone please have a look at it and point out where I need to make the amendments. <?php class Comment { private $author; private $comment; } $username = new comment (); $usercomment = new comment (); //if the form has been submitted then we want to execute some code in the brackets if ($_POST) { //below we have defined are variables. $username = $_POST ['name']; $usercomment = $_POST ['commentcontent']; //not quite sue what this means $handle = fopen("comments.html","a"); fwrite($handle, "<b>" . $username . "</b>:<br/>" . $usercomment . ",<br/>"); fclose($handle); } ?> <html> <body> <form action = "" method = "POST"> Comments: <textarea rows = "10" cols = "30" name = "commentcontent"></textarea><br/> name: <input type = "text" name = "name"<br/> <input type = "submit" value = "post!"><br/> </form> <?php include "comments.html"; ?> </body> </html>
  20. T I have already done that. I have created a few things in PHP and learned it's way of doing certain things like connecting to a database, echoing out messages and so on. I just feel I'm not learning anything solid because the tutorials I have seen different people do the same thing in different ways. I understand what they are doing and why they are doing it but they don't seem to be working within a set structure. It all just seems like random code glued togther. If I could find a way of doing things within a structure I feel I would learn a lot better. Yes I appreciate it doesn't need classes but I thought because it's so small and limited may be that would be a good way of learning how OOP works. It's purely for a learning excersise that's all. If this is not a good example to work on is there anything else that I can work on within PHP that I can create in OOP style? Nothing to complicated but something that I can learn from by practising myself.
  21. Hi Everyone. I am new to the world of coding in PHP and I am currently doing some practise stuff. At the moment I have a mysql database set up and I have a very basic html site. I want to place a simple comment box onto the website where someone leaves their name and their comment and it submits. Later I want to be able to log in as an administrator and manage the comments but for now I just want to create the form, create the php script and have the details stored in my database. This is turning out to be more difficult than I thought it would be. I checked out some tutorials but I've been told not to use their code because it's either badly written or it's not done in OOP style. I have been doing some reading on OOP and the theory behind it. This is what I understand of it so please correct me if I'm wrong. 1. Objects belong to classes. 2. The classes define the objects properties and methods (functions). 3. Each object may contain different values of properties and methods but can belong to the same class. 4. Each property may have it's own methods (functions). Is this correct? What I would like to do is create a comment box script so users can leave comments on my site but create the script in OOP style. I'm struggling to do this because I don't know how I would first create the class. Can someone please help me and tell me how I should create the class? Thanks.
  22. I have a simple newsletter sign up script that I'm using. I built it using a tutorial and because it's simple and basic I'm able to tweak it here and there as and when I need to. There are some messages such as "?? is already on the system" and "please enter an email address". All these work fine however when I display certain messages such as "is already on the sytem" or "you have been added sucessfully" I want the text boxes to disappear. I've been looking online and I haven't found anything which is most likely because I don't know the correct terminology of what I'm looking to do. I only what the boxes to dissappear when certain messages are display. Can someone please help me with this or direct me to a tutorial. Here is the script: <?php $name = ""; $email = ""; $msg_to_user = ""; if (isset($_POST['name']) && $_POST['name'] != "") { include_once "connect_to_mysql.php"; // Be sure to filter this data to deter SQL injection, filter before querying database $name = $_POST['name']; $email = $_POST['email']; $sql = mysql_query("SELECT * FROM newsletter WHERE email='$email'"); $numRows = mysql_num_rows($sql); if (!$email) { $msg_to_user = '<br /><br /><h4 class="header">Please type an email address ' . $name . '.</h4>'; } else if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $msg_to_user = '<br /><br /><h4><font color="FF0000">invalid email address.</font></h4>'; } else if ($numRows > 0) { $msg_to_user = '<br /><br /><h4><font color="FF0000">' . $email . ' is already in the system.</font></h4>'; } else { $sql_insert = mysql_query("INSERT INTO newsletter (name, email, dateTime) VALUES('$name','$email',now() )") or die (mysql_error()); $msg_to_user = '<br /><br /><h4>Thanks ' . $name . ', you have been added successfully.</h4>'; $name = ""; $email = ""; } } ?> </div> <html> <head> <style> .header { color:#D2691E; background-color: #000000; } </style> <title>Subscribe to Our Newsletter</title> </head> <body> <form style="width:440px;" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <fieldset style="text-align:left; padding:24px;"> <legend>Subscribe to Our Newsletter </legend> <br /> Name:<br /> <input name="name" type="text" maxlength="36" value="<?php echo $name; ?>" /><br /> Email:<br /> <input name="email" type="text" maxlength="36" value="<?php echo $email; ?>" /><br /><br /> <input name="mySubmitBtn" type="submit" value="Submit"> <?php echo $msg_to_user; ?> </fieldset> </form> </body> </html> By the way I tried: ****************************************************************************************************************************** <body> <form style="width:440px;" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <fieldset style="text-align:left; padding:24px;"> <legend>Subscribe to Our Newsletter </legend> <br /> <?php if($msg_to_user !=''){ ?> Name:<br /> <input name="name" type="text" maxlength="36" value="<?php echo $name; ?>" /><br /> Email:<br /> <input name="email" type="text" maxlength="36" value="<?php echo $email; ?>" /><br /><br /> <input name="mySubmitBtn" type="submit" value="Submit"> <?php } ?> <?php echo $msg_to_user; ?> </fieldset> </form> </body> ****************************************************************************************************************************** but it doesn't work. All that happens is when the page loads up the sign up boxes are not there. Any help will be much appreciated as I've been trying to find a solution for this over the last 3 days. Thanks in advance.
×
×
  • 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.