Jump to content

gsingh85

Members
  • Posts

    21
  • Joined

  • Last visited

gsingh85's Achievements

Member

Member (2/5)

0

Reputation

  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.
×
×
  • 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.