Jump to content

[SOLVED] I don't understand why I am getting an error


jamcoupe

Recommended Posts

I am just learning PHP atm, I have become a little stuck, my code works fine but I am getting an error on the news.php page I dont understand why.

 

The error says:

Notice: Undefined variable: articledata in /Applications/MAMP/htdocs/jstar/news.php on line 12

However my code still works fine but I want to know how to stop getting this error and what am I doing wrong.

 

I have connected, selected a database and closed the database sucessfully.

 

news.php

<?php require_once("includes/functions.php"); ?>
<?php require("includes/connect.php"); ?>
<?php
if(isset($_GET['article'])) {
	$articledata = article_info_by_id($_GET['article']);
} else {
	$artciledata = NULL;
}
?>
<?php include("includes/header.php"); ?>
<?php
    	if (!is_null($articledata)) {
		echo "<table style=\"width:450px;\">
				<tr>
					<td style=\"font-weight:bold;\">".$articledata["title"]."</td>
				</tr>
				<tr>
					<td>".$articledata["content"]."</td>
				</tr>
				<tr>
					<td style=\"text-align: right;\">".$articledata["date"]."</td>
				</tr>
			</table><br />";
	} else {
		$query = "SELECT *
				  FROM news
				  ORDER BY id DESC";	  
		$get_news = mysql_query($query, $connect);
		query_check($get_news);
			while ($news = mysql_fetch_array($get_news)) {
				echo
					"<table style=\"width:450px;\">
						<tr>
							<td style=\"font-weight:bold;\"><a href=\"news.php?article=". urlencode($news["id"]) ."\">{$news['title']}</a></td>
						</tr>
						<tr>
							<td>{$news['content']}</td>
						</tr>
						<tr>
							<td style=\"text-align: right;\">{$news['date']}</td>
						</tr>
					</table><br />";
				}
			}
?>
<?php require("includes/footer.php"); ?>

 

functions.php

<?php
function query_check($check) {
	if(!$check) {
		die("Database Query Failed: ".mysql_error());
	}
}

function article_info_by_id($news_id) {
	global $connect;
	$query = "SELECT *
			  FROM news
			  WHERE id=".$news_id.
			" LIMIT 1";
	$get_article = mysql_query($query, $connect);
	query_check($get_article);
	if ($article = mysql_fetch_array($get_article)) {
		return $article;
	} else {
		return NULL;
	}
}

?>

I believe you have a variable scope issue.

 

try declaring $articledata as a variable prior to inside the loop.

 

<?php

$articledata=null;
if(isset($_GET[.....

 

 

seeing the previous post - you have misspelled articledata on or around line 7.

The error indicates line 12 though.  If fixing line 7 does not work try declaring variable outside of the conditional statement.

 

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.