Can you spot the error?

well I’ve been practicing php for a while to make a blog with the help of a book, i copied every single line from the book pdf but doesn’t work for me. Its the functions file which is showing the error after I try to load my index.php file from localhost.

Parse error: syntax error, unexpected T_ELSE in C:\wamp\www\Simpleblog\inc\functions.inc.php on line 38

& yeah I’m follwing “PHP for absolute beginners”
here is the code:

<?php
function retrieveEntries($db, $id=NULL)
{
/*
*If an entry ID was supplied, load the associated entrys
*/
if(isset($id))
{
$sql = "SELECT title, entry
FROM entries
WHERE id=?
LIMIT 1";
$stmt = $db->prepare($sql);
$stmt->execute(array($_GET['id']));
// Save the returned entry array
$e = $stmt->fetch();
// Set the fulldisp flag for a single entry
$fulldisp = 1;
}

function sanitizeData($data)
   {
// If $data is not an array, run strip_tags()
if(!is_array($data))
{
// Remove all tags except <a> tags
return strip_tags($data, "<a>");
}
// If $data is an array, process each element
else
{
// Call sanitizeData recursively for each array element
return array_map('sanitizeData', $data);
}
}


else
{
$sql = "SELECT id, title
FROM entries
ORDER BY created DESC";
// Loop through returned results and store as an array
foreach($db->query($sql) as $row)
				 {
				$e[] = array(
				'id' => $row['id'],
				'title' => $row['title']
				);
				}
// Set the fulldisp flag for multiple entries
$fulldisp = 0;

/*
* If no entries were returned, display a default
* message and set the fulldisp flag to display a
* single entry
*/
if(!is_array($e))
{
$fulldisp = 1;
$e = array(
'title' => 'No Entries Yet',
'entry' => '<a href="/admin.php">Post an entry!</a>'
);
}
}
// Add the $fulldisp flag to the end of the array
array_push($e, $fulldisp);
return $e;
}
?>

That’s why indenting of the code is important:


<?php
function retrieveEntries($db, $id=NULL)
{
  /*
  *If an entry ID was supplied, load the associated entrys
  */
  if(isset($id))
  {
    $sql = "SELECT title, entry
            FROM entries
            WHERE id=?
            LIMIT 1";
    $stmt = $db->prepare($sql);
    $stmt->execute(array($_GET['id']));
    // Save the returned entry array
    $e = $stmt->fetch();
    // Set the fulldisp flag for a single entry
    $fulldisp = 1;
  }

  function sanitizeData($data)
  {
    // If $data is not an array, run strip_tags()
    if(!is_array($data))
    {
      // Remove all tags except <a> tags
      return strip_tags($data, "<a>");
    }
    // If $data is an array, process each element
    else
    {
      // Call sanitizeData recursively for each array element
      return array_map('sanitizeData', $data);
    }
  }


  else     // <-- this is line 38 !!!!!!!!!!!!!!!!!!!!!!
  {
    $sql = "SELECT id, title
            FROM entries
            ORDER BY created DESC";
    // Loop through returned results and store as an array
    foreach($db->query($sql) as $row)
    {
      $e[] = array(
                'id' => $row['id'],
                'title' => $row['title']
                );
    }
    // Set the fulldisp flag for multiple entries
    $fulldisp = 0;

    /*
    * If no entries were returned, display a default
    * message and set the fulldisp flag to display a
    * single entry
    */
    if(!is_array($e))
    {
      $fulldisp = 1;
      $e = array(
            'title' => 'No Entries Yet',
            'entry' => '<a href="/admin.php">Post an entry!</a>'
          );
    }
  }
  // Add the $fulldisp flag to the end of the array
  array_push($e, $fulldisp);
  return $e;
}
?> 

Can you see the problem with line 38?

I’d have to say that it’s probably because you have a function inside of a function, nestled in the middle of your IF/ELSE block. Move the function sanitizeData outside of function retrieveEntries.

nope…doesn’t work
function retrieveEntries($db, $id=NULL)
{
/*
*If an entry ID was supplied, load the associated entrys
*/
if(isset($id))
{
$sql = “SELECT title, entry
FROM entries
WHERE id=?
LIMIT 1”;
$stmt = $db->prepare($sql);
$stmt->execute(array($_GET[‘id’]));
// Save the returned entry array
$e = $stmt->fetch();
// Set the fulldisp flag for a single entry
$fulldisp = 1;
}
}

		function sanitizeData($data)
		{
		// If $data is not an array, run strip_tags()
		if(!is_array($data))
		{
		// Remove all tags except &lt;a&gt; tags
		return strip_tags($data, "&lt;a&gt;");
		}
		// If $data is an array, process each element
		else
		{
		// Call sanitizeData recursively for each array element
		return array_map('sanitizeData', $data);
		}
			}

I meant like this:

<?php
function sanitizeData($data)
{
    // If $data is not an array, run strip_tags()
    if(!is_array($data))
    {
      // Remove all tags except <a> tags
      return strip_tags($data, "<a>");
    }
    // If $data is an array, process each element
    else
    {
      // Call sanitizeData recursively for each array element
      return array_map('sanitizeData', $data);
    }
}

function retrieveEntries($db, $id=NULL)
{
  /*
  *If an entry ID was supplied, load the associated entrys
  */
  if(isset($id))
  {
    $sql = "SELECT title, entry
            FROM entries
            WHERE id=?
            LIMIT 1";
    $stmt = $db->prepare($sql);
    $stmt->execute(array($_GET['id']));
    // Save the returned entry array
    $e = $stmt->fetch();
    // Set the fulldisp flag for a single entry
    $fulldisp = 1;
  }
  else     // <-- this is line 38 !!!!!!!!!!!!!!!!!!!!!!
  {
    $sql = "SELECT id, title
            FROM entries
            ORDER BY created DESC";
    // Loop through returned results and store as an array
    foreach($db->query($sql) as $row)
    {
      $e[] = array(
                'id' => $row['id'],
                'title' => $row['title']
                );
    }
    // Set the fulldisp flag for multiple entries
    $fulldisp = 0;

    /*
    * If no entries were returned, display a default
    * message and set the fulldisp flag to display a
    * single entry
    */
    if(!is_array($e))
    {
      $fulldisp = 1;
      $e = array(
            'title' => 'No Entries Yet',
            'entry' => '<a href="/admin.php">Post an entry!</a>'
          );
    }
  }
  // Add the $fulldisp flag to the end of the array
  array_push($e, $fulldisp);
  return $e;
}
?> 

Thank you so much…it worked! well inside the tutorial it said to put sanitizedata function below retrieve entries so Id id according to that bt ur one worked!

Below retreive entries means below the entire function. Not half way. That’s why indenting your code properly is important.