Moving To Object Orientated Programming Please keep in mind I am new and still learning when reading the following.

What I am doing

I have the following code which pulls a sport, tournament and round NR, from a DB table called event where the event is still active.

The Problem

The code works and does what I want it to do, but my problem is looking at the code makes me sick, I know there are more efficient ways to achieve what I am trying to do but I am unsure of where to start to improve the code below, I would like to move to a more object orientated or at least a more efficient way of coding.

I would appreciate it if one of the more experienced members of the community could give the code a look and provide some pointers.



$date = date('Y-m-d');
//get sport &amp; tournament
$sql = "Select distinct sport, tournament, round FROM event WHERE date &gt; $date AND active = 'y'";
$result = mysqli_query($conn,$sql) or die(mysqli_error($conn)); 
while($row = mysqli_fetch_array($result)){
    $sport[] = $row['sport'];
    $tournament[] = $row['tournament'];
    $round[] = $row['round'];
}
//make form with tournament &amp; sport
?&gt;
&lt;form name="select" name="sport" method="post"&gt;
&lt;!--GET SPORT ON SELECT  --&gt;
&lt;select name="sport"&gt;
&lt;?php
//get sport
foreach($sport as $index =&gt; $sportCode){
    echo '&lt;option value="'. $sport[$index].'"&gt;'.$sport[$index].'&lt;/option&gt;'; 
}
?&gt;
&lt;/select&gt;
&lt;!--GET TOURNAMENT ON SELECT  --&gt;
&lt;select name="tournament"&gt;
&lt;?php
//get tournament
foreach($tournament as $index =&gt; $tournamentCode){
    echo '&lt;option value="'. $tournament[$index].'"&gt;'.$tournament[$index].'&lt;/option&gt;'; 
}
//get round
?&gt;
&lt;/select&gt;
&lt;select name="round"&gt;
&lt;?php
foreach($round as $index =&gt; $roundNr){
    echo '&lt;option value="'. $round[$index].'"&gt;'.$round[$index].'&lt;/option&gt;'; 
}
//get round
?&gt;
&lt;/select&gt;
&lt;/form&gt;
TL;DR: Read about MVC and understand the basics and then learn laravel

This is not about object oriented approach but more like a separation of concerns thing. You have two concerns for the moment:


You're collecting some data
You're displaying it


so what you have to do is to separate them, start by using a template system like twig or smarty. Collect the data in some php and pass it to the template file and render it so the html code and php code will be separated. 

The template will be responsible of "how" you display the data and your php file will be responsible of "preparing the data" for the template.



Second step is to separate the code that is "dealing with the data" from the code that is "setting up the template". You can move the data related code to some class so that you'll have smt. like this in the end:

&lt;?php 

// Require classes, setup things etc.

$obj = new Events;
$events = $obj-&gt;getAllEvents();

echo $tpl-&gt;render('events/index.tpl', array('events' =&gt; $events));




Third step may be about the // Require classes, setup things etc. part in the example above. You can use a front-controller to deal with that...

... and the list goes on...

After some struggling you'll start to realize that you need some helper classes to deal with the http requests, responses, sessions etc.

...and you'll need more and more "concerns" to "separate" in the future and in the end you'll understand why people are using mature frameworks these days.

I was writing like this in 1997-1998 (no kidding) because I had to, but you don't :)