Notice: Undefined index:

error:

Notice: Undefined index: postfull in
includes.php on line 27

Notice: Undefined index: dateposted in
includes.php on line 27

Notice: Undefined index: postfull in
includes.php on line 27

Notice: Undefined index: dateposted in
includes.php on line 27

<?php
include 'blogpost.php';

// Change this info so that it works with your system.
$connection = mysql_connect('', '', '') or die ("<p class='error'>Sorry, we were unable to connect to the database server.</p>");
$database = "basic";
mysql_select_db($database, $connection) or die ("<p class='error'>Sorry, we were unable to connect to the database.</p>");

function GetBlogPosts($inId=null, $inTagId =null)
{
	if (!empty($inId))
	{
		$query = mysql_query("SELECT * FROM blog_posts WHERE id = " . $inId . " ORDER BY id DESC"); 
	}
	else if (!empty($inTagId))
	{
		$query = mysql_query("SELECT blog_posts.* FROM blog_post_tags LEFT JOIN (blog_posts) ON (blog_post_tags.postID = blog_posts.id) WHERE blog_post_tags.tagID =" . $tagID . " ORDER BY blog_posts.id DESC");
	}
	else
	{
		$query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC");
	}
	
	$postArray = array();
	while ($row = mysql_fetch_assoc($query))
	{
		$myPost = new BlogPost($row["id"], $row['title'], $row['post'], $row['postfull'], $row["author_id"], $row['dateposted']);
		array_push($postArray, $myPost);
	}
	return $postArray;
}

?>

“undefined” is another way of saying “no such thing exists”, i.e. $row does not contain fields named postfull or dateposted.

You could trouble shoot by temporarily var_dump()-ing $row to see just what it does have.

thanks that’s what I thought it was but wasn’t 100% next time I’ll just go with my first mind!

Problem preparing query

<?php

$mysql = new mysqli('','','','') or die('There was a problem connecting to the database');
$stmt = $mysql->prepare("SELECT id, title, LEFT(post, 140) AS excerpt, post from blog_post") or die('Problem preparing query');
$stmt->execute();
$stmt->bind_result($id, $title, $excerpt, $post);


while($row = $stmt->fetch()) : ?>

	<h2> <?php echo $title; ?> </h2>
	<p> <?php echo $excerpt; ?> </p>

<?php endwhile; ?>

Change

die('Problem preparing query')

to

die('Problem preparing query<br /><br />' . $mysql->error)

The above helps a lot more with debugging.

Thanks “SgtLegend” forgot to add a ‘s’ at the end of blog_post it’s always the little things that trip me up lol thanks a lot!

I created a pagination file that works. and a blog that works now I’m trying to combined the two together but I’m getting this error:

Fatal error: Cannot redeclare GetBlogPosts() (previously declared in includes.php:9) in includes.php on line 31

here is the code for includes.php:

<?php
include 'blogpost.php';

$connection = mysql_connect('','','') or die ("<p class='error'>Sorry, we were unable to connect to the database server.</p>");

$database = "basic";
mysql_select_db($database, $connection) or die ("<p class='error'>Sorry, we were unable to connect to the database.</p>");

function GetBlogPosts($inId=null, $inTagId =null)
{
	if (!empty($inId))
	{
		$query = mysql_query("SELECT * FROM blog_posts WHERE id = " . $inId . " ORDER BY id DESC"); 
	}
	else if (!empty($inTagId))
	{
		$query = mysql_query("SELECT blog_posts.* FROM blog_post_tags LEFT JOIN (blog_posts) ON (blog_post_tags.postID = blog_posts.id) WHERE blog_post_tags.tagID =" . $tagID . " ORDER BY blog_posts.id DESC");
	}
	else
	{
		$query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC");
	}
	
	$postArray = array();
	while ($row = mysql_fetch_assoc($query))
	{
		$myPost = new BlogPost($row["id"], $row['title'], $row['post'], $row['postfull'], $row["author_id"], $row['date_posted']);
		array_push($postArray, $myPost);
	}
	return $postArray;
}

?>

and the code for the index.php:


<?php

$connection = mysql_connect('','','') or die ("<p class='error'>Sorry, we were unable to connect to the database server.</p>");

$database = "basic";
mysql_select_db($database, $connection) or die ("<p class='error'>Sorry, we were unable to connect to the database.</p>");

//max items per page
$per_page = 2;

//get start variable
$start =& $_GET['start'];

//count records
$record_count = mysql_num_rows(mysql_query("SELECT * FROM blog_posts"));

//count max pages
$max_page = $record_count / $per_page; //may come out as decimal

if (!$start)
	$start = 0;
	
