gansai Posted June 27, 2018 Share Posted June 27, 2018 Segment.php <?php $mysqli = mysqli_connect("localhost","root","","squarellt"); $cid = required_param('id', PARAM_INT); class segment { public function unitseg_set1() { $subject = $mysqli->query('SELECT * FROM order_subject WHERE id='.$cid.''); while($row=$subject->fetch_array() ) { echo '<div>'.$row['chem_name'].'</div>'; } } public function unitseg_set2() { $subject = $mysqli->query('SELECT * FROM order_subject WHERE id='.$cid.''); while($row=$subject->fetch_array() ) { echo '<div>'.$row['physiotherapy_nn'].'</div>'; } } public function unitseg_set3() { $subject = $mysqli->query('SELECT * FROM order_subject WHERE id='.$cid.''); while($row=$subject->fetch_array() ) { echo '<div>'.$row['commun_gg'].'</div>'; } } } ?> home_segment.php <?php require_once('segment.php'); $acc = new segment(); $account1 = $acc->unitseg_set1(); $account2 = $acc->unitseg_set2(); $account3 = $acc->unitseg_set3(); echo $account1; echo $account2; echo $account3; ?> I was trying to pullout the content of all the functions defined from the class segment in segment.php. And calling all the three functions from a class segment in home_segment.php Is this a correct way of calling multiple functions from one class. Or suggest with good OOP's concept. How could I improve the above similar functionality in Object Oriented Programming. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 27, 2018 Share Posted June 27, 2018 You have (at least) two bits of extra reading to do Variable scope - your segment class is completely unaware of the $mysqli and $cid variables. (I would pass the $mysqli var as a constructor argument and pass the $cid in the calls to the methods. Using functions - your functions do not return anything, so $account1 etc will not contain anything. Make your functins return the values instead of echoing. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 27, 2018 Share Posted June 27, 2018 No, that's really not the way to do it. 1. The code won't work. That's the biggest problem. 2. You're using a class that doesn't actually represent anything. It's simply a place you put a few functions. That is not OOP. 3. You're executing the exact same query multiple times. Doesn't that seem wasteful to you? 4. The functions that retrieve data also display it. If you wanted to display it differently then you would have to make even more functions. Having to repeat yourself like that is almost always a sign of larger a problem. 5. You act like the functions will return a value. They do not. Here is the non-object oriented answer. Because you have to understand the simple concepts (PHP functions) before you can understand the complex concepts (OOP). And because making this part object-oriented is overkill; if you had much more code then it might make more sense to do it. You have a query that needs to be executed. The data returned from the query can be used multiple ways, but the query itself is the same. Make that a function. Functions cannot use variables defined outside them, so you must pass what you need to it as arguments. And since this function will be used for multiple purposes, it must return the data - no HTML or other processing, just the data itself. <?php function unitseg($mysqli, $cid) { $subject = $mysqli->query('SELECT * FROM order_subject WHERE id = ' . (int)$cid); return $subject->fetch_all(MYSQLI_ASSOC); } ?> You call this function using the $mysqli and $cid that the other code had prepared. <?php require_once('segment.php'); $mysqli = mysqli_connect("localhost", "root", "", "squarellt"); $cid = required_param('id', PARAM_INT); $accounts = unitseg($mysqli, $cid); ?> It will return an array of rows from the results which you can foreach over. For example, <?php // ... foreach ($accounts as $row) { echo '<div>', $row['chem_name'], '</div>'; } ?> Quote Link to comment Share on other sites More sharing options...
gansai Posted June 27, 2018 Author Share Posted June 27, 2018 Thank You for the response. I'm looking for OOP concept, where I have a function with different queries. Since this is a public post, I am trying to give a sample query. I need to execute one/more functions within a class and each function has to track records from database. Now I can call this class, a particular function in any file. Kindly do not use the keywords like "wasteful". I was trying to learn something technical. May be you're high developers, just motivate your skills to people. Many Thanks. Quote Link to comment Share on other sites More sharing options...
gansai Posted June 27, 2018 Author Share Posted June 27, 2018 25 minutes ago, Barand said: You have (at least) two bits of extra reading to do Variable scope - your segment class is completely unaware of the $mysqli and $cid variables. (I would pass the $mysqli var as a constructor argument and pass the $cid in the calls to the methods. Using functions - your functions do not return anything, so $account1 etc will not contain anything. Make your functins return the values instead of echoing. I agree segment class is unware of $mysqli. Could you suggest with code by taking a single function in a class. Many Thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted June 27, 2018 Share Posted June 27, 2018 (edited) perhaps <?php class segment { private $dblink; public function __construct($dblink) { $this->dblink = $dblink; } public function get_chem_name($cid) { $subject = $this->dblink->query('SELECT chem_name FROM order_subject WHERE id='.$cid.''); return $subject->fetch_all(MYSQLI_ASSOC); } public function get_physio($cid) { $subject = $this->dblink->query('SELECT physiotherapy_nn FROM order_subject WHERE id='.$cid.''); return $subject->fetch_all(MYSQLI_ASSOC); } public function get_commun($cid) { $subject = $this->dblink->query('SELECT commun_gg FROM order_subject WHERE id='.$cid.''); return $subject->fetch_all(MYSQLI_ASSOC); } } /** * output array as a series of divs * * @param array $data */ function output_results($data) { foreach ($data as $val) { echo "<div>$val</div>"; } } /** * get and output the data */ $mysqli = mysqli_connect("localhost","root","","squarellt"); $cid = required_param('id', PARAM_INT); $acc = new segment($mysqli); output_results ($acc->get_chem_name($cid)); output_results ($acc->get_physio($cid)); output_results ($acc->get_commun($cid)); ?> Don't use "select star", specify what columns you want. No need to slow down the query by transferring a shedload of data you don't need. Use meaningful names - it makes life easier. EDIT: PS - I would also recommend you use prepared statements instead of placing variables directly into the sql string. And use PDO instead of mysqli - that too makes life much easier. Edited June 27, 2018 by Barand 1 Quote Link to comment Share on other sites More sharing options...
maxxd Posted June 30, 2018 Share Posted June 30, 2018 Now, combine the advice you've gotten here with the advice you've gotten at your phpbuilder thread and I should think you'll be able to figure it out quite quickly. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.