Help with understanding what this says/does

Can you let me know what this code below says and does? This is from a web video script that I have (but didn’t develop):

    if (!empty($_POST['set_p_v'])) {
        if (($pt->config->sell_videos_system == 'on' && $pt->config->who_sell == 'pro_users' && $pt->user->is_pro) || ($pt->config->sell_videos_system == 'on' && $pt->config->who_sell == 'users') || ($pt->config->sell_videos_system == 'on' && $pt->user->admin) && !empty($_POST['set_p_v'])) {
            if (!empty($_POST['set_p_v']) || (in_array('set_p_v', array_keys($_POST)) && $_POST['set_p_v'] < 0)) {
                if (!is_numeric($_POST['set_p_v']) || $_POST['set_p_v'] < 0 || (($pt->config->com_type == 0 && $_POST['set_p_v'] <= $pt->config->admin_com_sell_videos)) ) {
                    $error = $lang->video_price_error." ".($pt->config->com_type == 0 ? $pt->config->admin_com_sell_videos : 0);
                }

I know that “set_p_v” is the ‘buy price’ and I believe pt is the initials of the web script name. And in particular also I’m interested to know what specifcally causes the ‘video_price_error’ message. I look forward to any assistance. Thanks.

1 Like

My first step would be to spread the code out to make it a bit more readable.

Given that you only show a small part of the code and we don’t know what all the variables are, I think it’s highly unlikely anyone can tell you what the script is doing.

It… appears to be some sort of abort code for setting the price of a video (p_v).
If there is a price to be set (line 1)
and the user type is authorized to set prices (line 2)
and there is a price defined to be set somewhere (line3) (not quite sure why this is there)
but the value is not numeric, or less than 0, or the value being given is too low (at a guess… at the commision price of the sale?), (Line 4)
abort the setting of the price because the value is invalid. (line 5)

I can guess! :wink:

1 Like

Thanks for all the replies.
And thanks for the breakdown of “line 1” etc…

  1. I forgot to add that if the ‘Price’ form field is left empty (and submitted) the Price of the video is free. Can you tell me if/where that is in the code?

  2. And if text (or a negative number) is entered into the ‘Price’ form field the

$error = $lang->video_price_error

is activated and displays “The video price should be numeric and greater than 0”

Well the code you provided us with starts by asserting that the price field is not empty. So the only answer i have to #1 is “somewhere else in the code”. #2 isnt a question, so ill just nod my head sagely.

1 Like

Do you mean somewhere in other code that is not provided here, or somewhere else in the code that is provided here?

The entirity of the code you have provided is wrapped in the first If -

That { does not have a matching } in the code you have provided.
You have asked “what if $_POST[‘set_p_v’] is empty”, to wit I have no answer, because I dont know what happens before or after the close of that if statement. It may be that an else is attached to that if that tells it to set the price to free. It’s also possible that the price has a default value of 0 when it is created, so no error is generated or even considered in the PHP code. I can’t answer those possibilities with what you have shown us.

Thanks again for your reply. Sorry, for my negligence, here’s the proper code:

    if (!empty($_POST['set_p_v'])) {
        if (($pt->config->sell_videos_system == 'on' && $pt->config->who_sell == 'pro_users' && $pt->user->is_pro) || ($pt->config->sell_videos_system == 'on' && $pt->config->who_sell == 'users') || ($pt->config->sell_videos_system == 'on' && $pt->user->admin) && !empty($_POST['set_p_v'])) {
            if (!empty($_POST['set_p_v']) || (in_array('set_p_v', array_keys($_POST)) && $_POST['set_p_v'] < 0)) {
                if (!is_numeric($_POST['set_p_v']) || $_POST['set_p_v'] < 0 || (($pt->config->com_type == 0 && $_POST['set_p_v'] <= $pt->config->admin_com_sell_videos)) ) {
                    $error = $lang->video_price_error." ".($pt->config->com_type == 0 ? $pt->config->admin_com_sell_videos : 0);
                }
}
}
}

[/quote]

I am not experienced with PHP but probably that code is unnecessarily complicated. You should try to simplify so it is not so big. It references $_POST['set_p_v'] many times, perhaps as many as 16 times. The first line of your code checks if !empty($_POST['set_p_v']) and then the second and third lines (the first and second embeded if) does that again. The other two are unnecessary unless something happens in between that changes the value.

You probably can retrieve $_POST['set_p_v'] once (right?) and put it in a temporary variable and use the temporary variable instead in those lines.

As I said, I am not real experienced with PHP but you could move:

!is_numeric($_POST['set_p_v']) || $_POST['set_p_v'] < 0

Out to a separate function or whatever with a meaningful name. It might be slightly less efficient for the computer but if it helps you understand then it is highly economical to do that.

To be fair, all that would achieve is slightly less typing, unless the OP uses a long variable name. Otherwise all that’s happening is a new variable is being created for no reason.

But of course you’re quite correct about not having to check that $_POST['set_p_v'] is not empty in the second and third lines of code when those lines will only be executing if it is not, because of the check on the first line.

if (!empty($_POST['set_p_v'])) {
    if ($pt->config->sell_videos_system == 'on' 
      && (($pt->config->who_sell == 'pro_users' 
      && $pt->user->is_pro) 
      || 
      $pt->config->who_sell == 'users'
      || 
      $pt->user->admin)) {
        if ($_POST['set_p_v'] < 0)) {
            if (!is_numeric($_POST['set_p_v']) || 
                (($pt->config->com_type == 0 && 
                $_POST['set_p_v'] <= $pt->config->admin_com_sell_videos)) ) {
                $error = $lang->video_price_error." ".($pt->config->com_type == 0 ? $pt->config->admin_com_sell_videos : 0);
                }
            }
    }
}

Actually I’m not sure that makes it all that much clearer, but I’ve split out the various conditions and removed the spurious second and third checks that the post variable is not empty. On a proper editor screen, some of the multiple conditions might not need to be separated on to separate lines, that’s just to fit in the box here.

I also removed the in_array() check - off the top of my head if we’ve already established that $_POST['set_p_v'] is not empty, I don’t think there’s any need to then check if set_p_v is in the array keys of $_POST, is there?

I’ve also removed an OR condition which checks $_POST['set_p_v'] < 0 inside an if clause that already checks the same thing. I also simplified the condition checks at the top as all three of them require that $pt->config->sell_videos_system == 'on' . You could move that check out to the first if() clause, really.

See, my assumption was that there were cascading error messages depending on what went wrong. If you just have a bunch of }'s after all those if’s, there’s not really a lot of point in separating them (as Short Circuitng will take care of the rest as droop points out whether intended or not :wink: )

My answer such as it is hasn’t changed…
If $_POST[‘set_p_v’] is empty, then the code will skip this whole block, and move on to the next block. So whatever is making the video free, is either before this entire code block, or after this entire code block, or not even in this page. I still cant answer your question.

Thanks again for the replies.
I think you’re right when you say "If $_POST[‘set_p_v’] is empty, then the code will skip this whole block, So, whatever is making it free…Etc.

The only thing I can see related to this, after that code block is this:

        if (!empty($_POST['set_p_v']) && is_numeric($_POST['set_p_v']) && $_POST['set_p_v'] > 0) {
            $data_insert['sell_video'] = PT_Secure($_POST['set_p_v']);
        }

which might mean if there’s a number > 0 insert it in the database?
I look forward to any comments, thanks again.

The code provided does not have the quoted lines of text you are looking for so set aside WHAT each line does, and do a search in your code FOR THOSE lines of text so you can pin down the blocks of code you are looking for.

Thanks for all the great help.
I understand that code a lot better now. Much appreciated.
I’m trying to modify it so that that Price field can’t be left empty but have a number, the number zero or higher. In the html Form field code I can add min=“0”, but I’d like help with starting to add, something like this, to the php:

if set_p_v is exactly equal to zero check if user (wallet) amount > 0, if not > 0 display an error message

would this be close to correct:

  if (!empty($_POST['set_p_v']) && is_numeric($_POST['set_p_v']) && $_POST['set_p_v'] = 0) && ($wallet >0); 
{
else 
}
$error = $lang->video_price_error.";
}

