469,328 Members | 1,327 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

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($categories); $j++) {

if ($j == $post[$i]['id']) {
selected = ' selected="selected"';
} 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"><option value="1" selected="selected">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 20400
On Monday, February 9, 2004, 23:05:23 Afkamm wrote:
for ($i=0; $i<count($posts); $i++) { for ($j=1; $j<=count($categories); $j++) {


Try this

for ($i=0; $i<count($posts); $i++) {
$category_list = '';
for ($j=1; $j<=count($categories); $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($categories); $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($posts); $i<$max; $i++) {
$category_list = '';
for ($j=1, $max1=count($categories); $j<=$max1; $j++) {
Cause PHP evaluates for each $i count($posts) and for each $j
count($categories) 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_finest_beer_in_the_whole_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_finest_beer_in_the_whole_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($plenty);?
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($plenty);?
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.blot.au >
wrote in news:dI****************@news.optus.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($plenty);?
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($posts); $i<$max; $i++) {
$category_list = '';
for ($j=1, $max1=count($categories); $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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Oleg Leschov | last post: by
46 posts views Thread by Neptune | last post: by
10 posts views Thread by Pavan | last post: by
10 posts views Thread by Roshawn | last post: by
3 posts views Thread by tzuriel | last post: by
4 posts views Thread by toddlahman | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.