Admin setting a minimum price

The php web video script that I’m using calculates the cost of the video. Any uploaded video is priced at what the Admin has set in admin panel, but after the upload the uploader can change the price. All that works successfully. However, I’d like to add that the price set by Admin is the minimum price, so uploader can’t price a video below that (currently an uploader can change to any price - from his logged-in account).The admin-panel code pertaining to that is this:

    <div class="form-line">
<input type="text" name="video_play_price" class="form-control" value="{{CONFIG video_play_price}}">
<label class="form-label">Cost Video</label>
</div>

And the php code for buying videos is below. I believe adding in a minimum price formula, should be included around line 20? (in Bold)
Any additional help with the formula code will be appreciated.

    <?php
ob_start();
if (IS_LOGGED == false) {
    $data = array('status' => 400, 'error' => 'Not logged in');
    echo json_encode($data);
    exit();
}

if (!empty($_POST['id'])) {

    if (!is_array($_POST['id'])) {
        $id_array[] = $_POST['id'];
    } else {
        $id_array = $_POST['id'];
    }

    // get cost video
    $db->where('name', 'video_play_price');
    $db_cost = $db->getOne('config');
    **$video_cost = (float)$db_cost->value;**

    $count_video = count($id_array);
    $user_id = $user->id;
    $wallet = (float)str_replace(',', '', $user->wallet);


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

        $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 );
            // add data to paid table
            $insert_buy = $db->insert('u_paid_videos', [
                'id_user' => $user_id,
                'id_video' => $video_id,
                'session_key' => $_SESSION['session_key'],
                'video_play_price' => (string)$video_cost,
                'video_title' => $video->title,
                'user_id_uploaded' => $video->user_id,
                //'up_credit'=>$video_cost,
                //'up_credit'=>$uploader_amount,
            ]);

            if ($insert_buy) { $inserted_records++; }
            //add wallet users' video
        $userwallet = $db->where('id', $video->user_id)->getOne(T_USERS);

        $videouserwallet = $userwallet->balance+$uploader_amount;
        $db->where('id', $video->user_id);
        $update_balance = $db->update(T_USERS, [
          // 'wallet' => $videouserwallet,
            'balance' => number_format($videouserwallet, 1, '.', ''),
        ]);
        }


        $db->where('id', $user_id);
        //$update_wallet = $db->update(T_USERS, [
        $update_wallet = $db->update(T_USERS, [
            'wallet' => $wallet,
        ]);


        if (($inserted_records == $count_video) && $update_wallet) {
            $db->commit();

            echo json_encode([
                'status' => 200
            ]);
            exit();
        } else {
            $db->rollback();

            echo json_encode([
                'status' => 400,
                'error' => 'Buy process error'
            ]);
            exit();
        }

    } else {

        echo json_encode([
            'status' => 400,
            'error_num' => 1,
            'error' => 'Not enough money'
        ]);
        exit();

    }

} else {

    echo json_encode([
        'status' => 400,
        'error' => 'Bad Request, Invalid or missing parameter'
    ]);
    exit();

echo('$video_play_price: '.$video_play_price.PHP_EOL);
echo('$charge: '.$charge.PHP_EOL);
echo('$amout: '.$amout.PHP_EOL);
$uploader_amount = $video_play_price - $charge;
$uploader_amount = $amout - $charge;
exit;


echo "$uploader_amount";
}

Just add a check once you figure out the charge to insure it is above the minimum…

$charge = ( $video_cost *0.50 );
$min_charge = 1.5;  //  minimum charge of $1.50...
if ($charge<$min_charge) { $charge = $min_charge; }

Not really sure about this. You show a lot of code getting the data and using them. Normally, you would create this pricing when the uploader submits a video to you. I guess it does not matter if it is done when the video is uploaded or when you display the price of viewing it to a visitor. Normally, I would think it would be saved in your database as the minimum amount when the video is uploaded and you create an entry in your database. You would just make sure the play-charge is at least your minimum amount.

Not sure if this helps…

Thanks for your reply.

After thinking this through again, sorry for any inconvenience, but, I’m thinking the minimum maybe should be set where the Uploader/User is trying to change the video price after upload. The users’ html (edit-video.html) looks like this:

					<div class="form-group">
				<label class="col-md-12" for="cost">{{LANG up_video_price}}</label>
				<div class="col-md-12">
				<input id="cost" input type="text" name="video_play_price" class="form-control input-md" value="{{video_play_price_user}}">
				</div>
				</div>

