Help with charge formula


#1

A php code file, that I didn’t write, but am trying to modify, generates the cost of a video purchase.
Can you tell me what this line does?

$amout += $video->video_play_price?$video->video_play_price:$video_cost;

I believe $video->video_play_price is price set by the video-uploader. If it’s not set then the video amount is (default price set by admin) $video_cost


#2

It will sum the amount of $video->video_play_price to $amount if $video->video_play_price is truthy, otherwise it will sum $video_cost to it.
Please note if $video->video_play_price is 0 then it will add $video_cost to the value.


#3

Thanks for your reply. I’m sorry, I’m not clear on what your saying. I’m not sure what “truthy” is.

are you saying $amout will be: $video->video_play_price plus $video_cost ?


#4

That line is the same as doing

if ($video->video_play_price) {
    $amout += $video->video_play_price;
} else {
    $amout += $video_cost;
}

For this condition to take place: if ($video->video_play_price) then $video->video_play_price must be something other than 0 or false


#5

Thank you for your clarification. Great help.

The next part of the code file tries to calculate deducting 50% of the price.

	$amout = 0;
	foreach ($id_array as $id) {
            $video_id = (int)PT_Secure($id);

            // get video data
            $video = $db->where('id', $id)->getOne(T_VIDEOS);
            $amout += $video->video_play_price?$video->video_play_price:$video_cost;

	}

    $charge = ( $amout *0.50 );

    if ($wallet >= $amout) {

        $new_wallet = (string)($wallet - $amout);

        $db->startTransaction();

        $inserted_records = 0;
        foreach ($id_array as $id) {
        $video_id = (int)PT_Secure($id);

        $uploader_amount = $amout - $charge; 

So, ultimately, I’m trying to know if this line is correct:

$charge = ( $amout *0.50 );

and if this line is correct:

$uploader_amount = $amout - $charge; 

any additional guidance will be appreciated.


#6

What does “correct” mean for your project, setup and goal? Just put in mock-data and try yourself. That’s just two lines of code with first graders mathematics.


#7

It’s a bit strange that you build up a running total of all the video prices into $amout (sic), then take half of it and put it into $charge, but then seem to use that variable (which is a total) in each step of your second loop. Obviously the code stops before I can see what you actually do with it inside that loop, but it seems strange to be using the loop total on each individual item.

As for this line:

$uploader_amount = $amout - $charge;

as the latter is half the former, it’s the same as saying

$uploader_amount = $charge;

unless you’re taking into account rounding issues by doing that.


#9

Thanks for your replies. I have gotten this to work successfully, where admin sets default price, but uploader can change the price, and uploader gets 50% of current price.
However, I would like to set the default price as a minimum, so the uploader can’t set a price below the default price? (which I believe is $video_cost) any suggestion/example of code that would accomplish that, will be appreciated.

$amout = 0;
	foreach ($id_array as $id) {
            $video_id = (int)PT_Secure($id);

            // get video data
            $video = $db->where('id', $id)->getOne(T_VIDEOS);
			$amout += $video->video_play_price?$video->video_play_price:$video_cost;
	}

    $charge = ( $video_cost *0.50 );

    if ($wallet >= $amout) {

        $new_wallet = (string)($wallet - $amout);

        $db->startTransaction();

        $inserted_records = 0;
        foreach ($id_array as $id) {
            $video_id = (int)PT_Secure($id);


	// $uploader_amount = $video_cost - $charge; //100 - 20% = 80

            // get video data
            $video = $db->where('id', $id)->getOne(T_VIDEOS);


			$video_cost_new = $video->video_play_price?$video->video_play_price:$video_cost;
			$uploader_amount = ( $video_cost_new *0.50 );

#10

I would probably add this line

$video_cost_new = max($video_cost_new, $video_cost);

So if the new price is lower than the $video_cost price, it takes the value of $video_cost, otherwise it stays the same. Add it before the last line you posted, so after you’ve initially calculated the value of $video_cost_new.