/    Sign up×
Community /Pin to ProfileBookmark

Loop not finishing

I have a bad habit of over-explaining myself (in what I say and my coding), so I want to apologize for that ahead of time.

OK, here is what I am working with. I have 4 tables: news, recipes, components, and finished product. When I add a new recipe, component, or finished product to their table, I timestamp them with the date they are entered (called datecreated) or modified (called datemodified). When I make a news post, I timestamp it also for the day I made it (called dateid). Both time stamps are the same format.

I am wanting to make it so when I make a news posts, it checks the other 3 tables for entires created or modified on that day, and prints them at the end of the news post. I am going to make a news post each day and I have the news post set up in a loop.

Here is the problem. I made a news post for Janurary 5th. When I looked at the page, it displays the news posts, as well as the entries created that day. I then made a news post for Janurary 6th. Well, it is not looping and displaying the 6th’s news posts below the 5th’s.

Here is the page for reference of what it is doing: [URL=http://www.amyandjessica.com/salvage/index3.php]http://www.amyandjessica.com/salvage/index3.php[/URL]

Been staring at my coding for almost 2 days, and I can’t find it. Here is the code:

[code=php]<?php
//connecting to server
$dbh = mysql_connect (“localhost”, “username”, “password”) or die (‘I cannot connect to the server because: ‘ . mysql_error());
//connecting to database
mysql_select_db (“database”, $dbh) or die (‘I can not connect to the database because: ‘ . mysql_error());
//retriving data
$sql = “SELECT * FROM news”;
$result = mysql_query($sql, $dbh) or die(‘Could not retrive data from table because: ‘ . mysql_error());

$number_of_rows = mysql_num_rows($result);

//starting news loop

for ($y=1; $y<=($number_of_rows); $y++)
{
while ($newArray = mysql_fetch_array($result))
{

$id = $newArray[‘id’];
$date = $newArray[‘date’];
$submittedby = $newArray[‘submittedby’];
$post = $newArray[‘post’];
$dateid = $newArray[‘dateid’];

echo ‘<table width=”700″>’;
echo ‘<tr>’;
echo ‘ <td bgcolor=”#C09C54″><strong><font color=”FFFFFFF”><div align=”center”>’.$date.'</div></font></strong></td>’;
echo ‘</tr>’;
echo ‘<tr>’;
echo ‘ <td><font size=”2″>’;
echo ‘ <br><hr align=”center” width=”50%”>’.$post.'<hr align=”center” width=”50%”></font></td>’;
echo ‘</tr>’;
echo ‘<tr>’;
echo ‘<td><font size=”2″>’;

//checking 1st table for new entries and printing if any

//connecting to server
$dbh = mysql_connect (“localhost”, “username”, “password”) or die (‘I cannot connect to the server because: ‘ . mysql_error());
//connecting to database
mysql_select_db (“database”, $dbh) or die (‘I can not connect to the database because: ‘ . mysql_error());
//retriving data
$sql = “SELECT * FROM salvage where datecreated = ‘$dateid’ “;
$result = mysql_query($sql, $dbh) or die(‘Could not retrive data from table because: ‘ . mysql_error());

$number_of_rows = mysql_num_rows($result);

if ($number_of_rows >0)
{
echo ‘<strong>New Base Items :: </strong>’;

for ($y=1; $y<=($number_of_rows); $y++)
{
while ($newArray = mysql_fetch_array($result))
{

$id = $newArray[‘id’];
$name = $newArray[‘name’];

echo ‘<a href=”salvage.php?id=’.$id.'”>’.$name.'</a> <strong>::</strong> ‘;

}
}
echo ‘<p>’;

}

//checking 2nd table for new entries and printing if any

//connecting to server
$dbh = mysql_connect (“localhost”, “username”, “password”) or die (‘I cannot connect to the server because: ‘ . mysql_error());
//connecting to database
mysql_select_db (“database”, $dbh) or die (‘I can not connect to the database because: ‘ . mysql_error());
//retriving data
$sql = “SELECT * FROM recipies where datecreated = ‘$dateid’ “;
$result = mysql_query($sql, $dbh) or die(‘Could not retrive data from table because: ‘ . mysql_error());

$number_of_rows = mysql_num_rows($result);

if ($number_of_rows >0)
{
echo ‘<strong>New Recipes :: </strong>’;

for ($y=1; $y<=($number_of_rows); $y++)
{
while ($newArray = mysql_fetch_array($result))
{

$id = $newArray[‘id’];
$name = $newArray[‘name’];

echo ‘<a href=”recipes.php?id=’.$id.'”>’.$name.'</a> <strong>::</strong> ‘;

}
}
echo ‘<p>’;

}

//checking 3rd table for new entries and printing if any

//connecting to server
$dbh = mysql_connect (“localhost”, “username”, “password”) or die (‘I cannot connect to the server because: ‘ . mysql_error());
//connecting to database
mysql_select_db (“database”, $dbh) or die (‘I can not connect to the database because: ‘ . mysql_error());
//retriving data
$sql = “SELECT * FROM drops where datecreated = ‘$dateid’ “;
$result = mysql_query($sql, $dbh) or die(‘Could not retrive data from table because: ‘ . mysql_error());

$number_of_rows = mysql_num_rows($result);

if ($number_of_rows >0)
{
echo ‘<strong>New Salvage Drops :: </strong>’;

for ($y=1; $y<=($number_of_rows); $y++)
{
while ($newArray = mysql_fetch_array($result))
{

$id = $newArray[‘id’];
$name = $newArray[‘name’];

echo ‘<a href=”drops.php?id=’.$id.'”>’.$name.'</a> <strong>::</strong> ‘;

}
}
echo ‘<p>’;

}

//checking 1st table for modified entries and printing if any

//connecting to server
$dbh = mysql_connect (“localhost”, “username”, “password”) or die (‘I cannot connect to the server because: ‘ . mysql_error());
//connecting to database
mysql_select_db (“database”, $dbh) or die (‘I can not connect to the database because: ‘ . mysql_error());
//retriving data
$sql = “SELECT * FROM salvage where datemodified = ‘$dateid’ “;
$result = mysql_query($sql, $dbh) or die(‘Could not retrive data from table because: ‘ . mysql_error());

$number_of_rows = mysql_num_rows($result);

if ($number_of_rows >0)
{
echo ‘<strong>Updated Base Items :: </strong>’;

for ($y=1; $y<=($number_of_rows); $y++)
{
while ($newArray = mysql_fetch_array($result))
{

$id = $newArray[‘id’];
$name = $newArray[‘name’];

echo ‘<a href=”salvage.php?id=’.$id.'”>’.$name.'</a> <strong>::</strong> ‘;

}
}
echo ‘<p>’;

}

//checking 2nd table for modified entries and printing if any

//connecting to server
$dbh = mysql_connect (“localhost”, “username”, “password”) or die (‘I cannot connect to the server because: ‘ . mysql_error());
//connecting to database
mysql_select_db (“database”, $dbh) or die (‘I can not connect to the database because: ‘ . mysql_error());
//retriving data
$sql = “SELECT * FROM recipies where datemodified = ‘$dateid’ “;
$result = mysql_query($sql, $dbh) or die(‘Could not retrive data from table because: ‘ . mysql_error());

$number_of_rows = mysql_num_rows($result);

if ($number_of_rows >0)
{
echo ‘<strong>Updated Recipes :: </strong>’;

for ($y=1; $y<=($number_of_rows); $y++)
{
while ($newArray = mysql_fetch_array($result))
{

$id = $newArray[‘id’];
$name = $newArray[‘name’];

echo ‘<a href=”recipes.php?id=’.$id.'”>’.$name.'</a> <strong>::</strong> ‘;

}
}
echo ‘<p>’;

}

//checking 3rd table for modified entries and printing if any

//connecting to server
$dbh = mysql_connect (“localhost”, “username”, “password”) or die (‘I cannot connect to the server because: ‘ . mysql_error());
//connecting to database
mysql_select_db (“database”, $dbh) or die (‘I can not connect to the database because: ‘ . mysql_error());
//retriving data
$sql = “SELECT * FROM drops where datemodified = ‘$dateid’ “;
$result = mysql_query($sql, $dbh) or die(‘Could not retrive data from table because: ‘ . mysql_error());

$number_of_rows = mysql_num_rows($result);

if ($number_of_rows >0)
{
echo ‘<strong>Updated Salvage Items :: </strong>’;

for ($y=1; $y<=($number_of_rows); $y++)
{
while ($newArray = mysql_fetch_array($result))
{

$id = $newArray[‘id’];
$name = $newArray[‘name’];

echo ‘<a href=”drops.php?id=’.$id.'”>’.$name.'</a> <strong>::</strong> ‘;

}
}
echo ‘<p>’;

}

echo ‘<hr align=”center” width=”50%”>’;
echo ‘</font></td>’;
echo ‘</tr>’;
echo ‘</table>’;
}
}
?>[/code]

Thanks for looking at it.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@iydkmightkyauthorJan 08.2006 — Not trying to be pushy, but anyone know what is my problem? This is one of the last thing I need to do before I launch the site.
Copy linkTweet thisAlerts:
@NogDogJan 08.2006 — Well, after starting to look at the code, a couple things jumped out at me early on you should take a look at, perhaps:

1.
[code=php]
$result = mysql_query($sql, $dbh) or die('Could not retrive data from table because: ' . mysql_error());

$number_of_rows = mysql_num_rows($result);

//starting news loop

for ($y=1; $y<=($number_of_rows); $y++)
{
while ($newArray = mysql_fetch_array($result))
{ [/code]

The for loop seems to indicate you're going to walk through each query result row via that loop, but then you follow it immediately with a while loop that is also going to walk through each result row. This would imply to me that on any iteration of the for loop after the first, the result row counter will already be at the end of the result set due to the while loop cycling through all the results.

  • 2. Within those for and while loops, you do additional queries, but return the results to the same variable name as used in the initial query: $result. That would mean that those later queries would be overwriting the results of the earlier query, which I would be surprised if that's what you intended.
  • Copy linkTweet thisAlerts:
    @iydkmightkyauthorJan 08.2006 — 1.) Hm...always ran my loops like that and never had a problem. In fact the code is still using this set-up. But I will look at getting a better way to do my if statements.

    2.) Beautiful! This was what was giving me my problem. A simple change of the variable names for each query section fixed it!

    Thank you very much for the help!
    ×

    Success!

    Help @iydkmightky spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...