[program-l] Re: php logic

  • From: "Bryan Schulz" <b.schulz@xxxxxxxxxxxxx>
  • To: <program-l@xxxxxxxxxxxxx>
  • Date: Tue, 17 Jun 2014 10:59:09 -0500

hi,

my test file worked fine.
i don't have to do anything with the two lowest values as they are thrown away after the slice.
the remaining eight values are added with array_reduce.
code follows:
<?php
//load the scores array
$scores = array(1,2,3,4,5,6,7,8,9,10);
print_r("scores: ");
print_r($scores);
print_r("<br>");

//slice the two lowest values from an array
$slice2 = array_slice($scores, 2);
print_r("slice 2:");
print_r($slice2);
print_r("<br>");

//total all values in an array
function add_nums($rt, $cv) {
$rt += $cv;
return $rt;
}
$total = array_reduce($slice2, 'add_nums');
print_r("total is ");
print_r($total);

//output:
//scores: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 ) //slice 2:Array ( [0] => 3 [1] => 4 [2] => 5 [3] => 6 [4] => 7 [5] => 8 [6] => 9 [7] => 10 )
//total is 52
?>

Bryan Schulz

----- Original Message ----- From: "Parham Doustdar" <parham90@xxxxxxxxx>
To: <program-l@xxxxxxxxxxxxx>
Sent: Tuesday, June 17, 2014 12:06 AM
Subject: [program-l] Re: php logic


All right, just to be sure I'm getting this right, let's go ahead with an example:

Imagine this array:

array(
[r1] => 1,
[r2] => 2,
[r3] => 3,
[r4] => 4,
[r5] => 5,
[r6] => 6,
[r7] => 7,
[r8] => 8,
[r9] => 9,
[r10] => 10
)

After this operation that you're talking about, the columns r1 and r2 still contain the values 1 and 2 respectively. However, the total is 52 - (2 + 1) = 49. Correct?

Just to elaborate, you're adding the lowest values together (1 + 2) and the deducting the result from adding 3 through 10 together?

Thanks.
On 6/16/2014 8:31 PM, Bryan Schulz wrote:
hi,

ok,
1. What function do you use to retrieve the data from the database?
Are you using a framework?
guessing no on a framework.
i use mysql select * from the table and load/set whichever php variables are needed.

2. What are your columns called?
name, address, email, etc, r1 thru r10 and total

3. What happens to the two columns that have the lowest values?
they are still in the table but subtracted from the total.

4. What column keeps the sum of the remaining 8 columns?
total and it is updated after slicing the two lowest scores or i may add a second final total field.
Bryan Schulz

----- Original Message ----- From: "Parham Doustdar" <parham90@xxxxxxxxx>
To: <program-l@xxxxxxxxxxxxx>
Sent: Monday, June 16, 2014 6:57 AM
Subject: [program-l] Re: php logic


Awesome!

Now, we need to know more about how your columns work, how you're retrieving the data, and how you plan on saving it. Answering these questions should help us help you. :)

1. What function do you use to retrieve the data from the database? Are you using a framework?
2. What are your columns called?
3. What happens to the two columns that have the lowest values?
4. What column keeps the sum of the remaining 8 columns?

Thanks.
On 6/16/2014 11:09 AM, Bryan Schulz wrote:
parum and jacob,

thanks for answering the questions. i now have tested the php sort, slice, and reduce functions. now to make them work with values fed from mysql.
Bryan Schulz


----- Original Message ----- From: "Jacob Kruger" <jacob@xxxxxxxxxxxxx>
To: <program-l@xxxxxxxxxxxxx>
Sent: Sunday, June 15, 2014 11:36 PM
Subject: [program-l] Re: php logic


Use print_r($arrayToPrint) instead, or else var_dump();

Stay well

Jacob Kruger
Blind Biker
Skype: BlindZA
"Roger Wilco wants to welcome you...to the space janitor's closet..."

