Processing a db query - tough problem and need help

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

    #1

    Processing a db query - tough problem and need help

    Let's I do a mysql query and then I do a,
    for( $i = 1; $row = mysql_fetch_arr ay($result); $i++ ) {...}
    and it gives me this:

    PageID Title URL Description
    1 lee's gogle1.com This is lee's website.
    1 lee's gogle2.com This is lee's website.
    2 Jon's yaho1.com This is Jon's website.
    2 Jon's yaho2.com This is Jon's website.

    But I want this:

    PageID Title URL Description
    1 lee's gogle1.com gogle2.com This is lee's website.
    2 Jon's yaho1.com yaho2.com This is Jon's website.

    How??? I'm at a loss to figure this out! Thanks,
    Lee G.
    Wash DC

  • ImagineerKyle

    #2
    Re: Processing a db query - tough problem and need help

    I think that I have had to do something like this before. I believe what I
    ended up doing was creating an array for each, in your case, title. The
    array would not hold the title names but rahter the URLs. What you would do,
    is create a for loop before the for loop that you listed prior. In this loop
    you would run through the rows and have a variable called $lastUrl that
    would hold the value of the URL in the previous row. Then you check the
    current row and if it equals the the $lastUrl then just concatenate it to
    the previous array value. If it doesn't equal, than insert a new item in the
    array.

    $count = 0;
    for( $i = 1; $row = mysql_fetch_arr ay($result); $i++ )
    {
    $lastUrl = '';
    if($lastUrl == $row['url'])
    {
    $row[$count] .= $row['url'];
    }
    else
    {
    $row[$count += 1] = $row['url']
    }
    }

    With this method you may have to do the same with Title because you will
    want the array's to match up. See if this works.
    -Kyle Hayes

    "leegold2" <leegold@nospam .net> wrote in message
    news:wA0bd.5245 $gd1.3993@trndd c08...[color=blue]
    > Let's I do a mysql query and then I do a,
    > for( $i = 1; $row = mysql_fetch_arr ay($result); $i++ ) {...}
    > and it gives me this:
    >
    > PageID Title URL Description
    > 1 lee's gogle1.com This is lee's website.
    > 1 lee's gogle2.com This is lee's website.
    > 2 Jon's yaho1.com This is Jon's website.
    > 2 Jon's yaho2.com This is Jon's website.
    >
    > But I want this:
    >
    > PageID Title URL Description
    > 1 lee's gogle1.com gogle2.com This is lee's website.
    > 2 Jon's yaho1.com yaho2.com This is Jon's website.
    >
    > How??? I'm at a loss to figure this out! Thanks,
    > Lee G.
    > Wash DC
    >[/color]


    Comment

    • Harrie Verveer

      #3
      Re: Processing a db query - tough problem and need help

      for as far as I see, there is nothing wrong with this line of code...
      could you post the entire loop, because I think it goes wrong in there

      leegold2 wrote:[color=blue]
      > Let's I do a mysql query and then I do a,
      > for( $i = 1; $row = mysql_fetch_arr ay($result); $i++ ) {...}
      > and it gives me this:
      >
      > PageID Title URL Description
      > 1 lee's gogle1.com This is lee's website.
      > 1 lee's gogle2.com This is lee's website.
      > 2 Jon's yaho1.com This is Jon's website.
      > 2 Jon's yaho2.com This is Jon's website.
      >
      > But I want this:
      >
      > PageID Title URL Description
      > 1 lee's gogle1.com gogle2.com This is lee's website.
      > 2 Jon's yaho1.com yaho2.com This is Jon's website.
      >
      > How??? I'm at a loss to figure this out! Thanks,
      > Lee G.
      > Wash DC
      >[/color]

      Comment

      • Markus Ernst

        #4
        Re: Processing a db query - tough problem and need help

        leegold2 wrote:[color=blue]
        > Let's I do a mysql query and then I do a,
        > for( $i = 1; $row = mysql_fetch_arr ay($result); $i++ ) {...}
        > and it gives me this:
        >
        > PageID Title URL Description
        > 1 lee's gogle1.com This is lee's website.
        > 1 lee's gogle2.com This is lee's website.
        > 2 Jon's yaho1.com This is Jon's website.
        > 2 Jon's yaho2.com This is Jon's website.
        >
        > But I want this:
        >
        > PageID Title URL Description
        > 1 lee's gogle1.com gogle2.com This is lee's website.
        > 2 Jon's yaho1.com yaho2.com This is Jon's website.[/color]

        I guess you have a problem with your data structure. If there are more than
        1 URLs per page I suggest to handle URLs in separate tables:

        Table pages:
        - PageID
        - Title
        - Description

        Table urls:
        - PageID (foreign key)
        - URL

        So you can assign as many URLs to a page as you want. Then you can do a new
        query to get the URLs inside the loop, such as

        for ($i = 1; $row = mysql_fetch_arr ay($result); $i++ ) {
        $result2 = mysql_query("SE LECT URL FROM urls WHERE
        PageID=".$row['PageID']);
        while ($row2 = mysql_fetch_arr ay($result2) {...}
        }

        which is not very elegant but easy.

        HTH
        Markus


        Comment

        • leegold2

          #5
          Re: Processing a db query - tough problem and need help

          Markus Ernst wrote:[color=blue]
          > leegold2 wrote:
          >[color=green]
          >>Let's I do a mysql query and then I do a,
          >>for( $i = 1; $row = mysql_fetch_arr ay($result); $i++ ) {...}
          >>and it gives me this:
          >>
          >>PageID Title URL Description
          >> 1 lee's gogle1.com This is lee's website.
          >> 1 lee's gogle2.com This is lee's website.
          >> 2 Jon's yaho1.com This is Jon's website.
          >> 2 Jon's yaho2.com This is Jon's website.
          >>
          >>But I want this:
          >>
          >>PageID Title URL Description
          >> 1 lee's gogle1.com gogle2.com This is lee's website.
          >> 2 Jon's yaho1.com yaho2.com This is Jon's website.[/color]
          >
          >
          > I guess you have a problem with your data structure. If there are more than
          > 1 URLs per page I suggest to handle URLs in separate tables:
          >
          > Table pages:
          > - PageID
          > - Title
          > - Description
          >
          > Table urls:
          > - PageID (foreign key)
          > - URL
          >
          > So you can assign as many URLs to a page as you want. Then you can do a new
          > query to get the URLs inside the loop, such as[/color]

          Yeah, I eneded up putting a query inside the loop - Thanks!!!



          [color=blue]
          >
          > for ($i = 1; $row = mysql_fetch_arr ay($result); $i++ ) {
          > $result2 = mysql_query("SE LECT URL FROM urls WHERE
          > PageID=".$row['PageID']);
          > while ($row2 = mysql_fetch_arr ay($result2) {...}
          > }
          >
          > which is not very elegant but easy.
          >
          > HTH
          > Markus
          >
          >[/color]

          Comment

          Working...