Help - Search - Members - Calendar
Full Version: Undefined Variable
Zymic Webmaster Forums > Zymic Free Web Hosting > Databases & MySQL
jcvamp
This is a cut down version of my webpage code. I get the error 'Notice: Undefined variable: amnt in /www/zzl.org/j/r/c/jrcooper/htdocs/index.php on line 78'. I also had a piece of code where it was using the variable $cat, which is supposed to be blank unless passed to the page on loading. Again, this caused the same error.

QUOTE
<html>
<head>
<?php
mysql_connect("localhost", "myhost", "mypassword");
mysql_select_db("jrcooper_zzl_poems");// connect to database
$sql="SELECT * FROM poetry";
$res=mysql_query($sql);
while ($entry = mysql_fetch_object ($res)) {
if ($cat==$entry->category) {
// increment amnt
$amnt=$amnt+1;
//store in arrays
$title[$amnt]=$entry->title;
$dat[$amnt]=$entry->date;
$category[$amnt]=$entry->category;
$poem[$amnt]=$entry->poem;
$dedication[$amnt]=$entry->dedication;
$rate[$amnt]=$entry->rate;
}
}
mysql_free_result ($res);
?>
</head>

<body>
<?php
// display poems
for ( $i = 1; $i <= $amnt; $i += 1) {
echo '<h3><a name="'.$i.'">'.$title[$i].'</a></h3>';

print "<h2>";
Long_date($dat[$i]);
print "</h2>";
print "<p>$poem[$i]</p>";
}
}
?>
</body>
</html>
jcvamp
I managed to correct the problem by adding 'if(!isset($cat)){$cat="";}'. However, the point of the $cat variable is to have it passed into the page via the URL (ie 'index.php?cat=whatever'). This isn't working.

Any ideas?
jcvamp
Using '$cat=$_GET['cat'];' I was able to get the 'cat' variable from the URL, but I got the error 'Unidentified index: cat'.

Okay, I figured it out.

CODE
if(!isset($_GET['cat'])){
$cat="";
} else {
$cat=$_GET['cat'];
}
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.