/    Sign up×
Community /Pin to ProfileBookmark

Replacing a variable in a transaction file

I’m using a php video web script which includes this buy_video.php file, which calculates the video uploader’s earned amount, and insert’s that amount in the ‘u_paid_videos’ table column named ‘earned_amount’. The ‘earned_amount’ appears in this file on line 73:

`
<?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
// get the default video price, to use if there is no per video play price
$db->where(‘name’, ‘video_play_price’);
$db_cost = $db->getOne(‘config’);
$video_cost = (float)$db_cost->value;

// the number of submitted videos – used to determine if all records were inserted
$count_video = count($id_array);
$user_id = $user->id;

$wallet = (float)str_replace(‘,’, ”, $user->wallet);
$balance = (float)str_replace(‘,’, ”, $user->balance);

// add up the video prices
$amount = 0;
foreach ($id_array as $id) {

$video_id = (int)PT_Secure($id);

// get video data
$video = $db->where(‘id’, $id)->getOne(T_VIDEOS);
// add the video play price if any, or the default price
$amount += $video->video_play_price?$video->video_play_price:$video_cost;
}

// determine if the user has enough credits
if( ($wallet >= $amount) OR ($balance + $wallet >= $amount) ) {
//if( ($wallet >= $amount) OR ($balance >= $amount) ) {

$db->startTransaction();

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

$video_id = (int)PT_Secure($id);

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

// use the video play price if any, or the default price
$video_cost_new = $video->video_play_price?$video->video_play_price:$video_cost;

// credit the user 50% of the 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, // the cost at the time of purchase // this is the default video cost not the $video_cost_new
‘video_title’ => $video->title, // storing the title
‘user_id_uploaded’ => $video->user_id, // the user who uploaded the video
‘earned_amount’ => $uploader_amount,
]);

// count successful inserted records
if ($insert_buy) {
$inserted_records++;
}

//update the ‘balance’ of the user who uploaded the video
// get the user’s record
$userwallet = $db->where(‘id’, $video->user_id)->getOne(T_USERS);

// add to the balance
$videouserwallet = $userwallet->balance+$uploader_amount;

// update the record
$db->where(‘id’, $video->user_id);
$update_balance = $db->update(T_USERS, [
‘balance’ => number_format($videouserwallet, 1, ‘.’, ”),
]);
}

$update_wallet = null;
$update_user_balance = null;

ETC ……………………………………………..
`
Ultimately, I’m trying to integrate the buy_video.php file’s ‘$earned_amount’ into a web script’s existing transaction file (to calculate a daily (and monthly) ‘earned_amount).

Here are a couple of lines from that transaction file:

`
$this_day_video_earn = $db-&gt;rawQuery(“SELECT SUM(amount – admin_com) AS sum FROM “.T_VIDEOS_TRSNS.” c WHEREtime&gt;= “.$day_start.” ANDtime&lt;= “.$day_end.” AND user_id = “.$pt-&gt;user-&gt;id); $today_earn = $this_day_ads_earn[0]-&gt;sum + $this_day_video_earn[0]-&gt;sum ;
`

I’ve attempted this modification without success::

`
$this_day_video_earn = $db-&gt;rawQuery(“SELECT SUM(earned_amount) AS sum FROM “.T_U_PAID_VIDEOS.” c WHEREtime&gt;= “.$day_start.” ANDtime&lt;= “.$day_end.” AND user_id_uploaded = “.$pt-&gt;user-&gt;id);
`

“Without success” means when I select the web button that would normally direct me to the transaction html page, it just hangs, no redirect. When I put this file as it was originally, I can redirect to the transaction page. Here is the file, where I’ve modified that line (86):

