So I was wondering what's the best way/practise of querying the database in a function and then printing the results back to the .php
page. I got taught to do the query in the function()
, call the function()
inside the .php
page and then loop through the resultset inside the .php
page.
Here is an example of what i'm doing now - Im wondering if this is "Unsafe" or "Not secure" or slower than returning the results in the function? Because at the moment - This is taking a while to load on the page? Could this be because I'm doing using if statements inside the while loop
?
showPlaylist($staticTestUser);
if($numRecords == 0){
echo "<div class='no-found'>No playlists found</div>";
}
else{
$htmloutput = "<div class='playlist-wrap'>";
$i = 1;
while($arrRows = $stmt->fetch(PDO::FETCH_ASSOC)){
$title = $arrRows['song-title']
$thumbnail=$arrRows['thumb']
$playTitle = $arrRows['playlistName'];
$htmloutput .= "<div class='playlist-head'>";
//Make sure the title is only shown once - Loop through the rest.
if($i == 1){
$htmloutput .= $playTitle;
}
$htmloutput .= "</div>";
$htmloutput .= "<div class='playlist-item'>";
$htmloutput .= "<div class='play-thumb'>".$thumbnail."</div>";
$htmloutput .= "<div class='play-title'>".substr($title, 0,30)."...</div>";
$htmloutput .= "</div>";
if($i == $numRecords){
$playId = $arrRows['pID'];
$htmloutput .= "<div class='playlist-footer'>";
$htmloutput .= "<div class='play-stats'>";
$htmloutput .= "Likes:".$arrRows['likes']." -- Dislikes: ".$arrRows['dislikes'];
$htmloutput .= "</div>";
$htmloutput .= "<div class='play-vote'>";
$htmloutput .= "<button name='playLike' type='submit' value='$playId'>Like</button>";
$htmloutput .= "<button name='playDislike' type='submit' value='$playId'>Dislike</button>";
$htmloutput .= "</div>";
$htmloutput .= "</div>";
}
$i++;
unset($playTitle);
}
$htmloutput .= "</div>";
echo $htmloutput;
}