473,561 Members | 3,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nested FOR loops. (a loop within a loop)

I can't get my head around them.

I have two MySQL tables, one contains the categories, the other contains
the posts. Each post is linked to a category.

Now I'm trying to display each post with a drop down menu that has the
categories in it and have the category that the post is in selected.

Here's my code for the drop down menu where my woes lie.
for ($i=0; $i<count($posts ); $i++) {

for ($j=1; $j<=count($cate gories); $j++) {

if ($j == $post[$i]['id']) {
selected = ' selected="selec ted"';
} else {
selected = '';
}

$category_list .= '<option value="' . $j . '"' . $selected . '>' . $cat_name[$j] . '</option>';

}
unset($j);

$dropdown .= '<td><select name="' . $cat_name[$i]['name'] . '">'. $category_list . '</select></td>';

}
Here's my problem, for ease, lets say I have 2 categories and 100 posts.

For post one I have 2 categories in the menu. For the next post I have 4
categories in the menu (the 2 have been doubled), for the third I have 6
(tripled), and so on.

Each post after the next has another 2 categories added. So the drop down
menu for post 100 has 200 categories listed. Just the same two repeated
over and over.

The output should look like this, though an <option> for each category,
but just two for this example.

<td><select name="post1"><o ption value="1" selected="selec ted">Cat Name 1</option><option value="2">Cat Name 2</option></select></td>

I've been trying all sorts of things, but nothing seems to work correctly.
Can anyone see where I'm going wrong with my loops? I'm going loopy here
trying to figure it out. :)

Thx

Marc :O)
Jul 17 '05 #1
8 20640
On Monday, February 9, 2004, 23:05:23 Afkamm wrote:
for ($i=0; $i<count($posts ); $i++) { for ($j=1; $j<=count($cate gories); $j++) {


Try this

for ($i=0; $i<count($posts ); $i++) {
$category_list = '';
for ($j=1; $j<=count($cate gories); $j++) {
With $category_list= ''; before the inner loop you will reset
$category_list for each $i. ;)

--
Best regards
Heinz Hombergs

Jul 17 '05 #2
Drinkers log, beer date Mon, 09 Feb 2004 22:25:53 GMT. After downing
several pints in comp.lang.php, Heinz-Gerd Hombergs slurred the
following.
Try this

for ($i=0; $i<count($posts ); $i++) {
$category_list = '';
for ($j=1; $j<=count($cate gories); $j++) {
With $category_list= ''; before the inner loop you will reset
$category_list for each $i. ;)


Heinz-Gerd you hit the nail on the head. The drop down menus appear
correctly now and the page loads far quicker than it did before. :D

Thankyou so much.

Marc :O)
Jul 17 '05 #3
On Tuesday, February 10, 2004, 00:32:04 Afkamm wrote:
Drinkers log, beer date Mon, 09 Feb 2004 22:25:53 GMT. After downing
several pints in comp.lang.php, Heinz-Gerd Hombergs slurred the
I hope the bartender here has some lovely Guinness. ;)
Heinz-Gerd you hit the nail on the head. The drop down menus appear
correctly now and the page loads far quicker than it did before. :D


You want speed? Then make the following changes: ;)

for ($i=0, $max=count($pos ts); $i<$max; $i++) {
$category_list = '';
for ($j=1, $max1=count($ca tegories); $j<=$max1; $j++) {
Cause PHP evaluates for each $i count($posts) and for each $j
count($categori es) in your for loops. With the above code PHP wille
valuate it only once per loop start.

--
Best regards
Heinz Hombergs

Jul 17 '05 #4
Heinz-Gerd Hombergs wrote:
On Tuesday, February 10, 2004, 00:32:04 Afkamm wrote:
Drinkers log, beer date Mon, 09 Feb 2004 22:25:53 GMT. After downing
several pints in comp.lang.php, Heinz-Gerd Hombergs slurred the


I hope the bartender here has some lovely Guinness. ;)


<?php
$dead = false;
while (!$dead) {
$Guiness += 12;
while (--$Guiness) enjoy_the_fines t_beer_in_the_w hole_wide_world ();
get_more_beer($ Guiness) or die();
}
?>
Oh, My God!!!!

Fatal error: Call to undefined function: get_more_beer() in
joke.php on line 6

H E L P M E P L E A S E
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #5
"Pedro Graca" <he****@hotpop. com> wrote in message
news:c0******** *****@ID-203069.news.uni-berlin.de...
Heinz-Gerd Hombergs wrote:
On Tuesday, February 10, 2004, 00:32:04 Afkamm wrote:
Drinkers log, beer date Mon, 09 Feb 2004 22:25:53 GMT. After downing
several pints in comp.lang.php, Heinz-Gerd Hombergs slurred the


I hope the bartender here has some lovely Guinness. ;)


<?php
$dead = false;
while (!$dead) {
$Guiness += 12;
while (--$Guiness) enjoy_the_fines t_beer_in_the_w hole_wide_world ();
get_more_beer($ Guiness) or die();
}
?>
Oh, My God!!!!

Fatal error: Call to undefined function: get_more_beer() in
joke.php on line 6

H E L P M E P L E A S E
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--


Pedro,

Have you checked that the bottle daemon is running?
Have you invoked put_cash($plent y);?
Are you sure you have semaphored the wait(er) process?
Has your worker thread been placed in a hicroot jail?
Does the wall in front of you have lots of flourescent lights? (if so, you
are probably lying on your back in front of the bar).

HTH
Doug
--
Remove the blots from my address to reply
Jul 17 '05 #6
Doug Hutcheson wrote:
"Pedro Graca" <he****@hotpop. com> wrote in message
news:c0******** *****@ID-203069.news.uni-berlin.de...
Heinz-Gerd Hombergs wrote:
> On Tuesday, February 10, 2004, 00:32:04 Afkamm wrote:
>
>> Drinkers log, beer date Mon, 09 Feb 2004 22:25:53 GMT. After downing
>> several pints in comp.lang.php, Heinz-Gerd Hombergs slurred the
>
> I hope the bartender here has some lovely Guinness. ;)
(snip) get_more_beer($ Guiness) or die(); Fatal error: Call to undefined function: get_more_beer() in
Have you checked that the bottle daemon is running?
Have you invoked put_cash($plent y);?
Are you sure you have semaphored the wait(er) process?
Has your worker thread been placed in a hicroot jail?
Does the wall in front of you have lots of flourescent lights? (if so, you
are probably lying on your back in front of the bar).


