Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP OOP approach difficultiues
    primarykey
    data
    text
    <p><strong>Please see edit below</strong></p> <p><strike>I'm trying to dive into OOP development in PHP and i'm really starting to develop a headache or ulcer. I just can't wrap my head around it, parts seem too illogical to me, and i have no clue where to start and it really frustrates me because i believe it's worth the effort to try to learn it and it provides my a better overview about my code. </p> <p>Yesterday i spent the whole day searching for practical examples and explanational articles on the internet, but now i have the feeling that only confuses me more. I need some practical tips, not examples like</p> <p><pre><code> class person { var $name; function set_name($new_name) { $this->name = $new_name; } function get_name() { return $this->name; } } $stefan = new person(); $jimmy = new person;</p> <p>$stefan->set_name("Stefan"); $jimmy->set_name("Jimmy");</p> <p>echo "Stefan's full name: " . $stefan->get_name(); echo "Jimmy's full name: " . $jimmy->get_name(); </pre></code></p> <p>Examples like this makes me question why i shouldn't (because it's all defined in the same page) just do</p> <pre><code>$name = "Stefan"; echo "Stefan's full name: ".$name;</code></pre> <p>Shorter (less code) and more obvious if you ask me?</p> <p>Nou to my project.</p> <p>The thing i'm trying to make is a simple portfolio website. Herefore i only need 4 pages, a <em>home page</em>, <em>information</em> page, a page about <em>my work</em> and a <em>contact</em> form. I use three tables <code>clients</code>, <code>content</code> and <code>media</code> which i want to use on the <em>my work</em> page.</p> <p>I was thinking about my structure and came up (in my head, no clue how to achieve it) with something like</p> <p><pre><code>class Database{} class Content extends Database{} function showMenu(){} function showContent{} class Clients extends Database{} function showClientList{} function showClientDetails{} class Media extends Database{}</pre></code></p> <p>I already made a database class that establishes a database connection with a public method <code>query($qry)</code>. I started with the <code>Content</code> class and it looks like this at the moment:</p> <pre><code> class Content extends Database { public function topMenu($page){ $db = new Database(); $db->connect(); $db->query("SELECT m_link, m_title, m_accesskey FROM menu WHERE m_type = 'top'"); $res = $db->getResult(); foreach($res AS $show){ if($page == $show['m_link']){ echo("&lt;li id="active"&gt;$show['m_title']."&lt;/li&gt;\n"); }else{ echo("&lt;li&gt;$show['m_title']."&lt;/li&gt;\n"); } } } }</code></pre> <p>Does this make any sense at all by the way?</p> <p>On my index.php i have the following lines to display the menu (which work fine) <code>$content = new Content(); $content->topMenu($_aGET[0]);</code></p> <p>The point where i'm loosing it is the way top display either the <em>client list</em> or the <em>client details</em>, which depends on the variable <code>$_aGET[0]</code> (which holds the url)</p> <p>Suppose i want to display the client details, herefore i need records from all three the tables, so normally i would use a couple of join's, like so</p> <pre><code>$query = " SELECT cl.c_id, cl.c_client, cl.c_client_desc, ncc.clco_cl_id, ncc.clco_co_id, co.c_content, ncm.clme_me_id, ncm.clme_cl_id, GROUP_CONCAT(me.m_link) AS images_link FROM clienten AS cl LEFT JOIN norm_cl_co_rel AS ncc ON cl.c_id = ncc.clco_cl_id LEFT JOIN content AS co ON ncc.clco_co_id = co.c_id LEFT JOIN norm_cl_me_rel AS ncm ON cl.c_id = ncm.clme_cl_id LEFT JOIN media AS me ON me.m_id = ncm.clme_me_id WHERE cl.c_client = '".mysql_real_escape_string($_aGET[1])."' AND cl.c_language_id = '"._LANGUAGE."' AND cl.c_active = '1' ";</code></pre> <p>But as far is i understand the OOP thoughts, i should have a method for each tables defined in a class (right?) But how am i going to join these?</p> <p>Or does my 'story' looks something like: 'Dude, just drop it and stick with procedural coding'?</strike></p> <p><strong>EDIT:</strong></p> <p>Ok, after Fluffeh's post i used his example and modified it a bit to test it. The problem is that i do get some output, but not the desired. My output is like below</p> <pre>ID: CompanyName C Desc: C Media: : : : : :</pre> <p>while i should get (values are in the database):</p> <pre>ID: CompanyName CompanyName Desc: CompanyName is a company Media: : Image 1 : Image 2 : Image 3 : Image 4 : Image 5 </pre> <p>My code looks like so:</p> <pre><code>class media{ public $type; public $title; public $image; public $desc; } class client{ public $name; public $desc; } class clientDetails{ private $clientID; public $clientName; public $clientDesc; public $clientMedia = array(); public $clientMediaFiles = 0; public function __construct($myID){ $this-&gt;clientID = $myID; } public function getMyDetails(){ $db = new Database(); $db-&gt;connect(); $db-&gt;query(" SELECT c_client, c_client_desc FROM clienten WHERE c_client = '".mysql_real_escape_string($this-&gt;clientID)."' "); foreach($db-&gt;getResult() as $client){ $this-&gt;name = $client['c_client']; $this-&gt;desc = $client['c_client_desc']; } $db = new Database(); $db-&gt;connect(); $db-&gt;query(" SELECT ncm.clme_me_id, ncm.clme_cl_id, cl.c_id, me.m_id, me.m_type, me.m_title, me.m_desc, me.m_link FROM norm_cl_me_rel AS ncm LEFT JOIN clienten AS cl ON cl.c_id = ncm.clme_cl_id LEFT JOIN media AS me ON me.m_id = ncm.clme_me_id WHERE me.m_id IN(1,2,3,4,5) "); foreach($db-&gt;getResult() as $media){ $this-&gt;clientMedia[$i]= new media(); $this-&gt;clientMedia[$i]-&gt;type = $media['m_type']; $this-&gt;clientMedia[$i]-&gt;title = $media['m_title']; $this-&gt;clientMedia[$i]-&gt;image = $media['m_image']; $this-&gt;clientMedia[$i]-&gt;desc = $media['m_desc']; $this-&gt;clientMediaFiles++; } } public function displayMyResults(){ echo "ID: $this-&gt;clientID"; echo "&amp;lt;div&gt;&amp;lt;h3&gt;".$this-&gt;name."&amp;lt;/h3&gt;"; echo "Desc: ".$this-&gt;desc."&amp;lt;br&gt;"; echo "Media:&amp;lt;br&gt;"; for($i=0;$i&lt;$this-&gt;clientMediaFiles;$i++){ echo $this-&gt;clientMedia[$i]-&gt;title." : ".$this-&gt;clientMedia[$i]-&gt;desc."&amp;lt;br&gt;"; } echo "&amp;lt;/div&gt;"; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload