Solved

Did I write this OOP class correctly?

Posted on 2013-01-21
5
241 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 167 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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 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 33

Accepted Solution

by:
Slick812 earned 167 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now