[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Did I write this OOP class correctly?

Posted on 2013-01-21
5
Medium Priority
?
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

649 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