`
<?php

if (!IS_LOGGED || ($pt->config->sell_videos_system == ‘off’ && $pt->config->usr_v_mon == ‘off’) ) {
header(‘Location: ‘ . PT_Link(‘404’));
exit;
}

$types = array(‘today’,’this_week’,’this_month’,’this_year’);
$type = ‘today’;

if (!empty($_GET[‘type’]) && in_array($_GET[‘type’], $types)) {
$type = $_GET[‘type’];
}

if ($type == ‘today’) {
$start = strtotime(date(‘M’).” “.date(‘d’).”, “.date(‘Y’).” 12:00am”);
$end = strtotime(date(‘M’).” “.date(‘d’).”, “.date(‘Y’).” 11:59pm”);

$array = array(’00’ => 0 ,’01’ => 0 ,’02’ => 0 ,’03’ => 0 ,’04’ => 0 ,’05’ => 0 ,’06’ => 0 ,’07’ => 0 ,’08’ => 0 ,’09’ => 0 ,’10’ => 0 ,’11’ => 0 ,’12’ => 0 ,’13’ => 0 ,’14’ => 0 ,’15’ => 0 ,’16’ => 0 ,’17’ => 0 ,’18’ => 0 ,’19’ => 0 ,’20’ => 0 ,’21’ => 0 ,’22’ => 0 ,’23’ => 0);
$ads_array = $array;
$date_type = ‘H’;
$pt->cat_type = ‘today’;
$pt->chart_title = $lang->today;
$pt->chart_text = date(“l”);

}
elseif ($type == ‘this_week’) {

$time = strtotime(date(‘l’).”, “.date(‘M’).” “.date(‘d’).”, “.date(‘Y’));
if (date(‘l’) == ‘Saturday’) {
$start = strtotime(date(‘M’).” “.date(‘d’).”, “.date(‘Y’).” 12:00am”);
}
else{
$start = strtotime(‘last saturday, 12:00am’, $time);
}

if (date(‘l’) == ‘Friday’) {
$end = strtotime(date(‘M’).” “.date(‘d’).”, “.date(‘Y’).” 11:59pm”);
}
else{
$end = strtotime(‘next Friday, 11:59pm’, $time);
}

$array = array(‘Saturday’ => 0 , ‘Sunday’ => 0 , ‘Monday’ => 0 , ‘Tuesday’ => 0 , ‘Wednesday’ => 0 , ‘Thursday’ => 0 , ‘Friday’ => 0);
$ads_array = $array;
$date_type = ‘l’;
$pt->cat_type = ‘this_week’;
$pt->chart_title = $lang->this_week;
$pt->chart_text = date(‘y/M/d’,$start).” To “.date(‘y/M/d’,$end);

}
elseif ($type == ‘this_month’) {
$start = strtotime(“1 “.date(‘M’).” “.date(‘Y’).” 12:00am”);
$end = strtotime(cal_days_in_month(CAL_GREGORIAN, date(‘m’), date(‘Y’)).” “.date(‘M’).” “.date(‘Y’).” 11:59pm”);
if (cal_days_in_month(CAL_GREGORIAN, date(‘m’), date(‘Y’)) == 31) {
$array = array(’01’ => 0 ,’02’ => 0 ,’03’ => 0 ,’04’ => 0 ,’05’ => 0 ,’06’ => 0 ,’07’ => 0 ,’08’ => 0 ,’09’ => 0 ,’10’ => 0 ,’11’ => 0 ,’12’ => 0 ,’13’ => 0 ,’14’ => 0 ,’15’ => 0 ,’16’ => 0 ,’17’ => 0 ,’18’ => 0 ,’19’ => 0 ,’20’ => 0 ,’21’ => 0 ,’22’ => 0 ,’23’ => 0,’24’ => 0 ,’25’ => 0 ,’26’ => 0 ,’27’ => 0 ,’28’ => 0 ,’29’ => 0 ,’30’ => 0 ,’31’ => 0);
}elseif (cal_days_in_month(CAL_GREGORIAN, date(‘m’), date(‘Y’)) == 30) {
$array = array(’01’ => 0 ,’02’ => 0 ,’03’ => 0 ,’04’ => 0 ,’05’ => 0 ,’06’ => 0 ,’07’ => 0 ,’08’ => 0 ,’09’ => 0 ,’10’ => 0 ,’11’ => 0 ,’12’ => 0 ,’13’ => 0 ,’14’ => 0 ,’15’ => 0 ,’16’ => 0 ,’17’ => 0 ,’18’ => 0 ,’19’ => 0 ,’20’ => 0 ,’21’ => 0 ,’22’ => 0 ,’23’ => 0,’24’ => 0 ,’25’ => 0 ,’26’ => 0 ,’27’ => 0 ,’28’ => 0 ,’29’ => 0 ,’30’ => 0);
}elseif (cal_days_in_month(CAL_GREGORIAN, date(‘m’), date(‘Y’)) == 29) {
$array = array(’01’ => 0 ,’02’ => 0 ,’03’ => 0 ,’04’ => 0 ,’05’ => 0 ,’06’ => 0 ,’07’ => 0 ,’08’ => 0 ,’09’ => 0 ,’10’ => 0 ,’11’ => 0 ,’12’ => 0 ,’13’ => 0 ,’14’ => 0 ,’15’ => 0 ,’16’ => 0 ,’17’ => 0 ,’18’ => 0 ,’19’ => 0 ,’20’ => 0 ,’21’ => 0 ,’22’ => 0 ,’23’ => 0,’24’ => 0 ,’25’ => 0 ,’26’ => 0 ,’27’ => 0 ,’28’ => 0 ,’29’ => 0);
}elseif (cal_days_in_month(CAL_GREGORIAN, date(‘m’), date(‘Y’)) == 28) {
$array = array(’01’ => 0 ,’02’ => 0 ,’03’ => 0 ,’04’ => 0 ,’05’ => 0 ,’06’ => 0 ,’07’ => 0 ,’08’ => 0 ,’09’ => 0 ,’10’ => 0 ,’11’ => 0 ,’12’ => 0 ,’13’ => 0 ,’14’ => 0 ,’15’ => 0 ,’16’ => 0 ,’17’ => 0 ,’18’ => 0 ,’19’ => 0 ,’20’ => 0 ,’21’ => 0 ,’22’ => 0 ,’23’ => 0,’24’ => 0 ,’25’ => 0 ,’26’ => 0 ,’27’ => 0 ,’28’ => 0);
}
$ads_array = $array;
$pt->month_days = count($array);
$date_type = ‘d’;
$pt->cat_type = ‘this_month’;
$pt->chart_title = $lang->this_month;
$pt->chart_text = date(“M”);
}
elseif ($type == ‘this_year’) {
$start = strtotime(“1 January “.date(‘Y’).” 12:00am”);
$end = strtotime(“31 December “.date(‘Y’).” 11:59pm”);
$array = array(’01’ => 0 ,’02’ => 0 ,’03’ => 0 ,’04’ => 0 ,’05’ => 0 ,’06’ => 0 ,’07’ => 0 ,’08’ => 0 ,’09’ => 0 ,’10’ => 0 ,’11’ => 0 ,’12’ => 0);
$ads_array = $array;
$date_type = ‘m’;
$pt->cat_type = ‘this_year’;
$pt->chart_title = $lang->this_year;
$pt->chart_text = date(“Y”);
}

$day_start = strtotime(date(‘M’).” “.date(‘d’).”, “.date(‘Y’).” 12:00am”);
$day_end = strtotime(date(‘M’).” “.date(‘d’).”, “.date(‘Y’).” 11:59pm”);
$this_day_ads_earn = $db->rawQuery(“SELECT SUM(amount) AS sum FROM “.T_ADS_TRANS.” c WHERE `time` >= “.$day_start.” AND `time` <= “.$day_end.” AND type = ‘video’ AND video_owner = “.$pt->user->id);

$this_day_video_earn = $db->rawQuery(“SELECT SUM(earned_amount) AS sum FROM “.T_U_PAID_VIDEOS.” c WHERE `time` >= “.$day_start.” AND `time` <= “.$day_end.” AND user_id_uploaded = “.$pt->user->id);
//$this_day_video_earn = $db->rawQuery(“SELECT earned_amount FROM “.T_U_PAID_VIDEOS.” c WHERE `time` >= “.$day_start.” AND `time` <= “.$day_end.” AND user_id_uploaded = “.$pt->user->id);
//$this_day_video_earn = $db->rawQuery(“SELECT SUM(amount – admin_com) AS sum FROM “.T_VIDEOS_TRSNS.” c WHERE `time` >= “.$day_start.” AND `time` <= “.$day_end.” AND user_id = “.$pt->user->id);
$today_earn = $this_day_ads_earn[0]->sum + $this_day_video_earn[0]->sum ;

$month_start = strtotime(“1 “.date(‘M’).” “.date(‘Y’).” 12:00am”);
$month_end = strtotime(cal_days_in_month(CAL_GREGORIAN, date(‘m’), date(‘Y’)).” “.date(‘M’).” “.date(‘Y’).” 11:59pm”);
$this_month_ads_earn = $db->rawQuery(“SELECT SUM(amount) AS sum FROM “.T_ADS_TRANS.” c WHERE `time` >= “.$month_start.” AND `time` <= “.$month_end.” AND type = ‘video’ AND video_owner = “.$pt->user->id);
$this_month_video_earn = $db->rawQuery(“SELECT SUM(amount – admin_com) AS sum FROM “.T_VIDEOS_TRSNS.” c WHERE `time` >= “.$month_start.” AND `time` <= “.$month_end.” AND user_id = “.$pt->user->id);
$month_earn = $this_month_ads_earn[0]->sum + $this_month_video_earn[0]->sum ;
// print_r($this_month_video_earn);
// exit();

$trans = $db->where(‘user_id’,$user->id)->orderBy(‘id’,’DESC’)->get(T_VIDEOS_TRSNS);
$ads_trans = $db->where(‘time’,$start,’>=’)->where(‘time’,$end,'<=’)->where(‘video_owner’,$pt->user->id)->where(‘type’,’video’)->get(T_ADS_TRANS);
$total_ads = 0;
if (!empty($ads_trans)) {
foreach ($ads_trans as $key => $ad) {
if ($ad->time >= $start && $ad->time <= $end) {
$day = date($date_type,$ad->time);
if (in_array($day, array_keys($ads_array))) {
$ads_array[$day] += $ad->amount;
$total_ads += $ad->amount;
}
}
}
}
$ads_list = “”;

$total_earn = 0;
if (!empty($trans)) {
foreach ($trans as $tr) {
$video = PT_GetVideoByID($tr->video_id, 0, 0, 2);

$user_data = PT_UserData($tr->paid_id);

$currency = “”;
$admin_currency = “”;
$net = 0;
if ($tr->currency == “USD”) {
$currency = “$”;
$admin_currency = “$”.$tr->admin_com;
$net = $tr->amount – $tr->admin_com;
}
else if($tr->currency == “EUR”){
$currency = “€”;
$admin_currency = “€”.$tr->admin_com;
$net = $tr->amount – $tr->admin_com;
}
elseif ($tr->currency == “EUR_PERCENT”) {
$currency = “€”;
$admin_currency = $tr->admin_com.”%”;
$net = $tr->amount – ($tr->admin_com * $tr->amount)/100;
}
elseif ($tr->currency == “USD_PERCENT”) {
$currency = “$”;
$admin_currency = $tr->admin_com.”%”;
$net = $tr->amount – ($tr->admin_com * $tr->amount)/100;
}

if ($tr->time >= $start && $tr->time <= $end) {
$day = date($date_type,$tr->time);
if (in_array($day, array_keys($array))) {
$array[$day] += $net;
}
}

$total_earn = $total_earn + (float)$net;
if (!empty($video) && !empty($user_data)) {
$ads_list .= PT_LoadPage(‘transactions/list’,array(
‘ID’ => $tr->id,
‘PAID_USER’ => substr($user_data->name, 0,20),
‘PAID_URL’ => $user_data->url,
‘USER_NAME’ => $user_data->username,
‘VIDEO_NAME’ => substr($video->title, 0,20) ,
‘VIDEO_URL’ => $video->url,
‘VIDEO_ID_’ => PT_Slug($video->title, $video->video_id),
‘AMOUNT’ => $tr->amount,
“CURRENCY” => $currency,
“A_CURRENCY” => $admin_currency,
“NET” => $net,
“TIME” => PT_Time_Elapsed_String($tr->time)
));
}
}

}
$total_earn = $total_earn + $total_ads;

$pt->array = implode(‘, ‘, $array);
$pt->ads_array = implode(‘, ‘, $ads_array);
$pt->page_url_ = $pt->config->site_url.’/transactions’;
$pt->title = $lang->earnings . ‘ | ‘ . $pt->config->title;
$pt->page = “transactions”;
$pt->description = $pt->config->description;
$pt->keyword = @$pt->config->keyword;
$pt->content = PT_LoadPage(‘transactions/content’,array(
‘CURRENCY’ => $currency,
‘ADS_LIST’ => $ads_list,
‘TOTAL_EARN’ => $total_earn,
‘TODAY_EARN’ => $today_earn,
‘MONTH_EARN’ => $month_earn
));
`
I look forward to any questions and any assistance.
Much thanks.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@rootMar 19.2019 — It looks like, and I am not reading through all that, the program you have could be best served utilising a database rather than many arrays.