----- Original Message ----- From: "Bryan Schulz" <b.schulz@xxxxxxxxxxxxx>
To: <program-l@xxxxxxxxxxxxx>
Sent: Sunday, 15 June, 2014 9:16 PM
Subject: [program-l] Re: php logic


hi,

simple array test:
<?php
$nums = array(25,4,6,2,22,11,18);
print "array: ";
print $nums;
$size = count($nums);
print $size;
?>

why does it spit out:
array: array 7

Bryan Schulz

----- Original Message ----- From: "Parham Doustdar" <parham90@xxxxxxxxx>
To: <program-l@xxxxxxxxxxxxx>
Sent: Sunday, June 15, 2014 9:02 AM
Subject: [program-l] Re: php logic


Hi,

This doesn't really make sense. You have 10 columns. Are they sorted? How do you know what field each value goes to?

Popping is not really what you're looking for as you already know how many values, exactly, you need. You should slice the array:

$values = array_slice($values, 2, 8);

You can then use array_reduce() to add up every single value in the array.

Of course, I've assumed you've got them into an array and sorted them first. Be careful as sort and rsort sort your values like [1, 10, 2, 3], instead of [1, 2, 3, 10] under certain circumstances. If you want what's called natural (or human) sorting, use natsort.

If you still need more help, let us know.
On 6/15/2014 6:20 PM, Bryan Schulz wrote:
hi,

sorry it was a bit fuzzy.
each value is in a separate field in the same record.
i have ten values and want to throw out the two lowest values then total the remaining eight values.
jacobs idea is what i started thinking last night.
from reading, it seems that the php rsort function will put the lowest number at the end and php pop will delete the lowest value.

i could capture the remaining values and do that again or there may be another way to do the entire task in one step.
Bryan Schulz


----- Original Message ----- From: "Jacob Kruger" <jacob@xxxxxxxxxxxxx>
To: <program-l@xxxxxxxxxxxxx>
Sent: Sunday, June 15, 2014 6:40 AM
Subject: [program-l] Re: php logic


Well, then you might do best to pull them out of the database, load them into a PHP array, where you can sort them, and then use the lowest two values from there to generate an SQL statement to clear the values from the record..?

Stay well

Jacob Kruger
Blind Biker
Skype: BlindZA
"Roger Wilco wants to welcome you...to the space janitor's closet..."

----- Original Message ----- From: "Bryan Schulz" <b.schulz@xxxxxxxxxxxxx>
To: <program-l@xxxxxxxxxxxxx>
Sent: Saturday, 14 June, 2014 10:28 PM
Subject: [program-l] Re: php logic


hi,

close, they are a series of scores all in the same record for a player
Bryan Schulz


----- Original Message ----- From: "Jacob Kruger" <jacob@xxxxxxxxxxxxx>
To: <program-l@xxxxxxxxxxxxx>
Sent: Saturday, June 14, 2014 6:13 AM
Subject: [program-l] Re: php logic


If those are numbers in a specific field - field1 - in a mysql table, then something like the following SQL statement might select/delete the relevant two rows:

delete from tbl_one where id in (select id from tbl_one order by i_field1 desc limit 2);

And, haven't tested that, but, think it's roundabout right - might be minor syntax issues.

Stay well

Jacob Kruger
Blind Biker
Skype: BlindZA
"Roger Wilco wants to welcome you...to the space janitor's closet..."

----- Original Message ----- From: "Bryan Schulz" <b.schulz@xxxxxxxxxxxxx>
To: <program-l@xxxxxxxxxxxxx>
Sent: Saturday, 14 June, 2014 7:56 AM
Subject: [program-l] php logic


hi,

could someone provide pseudo thoughts of how to throw away two values from a mysql table using php?

a sample would be these numbers:
542, 567, 452, 624, 584, 472, 645.

how would you throw away the low values of 452 and 472?

thanks,
Bryan Schulz

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq


** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq


** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq


** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq


** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

Other related posts: