SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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/

    Code:
    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/

    Code:
    <?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

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    435
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Is this from a syntax error in the second file? For example:

    Code:
    <?php if (isset($jokes)): ?>
    wouldn't that normally read

    Code:
    <?php if (isset($jokes)){ ?>
    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.
    http://www.firenza.net - my homage to a car from the 1970s

  3. #3
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 :
    Code:
    <?php foreach ($foo as $bar) { ?>
    <p><?php echo $bar; } ?></p>
    is not valid php.
    neither :
    Code:
    <?php 
    foreach ($foo as $bar) {
    <p>echo $bar;</p> }
    ?>
    it s actually ugly

    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
    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'

  4. #4
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,065
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mazzaru View Post
    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):

    PHP Code:
    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).
    Last edited by SpacePhoenix; Jan 14, 2014 at 13:20.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    435
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mazzaru View Post
    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; ?>
    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.
    http://www.firenza.net - my homage to a car from the 1970s

  7. #7
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  8. #8
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    435
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.
    http://www.firenza.net - my homage to a car from the 1970s


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •