Jump to content

Recommended Posts

Hello!!!!

 

I have a while loop which outputs variables made from mysql query, but i want to order this output according to one of those php variables created later after the mysql query. I think that i can put all the outputted variables of the loop in an array and than order them in the array and output them. How can i make an array that will look like a matrix where nr.rows=nr.loops and nr.columns=nr.variables THAN order these rows by one column....like in excel: you create a table and than you sort them according to a column :). After i output the array with a "for" loop for each row :). PLS HEEELP. I know what i want but i don't know how can it be done :)

 

 

EDIT: If your answers will be you can do it in mysql query.....than i say simply it's impossible......because the variable which i want to order the results from is generated AFTER the query and it's a mix of mysql variables($row[''])+form input variables ($_POST['']). So simply i cannot do it in mysql unless you suggest s.m.th else :)

Edited by gdeliana

There are two choices as I see it,

  1. Query the database again and get the results in the order you want them
  2. learn about php arrays and the use of usort()

Option 2 fits what you are asking, but we will need to see some code from you to see where you have got to so far before we can really help more.

Thanks for the reply :). It's basically a financial claculator for savings. I have a list of saving companies in mysql which have their own account limitations. The calculations are done based in user form input and limited by the variables of mysql query. The output consists of divs where each div represents one saving company and for it i calculate the saving amount, tax, and the PROFIT. I have done all of this and the results output according to $row['interest_rate'], but i want them ordered by the later generated variable which is the $profit. I have already posted here my code before but as it was a bit long nobody wanted to look at it :D.

 

 

A loop creates 6 php variable. So i want an array which will have in each row a loop with these 6 variables so 6 columns. It will look like a matrix.

If someone could tell me how to create such an array like a matrix.

 

I know all the types of arrays (saw them in w3schools) but it seems that they just put the variables one after another and index them, one cannot know where a loop starts and ends.

Edited by gdeliana

you're looking to do something like

$i = 0;
while($row = mysql_fetch_assoc($result)){
$rArray[$i][$row['filedName1']] = $row['fieldName1'];
$rArray[$i][$row['filedName2']] = $row['fieldName2'];
$rArray[$i][$row['filedName3']] = $row['fieldName3'];
$rArray[$i][$row['filedName4']] = $row['fieldName4'];
$rArray[$i][$row['filedName5']] = $row['fieldName5'];
$rArray[$i][$row['filedName6']] = $row['fieldName6'];
$i++;
//your current code here
}

Ok, i tried it, but when i echo the results:

echo "$rArray[$i][$row['filedName6']]";

i get only the result for that loop, i think the values are not pushed.

 

Also noticed that you use the "mysql_fetch_assoc" and i use "mysql_fetch_array".

Edited by gdeliana

yeah, mysql_fetch_assoc() returns one dataset, where the keys are associative names from the table. mysql_fetch_array() returns two datasets, one with associative key names and one with numerical. By associative key names I meen that the array key for the value is the same as the column heading in the database.

 

