Jump to content

[SOLVED] Looping and Echoing


Akenatehm

Recommended Posts

Hey Guys,

 

I am trying to make a news script that echoes the results through a loop.

 

CUrrently, nothing is coming up on the page.

 

Here's index.php:

<?php
include 'functions.php';
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Exclusive Mac - Home</title>
<link rel="stylesheet" href="styles.css" type="text/css" />
<script type="text/javascript">
function mouseOver()
{
document.getElementById('entry').className = "hoverentry";
}
function mouseOut()
{
document.getElementById('entry').className = "normalentry";
}
</script>
</head>

<body>
<div class="header">
<img src="images/header.png" alt="Header" />
</div>

<ul id="nav">
  <li><a href="#">Home</a></li>
  <li><a href="#">News</a></li>
  <li><a href="#">Forums</a></li>
  <li><a href="#">Applications</a></li>
  <li><a href="#">Other</a></li>
</ul>
<?php
writeNewsEntry();
?>

</body>
</html>

and here's functions.php

<?php
function writeNewsEntry()
{
$user = "user";
$pass = "pass";
$host = "localhost";
$db = "exclusivemac";

$connect = mysql_connect($host,$user,$pass);
$selectdatabase = mysql_select_db($db);

$getentries = mysql_query("SELECT * FROM newsentries");

while($filterentries = mysql_fetch_assoc($getentries))
{
$title = $filterentries['title'];
$content = $filterentries['content'];
$author = $filterentries['author'];
$date = $filterentries['date'];

echo '<div id="entry" class="normalentry" onMouseOver="mouseOver()" onMouseOut="mouseOut()">';
echo '<h3 class="newsentry">' . "$title - " . "</h3>";
echo '<p class="newsdate">' . "$date"  . "</p> <br />";
echo '<p class="newsentry">' .  "$content" . '</p>';
echo '<p class="newsposter">Posted By' . "$author" . "</p> </div>";
}
}
?>

 

Help Would be Appreciated.

Link to comment
Share on other sites

Second is your while loop counting upward? what i mean is does the while loop ever end? You might want to use $i++ inside while loop.

What error u getting?

 

That's being extremely ignorant. He's fetching rows from a table, so the while loop will end automatically when all the rows have fetched.

Link to comment
Share on other sites

Thanks Festy. I was wondering what fry2010 was talking about?

 

Where is a better place to connect? If I connect through another function, will it still be connected and ready to use in other functions? Or If I connect through a main php page, will that also be usable in other functions?

 

Like I said, I am not getting errors, just blank white space with no echoed data.

Link to comment
Share on other sites

Turn your error reporting on.  At the top of your page, put

 

ini_set("display_errors","2");
ERROR_REPORTING(E_ALL);

 

Add ..or die(mysql_error()); to the end of all of your msql function calls. 

 

Any error messages display? The error messages from the ..die()'s are pretty straight forward.  You should be able to fix it from there, no problem.  If not, post the error message.

 

No error messages? 

 

Make sure you are using the right column names when assigning the fetch_assoc array data to your "easier to handle" variables.  Case sensitive!. 

 

Everything line up?  Check your database directly to ensure that the data you expect to be there, is there.  Run your query directly in the database.  Expected info return? 

 

Still no dice?  Obviously, what we have here is a case of spiderbots feasting on your data while its in transit from the db to your php script.  True story.

 

 

Link to comment
Share on other sites

actually it wasnt ignorant because I had the problem before, the while loop didnt end even when it finished collecting data from database. What help did u offer? none. With the limited info available i offered at least some help, even if it wasnt appreciated.

How stupid r u for not having any data in the database and not even realising it...

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.