Pick 5 random entrys

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

    #1

    Pick 5 random entrys

    What is wrong with this php code ? .

    (it works fine - but something that just works isnt good enough for this
    newsgroup is it)



    <?php

    $loopy=1; $kon="repo"; $spock=0;

    while ($kon=="repo")
    {
    $khan=(rand()%$ chekov); $khan=$khan+1;
    if ($kirk[$khan]=="") {$spock=$spock+ 1; $kirk[$spock]=$khan;}
    if ($spock>55) {break;}
    if ($loopy>222) {$kon="break";}
    $loopy=$loopy+1 ;
    }

    sort($kirk); $kirk=array_key s(array_flip($k irk));

    $loopy=0;
    while ($loopy<5)
    {
    $bones=$kirk[$loopy];
    if ($bones<>"") {include('displ ay_entry.php'); }
    $loopy=$loopy+1 ;
    }

    ?>
  • Egbert Teeselink

    #2
    Re: Pick 5 random entrys

    On Aug 6, 8:08 pm, Krustov <m...@privacy.n etwrote:
    What is wrong with this php code ? .
    >
    (it works fine - but something that just works isnt good enough for this
    newsgroup is it)
    >
    <?php
    >
    $loopy=1; $kon="repo"; $spock=0;
    >
    while ($kon=="repo")
    {
    $khan=(rand()%$ chekov); $khan=$khan+1;
    if ($kirk[$khan]=="") {$spock=$spock+ 1; $kirk[$spock]=$khan;}
    if ($spock>55) {break;}
    if ($loopy>222) {$kon="break";}
    $loopy=$loopy+1 ;
    >
    }
    >
    sort($kirk); $kirk=array_key s(array_flip($k irk));
    >
    $loopy=0;
    while ($loopy<5)
    {
    $bones=$kirk[$loopy];
    if ($bones<>"") {include('displ ay_entry.php'); }
    $loopy=$loopy+1 ;
    >
    }
    >
    ?>
    Troll, go away.

    Comment

    • Paul Herber

      #3
      Re: Pick 5 random entrys

      On Wed, 6 Aug 2008 19:08:40 +0100, Krustov <me@privacy.net wrote:
      >What is wrong with this php code ? .
      >
      >(it works fine - but something that just works isnt good enough for this
      >newsgroup is it)
      >
      >
      >
      ><?php
      >
      >$loopy=1; $kon="repo"; $spock=0;
      >
      >while ($kon=="repo")
      >{
      >$khan=(rand()% $chekov); $khan=$khan+1;
      >if ($kirk[$khan]=="") {$spock=$spock+ 1; $kirk[$spock]=$khan;}
      >if ($spock>55) {break;}
      >if ($loopy>222) {$kon="break";}
      >$loopy=$loopy+ 1;
      >}
      >
      >sort($kirk); $kirk=array_key s(array_flip($k irk));
      >
      >$loopy=0;
      >while ($loopy<5)
      >{
      >$bones=$kirk[$loopy];
      >if ($bones<>"") {include('displ ay_entry.php'); }
      >$loopy=$loopy+ 1;
      >}
      >
      >?>
      Krusty, one day soon your code will be used by all the great teaching
      establishments of the world. Here is example code to take the breath
      away. Clarity, beauty, functionality, conciseness - out the window
      with you! Here are perfect examples of how not to write code.

      Never in the field of computer programming has so much crap been
      written by so few.

      P.S. if you wrote your code properly you would see the problem on the
      6th line after <?php.


      --
      Regards, Paul Herber, Sandrila Ltd.
      http://www.sandrila.co.uk/ http://www.pherber.com/

      Comment

      • Geoff Berrow

        #4
        Re: Pick 5 random entrys

        Message-ID: <MPG.2303f8f618 aa841698b827@ne ws.newsreader.c omfrom
        Krustov contained the following:
        >What is wrong with this php code ? .
        >
        >(it works fine - but something that just works isnt good enough for this
        >newsgroup is it)
        Why don't you just tell us what it's supposed to do? If you just need
        five random numbers from a range you've got waaay too much code.

        --
        Geoff Berrow 011000100110110 0010000000110
        001101101011011 001000110111101 100111001011
        100110001101101 111001011100111 010101101011
        The Slippery Hill Boys Tel: 07985 425932. American themed barn dances and bluegrass performances. Stoke on Trent, Newcastle under Lyme, Staffordshire, Cheshire and surrounding areas.

        Comment

        • Krustov

          #5
          Re: Pick 5 random entrys

          <comp.lang.ph p>
          <Geoff Berrow>
          <Wed, 06 Aug 2008 22:45:24 +0100>
          <vn6k94hhfh1k9i h7kjfv8dndeql1s oolug@4ax.com>
          What is wrong with this php code ? .

          (it works fine - but something that just works isnt good enough for this
          newsgroup is it)
          >
          Why don't you just tell us what it's supposed to do? If you just need
          five random numbers from a range you've got waaay too much code.
          >



          I needed something to fill up the guestbook.php page and displaying some
          random guestbook entrys was the quick fix solution i came up with .

          As the (latest entrys) button is right next to it - obviously its best
          to display the oldest of the 5 random entrys first .

          Can you post 1 or 2 lines of 'magic wand' code to do the same job ? .

          Comment

          • Egbert Teeselink

            #6
            Re: Pick 5 random entrys

            On Aug 7, 12:18 am, Krustov <m...@privacy.n etwrote:
            <comp.lang.ph p>
            <Geoff Berrow>
            <Wed, 06 Aug 2008 22:45:24 +0100>
            <vn6k94hhfh1k9i h7kjfv8dndeql1s oo...@4ax.com>
            >
            >What is wrong with this php code ? .
            >
            >(it works fine - but something that just works isnt good enough for this
            >newsgroup is it)
            >
            Why don't you just tell us what it's supposed to do?  If you just need
            five random numbers from a range you've got waaay too much code.
            >

            >
            I needed something to fill up the guestbook.php page and displaying some
            random guestbook entrys was the quick fix solution i came up with .
            >
            As the (latest entrys) button is right next to it - obviously its best
            to display the oldest of the 5 random entrys first .
            >
            Can you post 1 or 2 lines of 'magic wand' code to do the same job ? .
            if you have $n entries, how about doing something like
            <?php
            $indices = range(0, $n-1);
            shuffle($indice s);
            $indices = array_splice($i ndices, 5);
            rsort($indices) ;
            ?>

            Granted, it isn't fast, but it's easy to read/maintain and the speed
            difference will be negligible if $n isn't very high.

            Sorry about my earlier post, I thought you were making a joke.

            -egbert

            Comment

            • Krustov

              #7
              Re: Pick 5 random entrys

              <comp.lang.ph p>
              <Egbert Teeselink>
              <Wed, 6 Aug 2008 15:26:08 -0700 (PDT)>
              <0a025c6c-48bf-4506-8523-56bfb944b287@34 g2000hsh.google groups.com>
              if you have $n entries, how about doing something like
              <?php
              $indices = range(0, $n-1);
              shuffle($indice s);
              $indices = array_splice($i ndices, 5);
              rsort($indices) ;
              ?>
              >
              Granted, it isn't fast, but it's easy to read/maintain and the speed
              difference will be negligible if $n isn't very high.
              >
              What about the code used to display the 5 random entrys .

              Comment

              • Geoff Berrow

                #8
                Re: Pick 5 random entrys

                Message-ID: <MPG.230433798e 733d3798b829@ne ws.newsreader.c omfrom
                Krustov contained the following:
                >Can you post 1 or 2 lines of 'magic wand' code to do the same job ? .
                Yep.

                Let's say you have entries numbered from 0 to $n

                $rand_entries = array_rand(rang e(0, $n), 5);
                //$rand_entries is now an array containing 5 random numbers between 0
                and $n
                --
                Geoff Berrow 011000100110110 0010000000110
                001101101011011 001000110111101 100111001011
                100110001101101 111001011100111 010101101011
                The Slippery Hill Boys Tel: 07985 425932. American themed barn dances and bluegrass performances. Stoke on Trent, Newcastle under Lyme, Staffordshire, Cheshire and surrounding areas.

                Comment

                • Geoff Berrow

                  #9
                  Re: Pick 5 random entrys

                  Message-ID: <ckak94lblu2bk7 v0epeecitbkvk4d uq8pg@4ax.comfr om Geoff
                  Berrow contained the following:
                  >Let's say you have entries numbered from 0 to $n
                  >
                  >$rand_entrie s = array_rand(rang e(0, $n), 5);
                  >//$rand_entries is now an array containing 5 random numbers between 0
                  >and $n

                  Hey look - a UK lottery ball picker!

                  asort($ar=array _rand(range(1, 49), 6));
                  echo implode(", ", $ar);
                  --
                  Geoff Berrow 011000100110110 0010000000110
                  001101101011011 001000110111101 100111001011
                  100110001101101 111001011100111 010101101011
                  The Slippery Hill Boys Tel: 07985 425932. American themed barn dances and bluegrass performances. Stoke on Trent, Newcastle under Lyme, Staffordshire, Cheshire and surrounding areas.

                  Comment

                  • Krustov

                    #10
                    Re: Pick 5 random entrys

                    <comp.lang.ph p>
                    <Geoff Berrow>
                    <Wed, 06 Aug 2008 23:51:01 +0100>
                    <ckak94lblu2bk7 v0epeecitbkvk4d uq8pg@4ax.com>
                    Can you post 1 or 2 lines of 'magic wand' code to do the same job ? .
                    >
                    Yep.
                    >
                    Let's say you have entries numbered from 0 to $n
                    >
                    $rand_entries = array_rand(rang e(0, $n), 5);
                    //$rand_entries is now an array containing 5 random numbers between 0
                    and $n
                    >
                    You left out how to sort them into the lowest number first - and you
                    left out how to display the 5 entrys .

                    That must be where the magic wand comes in eh! :-)

                    Comment

                    • Geoff Berrow

                      #11
                      Re: Pick 5 random entrys

                      Message-ID: <MPG.23043f00ec 867eba98b82c@ne ws.newsreader.c omfrom
                      Krustov contained the following:
                      >You left out how to sort them into the lowest number first -
                      See other post
                      >and you
                      >left out how to display the 5 entrys .
                      As I've no idea how you are storing your entries that's not really
                      possible is it?

                      I'd be storing them in a database in which case I'd simply pass the
                      values into a query.



                      --
                      Geoff Berrow 011000100110110 0010000000110
                      001101101011011 001000110111101 100111001011
                      100110001101101 111001011100111 010101101011
                      The Slippery Hill Boys Tel: 07985 425932. American themed barn dances and bluegrass performances. Stoke on Trent, Newcastle under Lyme, Staffordshire, Cheshire and surrounding areas.

                      Comment

                      • Geoff Berrow

                        #12
                        Re: Pick 5 random entrys

                        Message-ID: <MPG.23043f00ec 867eba98b82c@ne ws.newsreader.c omfrom
                        Krustov contained the following:
                        >
                        >That must be where the magic wand comes in eh! :-)
                        Looking at it again, if $bones is the variable to get the entry

                        $rand_entries = array_rand(rang e(0, $n), 5);
                        foreach($rand_e ntries as $bones){
                        include('displa y_entry.php');
                        }


                        --
                        Geoff Berrow 011000100110110 0010000000110
                        001101101011011 001000110111101 100111001011
                        100110001101101 111001011100111 010101101011
                        The Slippery Hill Boys Tel: 07985 425932. American themed barn dances and bluegrass performances. Stoke on Trent, Newcastle under Lyme, Staffordshire, Cheshire and surrounding areas.

                        Comment

                        • Michael Fesser

                          #13
                          Re: Pick 5 random entrys

                          ..oO(Krustov)
                          ><comp.lang.php >
                          ><Geoff Berrow>
                          ><Wed, 06 Aug 2008 23:51:01 +0100>
                          ><ckak94lblu2bk 7v0epeecitbkvk4 duq8pg@4ax.com>
                          >
                          >Can you post 1 or 2 lines of 'magic wand' code to do the same job ? .
                          >>
                          >Yep.
                          >>
                          >Let's say you have entries numbered from 0 to $n
                          >>
                          >$rand_entrie s = array_rand(rang e(0, $n), 5);
                          >//$rand_entries is now an array containing 5 random numbers between 0
                          >and $n
                          >>
                          >
                          >You left out how to sort them into the lowest number first - and you
                          >left out how to display the 5 entrys .
                          >
                          >That must be where the magic wand comes in eh! :-)
                          What about $ds9['sisko']? You have your entries stored somewhere, either
                          in a $ds9['dax'] or in a $ds9['odo'] so that they can be $ds9['beamed'].
                          Each entry should have its own $ds9['rank'].

                          Then just take the $ds9['defiant'], pick $ds9['quark'] out of the
                          $ds9['bar'] and load those $ds9['founders'] from wherever they're
                          stored. $ds9['garak'] might also offer some helpful tricks.

                          Micha

                          PS:

                          $ds9 = array_map('str_ rot13', array(
                          'sisko' ='EGSZ',
                          'dax' ='QO',
                          'odo' ='syng svyr',
                          'quark' ='svir enaqbz ahzoref',
                          'garak' ='Neenl fbegvat shapgvbaf',
                          'founders' ='negvpyrf',
                          'defiant' ='uvturfg ahzore',
                          'bar' ='enatr',
                          'rank' ='havdhr ahzrevp VQ',
                          'beamed' ='ernq vagb na neenl'
                          ));

                          Comment

                          • Geoff Berrow

                            #14
                            Re: Pick 5 random entrys

                            Message-ID: <84ek94dcce77ep r1es6nskffmj44m 54vut@4ax.comfr om Michael
                            Fesser contained the following:
                            >What about $ds9['sisko']?
                            The last time I said that I was told "Mark this well, you won't see a
                            more pedantic, asinine response to aquestion again." Message id
                            313bf271-9bab-4a0e-aa2a-a3e93f1b80d0...l egroups.com
                            <grin>
                            >You have your entries stored somewhere, either
                            >in a $ds9['dax'] or in a $ds9['odo'] so that they can be $ds9['beamed'].
                            It's $ds9['odo'] or more likely multiple $ds9['odo'] Krusty doesn't do
                            $ds9['dax']
                            >Each entry should have its own $ds9['rank'].
                            I'm guessing there will be a separate $ds9['odo'] for each entry.


                            --
                            Geoff Berrow 011000100110110 0010000000110
                            001101101011011 001000110111101 100111001011
                            100110001101101 111001011100111 010101101011
                            The Slippery Hill Boys Tel: 07985 425932. American themed barn dances and bluegrass performances. Stoke on Trent, Newcastle under Lyme, Staffordshire, Cheshire and surrounding areas.

                            Comment

                            • Jeff

                              #15
                              Re: Pick 5 random entrys

                              Krustov wrote:
                              What is wrong with this php code ? .
                              >
                              (it works fine - but something that just works isnt good enough for this
                              newsgroup is it)
                              >
                              Why don't you try making your code readable?
                              >
                              >
                              <?php
                              >
                              $loopy=1; $kon="repo"; $spock=0;
                              What's with that, and why is $spock a number? Why not name your
                              variables for what they really are?
                              >
                              while ($kon=="repo")
                              {
                              $khan=(rand()%$ chekov); $khan=$khan+1;
                              $some_integer++ is available and widely used. Where is $checkov?
                              if ($kirk[$khan]=="") {$spock=$spock+ 1; $kirk[$spock]=$khan;}
                              if ($spock>55) {break;}
                              if ($loopy>222) {$kon="break";}
                              $loopy=$loopy+1 ;
                              }
                              None of that makes any sense to me. Why all these specific values for
                              variables?
                              >
                              sort($kirk); $kirk=array_key s(array_flip($k irk));
                              >
                              $loopy=0;
                              while ($loopy<5)
                              {
                              $bones=$kirk[$loopy];
                              if ($bones<>"") {include('displ ay_entry.php'); }
                              $loopy=$loopy+1 ;
                              }
                              None of this makes any sense. Nobody has a magick wand, what we all
                              try to do is make logical code that is readable and maintainable. Try
                              looking at your code a year later and see if it makes any sense, and you
                              wrote it. Imagine what it looks like to us?

                              I'm not sure what you are doing but it looks like you are pulling 5
                              random entries from some file and sorting by date. A database is ideally
                              suited for this type of work and the select to do this is simple. This
                              has all the appearance of something you just kept fudging with until you
                              got what you thought you wanted.

                              Jeff
                              >
                              ?>

                              Comment

                              Working...