Joke CMS sitepoint book novice to ninja

hi there,

I’ve encountered an http 500 server error while sending the values of the array jokes from index.php to jokes.html.php.
I’ve print_r the $sql statement and the jokes before sending it to jokes.html.php and it s fine the array has the DB results but when it comes to move on to the jokes.html.php I get the error and it s not very explicit.

here is my code for the index.php in www/admin/jokes/

if (isset($_GET['action']) && $_GET['action'] == 'search') {
	
	//basic SELECT statement
	$select = 'SELECT id, joketext ';
	$from = 'FROM joke ';
	$where = 'WHERE TRUE ';

	$placeholders = array();
	
	// If an author has been selected
	if($_GET['author'] != '') {
		$where .= ' AND authorid = :authorid';
		$placeholders[':authorid'] = $_GET['author'];
		}
		
	//if a category has been selected
	if($_GET['category'] != '') {
		$from .= ' INNER JOIN jokecategory ON id = jokeid ';
		$where .= ' AND categoryid = :categoryid';
		$placeholders[':categoryid'] = $_GET['category'];
	}
	
	//if some text has been type in
	if($_GET['text'] != '') {
		$where .= 'AND joketext LIKE :joketext';
		$placeholders[':joketext'] = '%' . $_GET['text'] . '%';
	}
	
	try {
	
		$sql = $select . $from . $where;
		$s = $pdo->prepare($sql);
		$s->execute($placeholders);
	}
	
	catch (PDOException $e) {
	
			$error = 'Error fecthing jokes: ';
			$error .= $e->getMessage();
			include TEMPLATES . 'error.html.php';
			exit();
	}
	
	foreach($s as $row) {
		$jokes = array(	'id' => $row['id'],
						'text' => $row['joketext']);
		}

	include 'jokes.html.php';
	exit();
}

and here is my code in the jokes.html.php in /www/admin/jokes/

<?php

include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php';
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Manage jokes: Search results</title>
</head>
<body>
<h1>Search results:</h1>

<?php if (isset($jokes)): ?>
	<table>
		<tr>
			<th>Joke text</th><th>Options</th>
		</tr>
		<?php foreach($jokes as $joke):?>
		<tr>
			<td><?php htmlout($joke['text']);?></td>
			<td>
				<form action="?" method="post">
					<div>
						<input type="hidden" name="id" value="<?php htmlout($joke['id']?>" />
						<input type="submit" name="action" value="Edit" />
						<input type="submit" name="action" value="Delete" />
					</div>
				</form>
			</td>
		</tr>
		<?php endforeach;?>
	</table>
<?php endif;?>

	
<p><a href="?">New search</a></p>
<p><a href="..">Return to JMS home</a></p>
</body>
</html>

I’m a bit stuck there.
I’ve tried to echo “test”; in the first line of jokes.html.php (before the include_once) and I get the 500 error message.
but when I print_r($jokes); just before include ‘jokes.html.php’; I have the jokes in the array.
both files are in the same directory.

any lead on that please ?
cheers

Is this from a syntax error in the second file? For example:


<?php if (isset($jokes)): ?>

wouldn’t that normally read


<?php if (isset($jokes))[B]{[/B] ?>

Also the foreach ends in a colon, and the foreach loop ends in ‘endforeach’ rather than a closing brace.

Or is that an alternate syntax? I am no expert by any means, but a quick google suggests that a 500 error can arise from difficulties parsing the php file.

well when you mix html and php it doesn’t work like writing php only.
for the html to be understood has html you can’t use brakets in your php.
so instead you use foreach ($foo as $bar ): in your <?php ?> then write your html and then close you php code with <?php endforeach; ?>

this :


<?php foreach ($foo as $bar) { ?>
<p><?php echo $bar; } ?></p>

is not valid php.
neither :


<?php 
foreach ($foo as $bar) {
<p>echo $bar;</p> }
?>

it s actually ugly :slight_smile:

so no sorry
I really only parse the $jokes to the joke.html.php … that’s it, nothing else… that’s why I tried a simple echo “text” exit(); in the first line of jokes.html.php and it doesn’t show it, I have the 500 http error instead…
thanks anyway for your attempt to solve this :wink:
I might need to configure apache or php to be more explicit about displaying errors 'coz it s quite limited what I can do with just ‘500 http server error’

ok I found it… a parse error on a ‘;’ after htmlout($joke[‘id’]) in joke.html.php …silly… I had to go in the error log of apache to find out, /var/log/apache2/
well I have to find a way to get php to give more explicit error messages.

If you add at the start of the file (after the use of session_start() to avoid any problems with the sending of headers):

error_reporting(-1);
ini_set('display_errors', 1);

EDIT: Any error will be displayed (for a live server errors would need to be logged instead).

OK, thanks for the explanation, I haven’t come across this before. I’ve used mixed php and html in a single file without any trouble, using { and } syntax, but then I’m only using wamp on a local server so maybe it’s a configuration thing.

hi there,

@SpacePhoenix; : thanks mate I’ll do for now.

@droopsnoot; : you are better off not mixing php/html too much. the less you do the better it is. In the beginning I understand that it might be easier but on a long run with php programming you will have to separate frontend html and PHP. The more you get into programming the more you’ll see code that separate things and wrap tasks. good luck to you and you coding :wink:

Thanks. In the environment I’m much more used to (THEOS, in case you’ve heard of it), I would never mix program code and html - the program would output to template variables, then the http server would substitute the values as required into a separate html file.