The beauty being that a database parameter can be changed and it affects the whole script compared to having to wade through and find every instance to edit out or in...

I'd say clean your code up, employ proper post sanitation in to a safe array of data, do not use GET to control rather than to be a flag that if changed by someone externally, will not affect the program.

Use sessions when passing between pages, it eliminated GET al together.
Copy linkTweet thisAlerts:
@chrisjchrisjauthorMar 19.2019 — Thanks for your reply.

the web script does use a DB, the T_ denoted a dB table. Im not the developer, I’m just trying to modify a couple of lines, not rewrite the script. Any additional help is welcomed
Copy linkTweet thisAlerts:
@ginerjmMar 19.2019 — As Root says - way too much code here for a stranger to wade thru. The recommendation is to only post that code that is pertinent to an identified problem. You have gone way beyond that!
Copy linkTweet thisAlerts:
@NogDogMar 19.2019 — > @chrisjchrisj#1601927 $this_day_video_earn = $db-&amp;gt;rawQuery("SELECT SUM(earned_amount) AS sum FROM ".T_U_PAID_VIDEOS." c WHEREtime&amp;gt;= ".$day_start." ANDtime&amp;lt;= ".$day_end." AND user_id_uploaded = ".$pt-&amp;gt;user-&amp;gt;id);

