Jump to content

Do I put this in the Controller or View?


unemployment

Recommended Posts

I'm very new to MVC and I'm trying to figure out how to transition my code and I'm not sure where to place things.  It easy to want to just put everything in the view, but I know that doesn't make sense.

 

Can you please guide me on how I should transition the rest of my code for my header? I'm currently using codeigniter.

 

Current View

<?php

$page = substr(end(explode(DIRECTORY_SEPARATOR, $_SERVER['PHP_SELF'])), 0, -4);

$title = (array_key_exists($page, $page_names) !== false) ? $page_names[$page]: '';

if (array_key_exists($page, $page_names) !== false)
{
$title .= " | Jason Biondo";
}

$banner_imgs 	= array('contact.jpg', 'about.jpg', 'tools.jpg', 'portfolio.jpg', 'articles.jpg');
$nav_names 		= array('Contact', 'About', 'Tools', 'Portfolio', 'Articles');
$gutter_values 	= array('307', '230', '161', '84', '0');

$alt_page = 'Articles';

foreach($nav_names as $k => $name)
{
if($page === strtolower($name))
{
	$g_value = $gutter_values[$k];
}
}

if(!isset($g_value))
{
$g_value = 0;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" dir="ltr">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta http-equiv="content-script-type" content="text/javascript" />
<meta http-equiv="content-style-type" content="text/css" />
<meta http-equiv="content-language" content="en" />
<meta name="description" content="Jason is a serial entrepreneur that builds web applications using advanced programming technologies and unique interface design." />
<meta name="keywords" content="jason , entrepreneur, investors, venture capitalist, angel investor, vc, ventures, private equity, startups, startup community, startup investments, investment network, raise capital, where to find capital, fund raising, venture financing, contact investors, angel fund, angel group, investment strategy, business plan" />
<title><?=$title?></title>
<link rel="shortcut icon" type="image/x-icon" href="/assets/img/favicon.ico" />
<link rel="stylesheet" type="text/css" media="screen" href="/css/style.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="./js/jquery.validate.min.js"></script>
<script src="./js/jquery.corner.js"></script>
<script src="./js/init.js"></script>
<?php

if (file_exists("./assets/js/pages/${page}.js"))
{
	echo "<script type=\"text/javascript\" src=\"./js/pages/${page}.js\"></script>";
}

?>
</head>
<body>
<div class="header">
	<div class="header_container">
		<a href="/">
			<img class="logo" src="/assets/img/logo.png" alt="Jason" title="Jason" />
		</a>
		<div class="navigation f_right">
			<div class="gutter">
				<span id="nav_highlight" class="nav_highlight" style="left: <?php echo $g_value; ?>px;"></span>
			</div>
			<ul>
			<?php

			foreach($nav_names as $k => $name)
			{
				if(is_odd($k))
				{
					?>
					<li class="pike"></li>
					<?php
				}

				?>
				<li>
					<a id="<?php echo strtolower($name); ?>" <?php echo (($page === strtolower($name)) || ($alt_page === $name)) ? 'class="selected"' : ''; ?> href="/<?php echo ($name === $alt_page) ? '' : strtolower($name); ?>"><?php echo $name; ?></a>
				</li>
				<?php

				if(is_odd($k))
				{
					?>
					<li class="pike"></li>
					<?php
				}
			}

			?>
			</ul>
		</div>
	</div>
</div>
<div class="stripe"></div>
<div id="content_container" class="content_container">
	<div id="banner" class="banner">
		<?php

		foreach($nav_names as $k => $name)
		{
			if(($page === strtolower($name)) || ($alt_page === $name))
			{
				?>
				<img src="/assets/img/banners/<?php echo $banner_imgs[$k]; ?>" />
				<h1><?php echo $name; ?></h1>
				<?php

				break;
			}

			if($alt_page === $name)
			{
				?>
				<img src="/assets/img/banners/<?php echo $banner_imgs[$k]; ?>" />
				<h1><?php echo $name; ?></h1>
				<?php
			}
		}

		?>
	</div>
	<div class="banner_stripe"></div>
	<div class="content">

 

Current Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Writing extends CI_Controller {

public function index()
{
	$this->load->library('common');

	$data['page_names'] = $this->common->page_names();
	$data['is_odd'] = $this->common->is_odd();

	$this->load->view('includes/header', $data);

	$this->load->view('writing_view');

	$this->load->view('includes/footer');

	$this->output->enable_profiler(TRUE);
}
}

Link to comment
Share on other sites

Ok, I kind of understand that even though I have little experience doing that.  What do I  do with the array data at the top of the view?  What about all the other page data?  I have no idea how I should handle the output for the foreach loops.  Do I do the foreach loops in the model since it's generating data?

 

I could really use some examples.

Link to comment
Share on other sites

The arrays should likely be made into models.

 

foreach loops are fine in Views, but not allot else.

 

There are many, many tutorials around on how to organise your code within an MVC, there should be plenty on using CI in particular.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.