any help with that is appreciated

It would be easier to handle 0 than an empty value for “free videos” as 0 and an empty value both pass as TRUE for empty(). You could then get 0 to pass in places that have

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

but adding a check for 0

if (!empty($_POST['set_p_v']) || (empty($_POST['set_p_v']) && $_POST['set_p_v'] == 0)) {

But that just gets 0 past that first IF condition so you’ll need to make sure 0 handled in other sections of your code.

Thanks for your message. I believe I have found that the error message displays the script language $lang->video_price_error which is "The video price should be numeric and greater than 0”, based on this line:
if (!is_numeric($_POST['set_p_v']) || $_POST['set_p_v'] < 0 || (($pt->config->com_type == 0 && $_POST['set_p_v'] <= $pt->config->admin_com_sell_videos)) ) {

And that the script text language says that this ‘set p_v’ means this: “Price (Leave empty for free videos)”

You don’t make comparisons with a single = sign.

$_POST['set_p_v'] == 0

“Price (Leave empty for free videos)”

Then instead of checking for 0 you would check for empty.

if (!empty($_POST['set_p_v']) || (empty($_POST['set_p_v']) && $_POST['set_p_v'] == "")){

I am thinking you replace what you have with something like this.

if (!empty($_POST['set_p_v']) || (empty($_POST['set_p_v']) && $_POST['set_p_v'] == "")){
	if (($pt->config->sell_videos_system == 'on' && $pt->config->who_sell == 'pro_users' && $pt->user->is_pro) || ($pt->config->sell_videos_system == 'on' && $pt->config->who_sell == 'users') || ($pt->config->sell_videos_system == 'on' && $pt->user->admin)) {
		// is_numeric error
		if (!is_numeric($_POST['set_p_v']) && $_POST['set_p_v'] !== ""){
			$error = $lang->video_price_error;
		}
		// lack of funds error
		if (empty($error) && empty($_POST['set_p_v']) && $_POST['set_p_v'] == "" && $wallet <1){
		   $error = "lack of funds error"
		}
	}
}

Note: I’ve got somewhere I need to go so can’t look at this more but hope it helps.

Much thanks again for your kind help.
However, I replaced my originally posted code with your code,
and it allowed the empty Price form field to Submit. First, I’m trying to have a zero or higher only as a correct entry into the Form field.
Next, I’m trying to create the functionaility where if zero is entered check the user wallet amount, if <1 show error and don’t proceed, otherwise proceed (submit).

Any additional guidance is welcomed…