Link to home
Create AccountLog in
Avatar of DragonByte
DragonByte

asked on

'New' creating blank line in output

I am using the code for RSS from here:
http://www.webreference.com/authoring/languages/xml/rss/custom_feeds/2.html

The problem is that it's creating a blank line with a tab or spaces before the XML declaration:

<?
  header("Content-Type: application/xml; charset=ISO-8859-1");
  include("classes/RSS.class.php");
  $rss = new RSS();
  echo $rss->GetFeed();
?>

It seems like $rss = new RSS() is the line that is creating the blank line.

if I comment out echo and just echo something like "test", there's still an initial blank line.

What can I do to fix this?
Avatar of hielo
hielo
Flag of Wallis and Futuna image

It sounds like one of your php files starts with "blank" line(s). Your php files MUST start with <? OR <?php. Otherwise whatever blankline exists on the php file will be sent to the browser.
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of DragonByte
DragonByte

ASKER

None of these files are creating the blank lines that I can determine.

Like I said, If I remove the $rss = new RSS() I don't get a new line.

rss.class.php:
<?
  class RSS
  {
        function RSS()
        {
                require_once ('includes/mysql_connect.php');
        }

        function GetFeed()
        {
                return $this->getDetails() . $this->getItems();
        }

        function getDetails()
        {
                $link = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
                $detailsTable = "rss_details";
                $query = "SELECT * FROM ". $detailsTable;
                $result = mysql_db_query (DB_NAME, $query, $link);

                while($row = mysql_fetch_array($result))
                {
                        $details = '<?xml version="1.0" encoding="ISO-8859-1" ?>
                                <rss version="2.0">
                                        <channel>
                                                <title>'. $row['title'] .'</title>
                                                <link>'. $row['link'] .'</link>
                                                <description>'. $row['description'] .'</description>
                                                <language>'. $row['language'] .'</language>
                                                <image>
                                                        <title>'. $row['image_title'] .'</title>
                                                        <url>'. $row['image_url'] .'</url>
                                                        <link>'. $row['image_link'] .'</link>
                                                        <width>'. $row['image_width'] .'</width>
                                                        <height>'. $row['image_height'] .'</height>
                                                </image>';
                }

                return trim($details);
        }

        function getItems()
        {
                $link = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
                $itemsTable = "headlines";
                $query = "SELECT * FROM ". $itemsTable . " ORDER BY posted DESC LIMIT 20";
                $result = mysql_db_query (DB_NAME, $query, $link);
                $items = '';
                while($row = mysql_fetch_array($result))
                {
                        $items .= '<item>
                                <title>'. $row["title"] .'</title>
                                <link>http://xxxxxxx/xxxxxx.php?id='. $row["id"] .'</link>
                                <description><![CDATA['. $row["shortDescription"] .']]></description>
                        </item>';
                }
                $items .= '</channel>
                                </rss>';
                return $items;
        }
}
?>


mysql_connect:
<?
  DEFINE ('DB_USER', 'removed');
  DEFINE ('DB_PASSWORD', 'removed');
  DEFINE ('DB_HOST', 'removed');
  DEFINE ('DB_NAME', 'removed');

  // Make the connnection and then select the database.
  $dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
  mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
?>
Something in mysql_connect was doing it.
>>Like I said, If I remove the $rss = new RSS() I don't get a new line.
That's NOT what you said. You said:
'if I comment out echo and just echo something like "test", there's still an initial blank line.'

and none of the code posted is printing a new line. If in fact you only get a newline when you include
$rss = new RSS(), it's possible the newline is in the data that is retrieved.
I know none of the code posted is printing a new line...that's what's odd about it.

But I fixed it by changing this:

function RSS()
{
                require_once ('includes/mysql_connect.php');
}

To This:

function RSS()
{
                  //require_once("includes/mysql_connect.php");
                  DEFINE ('DB_USER', 'removed');
                  DEFINE ('DB_PASSWORD', 'removed');
                  DEFINE ('DB_HOST', 'removed');
                  DEFINE ('DB_NAME', 'removed');
}


It works JUST FINE!!!!!!!!!

It's really confused me.  The new line was NOT coming in the data.

I may have worded it wrong to begin with but here's what I did to test.

I commented out $rss = new RSS() and then I wrote echo "test" and it printed WITHOUT a new line.  I uncommented $rss and echoed test again and it printed WITH a new line.