Nested FOR loops. (a loop within a loop)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Afkamm

    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)
  • Heinz-Gerd Hombergs

    #2
    Re: Nested FOR loops. (a loop within a loop)

    On Monday, February 9, 2004, 23:05:23 Afkamm wrote:
    [color=blue]
    > for ($i=0; $i<count($posts ); $i++) {[/color]
    [color=blue]
    > for ($j=1; $j<=count($cate gories); $j++) {[/color]

    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



    Comment

    • Afkamm

      #3
      Re: Nested FOR loops. (a loop within a loop)

      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.
      [color=blue]
      > 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. ;)
      >[/color]

      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)

      Comment

      • Heinz-Gerd Hombergs

        #4
        Re: Nested FOR loops. (a loop within a loop)

        On Tuesday, February 10, 2004, 00:32:04 Afkamm wrote:
        [color=blue]
        > 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[/color]

        I hope the bartender here has some lovely Guinness. ;)
        [color=blue]
        > 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[/color]

        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



        Comment

        • Pedro Graca

          #5
          Re: Nested FOR loops. (a loop within a loop)

          Heinz-Gerd Hombergs wrote:[color=blue]
          > On Tuesday, February 10, 2004, 00:32:04 Afkamm wrote:
          >[color=green]
          >> 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[/color]
          >
          > I hope the bartender here has some lovely Guinness. ;)[/color]

          <?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 =--

          Comment

          • Doug Hutcheson

            #6
            Re: Nested FOR loops. (a loop within a loop)

            "Pedro Graca" <hexkid@hotpop. com> wrote in message
            news:c09avf$13s fv7$1@ID-203069.news.uni-berlin.de...[color=blue]
            > Heinz-Gerd Hombergs wrote:[color=green]
            > > On Tuesday, February 10, 2004, 00:32:04 Afkamm wrote:
            > >[color=darkred]
            > >> 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[/color]
            > >
            > > I hope the bartender here has some lovely Guinness. ;)[/color]
            >
            > <?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 =--[/color]

            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


            Comment

            • Pedro Graca

              #7
              Re: Nested FOR loops. (a loop within a loop)

              Doug Hutcheson wrote:[color=blue]
              > "Pedro Graca" <hexkid@hotpop. com> wrote in message
              > news:c09avf$13s fv7$1@ID-203069.news.uni-berlin.de...[color=green]
              >> Heinz-Gerd Hombergs wrote:[color=darkred]
              >> > 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. ;)[/color][/color][/color]
              (snip)[color=blue][color=green]
              >> get_more_beer($ Guiness) or die();[/color][/color]
              [color=blue][color=green]
              >> Fatal error: Call to undefined function: get_more_beer() in[/color][/color]
              [color=blue]
              > 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).[/color]

              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 =--

              Comment

              • Eric Bohlman

                #8
                Re: Nested FOR loops. (a loop within a loop)

                "Doug Hutcheson" <doug.blot.hutc heson@nrm.blot. qld.blot.gov.bl ot.au>
                wrote in news:dIWVb.565$ KW.34338@news.o ptus.net.au:
                [color=blue]
                > "Pedro Graca" <hexkid@hotpop. com> wrote in message
                > news:c09avf$13s fv7$1@ID-203069.news.uni-berlin.de...[color=green]
                >> Fatal error: Call to undefined function: get_more_beer() in
                >> joke.php on line 6
                >>[/color]
                >
                > 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).[/color]

                He should also check php.ini to make sure that sober_mode isn't set.

                Comment

                • Afkamm

                  #9
                  Re: Nested FOR loops. (a loop within a loop)

                  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.
                  [color=blue]
                  > 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++) {
                  >
                  >[/color]

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


                  Marc :o)

                  Comment

                  Working...