RyanMinor Posted August 12, 2011 Share Posted August 12, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/244561-pdo-with-mvc-help/ Share on other sites More sharing options...
trq Posted August 12, 2011 Share Posted August 12, 2011 Looping through a result set is normal and provides the most flexability. If you had methods that returned a formatted result set you would need to mix markup into your models. This is NEVER a good idea. The Model class that you have shown is also fare to specific to have all other Models extend from. Your base Model should have nothing more than what is needed to be shared to every other Model and should likely extend PDO not have a dependency on it. Quote Link to comment https://forums.phpfreaks.com/topic/244561-pdo-with-mvc-help/#findComment-1256208 Share on other sites More sharing options...
ignace Posted August 12, 2011 Share Posted August 12, 2011 MVC doesn't literally mean that you need to have a Model, View, and Controller class. They are just concepts which you need to understand before you can use/implement an MVC architecture. You could have a BlockAd class with a method getRandomAd() to get a random ad, and a VideoCategory model with a getCategories() method to get all categories. BlockAd and VideoCategory are your models. Quote Link to comment https://forums.phpfreaks.com/topic/244561-pdo-with-mvc-help/#findComment-1256317 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.