problem with table size [only occurs when echoed in php]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
legaxy
Forum Newbie
Posts: 19
Joined: Thu Mar 20, 2003 3:26 am
Location: Auckland, New Zealand
Contact:

problem with table size [only occurs when echoed in php]

Post by legaxy »

I'm making a news system on my site, so I made up a html-table which has a set width of 450px, so I have it like this

Code: Select all

<?php
fetch_rows(......)
echo " table info... $rows[1]... $rows[2] etc";
?>
all works fine, the first entry appears and it should but then the second one ignores the size rules and spans the whole page. The second entry (test scenario) is a lot bigger, but still it should obey the width rule right?
any help would be great!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Could we see your actual code? It'll help us determine where the problem might be.

Mac
legaxy
Forum Newbie
Posts: 19
Joined: Thu Mar 20, 2003 3:26 am
Location: Auckland, New Zealand
Contact:

Post by legaxy »

Code: Select all

<?php
<?
$connect = mysql_connect("*****", "*****", "")
        or die("Error! Could not connect: " . mysql_error());
  $db = mysql_select_db("venturer" , $connect);
  $query = "SELECT * FROM news ORDER BY id DESC LIMIT 5";
  $result = mysql_query($query);
  while ($rows = mysql_fetch_row ($result)) {
echo (" <table width="449" border="0" align="center" cellpadding="5" cellspacing="0" class="news"><tr><td valign="top"><p><span class="newstitle"> $rows[5] </span><span class="newstop"> <br>posted by <a href="index.php?page=profiles&profile=$row[3]" class="link">$rows[3]</a> on $rows[1]</span><blockquote class="bodytext"><p class="newstext">$rows[6]</p></blockquote></td></tr></table><p>&nbsp;</p> ");
};
  ?>
?>
it returns the first entry correctly, but the second one goes across the whole page. :?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

does class="news" have it's own width definition?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What might be easiest is if you have a some formatting in the HTML, try something like:

Code: Select all

<?php 

$connect = mysql_connect("*****", "*****", "") 
        or die("Error! Could not connect: " . mysql_error()); 
$db = mysql_select_db("venturer" , $connect); 
$query = "SELECT * FROM news ORDER BY id DESC LIMIT 5"; 
$result = mysql_query($query); 

// it's generally better to use mysql_fetch_assoc() because then you can reference
// things like $row['column_name'] and don't have to worry if a column is deleted
// or a new one added in the middle messing up all the numbering
while ($rows = mysql_fetch_row($result)) { 
	// splitting out the echo statements using something like heredoc format can make
	// it a lot easier to debug your HTML
	// NB. With here doc (the <<<END ... END; stuff) you have to make sure that there are
	// no spaces, characters, or tabs before or after END; or after <<<END otherwise you
	// get errors
	// Bonus of here doc - don't have to escape any quotes 
	echo <<<END

<table width="449" border="0" align="center" cellpadding="5" cellspacing="0" class="news">
<tr>
	<td valign="top">
		<p><span class="newstitle">{$rows[5]}</span><br />
		<span class="newstop">posted by <a href="index.php?page=profiles&profile={$row[3]}" class="link">{$rows[3]}</a> on {$rows[1]}</span></p>
		<blockquote class="bodytext"><p class="newstext">{$rows[6]}</p></blockquote>
	</td>
</tr>
</table>
<br />
END; 
}; 

?>
(and take heed of the comments)

Then check the source of the page when it's loaded to see what it says. Also perhaps as Oromian suggested, you may have some width definitions in your CSS - could we see the CSS class information that you're using within the table?

Mac
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

there's a </p> missing in your code.
using this style

Code: Select all

<?php
$connect = mysql_connect("*****", "*****", "")
        or die("Error! Could not connect: " . mysql_error());
  $db = mysql_select_db("venturer" , $connect);
  $query = "SELECT * FROM news ORDER BY id DESC LIMIT 5";
  $result = mysql_query($query);
  while ($rows = mysql_fetch_row ($result))
  {
?>  	
		<table width="449" border="0" align="center" cellpadding="5" cellspacing="0" class="news">
			<tr>
				<td valign="top">
					<p>
						<span class="newstitle"><?php echo $rows[5]; ?></span>
						<span class="newstop">
							<br />
							posted by
							<a href="index.php?page=profiles&profile=<?php echo $row[3];?>" class="link">
								<?php echo $rows[3]; ?>
							</a>
							on <?php echo $rows[1];?>
						</span>
						<blockquote class="bodytext">
							<p class="newstext">
								<?php echo $rows[6]; ?>
							</p>
						</blockquote>
					</p><!-- added -->
				</td>
			</tr>
		</table>
		<p> </p>
<?php
}
?>
it's much easier to find such errors there.
It's untested, so I don't know wether this fixes your problem.

btw: a table with only one cell... is it really necessary?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Either way, by using heredoc (which ok can be painful sometimes but I like it) or by exiting out of the PHP to do the HTML, should help you see your HTML properly formatted instead of all on one line (which is a real pain).

Mac
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

and another note: you're sure none of your $row-elements contains something that might break down your html-structure?

Code: Select all

<?php
$connect = mysql_connect("*****", "*****", "")
        or die("Error! Could not connect: " . mysql_error());
  $db = mysql_select_db("venturer" , $connect);
  $query = "SELECT * FROM news ORDER BY id DESC LIMIT 5";
  $result = mysql_query($query);
  while ($rows = mysql_fetch_row ($result))
  {
?>     
      <table width="449" border="0" align="center" cellpadding="5" cellspacing="0" class="news">
         <tr>
            <td valign="top">
               <p>
                  <span class="newstitle"><?php echo htmlentities($rows[5]); ?></span>
                  <span class="newstop">
                     <br />
                     posted by
                     <a href="index.php?page=profiles&profile=<?php echo urlencode($row[3]);?>" class="link">
                        <?php echo htmlentities($rows[3]); ?>
                     </a>
                     on <?php echo htmlentities($rows[1]);?>
                  </span>
                  <blockquote class="bodytext">
                     <p class="newstext">
                        <?php echo htmlentities($rows[6]); ?>
                     </p>
                  </blockquote>
               </p>
            </td>
         </tr>
      </table>
      <p> </p>
<?php
}
?>
legaxy
Forum Newbie
Posts: 19
Joined: Thu Mar 20, 2003 3:26 am
Location: Auckland, New Zealand
Contact:

Post by legaxy »

hey, fixed it, ... i tried putting it all in the way twigletmac suggested, didnt improve it but I emptied the table and put some more posts in, and its fixed itself up now, not too sure wat the problem was, must have been the contents of those posts, the large one had some line breaks in them, so that might have led to it.
thanks all!
Post Reply