Jump to content

PHP Blog "Undefined variable" issues!!


anix

Recommended Posts

Hi i am here as I am having issues with a blog that i have designed, when i designed the blog using wamp on my local machine all was good, then i did have the site on another server that was running the site fine, now that i have moved it to another server I am getting 3 strange errors as shown below and i Cant explain them, can someone please help? thanks

 

ERRORS!

 

Notice: Undefined variable: perpage in /www/emenace.com/a/n/i/anix/htdocs/controller/posts.php on line 24

 

Notice: Undefined index: user in /www/emenace.com/a/n/i/anix/htdocs/lib/view.php on line 40

 

Notice: Undefined variable: date in /www/emenace.com/a/n/i/anix/htdocs/views/posts/_post.php on line 3

 

POSTS.PHP

 

<?php

include(MODEL_PATH.'post.php');


switch ($route['view']) {

case "show":

	$post = find_post($params['id']);

	break;

case "home":

	$per_page = 4;

	$page = (isset($params['page']) && ctype_digit($params['page'])) ? $params['page'] : 1;

	$start = $per_page * $page - $per_page;

	$posts = find_posts($start, $per_page);

	$total_pages = ($posts['num_posts'] <= $perpage) ? 1: ceil($posts['num_posts'] / $per_page); 

	//echo $total_pages; //this checks the amount of total pages

	break;

case "update":
		check_auth();
		$errors = validate($post_validations, $params['post']);

		if($errors)
		{
		$post['id'] = $params['id'];
		$route['view'] = 'edit';
		site_warning('Please correct all the errors listed below');
		}
		else
		{
		$params['post']['id'] = $params['id'];
		post_update($params['post']);
		site_notice('Post Has Been Updated Posted!!');
		redirect('posts');	
		}

	break;

case "new":
	check_auth();
	break;

case "create":
		check_auth();
		$errors = validate($post_validations, $params['post']);

		if($errors)
		{
		$route['view'] = 'new';
		site_warning('Please correct all the errors listed below');			
		}
		else
		{
		create_post($params['post']);
		site_notice('Post Has Been Successfully Posted!!');
		redirect('posts');
		}

	break;

case "edit":

	check_auth();

	$post = find_post($params['id']);

	break;

	case "delete":
		check_auth();

	if(post_delete($params['id']))
		{
		site_notice('Post Has Been Successfully Deleted!!');
		redirect('posts');
		}

	break;
}


?>

 

 

 

 

VIEW.PHP

 

<?php

/**
 * This displays an error message if we have an error 
 * @param bool $error
 * @param string $msg
 * @return string
 */

function error_msg($error, $msg) 
{
	if($error) { return $msg; }
	return '';
}


/**
 * Returns the correct value for our form element
 * @param bool $error
 * @param string $row
 * @param string $param
 * @return string
 */
function element_value($error, $row, $param)
{
	if($error){ return ''; }
	if($row){ return $row; }
	if($param){ return $param; }

	return '';
}

/**
 * Checks user session if logged in returns true
 * Or else returns false
 * @return bool
 */
function logged_in() 
{
	if($_SESSION['user'])
	{
	return true;
	}
	else
	{
	return false;	
	}
}

/**
 * Returns user field
 * @param string field
 * @return string
 */
function current_user($field) 
{
return $_SESSION['user'][$field];	
}

/**
 * Format Date
 * @param mysql timestamp $mysql_timestamp
 * @return string
 */
function format_date($mysql_timestamp) 
{
	$unix_time = strtotime($mysql_timestamp);
	return date('M d Y', $unix_time);
}




?>

 

 

 

_POST.PHP

 

<div class="supportingtext">
<?php
if($date != format_date($post['created_at'])):
?>

<span class="date"><?php echo format_date($post['created_at']); ?></span>
<?php
endif;
?>

<div class="post">
         
         <span class="title"><a href="<?php echo '/'.APP_ROOT.'/'; ?>posts/<?php echo $post['id']; ?>">» <?php echo $post['title']; ?></a></span>
         <span class="body-txt"><?php echo $post['body']; ?></span>
			 <span class="author">by: <?php echo $post['username']; ?></span>

</div>
</div>

<?php $date = format_date($post['created_at']);

Link to comment
https://forums.phpfreaks.com/topic/153138-php-blog-undefined-variable-issues/
Share on other sites

The Notices (not errors) are pretty straight forward. You are using variables without checking they are defined first.

 

Without digging through all your code, a simple example....

 

Instead of....

 

switch ($route['view']) {
  // .. rest of code

 

You ought be using....

 

if (isset($route['view'])) {
  switch ($route['view']) {
    // ..rest of code
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.