Not sure what I'm looking at here...maybe a copy/paste issue?...but that should probably be:
``<i>
</i>$this_day_video_earn = $db-&gt;rawQuery("SELECT SUM(earned_amount) AS sum FROM ".
T_U_PAID_VIDEOS." c WHERE time &gt;= ".$day_start." AND time &lt;= ".$day_end.
" AND user_id_uploaded = ".$pt=&gt;user-&gt;id);<i>
</i>
``
Copy linkTweet thisAlerts:
@chrisjchrisjauthorMar 19.2019 — 
Thanks for the replies.

Yes, you are right, it is a copy paste issue. Your revision is correct.

So, the developer has defined tables with a T_, so that line is reference the 'u_paid_videos, table, and the 'earned_amount' column and the 'time' column.

Any ideas on why that modified line keeps the transaction page from appearing will be appreciated.
Copy linkTweet thisAlerts:
@EbizzInfoTechMar 19.2019 — Best Mobile app & Games Development Company, enterprise website design and development service provider.

Best [PHP Web Services](http://www.ebizzinfotech.com/php-web-development/) In the USA with Class Customization.
Copy linkTweet thisAlerts:
@chrisjchrisjauthorMar 19.2019 — Thanks again for your replies. I've made some progress.

This code no longer blocks the html page from displaying ( I apparently have it correct now ):


$this_day_video_earn = $db-&gt;rawQuery("SELECT SUM(earned_amount) AS sum FROM ".T_U_PAID_VIDEOS." c WHERE
time &gt;= ".$day_start." AND time &lt;= ".$day_end." AND user_id_uploaded = ".$pt-&gt;user-&gt;id);



$today_earn = $this_day_ads_earn[0]-&gt;sum + $this_day_video_earn[0]-&gt;sum ;


However, after a transaction (the 'earned_amount' successfully appears in the 'u_paid_videos' table > in the 'earned_amount' column) the html still shows $0 in this code: <p>{{TODAY_EARN}}</p> instead of an 'earned_amount'. Any additional guidance be appreciated.
Copy linkTweet thisAlerts:
@rootMar 19.2019 — @chrisjchrisj#1601950 So the developer uses databases but then goes and throws in a load of code that really would be best utilised in a DB table, instead of 8 or 9 lines of code, that could be distilled in to a single DB query and examine the result... maybe 4 or 5 lines at the extreme if you want to do some other work on the data...
Copy linkTweet thisAlerts:
@chrisjchrisjauthorMar 19.2019 — Thanks for your reply.

I appreciate your message, but, I don't know much about the developer or his skill level.

Any guidance with my issue, will be welcomed.
×

Success!

Help @chrisjchrisj spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...