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;
}
?>
<?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;
}
?>
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 <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);
}
}
<?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!