I a table that I have added the function to be able to sort, it works, but it is coming up with an errors:

Notice: Undefined index: sort in C:\wamp\www\test stuff\test.php on line 25

Notice: Undefined index: sort in C:\wamp\www\test stuff\test.php on line 28

Notice: Undefined index: sort in C:\wamp\www\test stuff\test.php on line 31

Notice: Undefined index: sort in C:\wamp\www\test stuff\test.php on line 34

Notice: Undefined index: sort in C:\wamp\www\test stuff\test.php on line 37

I am stumped, I have looked all over, but I can't figure out what the problem is. Any help would be apreciated.

Thanks,
ADM

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("mydb", $con);

if($_GET['sort'] == "FNAME"){
$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY FNAME";
}
elseif($_GET['sort'] == "LNAME"){
$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.LNAME ASC";
}
elseif($_GET['sort'] == "TITLE"){
$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.TITLE ASC";
}
elseif($_GET['sort'] == "NAME"){
$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY departments.NAME ASC";
}
elseif($_GET['sort'] == "PHONE"){
$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.PHONE ASC";
}
// Default sql if no parameters given
else {
$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.LNAME ASC";
};


$result = mysql_query($query) or die(mysql_error());
echo '<table border="1">
<tr>
<td><a href="test.php?sort=FNAME">First Name</a></td>
<td><a href="test.php?sort=LNAME">Last Name</a></td>
<td><a href="test.php?sort=TITLE">Title</a></td>
<td><a href="test.php?sort=NAME">Department</a></td>
<td><a href="test.php?sort=PHONE">Phone</a></td>
</tr>';
while($row = mysql_fetch_array($result))
{
if($row['LNAME'])
{
echo "<tr>";
echo "<td>".$row['FNAME']."</td>";
echo "<td>".$row['LNAME']."</td>";
echo "<td>".$row['TITLE']."</td>";
echo "<td>".$row['NAME']."</td>";
echo "<td>".$row['PHONE']."</td>";
echo "</tr>";
}
}
echo "</table>";
mysql_close($con);

?>

Try this:

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("mydb", $con);

if(isset($_REQUEST['sort'])){
	if($_GET['sort'] == "FNAME"){
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY FNAME";
	}
	elseif($_GET['sort'] == "LNAME"){
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.LNAME ASC";
	}
	elseif($_GET['sort'] == "TITLE"){
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.TITLE ASC";
	}
	elseif($_GET['sort'] == "NAME"){
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY departments.NAME ASC";
	}
	elseif($_GET['sort'] == "PHONE"){
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.PHONE ASC";
	}
	// Default sql if no parameters given
	else {
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.LNAME ASC";
	};
}


$result = mysql_query($query) or die(mysql_error());
echo '<table border="1">
<tr>
<td><a href="test.php?sort=FNAME">First Name</a></td>
<td><a href="test.php?sort=LNAME">Last Name</a></td>
<td><a href="test.php?sort=TITLE">Title</a></td>
<td><a href="test.php?sort=NAME">Department</a></td>
<td><a href="test.php?sort=PHONE">Phone</a></td>
</tr>';
while($row = mysql_fetch_array($result))
{
if($row['LNAME'])
{
echo "<tr>";
echo "<td>".$row['FNAME']."</td>";
echo "<td>".$row['LNAME']."</td>";
echo "<td>".$row['TITLE']."</td>";
echo "<td>".$row['NAME']."</td>";
echo "<td>".$row['PHONE']."</td>";
echo "</tr>";
}
}
echo "</table>";
mysql_close($con);

?>

Check if the 'sort' parameter is already defined which was sent by form. In the first time of this page, the form is not processed and there is no 'sort' parameter has been sent. So '$_GET' is undefined variable. You can check if the variable is already set with the 'isset()' method. Hope this help!

Hi Zero13,
Thank you for your help. Now it looks like it is giving me a similar error.

Notice: Undefined variable: query in C:\wamp\www\test stuff\test.php on line 47
Query was empty

Now that I put my if/elseif statements inside of the if(isset($_REQUEST)){}, in line 47 $query is not being defined. In $result = mysql_query($query) or die(mysql_error());, $query was defined in each of the if/elseif select statements. When it gets down to $result = mysql_query($query) or die(mysql_error());, it does not know what $query is.

Thanks again,
ADM

Try this:

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("mydb", $con);

if(isset($_REQUEST['sort'])){
	if($_GET['sort'] == "FNAME"){
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY FNAME";
	}
	elseif($_GET['sort'] == "LNAME"){
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.LNAME ASC";
	}
	elseif($_GET['sort'] == "TITLE"){
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.TITLE ASC";
	}
	elseif($_GET['sort'] == "NAME"){
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY departments.NAME ASC";
	}
	elseif($_GET['sort'] == "PHONE"){
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.PHONE ASC";
	}
	// Default sql if no parameters given
	else {
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.LNAME ASC";
	};
}