//display data
$get = mysql_query("SELECT * FROM blog_posts LIMIT $start, $per_page");
while ($row = mysql_fetch_assoc($get))

	include ("/blog/includes/includes.php");

	$blogPosts = GetBlogPosts();
	
	foreach ($blogPosts as $post)
	{
		echo "<div class='post'>";
		echo "<h2>" . $post->title . "</h2>";
		echo "<p>" . $post->post . "</p";
		echo "<br/><span class='footer'>Posted By: " . $post->author . " Posted On: " . $post->datePosted . " Tags: " . $post->tags . "</span>";
		echo "</div>";
	}

//setup prev and next variables
$prev = $start - $per_page;
$next = $start + $per_page;

//show prev button
if (!($start<=0))
	echo " <a href='index.php?page=index&section=pag&start=$prev'>Prev</a> ";

//show page numbers

//set variable for first page 
$i=1;

for ($x=0;$x<$record_count;$x=$x+$per_page)
{
	if ($start!=$x)
		echo " <a href='index.php?page=index&section=pag&start=$x'>$i</a> ";
	else
		echo " <a href='index.php?page=index&section=pag&start=$x'><b>$i</b></a> ";
	$i++;	
}
	
//show next button
if (!($start>=$record_count-$per_page))
	echo " <a href='index.php?page=index&section=pag&start=$next'>Next</a> ";
?>

Does the function also exist in the blogpost.php file?

yes but it defines it here is the code:


<?php

class BlogPost
{

public $id;
public $title;
public $post;
public $author;
public $tags;
public $datePosted;

function __construct($inId=null, $inTitle=null, $inPost=null, $inPostFull=null, $inAuthorId=null, $inDatePosted=null)
{
	if (!empty($inId))
	{
		$this->id = $inId;
	}
	if (!empty($inTitle))
	{
		$this->title = $inTitle;
	}
	if (!empty($inPost))
	{
		$this->post = $inPost;
	}

	if (!empty($inDatePosted))
	{
		$splitDate = explode("-", $inDatePosted);
		$this->datePosted = $splitDate[1] . "/" . $splitDate[2] . "/" . $splitDate[0];
	}

	if (!empty($inAuthorId))
	{
		$query = mysql_query("SELECT first_name, last_name FROM people WHERE id = " . $inAuthorId);
		$row = mysql_fetch_assoc($query);
		$this->author = $row["first_name"] . " " . $row["last_name"];
	}

	$postTags = "No Tags";
	if (!empty($inId))
	{
		$query = mysql_query("SELECT tags.* FROM blog_post_tags LEFT JOIN (tags) ON (blog_post_tags.tag_id = tags.id) WHERE blog_post_tags.blog_post_id = " . $inId);
		$tagArray = array();
		$tagIDArray = array();
		while($row = mysql_fetch_assoc($query))
		{
			array_push($tagArray, $row["name"]);
			array_push($tagIDArray, $row["id"]);
		}
		if (sizeof($tagArray) > 0)
		{
			foreach ($tagArray as $tag)
			{
				if ($postTags == "No Tags")
				{
					$postTags = $tag;
				}
				else
				{
					$postTags = $postTags . ", " . $tag;
				}
			}
		}
	}
	$this->tags = $postTags;
}

}

?>

So merged all of the files and now I’m getting this:

Parse error: syntax error, unexpected T_CLASS in line 27

what dose that mean?

Sorry bud i have been busy with some things, could you please post the full source you now have as it will be easier to debug it.

<?php

$connection = mysql_connect('','','') or die ("<p class='error'>Sorry, we were unable to connect to the database server.</p>");

$database = "basic";
mysql_select_db($database, $connection) or die ("<p class='error'>Sorry, we were unable to connect to the database.</p>");

//max items per page
$per_page = 2;

//get start variable
$start =& $_GET['start'];

//count records
$record_count = mysql_num_rows(mysql_query("SELECT * FROM blog_posts"));

//count max pages
$max_page = $record_count / $per_page; //may come out as decimal

if (!$start)
	$start = 0;
	
//display data
$get = mysql_query("SELECT * FROM blog_posts LIMIT $start, $per_page");
while ($row = mysql_fetch_assoc($get))

