473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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("SELECT sum($usage)...
list($total)... // now I have total number I need to find %

mysql_query...
while(list($id,$usage) = mysql_fetch_array)) {
mysql_query("SELECT 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
http://fotozine.org/?omen=janimir
--
Oct 25 '05 #1
7 1195
A waste of electrons? You decide, but Message-ID:
<1h****************************@mail.zz> from Ja NE contained the
following:
$made = sprintf("%.f", $used / $total * 100);

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)."%<br>";
}
--
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/
Oct 25 '05 #2
I need to sort some values by percentage, and don't know how...


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

Oct 25 '05 #3
Geoff Berrow <bl******@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
http://fotozine.org/?omen=janimir
--
Oct 25 '05 #4
Steve <go********@nastysoft.com> wrote:

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-)


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

--
Ja NE
http://fotozine.org/?omen=janimir
--
Oct 25 '05 #5
A waste of electrons? You decide, but Message-ID:
<1h****************************@mail.zz> from Ja NE contained the
following:
foreach ($made as $key=>$value){
$i++
processing (with_job);
if ($i == 1) {
break; //had to deal with the one only
}
}

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/
Oct 25 '05 #6
Geoff Berrow <bl******@ckdog.co.uk> wrote:
wouldn't this work?
I don't know... how would "script knew" when to break?

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

processing (with_job);

break;

}

--
Ja NE
http://fotozine.org/?omen=janimir
--
Oct 26 '05 #7
Message-ID: <1h****************************@mail.zz> from Ja NE
contained the following:

I don't know... how would "script knew" when to break?


Seems to work fine.

$array=array(101=>"25",102=>"35",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/
Oct 26 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jason | last post by:
I have a number of arrays that are populated with database values. I need to determine which array has the highest ubound out of all the arrays. The array size will always change based on the...
3
by: Simon Foster | last post by:
I have some code which attempts to convert Python arrays (tuples of tuples of tuples...) etc. into C arrays with equivalent contents. The prototype code is shown below. My only question is, is...
13
by: muser | last post by:
for the following code: strncpy(temp_issue, &temp1, 4); files.rec1.issue_rec = atol(temp_issue); cout<<files.rec1.issue_rec<<endl; on execution I get the following. 0x0fd10 a memory...
1
by: Dave A | last post by:
The following C code specifies the interface into a DLL. I need to access it from C#. How do I do declare it? I have done simple ones before but this particular API requires a pointer to a struct...
5
by: Cant Think Today | last post by:
I have multi-dimesional arrays that can be specifed by the user, e.g 1,2,3,4,5 1,2,3,4,5,6,7,8,9,10 1,2,3,4,5,6 I think a bit of code that will iterate over these arrays to print out the...
3
by: T | last post by:
Hi all. I have a problem I have not been able to find a reference about. I am using VB6 and am only a hobbyist programmer. I have 7 arrays of type MyData. Type MyData has 23 elements. Which...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.