Did I write this OOP class correctly?

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?
brucegustPHP DeveloperAsked:
Who is Participating?
 
Slick812Connect With a Mentor Commented:
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
 
mastooCommented:
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
 
Lukasz ChmielewskiConnect With a Mentor Commented:
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
 
Lukasz ChmielewskiCommented:
I would recommend a brilliant series of OOP tutorials here:
http://jream.com/learning/videos/php-oop
0
 
Ray PaseurConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.