Jump to content


Photo

PHP in HTML cuts off HTML (Fixed!)


  • Please log in to reply
12 replies to this topic

#1 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 10 August 2006 - 08:28 PM

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.

#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 10 August 2006 - 08:29 PM

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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 10 August 2006 - 08:34 PM

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


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

#4 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 10 August 2006 - 08:39 PM

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


#5 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 10 August 2006 - 08:45 PM

Ok I don't know if you just changed the
define
before posting it here but you need to do something like this
define("host", "yourhostname");


now in your function do something like this.

function whatever() {
    $result = "This is something simple";
    return $result;
}

$whatever = whatever();

echo "$whatever";

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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#6 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 10 August 2006 - 10:57 PM

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?

#7 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 11 August 2006 - 03:30 AM

Bump... I've tried about everything and this still wont work...

#8 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 11 August 2006 - 03:39 AM

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.
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#9 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 11 August 2006 - 03:40 AM

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. 

#10 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 11 August 2006 - 03:43 AM

Ah, I didn't read your code close enough to realize you were just defining a function up there...
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#11 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 11 August 2006 - 03:55 AM

Free cookies and cupcakes if you solve this! I'm on like hour 3 here...

#12 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 11 August 2006 - 12:44 PM

what I meant by this post is you need to
return
the desired result, the mysql_fetch_assoc or array.

What I would do is this

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

What this does is
return
the variable $table. Now I would do something like this (where ever you want the table to be located in your html.

<?php
$table = print_news();

echo "$table";
?>

This will echo the returned varable  from the function. I also recomend reading up on user defined functions and maybe return.

Hope this helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#13 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 11 August 2006 - 03:10 PM

Fixed! Turned out to be the 'or die' in the while loop.  Thanks for all the suggestions everyone!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users