mysql results <-- previous row, and next row, showing next ok

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

    mysql results <-- previous row, and next row, showing next ok

    Can anyone help?

    I query a database and return a result on the column "reference" .
    There might be 7 listings. Each row is displayed in a table, with
    links through to a detail page. I am working on having a "previous"
    record and a "next" record link on the detail page.

    This code below works, for the "next" record, by searching the values
    in the array $myarray for the variable $ref. It then returns the key
    value and the key value, as a number, is deducted by one and plus'd by
    one to give me the previous row and the next row.

    Returning the value of the next row works a treat, but for some reason
    the previous row, deduct 1, while it echoes a value to screen, doesn't
    work.

    I'd appreciate any feedback, any help. It's been a challenge for a
    newbie to sort this out.

    This is the code:

    (query database and then:)
    //$ref is a variable carried into the page

    $num_rows = mysql_num_rows( $result2);

    $i=0;
    while ($i < $num_rows) {
    $next=mysql_res ult($result2,$i ,"reference" );

    ++$i;

    $myarray = array("$i"=>"$n ext");
    foreach($myarra y as $key=>$value) {
    if ($value == "$ref"){

    $b=($key);
    $c=($key+1);
    $a=($key-1);

    }}

    if ($a == "0") {
    echo "No Preceding Record";

    } else {
    if ($key == "$a") echo "<a
    href=../detail/index.php?name= $value>Previous Record</a>";
    }

    if ($key == "$b") echo "This is the current Record";

    if ($num_rows < $c) {
    echo "Records End";
    } else {
    if ($key == "$c") echo "<a href=../detail/index.php?name= $value>Next
    Record</a>";
    }
    }

    } else {
    echo "Sorry, no records were found";
    }

    ?>
  • Geoff Berrow

    #2
    Re: mysql results &lt;-- previous row, and next row, showing next ok

    I noticed that Message-ID:
    <bcab4b38.03092 62327.e326958@p osting.google.c om> from george contained
    the following:
    [color=blue]
    >++$i;[/color]

    I always write $i++;


    --
    Geoff Berrow
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • Andy Hassall

      #3
      Re: mysql results &lt;-- previous row, and next row, showing next ok

      On Sat, 27 Sep 2003 10:11:31 +0100, Geoff Berrow <bl@ckdog.co.uk .the.cat>
      wrote:
      [color=blue]
      >I noticed that Message-ID:
      ><bcab4b38.0309 262327.e326958@ posting.google. com> from george contained
      >the following:
      >[color=green]
      >>++$i;[/color]
      >
      >I always write $i++;[/color]

      If it's on its own line, it makes no difference; whether it's pre- or post-
      increment only matters if it's part of a larger expression.

      (But it could get you into a very long religious argument in a C or C++
      group!)

      --
      Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
      Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

      Comment

      • Pedro

        #4
        Re: mysql results &lt;-- previous row, and next row, showing next ok

        Andy Hassall wrote:[color=blue]
        > On Sat, 27 Sep 2003 10:11:31 +0100, Geoff Berrow <bl@ckdog.co.uk .the.cat>
        > wrote:
        >[color=green][color=darkred]
        >>>++$i;[/color]
        >>
        >>I always write $i++;[/color]
        >
        > If it's on its own line, it makes no difference; whether it's pre- or post-
        > increment only matters if it's part of a larger expression.[/color]

        On my machine, on this script, pre-increment is 5% faster than post-incrment :-)

        <?php
        $p1 = microtime();
        for ($i=0; $i<1000000; $i++) {} // do nothing
        $p2 = microtime();
        for ($i=0; $i<1000000; ++$i) {} // do nothing
        $p3 = microtime();
        $t = explode(' ', $p1); $t1 = (float)$t[0]+(float)$t[1];
        $t = explode(' ', $p2); $t2 = (float)$t[0]+(float)$t[1];
        $t = explode(' ', $p3); $t3 = (float)$t[0]+(float)$t[1];
        $delta1 = $t2-$t1;
        $delta2 = $t3-$t2;
        echo '<table>';
        echo '<tr><td>post-increment</td><td>', $delta1, '</td></tr>';
        echo '<tr><td>pre-increment</td><td>', $delta2, '</td></tr>';
        echo '</table><br />';
        if ($delta2 < $delta1)
        echo 'pre-increment was ', number_format(1 00-$delta2/$delta1*100, 2), '% faster than post-increment';
        else
        echo 'post-increment was ', number_format(1 00-$delta2/$delta1*100, 2), '% faster than pre-increment';
        ?>

        [color=blue]
        > (But it could get you into a very long religious argument in a C or C++
        > group!)[/color]

        And why not a PHP group too? :-)

        --
        I have a spam filter working.
        To mail me include "urkxvq" (with or without the quotes)
        in the subject line, or your mail will be ruthlessly discarded.

        Comment

        • Pedro

          #5
          Re: mysql results &lt;-- previous row, and next row, showing next ok

          Pedro mis-wrote:[color=blue]
          > if ($delta2 < $delta1)
          > echo 'pre-increment was ', number_format(1 00-$delta2/$delta1*100, 2), '% faster than post-increment';
          > else
          > echo 'post-increment was ', number_format(1 00-$delta2/$delta1*100, 2), '% faster than pre-increment';[/color]

          $delta2 and $delta1 should have been swapped in the last echo

          --
          I have a spam filter working.
          To mail me include "urkxvq" (with or without the quotes)
          in the subject line, or your mail will be ruthlessly discarded.

          Comment

          • george

            #6
            Re: mysql results &lt;-- previous row, and next row, showing next ok

            The only clue I have to this behaviour is that $a loops through the
            array and returns (number of rows) instances of "No Preceding Record"
            - as if it can't find the row in the array, as if it has no value EVEN
            though it outputs to screen properly. $c also outputs as an increment
            and finds the array row. So my problem presumably lies in the
            expression $a=($key-1) ?? but I can't think how else to express it a
            simple deduct 1.

            Many thanks for any comments.

            Comment

            • Andy Hassall

              #7
              Re: mysql results &lt;-- previous row, and next row, showing next ok

              On 27 Sep 2003 15:27:31 GMT, Pedro <hexkid@hotpop. com> wrote:
              [color=blue]
              >Andy Hassall wrote:[color=green]
              >> On Sat, 27 Sep 2003 10:11:31 +0100, Geoff Berrow <bl@ckdog.co.uk .the.cat>
              >> wrote:
              >>[color=darkred]
              >>>>++$i;
              >>>
              >>>I always write $i++;[/color]
              >>
              >> If it's on its own line, it makes no difference; whether it's pre- or post-
              >> increment only matters if it's part of a larger expression.[/color]
              >
              >On my machine, on this script, pre-increment is 5% faster than post-incrment :-)[/color]

              Here we go :-)
              [color=blue]
              ><?php
              > $p1 = microtime();
              > for ($i=0; $i<1000000; $i++) {} // do nothing
              > $p2 = microtime();
              > for ($i=0; $i<1000000; ++$i) {} // do nothing
              > $p3 = microtime();[/color]

              However, if you make the test fairer by having the same loop, and doing an
              isolated pre- or post-increment inside, you get different answers.

              $p1 = microtime();
              $j=0;
              for ($i=0; $i<1000000; $i++) { $j++; }

              $p2 = microtime();
              $j=0;
              for ($i=0; $i<1000000; $i++) { ++$j; }
              $p3 = microtime();

              The amount differs, with pre-increment going 1-5% quicker, but even got this
              at one point:

              post-increment 1.09205305576
              pre-increment 1.09754097462

              post-increment was 0.50% faster than pre-increment
              [color=blue][color=green]
              >> (But it could get you into a very long religious argument in a C or C++
              >> group!)[/color]
              >
              >And why not a PHP group too? :-)[/color]

              Unfortunately it doesn't help the OP :-(

              --
              Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
              Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

              Comment

              • Pedro

                #8
                Re: mysql results &lt;-- previous row, and next row, showing next ok

                george wrote:[color=blue]
                > The only clue I have to this behaviour is that $a loops through the
                > array and returns (number of rows) instances of "No Preceding Record"
                > - as if it can't find the row in the array, as if it has no value EVEN
                > though it outputs to screen properly. $c also outputs as an increment
                > and finds the array row. So my problem presumably lies in the
                > expression $a=($key-1) ?? but I can't think how else to express it a
                > simple deduct 1.
                >
                > Many thanks for any comments.[/color]

                Where do $a, $b, and $c come from?
                You're only setting them inside the if ($value == "$ref") block;
                if ($value != "$ref") they will be undefined or have the value from last
                time through the loop.

                Also, do you realize $myarray will never have more than one entry?
                Maybe that's the way you want it -- but then: why make it an array? ???


                Insert these statement at the very beginning of your script

                ini_set('error_ reporting', E_ALL);

                so that every warning, notice and error gets displayed on the browser.


                HTH

                --
                I have a spam filter working.
                To mail me include "urkxvq" (with or without the quotes)
                in the subject line, or your mail will be ruthlessly discarded.

                Comment

                • Tom Thackrey

                  #9
                  Re: mysql results &lt;-- previous row, and next row, showing next ok


                  On 27-Sep-2003, Pedro <hexkid@hotpop. com> wrote:
                  [color=blue]
                  > Andy Hassall wrote:[color=green]
                  > > On Sat, 27 Sep 2003 10:11:31 +0100, Geoff Berrow
                  > > <bl@ckdog.co.uk .the.cat>
                  > > wrote:
                  > >[color=darkred]
                  > >>>++$i;
                  > >>
                  > >>I always write $i++;[/color]
                  > >
                  > > If it's on its own line, it makes no difference; whether it's pre- or
                  > > post-
                  > > increment only matters if it's part of a larger expression.[/color]
                  >
                  > On my machine, on this script, pre-increment is 5% faster than
                  > post-incrment :-)
                  >[/color]

                  When I changed the sequence of the for loops, that is which increment was
                  first and ran the loops 20x each, it turns out that the sequence of the
                  loops determines which type of increment is faster not pre or post
                  increment. This makes sense to me. I don't see why different code would be
                  executed for pre or post increment.


                  <?php
                  echo '<table border=1>';
                  for ($txm=0;$txm<20 ;$txm++)
                  {
                  $p1 = microtime();
                  for ($i=0; $i<1000000; ++$i) {} // do nothing
                  $p2 = microtime();
                  for ($i=0; $i<1000000; $i++) {} // do nothing
                  $p3 = microtime();
                  $t = explode(' ', $p1); $t1 = (float)$t[0]+(float)$t[1];
                  $t = explode(' ', $p2); $t2 = (float)$t[0]+(float)$t[1];
                  $t = explode(' ', $p3); $t3 = (float)$t[0]+(float)$t[1];
                  $delta1 = $t2-$t1;
                  $delta2 = $t3-$t2;
                  $dif = number_format(1 00-$delta2/$delta1*100, 2);
                  echo "<tr><td>$delta 1</td><td>$delta2</td><td>$dif%</td><td>++i
                  first</td></tr>";
                  }
                  echo "<tr><td></td><td></td><td></td><td></td></tr>";
                  for ($txm=0;$txm<20 ;$txm++)
                  {
                  $p1 = microtime();
                  for ($i=0; $i<1000000; ++$i) {} // do nothing
                  $p2 = microtime();
                  for ($i=0; $i<1000000; $i++) {} // do nothing
                  $p3 = microtime();
                  $t = explode(' ', $p1); $t1 = (float)$t[0]+(float)$t[1];
                  $t = explode(' ', $p2); $t2 = (float)$t[0]+(float)$t[1];
                  $t = explode(' ', $p3); $t3 = (float)$t[0]+(float)$t[1];
                  $delta2 = $t2-$t1;
                  $delta1 = $t3-$t2;
                  $dif = number_format(1 00-$delta2/$delta1*100, 2);
                  echo "<tr><td>$delta 1</td><td>$delta2</td><td>$dif%</td><td>i++
                  first</td></tr>";
                  }
                  echo '</table>';
                  ?>


                  --
                  Tom Thackrey

                  Comment

                  • george

                    #10
                    Re: mysql results &lt;-- previous row, and next row, showing next ok

                    Pedro <hexkid@hotpop. com> wrote in message news:<bl4ea6$81 7l5$1@ID-203069.news.uni-berlin.de>...[color=blue]
                    > if ($value != "$ref") they will be undefined or have the value from last
                    > time through the loop.[/color]

                    yes that's why I said:

                    (query database and then:)
                    //$ref is a variable carried into the page

                    [color=blue]
                    > Insert these statement at the very beginning of your script
                    >
                    > ini_set('error_ reporting', E_ALL);
                    >[/color]

                    yep, it's excellent, and I get undefined variable.
                    Ever tried searching the web on "php define variable" so if it's
                    undefined perhaps I can define it - and the PHP manuals are like the
                    old DOS4 manuals - great if you know what they're talking about and
                    totally useless if you don't. I learnt DOS by trial and error and then
                    - only then - did the manuals make sense. So I appreciate any help
                    thanks !

                    Comment

                    • Pedro

                      #11
                      Re: mysql results &lt;-- previous row, and next row, showing next ok

                      george wrote:[color=blue]
                      > Pedro <hexkid@hotpop. com> wrote in message news:<bl4ea6$81 7l5$1@ID-203069.news.uni-berlin.de>...[color=green]
                      >> Insert these statement at the very beginning of your script
                      >>
                      >> ini_set('error_ reporting', E_ALL);
                      >>[/color]
                      >
                      > yep, it's excellent, and I get undefined variable.[/color]

                      If you develop your scripts on a different server than the one where
                      they will be available, consider changing development's php.ini to show
                      all errors, warnings and notices.
                      This way you don't have to remember "ini_settin g".

                      Happy Coding :-)

                      --
                      I have a spam filter working.
                      To mail me include "urkxvq" (with or without the quotes)
                      in the subject line, or your mail will be ruthlessly discarded.

                      Comment

                      Working...