Jump to content

Archived

This topic is now archived and is closed to further replies.

ChaosXero

PHP in HTML cuts off HTML (Fixed!)

Recommended Posts

I have a php script inside of an HTML document (index.php for my site) and after the PHP executes correctly it stops processing the page.  It just stops everything.  Even echoing the rest of the content does no good.  I can post the code if necessary but has anyone had a similar problem or no of a solution?
Thanks in advance.

Share this post


Link to post
Share on other sites
yea post the code. We need to see it. Also what did you name this piece of code php or html?

Share this post


Link to post
Share on other sites
[code]
<?PHP
define("host",
define("UNAME",
define("pword",

function print_news() {
mysql_connect(host,UNAME,pword) or die(mysql_error());
mysql_select_db();
$sql = "SELECT * FROM `news` ";
$result = mysql_query($sql) or die(mysql_error());
while($res = mysql_fetch_array($result) or die(mysql_error())){
echo "<table border=\"1\">";
echo "<tr><td>".$res['user']."</td><td>".$res['title']."</td><td>".$res['date']."</td></tr>";
echo "<tr><td colspan=\"3\">".$res['detail']."</td></tr>";
echo "</table>";
echo "<hr />";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META NAME="DESCRIPTION" CONTENT="Normal Public Library Teen Advisory Council Book Discussion Group">
<META NAME="KEYWORDS" CONTENT="books,book,discussion,teens,reading,writing,normal,public,library,">
<title>Teen Advisory Home</title>
<link href="styles.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="header"><p>Home | About | Meetings | Books | Links&nbsp;</p></div>
<div id="container">
  <div id="content">
    <p>You've reached the official home of the Normal Public Library's Teen Advisory Council. The groups main goal is to bring in teens who share common ground in the love of books, reading, writing and just having fun. For more info about the group check the 'about' page.</p>
    <hr />
<p><strong>News:</strong></p>
<? print_news(); ?>
  </div>
</div>
<div id="footer">Page Created and Maintained By: Dave Lyon | Content and Design &copy;2006 Dave Lyon<br />
Hosted By: <a href="http://tialys.net">Tialys.net</a></div>
<div class="ad_nugget"><script type="text/javascript"><!--
google_ad_client =
google_ad_width = 120;
google_ad_height = 240;
google_ad_format = "120x240_as";
google_ad_type = "text";
google_ad_channel ="6524514903";
google_color_border = "000000";
google_color_bg = "990000";
google_color_link = "FFFFFF";
google_color_text = "FFFFFF";
google_color_url = "000000";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
</body>
</html>

[/code]

everything up to the print_news() function displays correctly.  After the function nothing shows up.

Share this post


Link to post
Share on other sites
Is the like or die(mysql_error()) in the while loop necessary?

If it's just stopping then the die() seems to be getting triggered.  Though it might not be display the error message because the function might not be returning the value of it??

Share this post


Link to post
Share on other sites
Ok I don't know if you just changed the [code=php:0]define[/code] before posting it here but you need to do something like this [code=php:0]define("host", "yourhostname");[/code]

now in your function do something like this.

[code=php:0]
function whatever() {
   $result = "This is something simple";
   return $result;
}

$whatever = whatever();

echo "$whatever";
[/code]

Not sure if this will solve your problem or not but give it a shot.

Share this post


Link to post
Share on other sites
I removed part of the define because it had my database info in it.  Not sure what you meant by your example.  Can you explain more based on my code?

Share this post


Link to post
Share on other sites
You are outputting HTML to the page before you even put the <HTML> tag.  Move your PHP down to after the <BODY> tag so that you have valid HTML.  This is most likely your error.

Share this post


Link to post
Share on other sites
The PHP used to be where that function call is and still caused the same exact error.  I was hoping making it a function as such would fix it but no luck. 

Share this post


Link to post
Share on other sites
Ah, I didn't read your code close enough to realize you were just defining a function up there...

Share this post


Link to post
Share on other sites
what I meant by this post is you need to [code=php:0]return[/code] the desired result, the mysql_fetch_assoc or array.

What I would do is this

[code]
<?php
define("host", "yourhost");
define("UNAME", "whatever");
define("pword", "password");

function print_news() {
     mysql_connect(host,UNAME,pword) or die(mysql_error());
     mysql_select_db();
     $sql = "SELECT * FROM `news` ";
     $result = mysql_query($sql) or die(mysql_error());
     while($res = mysql_fetch_assoc($result)){

            $table = '<table border="1">
                           <tr>
                               <td>' . $res['user'] . '</td>
                               <td>' . $res['title'] . '</td><td>' . $res['date'] . '</td>
                           </tr>
                           <tr>
                               <td colspan="3">' . $res['detail'] . '</td>
                           </tr>
                        </table>';
    }
    return $table;
}
?>[/code]

What this does is [code=php:0]return[/code] the variable $table. Now I would do something like this (where ever you want the table to be located in your html.

[code=php:0]
<?php
$table = print_news();

echo "$table";
?>[/code]

This will echo the returned varable  from the function. I also recomend reading up on [url=http://us3.php.net/manual/en/language.functions.php]user defined functions[/url] and maybe [url=http://us3.php.net/manual/en/function.return.php]return[/url].

Hope this helps,
Tom

Share this post


Link to post
Share on other sites
Fixed! Turned out to be the 'or die' in the while loop.  Thanks for all the suggestions everyone!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.