Rather than echoing, do var_dump($rArray); when you echo "$rArray[$i][$row['filedName6']]"; you will only display the value for the array record at the point of [$i] (which will be set to the last value in the array.

 

If you want to echo each line on its own you will need to step through the array using a loop (as it's multi-dimensional you will need to nest two loops, one inside the other) something like the following

for($counter = 0; $counter <= count($rArray)-1; $counter++){
foreach($rArray[$counter] as $key => $value){
echo "\$rArray --> record no $counter --> record key = $key --> record value = $value <br />";
}
}

Ok i did it, however i need to sort the arrays, but not the values inside them. Here is the output:

 

 

 

[b]array[/b] [i](size=13)[/i]
 1 => 
   [b]array[/b] [i](size=6)[/i]
     54954 => float 54954.74347101
     13125 => float 13125.628562903
     1000374 => float 1000374.6160332
     10097 => float 10097.841093294
     11394 => float 11394.765048752
     12575 => float 12575.18524806
 2 => 
   [b]array[/b] [i](size=6)[/i]
     54954 => float 54954.74347101
     13125 => float 13125.628562903
     1000374 => float 1000374.6160332
     10097 => float 10097.841093294
     11394 => float 11394.765048752
     12575 => float 12575.18524806
 3 => 
   [b]array[/b] [i](size=6)[/i]
     54954 => float 54954.74347101
     13125 => float 13125.628562903
     1000374 => float 1000374.6160332
     10097 => float 10097.841093294
     11394 => float 11394.765048752
     12575 => float 12575.18524806
 4 => 
   [b]array[/b] [i](size=6)[/i]
     54954 => float 54954.74347101
     13125 => float 13125.628562903
     1000374 => float 1000374.6160332
     10097 => float 10097.841093294
     11394 => float 11394.765048752
     12575 => float 12575.18524806
 5 => 
   [b]array[/b] [i](size=6)[/i]
     49710 => float 49710.953291902
     13198 => float 13198.458982057
     1000734 => float 1000734.5436461
     9178 => float 9178.6376459356
     11261 => float 11261.62419264
     12521 => float 12521.708395092
 6 => 
   [b]array[/b] [i](size=6)[/i]
     49710 => float 49710.953291902
     13198 => float 13198.458982057
     1000734 => float 1000734.5436461
     9178 => float 9178.6376459356
     11261 => float 11261.62419264
     12521 => float 12521.708395092
 7 => 
   [b]array[/b] [i](size=6)[/i]
     49674 => float 49674.293645881
     13198 => float 13198.968143807
     1000773 => float 1000773.1493464
     9178 => float 9178.991733577
     11261 => float 11261.62419264
     12521 => float 12521.708395092
 8 => 
   [b]array[/b] [i](size=6)[/i]
     52384 => float 52384.439029542
     13161 => float 13161.327235701
     997919 => float 997919.13759795
     9152 => float 9152.8150218379
     8446 => float 8446.21814448
     12521 => float 12521.708395092
 9 => 
   [b]array[/b] [i](size=6)[/i]
     49772 => float 49772.052701936
     13197 => float 13197.61037914
     1000670 => float 1000670.2008123
     9178 => float 9178.047499867
     11261 => float 11261.62419264
     12521 => float 12521.708395092
 10 => 
   [b]array[/b] [i](size=6)[/i]
     49772 => float 49772.052701936
     13197 => float 13197.61037914
     1000670 => float 1000670.2008123
     9178 => float 9178.047499867
     11261 => float 11261.62419264
     12521 => float 12521.708395092
 11 => 
   [b]array[/b] [i](size=6)[/i]
     49772 => float 49772.052701936
     13197 => float 13197.61037914
     1000670 => float 1000670.2008123
     9178 => float 9178.047499867
     11261 => float 11261.62419264
     12521 => float 12521.708395092
 12 => 
   [b]array[/b] [i](size=6)[/i]
     25214 => float 25214.225740612
     13538 => float 13538.691309158
     1000320 => float 1000320.7701461
     4575 => float 4575.8889235416
     10615 => float 10615.20150601
     12257 => float 12257.908486479
 13 => 
   [b]array[/b] [i](size=6)[/i]
     25250 => float 25250.555281567
     13538 => float 13538.1867322
     1000283 => float 1000283.4889348
     4575 => float 4575.7183835641
     10615 => float 10615.20150601
     12257 => float 12257.908486479

 

I need to sort them by the first value, but maintain the structure inside them. So the first array to be the one which has the first value highest. I know there are a lot of sorting functions but i can't get the right one for this. Thanks for your help!!!

By default, a 2 dim array should sort in order of the first array elements so arsort($rArray) should do it.

 

However, with such a ridiculous key structure you will have difficulty in comparing any of the other elements if you want to sort on another item.

I think it's not so difficult if the arrays keep their structure. I just need to sort their order by the first number you see in them. So the array that has the first value higher should be in the first place. If i use your code $rArray is my entire multidimensional array, but i should specify it that i need the SUB arrays sorted by their first value. I used your code but they are not sorted from the highest to the smallest

 

arsort($rArray, SORT_DESC);

 

and if i do it like this:

 

arsort($rArray[$num][$profit], SORT_DESC);

 

i get this error: Warning: arsort() expects parameter 1 to be array, double given in C:\wamp\www\calc\calc1 - Copy.php on line 222

Edited by gdeliana

Well, i'm also wondering the same :D. It shouldn't be so difficult. I found also this in stackoverflow:http://stackoverflow.com/questions/2699086/sort-multidimensional-array-by-value-2, I think that we should specify the criteria of sorting.

OK Here's an example

<?php
$a = array (
   array(
   'a' => 3,
   'b' => 2
   ),
   array(
   'a' => 8,
   'b' => 5
   ),
   array(
   'a' => 5,
   'b' => 3
   )
);
echo '<pre>Unsorted '.print_r($a, 1).'</pre>';   
arsort($a);									  // sort the sub arrays
echo '<pre>Sorted '.print_r($a, 1).'</pre>';   
?>

 

And here is the output

 

Unsorted Array
(
   [0] => Array
    (
	    [a] => 3
	    [b] => 2
    )

   [1] => Array
    (
	    [a] => 8
	    [b] => 5
    )

   [2] => Array
    (
	    [a] => 5
	    [b] => 3
    )

)

Sorted Array
(
   [1] => Array
    (
	    [a] => 8
	    [b] => 5
    )

   [2] => Array
    (
	    [a] => 5
	    [b] => 3
    )

   [0] => Array
    (
	    [a] => 3
	    [b] => 2
    )

)

This is the result:

 

arsort($rArray, SORT_DESC);
var_dump($rArray);


[b]array[/b] [i](size=13)[/i]
10 =>
[b]array[/b] [i](size=6)[/i]
95746 => float 95746.118300622
6279 => float 6279.5408451346
1002932 => float 1002932.0669677
18677 => float 18677.945713529
12682 => float 12682.417945625
13854 => float 13854.450434711
9 =>
[b]array[/b] [i](size=6)[/i]
95746 => float 95746.118300622
6279 => float 6279.5408451346
1002932 => float 1002932.0669677
18677 => float 18677.945713529
12682 => float 12682.417945625
13854 => float 13854.450434711
11 =>
[b]array[/b] [i](size=6)[/i]
95746 => float 95746.118300622
6279 => float 6279.5408451346
1002932 => float 1002932.0669677
18677 => float 18677.945713529
12682 => float 12682.417945625
13854 => float 13854.450434711
12 =>
[b]array[/b] [i](size=6)[/i]
47967 => float 47967.660887516
6611 => float 6611.3356882811
1002419 => float 1002419.5001146
9210 => float 9210.0558512346
11268 => float 11268.25030132
12896 => float 12896.497848447
13 =>
[b]array[/b] [i](size=6)[/i]
48040 => float 48040.653648759
6610 => float 6610.8287941058
1002342 => float 1002342.6441463
9209 => float 9209.3497119785
11268 => float 11268.25030132
12896 => float 12896.497848447
8 =>
[b]array[/b] [i](size=6)[/i]
98481 => float 98481.424684471
6260 => float 6260.5456619134
999898 => float 999898.26579654
18621 => float 18621.446200304
9511 => float 9511.8134592191
13854 => float 13854.450434711
7 =>
[b]array[/b] [i](size=6)[/i]
95548 => float 95548.769055707
6280 => float 6280.911326002
1003150 => float 1003150.9522721
18682 => float 18682.022089155
12682 => float 12682.417945625
13854 => float 13854.450434711
3 =>
[b]array[/b] [i](size=6)[/i]
105748 => float 105748.35846424
6210 => float 6210.0808439984
1002257 => float 1002257.2803817
20586 => float 20586.115871946
12984 => float 12984.067051625
14053 => float 14053.750153676
2 =>
[b]array[/b] [i](size=6)[/i]
105748 => float 105748.35846424
6210 => float 6210.0808439984
1002257 => float 1002257.2803817
20586 => float 20586.115871946
12984 => float 12984.067051625
14053 => float 14053.750153676
4 =>
[b]array[/b] [i](size=6)[/i]
105748 => float 105748.35846424
6210 => float 6210.0808439984
1002257 => float 1002257.2803817
20586 => float 20586.115871946
12984 => float 12984.067051625
14053 => float 14053.750153676
5 =>
[b]array[/b] [i](size=6)[/i]
95622 => float 95622.77502255
6280 => float 6280.3973956767
1003068 => float 1003068.8702829
18680 => float 18680.493448295
12682 => float 12682.417945625
13854 => float 13854.450434711
6 =>
[b]array[/b] [i](size=6)[/i]
95622 => float 95622.77502255
6280 => float 6280.3973956767
1003068 => float 1003068.8702829
18680 => float 18680.493448295
12682 => float 12682.417945625
13854 => float 13854.450434711
1 =>
[b]array[/b] [i](size=6)[/i]
105748 => float 105748.35846424
6210 => float 6210.0808439984
1002257 => float 1002257.2803817
20586 => float 20586.115871946
12984 => float 12984.067051625
14053 => float 14053.750153676

 

 

I don't understand by which criteria they got sorted, but they are not sorted by their first value. I also used the rsort() but it simply reindexes them and the results stay the same.

Edited by gdeliana

It's working actually :D, THANKS A LOT!!!!!!! I just had the indexes of the sub arrays assigned as values also, which i had to rename only :). Now i will echo the results, let's see :D

Edited by gdeliana

Not one to learn by example, are you?

 

If you must use a sort flag, use a valid one (though the default should work) eg

 

arsort($a, SORT_NUMERIC);

 

I also showed you how to get "pretty" results from print_r()

Ok, let's say i want from the sorted multidimensional array to output the third value of each array. I did like this as suggested by Muddy_Funster

 

for($counter = 0; $counter <= count($rArray)-1; $counter++){
foreach($rArray[$counter] as $key => $value){
echo "$key = $value <br />";
}
echo "<hr/>";
}

 

just a lil bit modified but works the same. It outputs every value. My problem now is since i got the results sorted by the first value, i cannot change it now (unless i sort by second value, but i don't know how to do that) and in the output i want the third value to be outputted first.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.