Odd data column display problems

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

    #1

    Odd data column display problems

    I'm doing the following to try to display queried data into verticle
    columns, and I'm getting odd behaviors.
    For example, if there are only 4 items to display, it skips item number 3:

    Item1 Item4
    Item 2

    If there are 10 items, it duplicates some:

    Item 1 Item 4 Item 7
    Item 2 Item 5 Item 8
    Item 3 Item 6 Item 9
    Item 4 Item 7 Item 10

    Any help with what I'm doing wrong, I would really appreciate!!
    Thanks


    $sql_cats = "SELECT * FROM tbl_subcategory WHERE sct_category =
    '$proto_cat'";
    $result_cats = @mysql_query($s ql_cats, $dbh);
    $numrows_cats = mysql_num_rows( $result_cats);
    while ($row_cats = mysql_fetch_arr ay($result_cats )) {
    $sct_id = $row_cats[sct_id];
    $sct_name = $row_cats[sct_name];
    $sct_tempdisp .= "<a href=\"category .php?disp=2&cat _id=".$sct_id." \"
    class=\"tah12Or angeBold\">".$s ct_name."</a><br /><br />|";
    }

    $sct_display_X = explode("|", $sct_tempdisp);
    $items = $sct_display_X;
    For($i = 0; $i< (count($items)/3); $i +=1){
    $cat_listx .= "<tr><td width=\"33%\">" .$items[$i]."</td><td
    width=\"33%\">" .$items[$i+3]."</td><td>".$items[$i+6]."</td></tr>";
    }

    $cat_list = "<table width=\"100%\"> ".$cat_list x."</table>";


  • Pedro Graca

    #2
    Re: Odd data column display problems

    LRW wrote:
    <snip>[color=blue]
    > $sql_cats = "SELECT * FROM tbl_subcategory WHERE sct_category =
    > '$proto_cat'";
    > $result_cats = @mysql_query($s ql_cats, $dbh);
    > $numrows_cats = mysql_num_rows( $result_cats);
    > while ($row_cats = mysql_fetch_arr ay($result_cats )) {
    > $sct_id = $row_cats[sct_id];
    > $sct_name = $row_cats[sct_name];
    > $sct_tempdisp .= "<a href=\"category .php?disp=2&cat _id=".$sct_id." \"
    > class=\"tah12Or angeBold\">".$s ct_name."</a><br /><br />|";
    > }[/color]

    Try this here:

    echo htmlentities($s ct_tempdisp);

    <snip>
    --
    Mail to my "From:" address is readable by all at http://www.dodgeit.com/
    == ** ## !! ------------------------------------------------ !! ## ** ==
    TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
    may bypass my spam filter. If it does, I may reply from another address!

    Comment

    • LRW

      #3
      Re: Odd data column display problems


      "Pedro Graca" <hexkid@dodgeit .com> wrote in message
      news:slrncqkjtp .vuv.hexkid@ID-203069.user.uni-berlin.de...[color=blue]
      > LRW wrote:
      > <snip>[color=green]
      >> $sql_cats = "SELECT * FROM tbl_subcategory WHERE sct_category =
      >> '$proto_cat'";
      >> $result_cats = @mysql_query($s ql_cats, $dbh);
      >> $numrows_cats = mysql_num_rows( $result_cats);
      >> while ($row_cats = mysql_fetch_arr ay($result_cats )) {
      >> $sct_id = $row_cats[sct_id];
      >> $sct_name = $row_cats[sct_name];
      >> $sct_tempdisp .= "<a
      >> href=\"category .php?disp=2&cat _id=".$sct_id." \"
      >> class=\"tah12Or angeBold\">".$s ct_name."</a><br /><br />|";
      >> }[/color]
      >
      > Try this here:
      >
      > echo htmlentities($s ct_tempdisp);
      >
      > <snip>[/color]

      Uhm, that simply prints the actual tags to the page instead of DOING the
      HTML tags. Printing "<a etc" to the screen instead of making a link.


      Comment

      • Pedro Graca

        #4
        Re: Odd data column display problems

        LRW wrote:[color=blue]
        >
        > "Pedro Graca" <hexkid@dodgeit .com> wrote in message
        > news:slrncqkjtp .vuv.hexkid@ID-203069.user.uni-berlin.de...[color=green]
        >> LRW wrote:
        >> <snip>[color=darkred]
        >>> $sql_cats = "SELECT * FROM tbl_subcategory WHERE sct_category =
        >>> '$proto_cat'";
        >>> $result_cats = @mysql_query($s ql_cats, $dbh);
        >>> $numrows_cats = mysql_num_rows( $result_cats);
        >>> while ($row_cats = mysql_fetch_arr ay($result_cats )) {
        >>> $sct_id = $row_cats[sct_id];
        >>> $sct_name = $row_cats[sct_name];
        >>> $sct_tempdisp .= "<a
        >>> href=\"category .php?disp=2&cat _id=".$sct_id." \"
        >>> class=\"tah12Or angeBold\">".$s ct_name."</a><br /><br />|";
        >>> }[/color]
        >>
        >> Try this here:
        >>
        >> echo htmlentities($s ct_tempdisp);
        >>
        >> <snip>[/color]
        >
        > Uhm, that simply prints the actual tags to the page instead of DOING the
        > HTML tags. Printing "<a etc" to the screen instead of making a link.[/color]

        Yes, ... and then you proceed to explode() that on "|".

        Q. Where are the "|"s you are about to explode() on?
        --
        Mail to my "From:" address is readable by all at http://www.dodgeit.com/
        == ** ## !! ------------------------------------------------ !! ## ** ==
        TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
        may bypass my spam filter. If it does, I may reply from another address!

        Comment

        • LRW

          #5
          Re: Odd data column display problems


          "Pedro Graca" <hexkid@dodgeit .com> wrote in message
          news:slrncql2f7 .vuv.hexkid@ID-203069.user.uni-berlin.de...[color=blue]
          > LRW wrote:[color=green]
          >>
          >> "Pedro Graca" <hexkid@dodgeit .com> wrote in message
          >> news:slrncqkjtp .vuv.hexkid@ID-203069.user.uni-berlin.de...[color=darkred]
          >>> LRW wrote:
          >>> <snip>
          >>>> $sql_cats = "SELECT * FROM tbl_subcategory WHERE sct_category =
          >>>> '$proto_cat'";
          >>>> $result_cats = @mysql_query($s ql_cats, $dbh);
          >>>> $numrows_cats = mysql_num_rows( $result_cats);
          >>>> while ($row_cats = mysql_fetch_arr ay($result_cats )) {
          >>>> $sct_id = $row_cats[sct_id];
          >>>> $sct_name = $row_cats[sct_name];
          >>>> $sct_tempdisp .= "<a
          >>>> href=\"category .php?disp=2&cat _id=".$sct_id." \"
          >>>> class=\"tah12Or angeBold\">".$s ct_name."</a><br /><br />|";
          >>>> }
          >>>
          >>> Try this here:
          >>>
          >>> echo htmlentities($s ct_tempdisp);
          >>>
          >>> <snip>[/color]
          >>
          >> Uhm, that simply prints the actual tags to the page instead of DOING the
          >> HTML tags. Printing "<a etc" to the screen instead of making a link.[/color]
          >
          > Yes, ... and then you proceed to explode() that on "|".
          >
          > Q. Where are the "|"s you are about to explode() on?[/color]

          Here...

          $sct_tempdisp .= "<a href=\"category .php?disp=2&cat _id=".$sct_id." \"
          class=\"tah12Or angeBold\">".$s ct_name."</a><br /><br />|";

          See it at the end of the line there?
          I don't think it's the exploding the $sct_tempdisp into an array that's the
          problem (is it?) Because I get individual...ch unks, for lack of the proper
          term, just either skipped ones or doubled ones. I THINK the problem is in
          the dividing up the array into the columns.



          Comment

          • Pedro Graca

            #6
            Re: Odd data column display problems

            LRW wrote:[color=blue]
            > I'm doing the following to try to display queried data into verticle
            > columns, and I'm getting odd behaviors.
            > For example, if there are only 4 items to display, it skips item number 3:
            >
            > Item1 Item4
            > Item 2
            >
            > If there are 10 items, it duplicates some:
            >
            > Item 1 Item 4 Item 7
            > Item 2 Item 5 Item 8
            > Item 3 Item 6 Item 9
            > Item 4 Item 7 Item 10
            >
            > Any help with what I'm doing wrong, I would really appreciate!!
            > Thanks
            >
            >
            > $sql_cats = "SELECT * FROM tbl_subcategory WHERE sct_category =
            > '$proto_cat'";
            > $result_cats = @mysql_query($s ql_cats, $dbh);
            > $numrows_cats = mysql_num_rows( $result_cats);
            > while ($row_cats = mysql_fetch_arr ay($result_cats )) {
            > $sct_id = $row_cats[sct_id];
            > $sct_name = $row_cats[sct_name];
            > $sct_tempdisp .= "<a href=\"category .php?disp=2&cat _id=".$sct_id." \"
            > class=\"tah12Or angeBold\">".$s ct_name."</a><br /><br />|";
            > }
            >
            > $sct_display_X = explode("|", $sct_tempdisp);
            > $items = $sct_display_X;
            > For($i = 0; $i< (count($items)/3); $i +=1){
            > $cat_listx .= "<tr><td width=\"33%\">" .$items[$i]."</td><td
            > width=\"33%\">" .$items[$i+3]."</td><td>".$items[$i+6]."</td></tr>";
            > }
            >
            > $cat_list = "<table width=\"100%\"> ".$cat_list x."</table>";[/color]

            You're right about the visible problem not being the "|".

            You have to review your for() loop. The way you have it coded produces
            this output:

            $items[0] $items[3] $items[6]
            $items[1] $items[4] $items[7]
            $items[2] $items[5] $items[8]
            $items[3] $items[6] $items[9]
            $items[4] $items[7] $items[10]
            $items[5] $items[8] $items[11]
            $items[6] $items[9] $items[12]
            $items[7] $items[10] $items[13]
            $items[8] $items[11] $items[14]
            ... ... ...

            The value you add to the index must be variable, not the constant 3 (or
            6) as you have.


            So just 'find' the initial value for the second and third column

            $col1st = ???; // for 10 elements, should be 4
            $col2nd = ???; // for 10 elements, should be 6

            and redo your loop, don't forgetting to check whether ($col1st+$i) and
            ($col2nd+$i) are greater than count($items), in which case you shouldn't
            add them to $cat_lists.

            for ($i = 0; $i < count($items)/3); ++$i) {
            $cat_lists .= "<tr><td>" . $items[$i] . "</td><td>"
            . $items[$col1st+$i] . "</td><td>"
            . $items[$col2nd+$i] . "</td></tr>";
            }


            Happy Coding :-)



            PS. But you still have a (little) problem with the "|"

            --
            Mail to my "From:" address is readable by all at http://www.dodgeit.com/
            == ** ## !! ------------------------------------------------ !! ## ** ==
            TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
            may bypass my spam filter. If it does, I may reply from another address!

            Comment

            • LRW

              #7
              Re: Odd data column display problems

              Pedro Graca <hexkid@dodgeit .com> wrote in message news:<slrncqlu5 h.vuv.hexkid@ID-203069.user.uni-berlin.de>...[color=blue]
              >
              > You're right about the visible problem not being the "|".
              >
              > You have to review your for() loop. The way you have it coded produces
              > this output:
              >
              > $items[0] $items[3] $items[6]
              > $items[1] $items[4] $items[7]
              > $items[2] $items[5] $items[8]
              > $items[3] $items[6] $items[9]
              > $items[4] $items[7] $items[10]
              > $items[5] $items[8] $items[11]
              > $items[6] $items[9] $items[12]
              > $items[7] $items[10] $items[13]
              > $items[8] $items[11] $items[14]
              > ... ... ...
              >
              > The value you add to the index must be variable, not the constant 3 (or
              > 6) as you have.
              >
              >
              > So just 'find' the initial value for the second and third column
              >
              > $col1st = ???; // for 10 elements, should be 4
              > $col2nd = ???; // for 10 elements, should be 6
              >
              > and redo your loop, don't forgetting to check whether ($col1st+$i) and
              > ($col2nd+$i) are greater than count($items), in which case you shouldn't
              > add them to $cat_lists.
              >
              > for ($i = 0; $i < count($items)/3); ++$i) {
              > $cat_lists .= "<tr><td>" . $items[$i] . "</td><td>"
              > . $items[$col1st+$i] . "</td><td>"
              > . $items[$col2nd+$i] . "</td></tr>";
              > }
              >
              >
              > Happy Coding :-)
              >
              >
              >
              > PS. But you still have a (little) problem with the "|"[/color]

              $sct_tempdisp .= "<a
              href=\"category .php?disp=2&cat _id=".$sct_id." \"
              class=\"tah12Or angeBold\">".$s ct_name."</a><br /><br />|";
              }
              $sct_display_X = explode("|", $sct_tempdisp);
              $items = $sct_display_X;//array() //assume a bunch of items in this
              array
              $col1 = (count($items)/2); //
              $col2 = (count($items)/3); //
              For($i = 0; $i< (count($items)/3); ++$i){
              $cat_listx .= "<tr><td width=\"33%\">" .$items[$i]."</td><td
              width=\"33%\">" .$items[$col1+$i]."</td><td>".$items[$col2+$i]."</td></tr>";
              }
              $cat_list = "<table width=\"100%\"
              align=\"center\ ">".$cat_listx. "</table>";

              GAHR! I suck! I either can't do math or have no concept of logic...or
              both!
              I've tried so many different ways of trying to define $col1 and $col2,
              and whether I should or not have played with
              (count($items)/3)
              and I just can't get it to evenly divide the items into three columns
              with no repeats! =P
              I suck.
              What do I need to do to define $col1 and $col2? IS it a division of
              the number of items in $items? If so, I can keep playing with that. If
              it's some other formula, or if I need to focus on something else,
              lemme know? I can keep playing with the proper lines until I figure it
              out, but not knowing if I'm even working on the right lines makes it
              really frustrating.

              The exploding on the pipe? I can't see the problem...unles s, it's the
              threat of what would happen if there's a legitimate pipe within the
              string that it shouldn't explode on? Is that it?

              Thanks for your help!
              Liam

              Comment

              • Pedro Graca

                #8
                Re: Odd data column display problems

                LRW wrote:[color=blue]
                > Pedro Graca <hexkid@dodgeit .com> wrote in message news:<slrncqlu5 h.vuv.hexkid@ID-203069.user.uni-berlin.de>...[color=green]
                >> But you still have a (little) problem with the "|"[/color]
                > The exploding on the pipe? I can't see the problem...[/color]

                When you explode("|", ...) a string with 4 "|" you get *five* elements.
                Your original string generates one too many element for the $items
                array.

                explode() a shorter version of it, and you should be ok:

                $items = explode('|', substr($xxx, 0, -1));
                [color=blue]
                > $col1 = (count($items)/2); //
                > $col2 = (count($items)/3); //
                > For($i = 0; $i< (count($items)/3); ++$i){
                > $cat_listx .= "<tr><td width=\"33%\">" .$items[$i]."</td><td
                > width=\"33%\">" .$items[$col1+$i]."</td><td>".$items[$col2+$i]."</td></tr>";
                > }[/color]
                <snip>[color=blue]
                > I've tried so many different ways of trying to define $col1 and $col2,
                > and whether I should or not have played with
                > (count($items)/3)[/color]

                Yes, count($items)/3 is important.
                [color=blue]
                > and I just can't get it to evenly divide the items into three columns
                > with no repeats! =P[/color]

                Write down what you want to happen for 6, 7, 8, 9, 10, 11, ... elements
                and try to figure the formulas from there.

                6 elements $items[0] $items[2] $items[4] 6 => 2 => 4
                $items[1] $items[3] $items[5]

                7 elements $items[0] $items[3] $items[5] 7 => 3 => 5
                $items[1] $items[4] $items[6]
                $items[2] -- --

                8 elements $items[0] $items[3] $items[6] 8 => 3 => 6
                $items[1] $items[4] $items[7]
                $items[2] $items[5] --

                9 elements $items[0] $items[3] $items[6] 9 => 3 => 6
                $items[1] $items[4] $items[7]
                $items[2] $items[5] $items[8]

                ...... ... ... ... ...

                [color=blue]
                > What do I need to do to define $col1 and $col2? IS it a division of
                > the number of items in $items? If so, I can keep playing with that. If
                > it's some other formula, or if I need to focus on something else,
                > lemme know? I can keep playing with the proper lines until I figure it
                > out, but not knowing if I'm even working on the right lines makes it
                > really frustrating.[/color]

                I used division [count()/3] and modulus [count()%3] ...

                I put my (not so) little version below. Try to make up your own rather
                than using mine :-)


                M |
                Y |
                V
                ( |
                N |
                O |
                T |
                V
                S |
                O |
                ) |
                V
                L |
                I |
                T |
                T |
                L |
                E |
                V
                V |
                E |
                R |
                S |
                I |
                O |
                N |
                V


                // example data
                $items = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');

                // 6 items ==> $col1 = 2;
                // 7 ==> 3
                // 8 ==> 3
                // 9 ==> 3
                $col1 = (int)((count($i tems) + 2) / 3);


                // 6 items ==> $col1 = 2; $col2 = 4;
                // 7 ==> 3 5
                // 8 ==> 3 6
                // 9 ==> 3 6
                $col2 = $col1 * 2;
                if (count($items) % 3 == 1) --$col2;

                // initialize $cat_listx
                $cat_listx = '';

                // build HTML
                for ($i = 0; $i < $col1; ++$i) {
                $cat_listx .= '<tr>';

                // first column is always completely filled
                $cat_listx .= '<td>' . $items[$i] . '</td>';

                // only fill second column if it won't repeat in third column
                if ($col1 + $i < $col2) {
                $cat_listx .= '<td>' . $items[$col1 + $i] . '</td>';
                } else {
                $cat_listx .= '<td>(no data)</td>';
                }

                // only fill second column if there is enough data
                if ($col2 + $i < count($items)) {
                $cat_listx .= '<td>' . $items[$col2 + $i] . '</td>';
                } else {
                $cat_listx .= '<td>(no data)</td>';
                }

                $cat_listx .= '</tr>';
                }

                echo $cat_listx;

                --
                Mail to my "From:" address is readable by all at http://www.dodgeit.com/
                == ** ## !! ------------------------------------------------ !! ## ** ==
                TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
                may bypass my spam filter. If it does, I may reply from another address!

                Comment

                • LRW

                  #9
                  Re: Odd data column display problems

                  Pedro Graca <hexkid@dodgeit .com> wrote in message news:<slrncqn53 7.vuv.hexkid@ID-203069.user.uni-berlin.de>...
                  --snip--[color=blue]
                  >
                  > Write down what you want to happen for 6, 7, 8, 9, 10, 11, ... elements
                  > and try to figure the formulas from there.
                  > - snip -
                  > I put my (not so) little version below. Try to make up your own rather
                  > than using mine :-)
                  >[/color]

                  Man, I feel like those times sitting in college math with a blank
                  expression on my face because I'm completely not understanding
                  something I feel like if extremely basic and simple. A logic synapse
                  in mah brain is refusing to fire.

                  While I "get" what the later part of the code is doing, counting up to
                  the next item for the column, etc...I still just can't grok the method
                  of figuring out how to divide the amount into three columns. I have a
                  feeling it's basic, simple stuff...but I'm just not getting it.

                  You've already spent more time than necessary trying to help me, and I
                  appreciate it! I think I have enough info in this thread for me to try
                  to figure it out...expeciall y with your example. I understand things
                  better if I can just sit and reverse engineer something that's working
                  and figure out why it's working.

                  Thanks for your time and help!!
                  Liam

                  Comment

                  Working...