If no price is edited there by the Uploader/User the Admin price remains, but he can change it there, and the accompanying php (edit-video.php) is this (with the last two lines pertaining to the pricing):

<?php
if (IS_LOGGED == false) {
header("Location: " . PT_Link('login'));
exit();
}
if (empty($_GET['id'])) {
header("Location: " . PT_Link('login'));
exit();
}
$id    = PT_Secure($_GET['id']);
$video = $db->where('id', $id)->getOne(T_VIDEOS);
if (empty($video)) {
header("Location: " . PT_Link('login'));
exit();
}
if (!PT_IsAdmin()) {
if (empty($db->where('id', $id)->where('user_id', $user->id)->getValue(T_VIDEOS, 'count(*)'))) {
    header("Location: " . PT_Link('login'));
    exit();
}
}
$video           = PT_GetVideoByID($video, 0, 0, 0);
$pt->video       = $video;
$pt->page        = 'edit-video';
$pt->title       = $lang->edit_video . ' | ' . $pt->config->title;
$pt->description = $pt->config->description;
$pt->keyword     = $pt->config->keyword;
$pt->content     = PT_LoadPage('edit-video/content', array(
'ID' => $video->id,
'USER_DATA' => $video->owner,
'THUMBNAIL' => $video->thumbnail,
'URL' => $video->url,
'TITLE' => $video->title,
'DESC' => br2nl($video->edit_description),
'DESC_2' => $video->markup_description,
'VIEWS' => $video->views,
'TIME' => $video->time_ago,
'TAGS' => $video->tags,
	    'video_play_price' => $u_paid_videos->video_play_price,
	    'video_play_price_user' => number_format($video->video_play_price?$video->video_play_price:$config['video_play_price'],1)

));

Maybe this php is the place that some “nothing lower then the Admin’s set price can be entered into the html field” code?

Any additional guidance will be welcomed