I finally found out what it is!
The extension car.so (car.dll for my other computer) isn't working :(
.... hopefully it will be done updating today.
Thank you for the suggestions (and the smile)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #7
"Doug Hutcheson" <do************ *****@nrm.blot. qld.blot.gov.bl ot.au>
wrote in news:dI******** ********@news.o ptus.net.au:
"Pedro Graca" <he****@hotpop. com> wrote in message
news:c0******** *****@ID-203069.news.uni-berlin.de...
Fatal error: Call to undefined function: get_more_beer() in
joke.php on line 6


Have you checked that the bottle daemon is running?
Have you invoked put_cash($plent y);?
Are you sure you have semaphored the wait(er) process?
Has your worker thread been placed in a hicroot jail?
Does the wall in front of you have lots of flourescent lights? (if so,
you are probably lying on your back in front of the bar).


He should also check php.ini to make sure that sober_mode isn't set.
Jul 17 '05 #8
Drinkers log, beer date Tue, 10 Feb 2004 00:37:56 GMT. After downing
several pints in comp.lang.php, Heinz-Gerd Hombergs slurred the
following.
You want speed? Then make the following changes: ;)

for ($i=0, $max=count($pos ts); $i<$max; $i++) {
$category_list = '';
for ($j=1, $max1=count($ca tegories); $j<=$max1; $j++) {


I appreciate that thanks. Don't know how much faster, but knowing it's
faster will do me. :D
Marc :o)

Jul 17 '05 #9

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

Similar topics

3
5106
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...
46
9884
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...
10
1867
by: Pavan | last post by:
Hi i have two nested loops as shown below: 1. for(i=0;i<=1000;i++) { for(i=0;i<=100;i++) { .....; .....; }
17
3016
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html Why is C# 500% slower than C++ on Nested Loops ??? Will this problem be solved in the future???
10
2023
by: Roshawn | last post by:
Hi, I am experimenting with nested For...Next loops. My code looks like this: Dim i as Byte Dim itm as Byte For i = 0 to 9 For itm = 0 to 9 'code omitted
5
2498
by: Uday Deo | last post by:
Hi everyone, I am looping through 4 nested loops and I would like to break in the inner most loop on certain condition and get the control on the 2 nd loop instead of 3rd loop. Here is briefly what I am trying to accomplish. //Loop 1 For each lp_1 in Loop1 // Certain conditions
77
5175
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?
3
1927
by: tzuriel | last post by:
Hello all, I think nested loops will do what I want, but I can't seem to get them right. I have two tables, members and casts. I run a query as follows: $sql_query="SELECT DISTINCT casts.yearID, members.memberName" . " FROM members INNER JOIN casts ON members.memberID = casts.memberID"
4
2326
by: toddlahman | last post by:
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...
0
7647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7570
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7618
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6210
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...
1
5472
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...
0
5187
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3617
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...
0
3600
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
896
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...

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.