473,789 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_arr ay($urls, MYSQL_ASSOC)) {
$pn = $url['post_name'];

while ($date = mysql_fetch_arr ay($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 2337
to********@gmai l.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_arr ay($urls, MYSQL_ASSOC)) {
$pn = $url['post_name'];

while ($date = mysql_fetch_arr ay($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...@gmai l.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_arr ay($urls, MYSQL_ASSOC)) {
$pn = $url['post_name'];

while ($date = mysql_fetch_arr ay($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_arr ay($urls, MYSQL_ASSOC)) && ($date =
mysql_fetch_arr ay($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********@gmai l.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_arr ay($urls, MYSQL_ASSOC)) && ($date =
mysql_fetch_arr ay($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_u l u
join the_table_for_d ate d on
(u.something = d.something_els e)
the something and something_else are matching keys in the two tables.

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

while ($row = mysql_fetch_arr ay($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
5119
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 we need to break once more... So maybe there should be an argument to break that is an int which is a number of loops to break. So it would default to 1 (or whatever means the current enclosing loop), not breaking any code...
6
2570
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: (http://www.python.org/search/hypermail/python-1993/0343.html) "This is because nested function definitions don't have access to the local variables of the surrounding block -- only to the globals of the
46
9941
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 already in a loop." Then he goes on to portray a contrived example that doesn't tell me under what conditions a nested loop might be favoured as a solution? i.e. what are nested loops useful for? What kinds of algorithms are served by nested loops?...
5
11230
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
2490
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
5251
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. Is this because .NET is inherently slower or does the C# compiler merely produce code that is not as well optimized as the C++ compiler?
9
2854
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
3127
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 trying to understand Goto in this contect. PS: This is not homework.
8
7265
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 <Boolean ExpressionThen Exit For Next If Not <Boolean ExpressionThen Exit For
0
10408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10199
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3700
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.