$pt->content = PT_LoadPage(‘edit-video/content’, array(

Yes, you would do it before here. Not sure what the different is between both of these prices, but you would just add a compare before the one that needs changing like I showed you before. Since this creates an array of data and the last couple lines are pricing, you would need to create a temporary price and then check if it is lower than the minimum. Then, use that in the array instead of the actual value. So, if the video_play_price is the actual variable we are talking about, you would set it up before the line above like this:
$min_price=1.5
$temp_price = $u_paid_videos->video_play_price;
if ($temp_price<$min_price) { $temp_price = $min_price; }
$pt->content = PT_LoadPage(‘edit-video/content’, array(
‘video_play_price’ => $temp_price,
Since you need to override the price, you need to set up the minimum price somewhere. Most likely you have that stored somewhere. In this example, I set it manually and used it in a variable. But, this should give you an idea on how to handle it. Good luck…

Thanks so much for your guidance.
I am not a great coder, but I do know some things about this code script.
When the Admin sets a price that number/value appears in the config table under the name: video_play_price (this should be the minimum price).

That same Admin set number also appears in the videos table, under the column videos_play_price, but when the User/Uploader changes the price, the price changes there (in the videos table > videos_play_price column). So, based on all that, and on what you suggested, I attempted to modify the file, but without success. I added lines 30, 32,32 and 46). I look forward to hearing about what I need to correct, to try again. Thanks

<?php
if (IS_LOGGED == false) {
    header("Location: " . PT_Link('login'));
    exit();
}
if (empty($_GET['id'])) {
    header("Location: " . PT_Link('login'));
    exit();
}
$id    = PT_Secure($_GET['id']);
$video = $db->where('id', $id)->getOne(T_VIDEOS);
if (empty($video)) {
    header("Location: " . PT_Link('login'));
    exit();
}
if (!PT_IsAdmin()) {
    if (empty($db->where('id', $id)->where('user_id', $user->id)->getValue(T_VIDEOS, 'count(*)'))) {
        header("Location: " . PT_Link('login'));
        exit();
    }
}
$video           = PT_GetVideoByID($video, 0, 0, 0);
$pt->video       = $video;
$pt->page        = 'edit-video';
$pt->title       = $lang->edit_video . ' | ' . $pt->config->title;
$pt->description = $pt->config->description;
$pt->keyword     = $pt->config->keyword;

**$min_price=$config['video_play_price'];**
**    $temp_price = $videos->video_play_price;**
**    if ($temp_price<$min_price) { $temp_price = $min_price; };**


$pt->content     = PT_LoadPage('edit-video/content', array(
    'ID' => $video->id,
    'USER_DATA' => $video->owner,
    'THUMBNAIL' => $video->thumbnail,
    'URL' => $video->url,
    'TITLE' => $video->title,
    'DESC' => br2nl($video->edit_description),
    'DESC_2' => $video->markup_description,
    'VIEWS' => $video->views,
    'TIME' => $video->time_ago,
    'TAGS' => $video->tags,
    **‘video_play_price’ => $temp_price,**
	    'video_play_price' => $u_paid_videos->video_play_price,
	    'video_play_price_user' => number_format($video->video_play_price?$video->video_play_price:$config['video_play_price'],1)

));

So, in this example, you get the $min_price from the previously saved one. ( All good )
Then, you get the video_play_price from the video array. ( All good )
Lastly you check if the play price is less than the minimum price. ( All good )
BUT, you have added a semi-colon after that command which is not needed. (Removed in this one)

In the $pt->content array, you create the video_play_price using the temp price ( All good )
So, this should work except the additional semi-colon on line#32. AND, also, the line #47 needs to be removed as it is not needed as it would just overwrite the previous one. But, I am not sure if you are creating the play price correctly. In the original, it got it from

$u_paid_videos->video_play_price;

But now it is from

$temp_price = $videos->video_play_price;
Not sure if it should be
$temp_price = $u_paid_videos->video_play_price;

I think you are pulling the owner’s price from two different places here. I do not know enough about your code to sort that out. Remove the extra semicolon and remove the second price assign at line #47 and see if that fixes it. If not try this last item… Good luck!

Thanks again for your reply/help.

$min_price=$config[‘video_play_price’]; // this where admin's default price is stored 

$temp_price = $videos[‘video_play_price’]; //in db videos table > video_play_price column - is where uploader price changes are stored 

if ($temp_price<$min_price) { $temp_price = $min_price; } 

I believe I just need to reflect this where it saves the video price in “video_play_price_user”, which, I believe is this line:

'video_play_price_user' => number_format($video->video_play_price?$video->video_play_price:$config['video_play_price'],1)

Any additional guidance will be welcomed.

Well, that last line is a conditional line. It follows this syntax:
If Condition is true ? Then value X : Otherwise value Y
Therefore, it is really like this:
‘video_play_price_user’ => number_format(
if
$video->video_play_price
?
$video->video_play_price
:
config[‘video_play_price’]
,1)`
So, this line means that it checks if the value of $video->video_play_price is valid meaning if it exists. If valid, then it displays it. If not, it displays the one in the config’s price. So, this DOES work as it should using the video’s set price or the config’s set price. BUT, it does not check for the minimum price. Hope all this makes sense so far.

To fix it, you would still need to check for two things. If there is a set price as the first part of this does, and then check for if it is less than the minimum price and then set it accordingly. So, alter this line:

To something like this:
'video_play_price_user' => number_format( $video->video_play_price < $config['video_play_price'] ? $config['video_play_price'] : $video->video_play_price, 1);
What the difference is, the “IF” part of the command checks if the current video price is less than the saved config minimum price. Then, it displays the highest one. Hope this version works for you!

Thanks again SO much for your message.
I did follow what you’re saying and yes, that worked in the html where when the User changes the price to something lower than minimum here:

				<div class="form-group">
				<label class="col-md-12" for="cost">{{LANG up_video_price}}</label>
				<div class="col-md-12">
				<input id="cost" input type="text" name="video_play_price" class="form-control input-md" value="{{video_play_price_user}}">
				</div>
				</div>

the price reverts to the minimum price (success). However, any change made by User still posts in the db > videos > video_play_price column. In other words, if the User changes it to “1” in the html (and admin default is “2”) the number will show “2” there, after submit, but db > videos > video_play_price column still shows “1” (which will be the purchase price on display).

My current code looks like this:

$min_price=$config[‘video_play_price’];
$temp_price = $videos[‘video_play_price’];
if ($temp_price<$min_price) { $temp_price = $min_price;}


$pt->content     = PT_LoadPage('edit-video/content', array(
    'ID' => $video->id,
    'USER_DATA' => $video->owner,
    'THUMBNAIL' => $video->thumbnail,
    'URL' => $video->url,
    'TITLE' => $video->title,
    'DESC' => br2nl($video->edit_description),
    'DESC_2' => $video->markup_description,
    'VIEWS' => $video->views,
    'TIME' => $video->time_ago,
    'TAGS' => $video->tags,
    //'video_play_price' => $u_paid_videos->video_play_price,
	'video_play_price_user' => number_format( $video->video_play_price < $config['video_play_price'] ? $config['video_play_price'] : $video->video_play_price)
));

Buy it may have to do with this file, that we hadn’t discussed: ajax/edit-video.php, beginning about line 76 towards the bottom?:

<?php
if (IS_LOGGED == false) {
    $data = array('status' => 400, 'error' => 'Not logged in');
    echo json_encode($data);
    exit();
}


if (empty($_POST['title']) || empty($_POST['description']) || empty($_POST['tags']) || empty($_POST['video-id']) || empty($_POST['video_play_price'])) {
    $error = $lang->please_check_details;
}

if (empty($error)) {
    $id = PT_Secure($_POST['video-id']);
    $video = $db->where('id', $id)->getOne(T_VIDEOS);
    $can_update = false;
    if (PT_IsAdmin() == false) {
    	if ($db->where('user_id', $user->id)->where('id', $id)->getValue(T_VIDEOS, 'count(*)') > 0) {
    		$can_update = true;
    	}
    } else {
    	$can_update = true;
    }
    if ($can_update == true && !empty($video)) {
    	$video = PT_GetVideoByID($video, 0, 0, 0);
    	$thumbnail = $video->org_thumbnail;
    	if (!empty($_FILES['thumbnail']['tmp_name'])) {
	        $file_info   = array(
	            'file' => $_FILES['thumbnail']['tmp_name'],
	            'size' => $_FILES['thumbnail']['size'],
	            'name' => $_FILES['thumbnail']['name'],
	            'type' => $_FILES['thumbnail']['type'],
	            'allowed' => 'jpg,png,jpeg,gif',
	            'crop' => array(
	                'width' => 1076,
	                'height' => 604
	            )
	        );
	        $file_upload = PT_ShareFile($file_info);
	        if (!empty($file_upload['filename'])) {
	            $thumbnail = PT_Secure($file_upload['filename']);
	        }
	    }
	    $category_id = 0;
	    if (!empty($_POST['category_id'])) {
	        if (in_array($_POST['category_id'], array_keys($categories))) {
	            $category_id = PT_Secure($_POST['category_id']);
	        }
	    }
	    $link_regex = '/(http\:\/\/|https\:\/\/|www\.)([^\ ]+)/i';
	    $i          = 0;
	    preg_match_all($link_regex, PT_Secure($_POST['description']), $matches);
	    foreach ($matches[0] as $match) {
	        $match_url           = strip_tags($match);
	        $syntax              = '[a]' . urlencode($match_url) . '[/a]';
	        $_POST['description'] = str_replace($match, $syntax, $_POST['description']);
	    }
	    $featured = $video->featured;
	    if (isset($_POST['featured']) && PT_IsAdmin()) {
	    	$featured = PT_Secure($_POST['featured']);
	    }

	    $video_privacy = 0;
	    if (!empty($_POST['privacy'])) {
	        if (in_array($_POST['privacy'], array(0, 1, 2))) {
	            $video_privacy = PT_Secure($_POST['privacy']);
	        }
	    }
	    $age_restriction = 1;
        if (!empty($_POST['age_restriction'])) {
            if (in_array($_POST['age_restriction'], array(1, 2))) {
                $age_restriction = PT_Secure($_POST['age_restriction']);
            }
        }

		$video_play_price = floatval(PT_Secure($_POST['video_play_price']));

		$data_update = array(
	        'title' => PT_Secure($_POST['title']),
	        'description' => PT_Secure($_POST['description']),
	        'tags' => PT_Secure($_POST['tags']),
	        'category_id' => $category_id,
	        'featured' => $featured,
	        'thumbnail' => $thumbnail,
	        'privacy' => $video_privacy,
	        'age_restriction' => $age_restriction,
	        'video_play_price' => $video_play_price,
	    );

	    $update  = $db->where('id', $id)->update(T_VIDEOS, $data_update);
	    if ($update) {
	        $data = array(
	            'status' => 200,
	            'message' => $success_icon . $lang->video_saved
	        );
	    }
    }
} else {
    $data = array(
        'status' => 400,
        'message' => $error_icon . $error
    );
}
?>

Any additional help is greatly appreciated.

It appears that this line pulls the value from the posted inputs when the user uploads a video. Since I did not see this code before, we were working on the display end of it not the database creation part of it. If you make changes here, then the other display-section would not be needed as the minimum price would be already in the database. But, if a user can alter their prices later on, then you would want to keep the display-section code in place to insure the minimum price is active.

So, you would have to just alter that area to save the minimum price if the one they enter is too low.
Just add the check after that line. Then, it will create the update data correctly. Something like.

I think that should do it for you…

Thanks again so much for your reply.
Yes, everything you explained makes sense and seems correct.
I found one more missing piece which was the code that displays the price.
I changed a line to your modification like so:

if (!empty($get_videos)) {
    $len = count($get_videos);
    foreach ($get_videos as $key => $video) {
        $video = PT_GetVideoByID($video, 0, 0, 0);
        $pt->last_video = false;
        if ($key == $len - 1) {
            $pt->last_video = true;
        }
        $final .= PT_LoadPage('search/list', array(
            'ID' => $video->id,
            'USER_DATA' => $video->owner,
            'THUMBNAIL' => $video->thumbnail,
            'URL' => $video->url,
            'TITLE' => $video->title,
            'DESC' => $video->markup_description,
            'VIEWS' => $video->views,
            'VIEWS_NUM' => number_format($video->views),
            'TIME' => $video->time_ago,
            'DURATION' => $video->duration,
    		//'PRICE' =>number_format($video->video_play_price?$video->video_play_price:$config['video_play_price'])
    		'PRICE' => number_format($video->video_play_price<$config['video_play_price']?$config['video_play_price']:$video->video_play_price)
        ));
    }
}

And now the minimum price that now displays, as a purchase price, if the User tries to set a lower price than admin’s default price. Success! Much thanks again.

I am curious though, even though it all works successfully, the price changed by the User to “1” for example (but displays as “2” default), still shows 1 in the db table videos > video_play_price column. It doesn’t get updated, because a change isn’t submitted on the html edit form?

Well, I am guessing that the user has a place where they can change the value and it does not override it with the minimum value. So, you need to find out where that is done in your code and then add in the override code we created to set it to the minumum amount. So, you need to locate the edit page first.
( I can not see all of your code, so there must be a place you didn’t show. )

Well, I am guessing that the user has a place where they can change the value and it does not override it with the minimum value. So, you need to find out where that is done in your code and then add in the override code we created to set it to the minumum amount. So, you need to locate the edit page first.
( I can not see all of your code, so there must be a place you didn’t show. )

Thanks again for your help.

It all works successfully, but when I added this (upon someone’s suggestion) to the php:

declare( strict_types=1);
// these should be set in PHP.ini
error_reporting(-1); // set maximum errors
ini_set('display_errors' , 'true');

I see this:

Fatal error: Uncaught TypeError: number_format() expects parameter 1 to be float, string given in /home/public_html/sources/search/content.php:55 Stack trace: #0 /home/public_html/sources/search/content.php(55): number_format(‘2.00’) #1 /home/public_html/index.php(57): include(’/home/p…’) #2 {main} thrown in /home/public_html/sources/search/content.php on line 55

and line 55 is this:

'PRICE' => number_format($video->video_play_price<$config['video_play_price']?$config['video_play_price']:$video->video_play_price)

Any help with resolving this Error will be appreciated

This means that you must lock in ALL of your variable “types” for each and every variable.
You can’t just use any variable to pass to functions that way unless they are declared first.
Try removing just that line and see if you still get errors.

Well, no. I will refer you both to the manual.
http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict

Here is a simple code example.

<?php
declare(strict_types=1);

function sum(int $a, int $b) {
    return $a + $b;
}

var_dump(sum(1, 2)); This works. Function expects two integers and gets two integers.
var_dump(sum(1.5, 2.5));// Doesn't work. Function expects integers but gets a floats.

I realize that! But, he is NOT casting the declaration in his functions, therefore, he should not be declaring his variables as strict. He would need to set his variable types before using the variables. He did not. Unless I missed that code somewhere…

Thanks for those replies.
I have commented out the

declare(strict_types=1);

and the error is gone.
Thank you

Thanks again for all of the help with this modification.
It works successfully where the User can edit his price and the admin sets the minimum price. But, I am wondering if there is a way to keep it all this way, but allow just the user “Admin” to set a price lower than the minimum?

Well, that by definition would change the minimum. Which if that is the case, why can’t you jsut have the admin change the actual minimum?

Sponsor our Newsletter | Privacy Policy | Terms of Service