473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

nested loops only loop once

I am using two while loops that are nested. The first loop (post name)
returns the full column of results, but the second (post modified)
only returns the first row of the column. Is there another way I could
write this to get both loops to complete fully?

I am using the two while loops to pull data from different tables, and
insert that data into a list that has html code surrounding each loop.

while ($url = mysql_fetch_array($urls, MYSQL_ASSOC)) {
$pn = $url['post_name'];

while ($date = mysql_fetch_array($lmdate, MYSQL_ASSOC)) {
$lm = $date['post_modified'];

echo "<b>";
echo $pn;
echo "</b>";
echo "more html";
echo date('Y-m-d', strtotime($lm));
echo "and more html";
}
}

Thank you.

HG
Jul 23 '08 #1
4 2315
to********@gmail.com wrote:
I am using two while loops that are nested. The first loop (post name)
returns the full column of results, but the second (post modified)
only returns the first row of the column. Is there another way I could
write this to get both loops to complete fully?

I am using the two while loops to pull data from different tables, and
insert that data into a list that has html code surrounding each loop.

while ($url = mysql_fetch_array($urls, MYSQL_ASSOC)) {
$pn = $url['post_name'];

while ($date = mysql_fetch_array($lmdate, MYSQL_ASSOC)) {
$lm = $date['post_modified'];

echo "<b>";
echo $pn;
echo "</b>";
echo "more html";
echo date('Y-m-d', strtotime($lm));
echo "and more html";
}
}

Thank you.

HG
What is it that you want to see?

Are you sure that it is the inner loop that only appears once? From the
way it is written it appears that it is the outer loop that will echo
only once and the inner loop will exhaust itself on the first row from
the outer loop. IOW, it looks like you should get:

pn1 lm1
pn1 lm2
.....
pn1 lmn
pn2 no echo since the echo is inside the inner loop
.....
pnn no echo since the echo is inside the inner loop
Jul 23 '08 #2
On Jul 23, 6:00 am, toddlah...@gmail.com wrote:
I am using two while loops that are nested. The first loop (post name)
returns the full column of results, but the second (post modified)
only returns the first row of the column. Is there another way I could
write this to get both loops to complete fully?

I am using the two while loops to pull data from different tables, and
insert that data into a list that has html code surrounding each loop.

while ($url = mysql_fetch_array($urls, MYSQL_ASSOC)) {
$pn = $url['post_name'];

while ($date = mysql_fetch_array($lmdate, MYSQL_ASSOC)) {
$lm = $date['post_modified'];

echo "<b>";
echo $pn;
echo "</b>";
echo "more html";
echo date('Y-m-d', strtotime($lm));
echo "and more html";
}

}

Thank you.

HG
Its not a PHP problem - I write nested loops all the time. How
confident are you that both queries are returning more than row? Have
you checked if it is throwing an error?

I don't know if you've deliberately edited down the code for
illustration purposes, but do you really intend to get a Cartesian
Product of the two queries?

C.
Jul 23 '08 #3
I am a new to this so forgive my ignorance. Realizing that using two
loops for two arrays was not outputting the desired results, I then
combined both arrays into one while loop. Then I pulled out the data I
wanted from each loop into a variable, and outputted that into the
form. The trick was to output two lists at the same time like this:
1
2
1
2
1
2

To create one big list. Here's how I did it for anyone having the same
difficulty.

while ( ($url = mysql_fetch_array($urls, MYSQL_ASSOC)) && ($date =
mysql_fetch_array($urls, MYSQL_ASSOC)) ) {
$pn = $url['post_name'];
$lm = $date['post_modified'];

echo "<b>";
echo $pn;
echo "</b>";
echo "more html";
echo date('Y-m-d', strtotime($lm));
echo "and more html";

}

Outputs
$pn
$lm
$pn
$lm
$pn
$lm
Jul 23 '08 #4
to********@gmail.com wrote:
I am a new to this so forgive my ignorance. Realizing that using two
loops for two arrays was not outputting the desired results, I then
combined both arrays into one while loop. Then I pulled out the data I
wanted from each loop into a variable, and outputted that into the
form. The trick was to output two lists at the same time like this:
1
2
1
2
1
2

To create one big list. Here's how I did it for anyone having the same
difficulty.

while ( ($url = mysql_fetch_array($urls, MYSQL_ASSOC)) && ($date =
mysql_fetch_array($urls, MYSQL_ASSOC)) ) {
$pn = $url['post_name'];
$lm = $date['post_modified'];

echo "<b>";
echo $pn;
echo "</b>";
echo "more html";
echo date('Y-m-d', strtotime($lm));
echo "and more html";

}

Outputs
$pn
$lm
$pn
$lm
$pn
$lm
Uh, if the above output is what you want, why don't you simply combine
the two into a single query with a "join"?

select u.post_name as pn, d.post_modified as lm
from the_table_for_ul u
join the_table_for_date d on
(u.something = d.something_else)
the something and something_else are matching keys in the two tables.

Then, after doing:
$result = mysql_query(the_select_statement_above)
all you need do is have

while ($row = mysql_fetch_array($result)) {
$pn = $row['pn'];
$lm = $row['lm'];
}
Jul 23 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Oleg Leschov | last post by:
Could there be means of exiting nested loops in python? something similar to labelled loops in perl.. I consider it irrating to have to make a flag for sole purpose of checking it after loop if...
6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
5
by: dawn | last post by:
Hi, Once again I have a question : In my app, I have nested loops, very basic stuff, like for (int i = 0; i < 50; i++) { for (int j = 0 ; j < 75; j++) {
9
by: Javaman59 | last post by:
Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } }
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.