Jump to content

PHP in HTML cuts off HTML (Fixed!)


ChaosXero

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.
Link to comment
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.
Link to comment
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??
Link to comment
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.
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.