class BlogPost
{

public $id;
public $title;
public $post;
public $author;
public $tags;
public $datePosted;

function __construct($inId=null, $inTitle=null, $inPost=null, $inPostFull=null, $inAuthorId=null, $inDatePosted=null)
{
	if (!empty($inId))
	{
		$this->id = $inId;
	}
	if (!empty($inTitle))
	{
		$this->title = $inTitle;
	}
	if (!empty($inPost))
	{
		$this->post = $inPost;
	}

	if (!empty($inDatePosted))
	{
		$splitDate = explode("-", $inDatePosted);
		$this->datePosted = $splitDate[1] . "/" . $splitDate[2] . "/" . $splitDate[0];
	}

	if (!empty($inAuthorId))
	{
		$query = mysql_query("SELECT first_name, last_name FROM people WHERE id = " . $inAuthorId);
		$row = mysql_fetch_assoc($query);
		$this->author = $row["first_name"] . " " . $row["last_name"];
	}

	$postTags = "No Tags";
	if (!empty($inId))
	{
		$query = mysql_query("SELECT tags.* FROM blog_post_tags LEFT JOIN (tags) ON (blog_post_tags.tag_id = tags.id) WHERE blog_post_tags.blog_post_id = " . $inId);
		$tagArray = array();
		$tagIDArray = array();
		while($row = mysql_fetch_assoc($query))
		{
			array_push($tagArray, $row["name"]);
			array_push($tagIDArray, $row["id"]);
		}
		if (sizeof($tagArray) > 0)
		{
			foreach ($tagArray as $tag)
			{
				if ($postTags == "No Tags")
				{
					$postTags = $tag;
				}
				else
				{
					$postTags = $postTags . ", " . $tag;
				}
			}
		}
	}
	$this->tags = $postTags;
}

}

function GetBlogPosts($inId=null, $inTagId =null)
{
	if (!empty($inId))
	{
		$query = mysql_query("SELECT * FROM blog_posts WHERE id = " . $inId . " ORDER BY id DESC"); 
	}
	else if (!empty($inTagId))
	{
		$query = mysql_query("SELECT blog_posts.* FROM blog_post_tags LEFT JOIN (blog_posts) ON (blog_post_tags.postID = blog_posts.id) WHERE blog_post_tags.tagID =" . $tagID . " ORDER BY blog_posts.id DESC");
	}
	else
	{
		$query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC");
	}
	
	$postArray = array();
	while ($row = mysql_fetch_assoc($query))
	{
		$myPost = new BlogPost($row["id"], $row['title'], $row['post'], $row['postfull'], $row["author_id"], $row['date_posted']);
		array_push($postArray, $myPost);
	}
	return $postArray;
}

	$blogPosts = GetBlogPosts();
	
	foreach ($blogPosts as $post)
	{
		echo "<div class='Blog'>";
		echo "<div class='post'>";
		echo "<h2>" . $post->title . "</h2>";
		echo "<p>" . $post->post . "</p";
		echo "<br/><span class='footer'>Posted By: " . $post->author . " Posted On: " . $post->datePosted . " Tags: " . $post->tags . "</span>";
		echo "</div>";
		echo "</div>";
	}

//setup prev and next variables
$prev = $start - $per_page;
$next = $start + $per_page;

//show prev button
if (!($start<=0))
	echo " <a href='index.php?page=index&section=pag&start=$prev'>Prev</a> ";

//show page numbers

//set variable for first page 
$i=1;

for ($x=0;$x<$record_count;$x=$x+$per_page)
{
	if ($start!=$x)
		echo " <a href='index.php?page=index&section=pag&start=$x'>$i</a> ";
	else
		echo " <a href='index.php?page=index&section=pag&start=$x'><b>$i</b></a> ";
	$i++;	
}
	
//show next button
if (!($start>=$record_count-$per_page))
	echo " <a href='index.php?page=index&section=pag&start=$next'>Next</a> ";
?>

whats a good debugging program?

The main issue is you have a class now nested inside the while loop which is not valid, you will need to move it to be before the while loop and then create a new instance of the class in the while loop. See the example below…

class BlogPost {
    public $id;
    public $title;
    public $post;
    public $author;
    public $tags;
    public $datePosted;
    
    function __construct() {
        // .....
    }
}

$get = mysql_query("SELECT * FROM blog_posts LIMIT $start, $per_page");

while ($row = mysql_fetch_assoc($get)) {
    $blog = new BlogPost;
}

Sweet thanks SgtLegend last thing is the per_page settings there not working. Do I need to define the function at the beginning as well?

<?php

$connection = mysql_connect('','','') or die ("<p class='error'>Sorry, we were unable to connect to the database server.</p>");

$database = "basic";
mysql_select_db($database, $connection) or die ("<p class='error'>Sorry, we were unable to connect to the database.</p>");

