Doing something like this is mostly a style choice?

<?php
class IndexPage{
	
function drawIndex(){
	try{
	$db = new Database();
	$sql = $db->prepare('SELECT name AS restaurant, id, description, imagePath FROM restaurant');
	$sql->execute();
	
	echo "<div class='flex-grid'>";
	echo "<div class='header'>
	<section>
			<h1>Eating out in Kirkcaldy</h1>
			<span class='para'><p>Explore Kirkcaldy's local restaurants find deals on your favourite international cuisine</p></span>
	</section>
	
	</div>";
	
	echo "<div class='restaurants'>
	<section class='kirkcaldy'>
	<h1>Eating out in Kirkcaldy</h1>
	<p>
		Explore restaurants in Kirkcaldy boasts a verity international cuisine from around the world and local traditional meals, 
		from traditional fish & chips, Indian, Nepalese,
		Turkish and Japanese street food Kirkcaldy has it all.
	</p>
	</section>
	</div>";
	
	echo "<div class='restaurant-heading'><h1>Restaurants</h1></div>";
	echo "<div class='flex-grid-shops'>";
		foreach($sql as $rows){
		echo "<div class='rows'>";
		echo "<div class='shop-img'>";
		echo "<a href='restaurant.php?id={$rows['id']}'><img src='{$rows['imagePath']}'></a>";
		echo '</div>';
		echo "<section class='description'>";
		echo "<a href='restaurant.php?id={$rows['id']}'>";
		echo	"<h1>". $rows['restaurant']."</h1>";
		echo	"<p>". $rows['description']."</p>";
		echo "</a>";
		echo '</section>';
		echo '</div>';
		}
	echo "</div>";

	echo "</div>";
	}
	catch (PDOException $e) {
	echo 'Connection failed: ' . $e->getMessage();
	}
}

}

    I'd be more likely to use a heredoc than lots of echo statements.

    And ... I dunno, I might have the database query separate from generating the output.

    Have you looked into either a framework or a templating language? Personally, I'm a big fan of Twig for templates if you're spinning your own PHP or can choose an output vector. As far as frameworks are concerned, I actually like the new CodeIgniter (please don't judge me - I think it's got a lot of the functionality of other modern frameworks without being so opinionated or bloated) but I'm currently working with Laravel, which is a thing a lot of people use.

    Either way, separating your business logic and display is a good thing - you don't have to go full MVC, but it does make life easier once you get used to it. For instance, in your setup if you need to change the heading level of something you risk breaking the functionality of the data flow instead of just the display because the data gathering SQL is in the same function as the HTML output. If the output is in a separate file there's far less danger in making a change to that output - and you can use the gathered data anywhere else you might need it (exporting, pdf, etc).

    Rather than Google, here's a direct line to the manual. Heredoc

    echo <<<EOF
    <div class='flex-grid'>
    <div class='header'>
    <section>
    		<h1>Eating out in Kirkcaldy</h1>
    		<span class='para'><p>Explore Kirkcaldy's local restaurants find deals on your favourite international cuisine</p></span>
    </section>
    
    </div>
    <div class='restaurants'>
    <section class='kirkcaldy'>
    <h1>Eating out in Kirkcaldy</h1>
    <p>
    	Explore restaurants in Kirkcaldy boasts a verity international cuisine from around the world and local traditional meals, 
    	from traditional fish & chips, Indian, Nepalese,
    	Turkish and Japanese street food Kirkcaldy has it all.
    </p>
    </section>
    </div>
    
    <div class='restaurant-heading'><h1>Restaurants</h1></div>
    <div class='flex-grid-shops'>
    EOF;
    
    foreach($sql as $rows) {
    	echo <<<EOF
    <div class='rows'>
    <div class='shop-img'>
    <a href='restaurant.php?id={$rows['id']}'><img src='{$rows['imagePath']}'></a>
    </div>
    <section class='description'>
    <a href='restaurant.php?id={$rows['id']}'>
    <h1>{$rows['restaurant']}</h1>
    <p>{$rows['description']}</p>
    </a>
    </section>
    </div>
    EOF;
    }
    echo "</div></div>";
    

    Weedpacket Ohh, I did try to indent it but that seems to be a bad idea.

    maxxd

    Have you looked into either a framework or a templating language? Personally, I'm a big fan of Twig for templates if you're spinning your own PHP or can choose an output vector. As far as frameworks are concerned, I actually like the new CodeIgniter (please don't judge me - I think it's got a lot of the functionality of other modern frameworks without being so opinionated or bloated) but I'm currently working with Laravel, which is a thing a lot of people use.

    Heard of some of them never used yet, although I did install Laravel

    I'll leave the MVC a bit longer right now I just wrapped my head around basic autoloading...well I think I done it right

    Weedpacket and maxxd

    I guess you both meant put my query in a different class and extend it?

    I'd say it doesn't necessarily need to be a separate class; just a separate method in the current class is a step in the right direction. Think of it this way - you're outputting a list of local restaurants in HTML. If the requirements change and suddenly you need to output the list in a PDF, you'll need to duplicate the data selection functionality in another method that outputs the PDF. Now, a few months to years later you decide to add established date to the listings - you'll have to update the query in two places instead of just the one. Separating the responsibilities like this also creates smaller methods, and - though I can only speak for my own experiences and preferences here - smaller methods are easier to reason about and debug when necessary.

    As to frameworks, there's no need to rush into using one unless it's dictated by your job. I will say I find Laravel somewhat frustrating at times due to the sheer amount of magic that happens with it, and the heavy reliance on facades. Now, having said that it is a nice overall developer experience once you get your head around it a bit.

    maxxd Makes sense, I'll give it a try...starting to think I should have used constructors

    My job? lol I still haven't really applied for anything yet

      If that's the case, you may want to go ahead and take a look at Laravel. I just went through about a year of job hunting and the number of listings specifying Laravel is kinda shocking, actually. Of course, it's always possible that the people don't know a thing about PHP frameworks and just assume "Laravel" is synonymous with "framework", but about 90% of my interviews and code challenges involved Laravel.

        Weedpacket Indenting? I didn't do any of that.

        Yeah - I'm also a bit confused about that...

          Write a Reply...