?
Solved

Did I write this OOP class correctly?

Posted on 2013-01-21
5
Medium Priority
?
253 Views
Last Modified: 2013-01-25
Here's my code:

<?php
class SongwritingWinner {
public $the_winner_date;
public $the_winner_type;

	function the_winner(){
	$bruce = "select * from songwriting_winners where contestant_id='_GET[id]' order by winner_date";
	$bruce_query=mysqli_query($cxn, $bruce);
	$bruce_count=mysqli_num_rows($bruce);
		if($bruce_count>1)
		{
		$michelle= "select * from songwriting_winners where contestant_id='$_GET[id]' order by winner_date DESC LIMIT 1";
		$michelle_query=mysqli_query($cxn, $michelle);
		$michelle_row=mysqli_fetch_assoc($michelle);
		$this->the_winner_date=date("m/d/Y", strtotime($michelle_row['winning_date']));
		$this->the_winner_type = $michelle_row['winner_type'];
		}
		if($bruce_count>0 and $bruce_count<2)
		{
		$michelle= "select * from songwriting_winners where contestant_id='$_GET[id]' order by winner_date DESC LIMIT 1";
		$michelle_query=mysqli_query($cxn, $michelle);
		$michelle_row=mysqli_fetch_assoc($michelle);
		$this->the_winner_date=date("m/d/Y", strtotime($michelle_row['winning_date']));
		$this->the_winner_type = $michelle_row['winner_type'];
		}
		if(!$bruce_count>0)
		{
		$this->the_winner_date=date("m/d/Y");
		$this->the_winner_type="";
		}
	}

}

?>

Open in new window


This is my first crack at OOP and I want to think that I'm close, but I'm not there yet. Reason being is that when I go to instantiate my class and attempt to retrieve a variable, I get nothing.

Here's the way I'm calling the class...

	<?php 
							require_once('classes/songwriting_class.php'); 
							$dean = new SongwritingWinner();
							echo $dean->the_winner_date;
							?>

Open in new window


No errors, but no data. What am I doing wrong?
0
Comment
Question by:brucegust
5 Comments
 
LVL 21

Expert Comment

by:mastoo
ID: 38803811
I'm not a php person, but it looks like you have a class with a method called the_winner.  You haven't called that method anywhere.  That method seems to initialize the_winner_date, so if you don't call it you're not going to get a meaningful value.
0
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 668 total points
ID: 38805145
Whe you initialize a new class there is a method called __construct,
and it does evertything defined inside. Initializing class with only a new Class does nothing, it just creates the object of that class.

Also:
$bruce_query=mysqli_query($cxn, $bruce);
since $cxn is defined somewhere else, you need to pass this cxn variable into the class

Take a look at this example:

<?php
error_reporting(E_ALL);

class SongwritingWinner {
    
public $the_winner_date;
public $the_winner_type;


function __construct(){
    $this->the_winner();
}

function the_winner(){
$bruce = "select * from songwriting_winners where contestant_id='_GET[id]' order by winner_date";
$bruce_query=mysqli_query($cxn, $bruce);
$bruce_count=mysqli_num_rows($bruce);
if($bruce_count>1)
{
$michelle= "select * from songwriting_winners where contestant_id='$_GET[id]' order by winner_date DESC LIMIT 1";
$michelle_query=mysqli_query($cxn, $michelle);
$michelle_row=mysqli_fetch_assoc($michelle);
$this->the_winner_date=date("m/d/Y", strtotime($michelle_row['winning_date']));
$this->the_winner_type = $michelle_row['winner_type'];
}
if($bruce_count>0 and $bruce_count<2)
{
$michelle= "select * from songwriting_winners where contestant_id='$_GET[id]' order by winner_date DESC LIMIT 1";
$michelle_query=mysqli_query($cxn, $michelle);
$michelle_row=mysqli_fetch_assoc($michelle);
$this->the_winner_date=date("m/d/Y", strtotime($michelle_row['winning_date']));
$this->the_winner_type = $michelle_row['winner_type'];
}
if(!$bruce_count>0)
{
$this->the_winner_date=date("m/d/Y");
$this->the_winner_type="";
}
}

}


$dean = new SongwritingWinner();
echo $dean->the_winner_date;
?>

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38805147
I would recommend a brilliant series of OOP tutorials here:
http://jream.com/learning/videos/php-oop
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 664 total points
ID: 38805156
Sigh.  I wonder how many times I have asked developers to set error_reporting(E_ALL) in the top of all of their scripts, every time, without exception.  If I had a dollar for every time, I would be somewhere on a sunny beach right now.  Bad error reporting is one of the worst things about PHP because it makes you miss even the simplest typographical errors, like this one:

contestant_id='_GET[id]'

Maybe this is more on target?

contestant_id=$_GET['id']

For starters, you might want to read the PHP man pages on the subject, showing here:
http://php.net/manual/en/language.oop5.php

Try this code and see what you get.  Var_dump() is your friend as you explore the new data structures.

<?php 
require_once('classes/songwriting_class.php'); 
$dean = new SongwritingWinner();
var_dump($dean);

Open in new window

0
 
LVL 34

Accepted Solution

by:
Slick812 earned 668 total points
ID: 38807431
greetings : brucegust, , I looked at your code for the SongwritingWinner Class thing, , and you seem to have moved ahead of your understanding of Class structure and how Class properties (variables) in your code are relevant to what you do in your
           function the_winner(){
code, you seem to have this function as a "requirement" to place any Values in your -
public $the_winner_date;
public $the_winner_type;

But you have this line -
$bruce_query=mysqli_query($cxn, $bruce);

which I would think you copy and pasted out of NON-CLASS code work of yours, this has absolutely NO Chance to work because the $cxn  has never been connected to the MySQL server, $cxn has nothing (no value, unset) in it, when you use it.

but even as bad you never call (use) the the_winner() method as -

$dean = new SongwritingWinner();
$dean->the_winner(); // would ERROR OUT on this line
echo $dean->the_winner_date;

I would suggest that starting out wid Class you always initialize your Class properties -
class SongwritingWinner {
public $the_winner_date = "Set as Not Yet set";
public $the_winner_type = "Start Value";


that way you can tell if they Have been set to a new value properly, or are still in the Default value when calling -
echo $dean->the_winner_date;

although there are many errors in your code you should look closer at some online examples for CLASS and start with Class code that works, and then add and modify that working code to do your database SELECT SQL operation.
please do not be discouraged that you do not get the Class thing at first, almost all that start in with Class coding are lost at first.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question