//Define the post class
class BlogPost{

public $id;
public $title;
public $post;
public $author;
public $tags;
public $datePosted;

function __construct($inId=null, $inTitle=null, $inPost=null, $inPostFull=null, $inAuthorId=null, $inDatePosted=null)
{
	if (!empty($inId))
	{
		$this->id = $inId;
	}
	if (!empty($inTitle))
	{
		$this->title = $inTitle;
	}
	if (!empty($inPost))
	{
		$this->post = $inPost;
	}

	if (!empty($inDatePosted))
	{
		$splitDate = explode("-", $inDatePosted);
		$this->datePosted = $splitDate[1] . "/" . $splitDate[2] . "/" . $splitDate[0];
	}

	if (!empty($inAuthorId))
	{
		$query = mysql_query("SELECT first_name, last_name FROM people WHERE id = " . $inAuthorId);
		$row = mysql_fetch_assoc($query);
		$this->author = $row["first_name"] . " " . $row["last_name"];
	}

	$postTags = "No Tags";
	if (!empty($inId))
	{
		$query = mysql_query("SELECT tags.* FROM blog_post_tags LEFT JOIN (tags) ON (blog_post_tags.tag_id = tags.id) WHERE blog_post_tags.blog_post_id = " . $inId);
		$tagArray = array();
		$tagIDArray = array();
		while($row = mysql_fetch_assoc($query))
		{
			array_push($tagArray, $row["name"]);
			array_push($tagIDArray, $row["id"]);
		}
		if (sizeof($tagArray) > 0)
		{
			foreach ($tagArray as $tag)
			{
				if ($postTags == "No Tags")
				{
					$postTags = $tag;
				}
				else
				{
					$postTags = $postTags . ", " . $tag;
				}
			}
		}
	}
	$this->tags = $postTags;
}

}

//max items per page
$per_page = 2;

//get start variable
$start = $_GET['start'];

//count records
$record_count = mysql_num_rows(mysql_query("SELECT * FROM blog_posts"));

//count max pages
$max_page = $record_count / $per_page; //may come out as decimal

if (!$start)
	$start = 0;
	
	
//display data
$get = mysql_query("SELECT * FROM blog_posts LIMIT $start, $per_page");
while ($row = mysql_fetch_assoc($get)) {
	$boog = new BlogPost;
}


function GetBlogPosts($inId=null, $inTagId =null)
{
	if (!empty($inId))
	{
		$query = mysql_query("SELECT * FROM blog_posts WHERE id = " . $inId . " ORDER BY id DESC"); 
	}
	else if (!empty($inTagId))
	{
		$query = mysql_query("SELECT blog_posts.* FROM blog_post_tags LEFT JOIN (blog_posts) ON (blog_post_tags.postID = blog_posts.id) WHERE blog_post_tags.tagID =" . $tagID . " ORDER BY blog_posts.id DESC");
	}
	else
	{
		$query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC");
	}
	
	$postArray = array();
	while ($row = mysql_fetch_assoc($query))
	{
		$myPost = new BlogPost($row["id"], $row['title'], $row['post'], $row['postfull'], $row["author_id"], $row['date_posted']);
		array_push($postArray, $myPost);
	}
	return $postArray;
}

	$blogPosts = GetBlogPosts();
	
	foreach ($blogPosts as $post)
	{
		echo "<div class='Blog'>";
		echo "<div class='post'>";
		echo "<h2>" . $post->title . "</h2>";
		echo "<p>" . $post->post . "</p";
		echo "<br/><span class='footer'>Posted By: " . $post->author . " Posted On: " . $post->datePosted . " Tags: " . $post->tags . "</span>";
		echo "</div>";
		echo "</div>";
	}

//setup prev and next variables
$prev = $start - $per_page;
$next = $start + $per_page;

//show prev button
if (!($start<=0))
	echo " <a href='index.php?page=index&section=pag&start=$prev'>Prev</a> ";

//show page numbers

//set variable for first page 
$i=1;

for ($x=0;$x<$record_count;$x=$x+$per_page)
{
	if ($start!=$x)
		echo " <a href='index.php?page=index&section=pag&start=$x'>$i</a> ";
	else
		echo " <a href='index.php?page=index&section=pag&start=$x'><b>$i</b></a> ";
	$i++;	
}
	
//show next button
if (!($start>=$record_count-$per_page))
	echo " <a href='index.php?page=index&section=pag&start=$next'>Next</a> ";
?>

I see a few of issues/concerns in your code…

  1. Your variable $start will cause an undefined index error unless $_GET[‘start’] is always set in the URL, to avoid this use

    php $start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
  2. In your while loop your using the example i gave you which at the moment isn’t doing anything, could you please give some more oversight as to how you want your class and [B]GetBlogPosts/B to work as at the moment im a little confused at what your main goal is.
  3. When you divide your $record_count variable by your $per_page variable i would recommend you use ceil() so the return value gets rounded to the next highest integer to avoid incorrect results.

The way I wanted it to work is have the blog post as the house to house all of the post and then display multiple post with in.