arrays... and long-term-novice

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

    arrays... and long-term-novice

    I need to sort some values by percentage, and don't know how...

    here is where I got so far:

    mysql_query("SE LECT sum($usage)...
    list($total)... // now I have total number I need to find %

    mysql_query...
    while(list($id, $usage) = mysql_fetch_arr ay)) {
    mysql_query("SE LECT sum($usage) WHERE id=$id...
    list($used)... // now I have some usage for each $id

    $made = sprintf("%.f", $used / $total * 100);

    echo"$id $made<br>"; //will output:

    101 @ 50.38%
    102 @ 25.19%
    103 @ 24.43%

    and I want to deal with the last only from this example. with the one of
    smallest percentage and I can't know which one will that be.

    I think I have to create and sort array, but whatever I have made, I had
    no success.
    if I create array inside of while{} I end with 3 different arrays.
    if I end while {} I have only one to work with...

    can anyone help me? (I have avoided arrays so far with "great" success,
    but from now on, I just have to use them and I don't understood them at
    all. my english isn't that good, and haven't find proper documentation
    on my language to learn)

    tnx in advance

    --
    Ja NE
    fotografija = zapisano svjetlom | fotozine = foto-e-zin

    --
  • Geoff Berrow

    #2
    Re: arrays... and long-term-novice

    A waste of electrons? You decide, but Message-ID:
    <1h4zkrs.19pfkq d1sjz9uqN%hidde n@mail.zz> from Ja NE contained the
    following:
    [color=blue]
    > $made = sprintf("%.f", $used / $total * 100);
    >[/color]
    Arrays are easy and incredibly useful

    Try something like:

    //create an array using the id as the key and %age as value
    $made[$id]=$used / $total * 100;

    in your while loop.

    Sort the array in reverse order

    arsort($made);

    and outside the loop echo it to the screen

    foreach ($made as $key=>$value){
    echo $key." @ ".number_format ($value,2)."%<b r>";
    }
    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • Steve

      #3
      Re: arrays... and long-term-novice

      [color=blue]
      > I need to sort some values by percentage, and don't know how...[/color]

      Let MySQL do the work for you:

      SELECT @TOT:=SUM( `usage` )
      FROM mytable

      and in the same session, calculate all the percentages and return just
      the first one:

      SELECT `usage`, `usage` / @TOT AS `perc`
      FROM mytable
      ORDER BY `perc`
      LIMIT 1

      No need to learn about arrays just yet 8-)

      ---
      Steve

      Comment

      • Ja NE

        #4
        Re: arrays... and long-term-novice

        Geoff Berrow <blthecat@ckdog .co.uk> wrote:

        thank you, that helped with little addon:

        $i=0;
        foreach ($made as $key=>$value){
        $i++
        processing (with_job);
        if ($i == 1) {
        break; //had to deal with the one only
        }
        }


        --
        Ja NE
        fotografija = zapisano svjetlom | fotozine = foto-e-zin

        --

        Comment

        • Ja NE

          #5
          Re: arrays... and long-term-novice

          Steve <googlespam@nas tysoft.com> wrote:
          [color=blue]
          >
          > SELECT @TOT:=SUM( `usage` )
          > FROM mytable
          >
          > SELECT `usage`, `usage` / @TOT AS `perc`
          > FROM mytable
          > ORDER BY `perc`
          > LIMIT 1
          >
          > No need to learn about arrays just yet 8-)[/color]

          Thank you Steve, will try that latter too. php solution have olready
          went on site...

          --
          Ja NE
          fotografija = zapisano svjetlom | fotozine = foto-e-zin

          --

          Comment

          • Geoff Berrow

            #6
            Re: arrays... and long-term-novice

            A waste of electrons? You decide, but Message-ID:
            <1h4zos3.163ag8 a1m0ru1dN%hidde n@mail.zz> from Ja NE contained the
            following:
            [color=blue]
            > foreach ($made as $key=>$value){
            > $i++
            > processing (with_job);
            > if ($i == 1) {
            > break; //had to deal with the one only
            > }
            > }[/color]
            wouldn't this work?

            foreach ($made as $key=>$value){

            processing (with_job);

            break;

            }

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

            Comment

            • Ja NE

              #7
              Re: arrays... and long-term-novice

              Geoff Berrow <blthecat@ckdog .co.uk> wrote:
              [color=blue]
              > wouldn't this work?[/color]

              I don't know... how would "script knew" when to break?
              [color=blue]
              >
              > foreach ($made as $key=>$value){
              >
              > processing (with_job);
              >
              > break;
              >
              > }[/color]


              --
              Ja NE
              fotografija = zapisano svjetlom | fotozine = foto-e-zin

              --

              Comment

              • Geoff Berrow

                #8
                Re: arrays... and long-term-novice

                Message-ID: <1h51a61.12does j150477aN%hidde n@mail.zz> from Ja NE
                contained the following:
                [color=blue]
                >
                >I don't know... how would "script knew" when to break?[/color]

                Seems to work fine.

                $array=array(10 1=>"25",102=>"3 5",103=>"30" );

                arsort($array);

                foreach ($array as $key=>$value){

                //do some processing
                print"<p>Key is:$key Value is: $value</p>";
                break;

                }

                print "All done";

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

                Comment

                Working...