Hi, I am building my own MVC to try and become more familiar with it's inner workings. As of now, everything works pretty well and I am learning a good deal. I do have one question though. My framework uses a main model that instantiates an instance of the PDO database class (which really just connects to the database). Then it has several methods that query data that will show on every page. I put them in the main model instead of having to put them in each child model. The child models extend this class and then do their own thing. Right now I am grabbing the results using a foreach loop which works, but for things like page information (title, meta keywords, and meta description) do I really need to loop through the query result? I am almost positive there is a better way where I can just call on the individual results. My relevant code is below. Let me know if there is a better way in which I can just echo the query result for the three variables ($pageTitle, $pageKeywords, $pageDescription). Thanks in advance.
Here is my Main Model
<?php
class Model {
/**
* Constructor function instantiates a new database instance.
*/
public function __construct() {
$this->db = new Database();
}
/**
* pageInfo function returns data about each page
*/
public function pageInfo($path) {
$stmt = $this->db->prepare("SELECT * FROM page WHERE page_path = ?");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute(array($path));
return $stmt->fetchAll();
}
/**
* bannerAds function returns banner advertisements
*/
public function bannerAds() {
$stmt = $this->db->prepare("SELECT banner_ad_photo, banner_ad_url FROM banner_ad WHERE banner_ad_delete = 0 ORDER BY RAND() LIMIT 0, 2");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
return $stmt->fetchAll();
}
/**
* popularVideos function returns the most purchased videos
*/
public function popularVideos() {
$stmt = $this->db->prepare("SELECT video_title, video_id, COUNT(video_purchase_video_id) AS rank FROM video
INNER JOIN video_purchase
ON video_id = video_purchase_video_id
GROUP BY video_title
ORDER BY rank DESC LIMIT 0, 5");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
return $stmt->fetchAll();
}
/**
* categories returns all categories
*/
public function categories() {
$stmt = $this->db->prepare("SELECT DISTINCT video_category_name FROM video_category ORDER BY video_category_name");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
return $stmt->fetchAll();
}
/**
* pageInfo function returns data about each page
*/
public function blockAds() {
$stmt = $this->db->prepare("SELECT block_ad_photo, block_ad_url FROM block_ad WHERE block_ad_delete = 0 ORDER BY RAND() LIMIT 0, 2");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
return $stmt->fetchAll();
}
}
Here is my view for the home page
/* here is where I am confused...isn't there a way to just grab each individual result? */
<?php foreach ($pageInfo as $page) {
$pageTitle = $page['page_title'];
$pageKeywords = $page['page_keywords'];
$pageDescription = $page['page_description'];
} ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?=SITE_NAME?> | <?=$pageTitle?></title>
<meta name="Keywords" content="<?=$pageKeywords?>" />
<meta name="Description" content="<?=$pageDescription?>" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="<?=CSS_PATH?>global.css" type="text/css" />
<script type="text/javascript" src="<?=FLOWPLAYER_PATH?>flowplayer-3.2.6.min.js"></script>
<script type="text/javascript" src="<?=JS_PATH?>jquery_1.6.1.js"></script>
</head>
<body>
<!-- wrap -->
<div id="wrap">
<?php include ('header_view.php'); ?>
<?php include ('menu_view.php'); ?>
<!-- content-wrap -->
<div id="content-wrap" class="two-col">
<?php include('sidebar_view.php'); ?>
<!-- main content -->
<div id="main">
<h1></h1>
<!-- flowplayer -->
<a
href="../media/preview.mov"
style="display:block; width:425px; height:300px; margin:0 auto; margin-top:10px; margin-bottom:10px;"
id="player">
</a>
<script language="JavaScript">
flowplayer("player", "../public/flowplayer/flowplayer-3.2.7.swf", {
version: [9, 115],
plugins: {
sharing: {
url: '../public/flowplayer/flowplayer.sharing-3.2.1.swf',
buttons: {
overColor: '#0099FF'
},
facebook: false
}
},
clip: {
autoPlay: true
//onStart: function (clip) {
//var w = parseInt(clip.metaData.width, 10),
//h = parseInt(clip.metaData.height, 10);
//$(this.getParent()).css({width: w, height: h});
//}
}
});
</script>
<!-- end flowplayer -->
<!--random videos start here-->
<table width="100%" cellpadding="5">
<tr>
<?php $end_row = 0;
$columns = 3;
$loop_row = 0;
foreach ($randomVideos as $video) {
$model = stripslashes($video['video_model']);
$model = strtolower($model);
$model = str_replace(' ', '_', $model);
$directory = stripslashes($video['video_title']);
$directory = strtolower($directory);
$directory = str_replace(' ', '_', $directory);
if($end_row == 0 && $loop_row++ != 0) { ?>
<tr>
<?php } ?>
<td align="center" valign="top">
<a href="video_info.php?video=<?php echo str_replace(' ', '_', stripslashes($video['video_title'])); ?>">
<?=stripslashes($video['video_title'])?></a>
<br />
<?=stripslashes($video['video_model'])?>
<br />
<a href="video_info.php?video=<?=str_replace(' ', '_', stripslashes($video['video_title']))?>">
<img src="media/<?=$model?>/<?=$directory?>/<?=$video['video_photo']?>" width="125px" border="0" /></a>
<br />
</td>
<?php $end_row++;
if($end_row >= $columns) { ?>
</tr>
<?php $end_row = 0;
}
}
if($end_row != 0) {
while ($end_row < $columns) { ?>
<td> </td>
<?php $end_row++; ?>
<?php } ?>
</tr>
<?php } ?>
</table>
<!--random videos end here-->
<?php include ('banner_ad_view.php'); ?>
<!-- end main -->
</div>
<!-- end content-wrap -->
</div>
<?php include ('footer_view.php'); ?>
<!-- end wrap -->
</div>
</body>
</html>