$result = mysql_query($query) or die(mysql_error());
echo '<table border="1">
<tr>
<td><a href="test.php?sort=FNAME">First Name</a></td>
<td><a href="test.php?sort=LNAME">Last Name</a></td>
<td><a href="test.php?sort=TITLE">Title</a></td>
<td><a href="test.php?sort=NAME">Department</a></td>
<td><a href="test.php?sort=PHONE">Phone</a></td>
</tr>';
while($row = mysql_fetch_array($result))
{
if($row['LNAME'])
{
echo "<tr>";
echo "<td>".$row['FNAME']."</td>";
echo "<td>".$row['LNAME']."</td>";
echo "<td>".$row['TITLE']."</td>";
echo "<td>".$row['NAME']."</td>";
echo "<td>".$row['PHONE']."</td>";
echo "</tr>";
}
}
echo "</table>";
mysql_close($con);

?>

Check if the 'sort' parameter is already defined which was sent by form. In the first time of this page, the form is not processed and there is no 'sort' parameter has been sent. So '$_GET' is undefined variable. You can check if the variable is already set with the 'isset()' method. Hope this help!

Uhhh.. ADM, you need default statement if there is no parameter passed. I found that you want to sort with the 'employee.LNAME' as default. Try this one:

if(isset($_REQUEST['sort'])){
	if($_GET['sort'] == "FNAME"){
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY FNAME";
	}
	elseif($_GET['sort'] == "LNAME"){
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.LNAME ASC";
	}
	elseif($_GET['sort'] == "TITLE"){
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.TITLE ASC";
	}
	elseif($_GET['sort'] == "NAME"){
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY departments.NAME ASC";
	}
	else{
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.PHONE ASC";
	}
}
else{ // Default if no parameters passed
	$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.LNAME ASC";
}

It should work. Good luck dear.

Hello Zero13,

Thank you, thank you, thank you soooo much, It is working now. I appreciate all of your help, you are a lifesaver. My wife thanks you too, cause she's been listening to me complain about my problems with this for days.

Thanks,

ADM

Here is my completed code:

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("mydb", $con);

if(isset($_REQUEST['sort'])){
    if($_GET['sort'] == "FNAME"){
$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY FNAME";
}
elseif($_GET['sort'] == "LNAME"){
$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.LNAME ASC";
}
elseif($_GET['sort'] == "TITLE"){
$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.TITLE ASC";
}
elseif($_GET['sort'] == "NAME"){
$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY departments.NAME ASC";
}
elseif($_GET['sort'] == "PHONE"){
$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.PHONE ASC";
}
// Default sql if no parameters given
else {
$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.LNAME ASC";
};
}
else {
$query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.LNAME ASC";
};
$result = mysql_query($query) or die(mysql_error());
echo '<table border="1">
<tr>
<td><a href="test.php?sort=FNAME">First Name</a></td>
<td><a href="test.php?sort=LNAME">Last Name</a></td>
<td><a href="test.php?sort=TITLE">Title</a></td>
<td><a href="test.php?sort=NAME">Department</a></td>
<td><a href="test.php?sort=PHONE">Phone</a></td>
</tr>';
while($row = mysql_fetch_array($result))
{
if($row['LNAME'])
{
echo "<tr>";
echo "<td>".$row['FNAME']."</td>";
echo "<td>".$row['LNAME']."</td>";
echo "<td>".$row['TITLE']."</td>";
echo "<td>".$row['NAME']."</td>";
echo "<td>".$row['PHONE']."</td>";
echo "</tr>";
}
}
echo "</table>";
mysql_close($con);

?>

Uhhh.. ADM, you need default statement if there is no parameter passed. I found that you want to sort with the 'employee.LNAME' as default. Try this one:

if(isset($_REQUEST['sort'])){
    if($_GET['sort'] == "FNAME"){
    $query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY FNAME";
    }
    elseif($_GET['sort'] == "LNAME"){
    $query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.LNAME ASC";
    }
    elseif($_GET['sort'] == "TITLE"){
    $query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.TITLE ASC";
    }
    elseif($_GET['sort'] == "NAME"){
    $query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY departments.NAME ASC";
    }
    else{
    $query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.PHONE ASC";
    }
}
else{ // Default if no parameters passed
    $query = "SELECT * FROM departments LEFT JOIN (employees) ON (departments.ID=employees.DEPARTMENTID) ORDER BY employees.LNAME ASC";
}

It should work. Good luck dear.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.