If statement not fully functional?

I wouldn’t recommend CodeIgniter just yet. If you don’t understand OO yet, it is going to be super hard for you to use CodeIgniter because the entire use is in OO. Instead, what I am proposing is a way to separate your stuff in a way that it “kind of” acts like MVC or the idea anyways.

Take this simple sample snippet for example.

<?php
$variable = 'This variable contains some random information that should be passed down.';

require_once('index_view.php');
<!DOCTYPE html>
<html>
<head>
<title>Here is a random file</title>
</head>

<body>
<h1>Welcome!</h1>
<p>Tell me what is in the <strong>$variable</strong> variable.
<pre><?php print($variable); ?></pre>
</body>
</html>

The first snippet should be where the user lands. So if it was the index page, the first snippet would be called index.php. So what this really achieves is that it makes everything easier for you to read. Do you see the difference between yours and this snippet? I’m also not talking about length wise. I am talking about readability.

The index.php file acts like a controller and “tries” to mimic the idea of MVC, but it isn’t actually MVC at all. By doing this, you separate your business logic from your presentation. I am assuming you don’t know the ideology of MVC. MVC is just a way to separate your logic. There are 3 parts to this ideology. M stands for Models which should only contain your database stuff. V stands for Views which contains your presentation or your visual stuff. So in your views, you’ll mostly see a lot of HTML stuff. And C stands for Controllers which is the main place you’ll be doing your processing. You could put PHP code in your Views if you want, but you want to limit the amount of PHP codes that go into your Views. If you have data processing, that should never go in your views, this should actually be in your controllers.

So why am I telling you this? Because the way I am proposing to you now “tries” to mimic this in a procedural way. Short of it using OO and re-usability, it in a way does feel like MVC. That is the only reason why I mentioned MVC to you. Otherwise, I wouldn’t because you don’t understand basic logic yet. Jumping right into MVC will confuse you so much more.

You may think that this way is redundant or unnecessary, but it actually isn’t. By doing it this way, you allow yourself the ability to maintain clean code and you allow yourself to work in a logic flow that is easy for you to follow.

Let’s take another simple sample snippet for example.

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {

	// Let's do our validations here.
	print_r($_POST);

} else {

	$variable = 'This variable contains some random information that should be passed down.';

	require_once('index_view.php');

}
<!DOCTYPE html>
<html>
<head>
<title>Here is a random file</title>
</head>

<body>
<h1>Welcome!</h1>
<p>Tell me what is in the <strong>$variable</strong> variable.
<pre><?php print($variable); ?></pre>
<form action="" method="POST">
	<input type="text" name="random" placeholder="Random" autocomplete="off"><br>
	<button type="text">Submit</button>
</form>
</body>
</html>

So in these two snippets, we are processing user input. All we have actually done that’s different from the first 2 sample snippet is adding in a form. The idea is the same. We use the first sample snippet as a controller. The first step we do is check to see if the page was requested via POST. If it isn’t, then we will include our index_view.php file. If the form was submitted, we then do a print_r($_POST) to display what was posted. You usually don’t want to output anything in your controllers. That should always be handled by your Views.


So why is this important to your problem? Because again, it relates to your logic. The reason why you aren’t getting the results you want is because of the way you set your codes up. You are using a bunch of random functions that really are unnecessary. Functions should really only be used for returning data or doing something in pure PHP. If all you are trying to do is include the header. You could simply do this by including it using require or include. It’s that simple.

There are a lot of other things that is wrong with your code, but one step at a time.

3 Likes