I am new to this. I have this function here thaat returns my table and inside a table there’s a button now I want to be clicked to update a table but somehow it’s updating everything when I click on the Pay button in one of the row in my table. I am not sure where I am going wrong with it. Please help
function lue_custom_view() {
global $wpdb,$table_prefix;
$output .= '<table>
<tr style="color: #fff; background: #08a873; font-weight: bold;">
<th> Account ID</th>
<th>Deposited Amount</th>
<th>Deposit Date</th>
<th>Period</th>
<th>Counter [Days]</th>
<th>Amount Growth</th>
<th colspan="2">Total Return</th>
</tr>';
$approve = "Approved";
$tbl_name = $wpdb->prefix."member_deposit";
$results = $wpdb->get_results("select * from $tbl_name where conf_deposit = 'Yes' AND approve = 'unapprove'");
$current_date = date('Y-m-d H:i:s');
foreach( $results as $user_data) {
$startTimeStamp = strtotime($user_data->deposit_date);
$endTimeStamp = strtotime($current_date);
$timeDiff = abs($endTimeStamp - $startTimeStamp);
$numberDays = $timeDiff/86400; // 86400 seconds in one day
// and you might want to convert to integer
$numberDays = intval($numberDays);
$realP = $user_data->period;
$myPeriod = 0;
if($realP == 2){
$myPeriod = 60;
}
if($realP == 3){
$myPeriod = 90;
}
if($realP == 4){
$myPeriod = 120;
}
if($realP == 5){
$myPeriod = 150;
}
if($realP == 6){
$myPeriod = 180;
}
if($realP == 7){
$myPeriod = 210;
}
$roi = $myPeriod * 0.02 * $user_data->amount + $user_data->amount;
$amount_growth = $myPeriod * 0.02 * $user_data->amount;
$name = $wpdb->get_var('SELECT user_nicename FROM '.$table_prefix.'users WHERE ID = '.$user_data->ID);
if($realP==2 && $numberDays >= 60){
$output .= "<tr style='background-color: red; color: #fff'>
<td> $user_data->ID</td>
<td> R$user_data->amount</td>
<td> $user_data->deposit_date</td>
<td> $realP</td>
<td> $numberDays</td>
<td> R$amount_growth</td>
<td> R$roi</td>
<td> <form method='post'> <input type='submit' name='submit' value='Pay' /> </form> </td>
</tr>";
//Add Withdrawal
if($_POST['submit']) {
$todate = date('Y-m-d H:i:s');
$success = $wpdb->insert("wp_member_history", array(
"ID" => $user_data->ID,
"amount" => $user_data->amount,
"wit_date" => $todate
));
//Update Member Status
$status = "Approved";
$where = array('ID' => $user_data->ID);
$subs = array('approve' => $status);
$table_name = $wpdb->prefix."member_deposit";
$wpdb->update($table_name, $subs, $where);
if($success) {
header('Location: http://empireinvest.co.za/RegSuccess');
exit;
} else {
echo 'not';
}
}
} elseif($realP==3 && $numberDays >= 90){
$output .= "<tr style='background-color: red; color: #fff'>
<td> $user_data->ID</td>
<td> R$user_data->amount</td>
<td> $user_data->deposit_date</td>
<td> $realP</td>
<td> $numberDays</td>
<td> R$amount_growth</td>
<td> R$roi</td>
<td> <form method='post'> <input type='submit' name='submit' value='Pay' /> </form> </td>
</tr>";
//Add Withdrawal
if($_POST['submit']) {
$todate = date('Y-m-d H:i:s');
$success = $wpdb->insert("wp_member_history", array(
"ID" => $user_data->ID,
"amount" => $user_data->amount,
"wit_date" => $todate
));
//Update Member Status
$status = "Approved";
$where = array('ID' => $user_data->ID);
$subs = array('approve' => $status);
$table_name = $wpdb->prefix."member_deposit";
$wpdb->update($table_name, $subs, $where);
if($success) {
header('Location: http://empireinvest.co.za/RegSuccess');
exit;
} else {
echo 'not';
}
}
}
elseif($realP==4 && $numberDays >= 120){
$output .= "<tr style='background-color: red; color: #fff'>
<td> $user_data->ID</td>
<td> R$user_data->amount</td>
<td> $user_data->deposit_date</td>
<td> $realP</td>
<td> $numberDays</td>
<td> R$amount_growth</td>
<td> R$roi</td>
<td> <form method='post'> <input type='submit' name='submit' value='Pay' /> </form> </td>
</tr>";
//Add Withdrawal
if($_POST['submit']) {
$todate = date('Y-m-d H:i:s');
$success = $wpdb->insert("wp_member_history", array(
"ID" => $user_data->ID,
"amount" => $user_data->amount,
"wit_date" => $todate
));
//Update Member Status
$status = "Approved";
$where = array('ID' => $user_data->ID);
$subs = array('approve' => $status);
$table_name = $wpdb->prefix."member_deposit";
$wpdb->update($table_name, $subs, $where);
if($success) {
header('Location: http://empireinvest.co.za/RegSuccess');
exit;
} else {
echo 'not';
}
}
}
elseif($realP==5 && $numberDays >= 150){
$output .= "<tr style='background-color: red; color: #fff'>
<td> $user_data->ID</td>
<td> R$user_data->amount</td>
<td> $user_data->deposit_date</td>
<td> $realP</td>
<td> $numberDays</td>
<td> R$amount_growth</td>
<td> R$roi</td>
<td> <form method='post'> <input type='submit' name='submit' value='Pay' /> </form> </td>
</tr>";
//Add Withdrawal
if($_POST['submit']) {
$todate = date('Y-m-d H:i:s');
$success = $wpdb->insert("wp_member_history", array(
"ID" => $user_data->ID,
"amount" => $user_data->amount,
"wit_date" => $todate
));
//Update Member Status
$status = "Approved";
$where = array('ID' => $user_data->ID);
$subs = array('approve' => $status);
$table_name = $wpdb->prefix."member_deposit";
$wpdb->update($table_name, $subs, $where);
if($success) {
header('Location: http://empireinvest.co.za/RegSuccess');
exit;
} else {
echo 'not';
}
}
}
elseif($realP==6 && $numberDays >= 180){
$output .= "<tr style='background-color: red; color: #fff'>
<td> $user_data->ID</td>
<td> R$user_data->amount</td>
<td> $user_data->deposit_date</td>
<td> $realP</td>
<td> $numberDays</td>
<td> R$amount_growth</td>
<td> R$roi</td>
<td> <form method='post'> <input type='submit' name='submit' value='Pay' /> </form> </td>
</tr>";
//Add Withdrawal
if($_POST['submit']) {
$todate = date('Y-m-d H:i:s');
$success = $wpdb->insert("wp_member_history", array(
"ID" => $user_data->ID,
"amount" => $user_data->amount,
"wit_date" => $todate
));
//Update Member Status
$status = "Approved";
$where = array('ID' => $user_data->ID);
$subs = array('approve' => $status);
$table_name = $wpdb->prefix."member_deposit";
$wpdb->update($table_name, $subs, $where);
if($success) {
header('Location: http://empireinvest.co.za/RegSuccess');
exit;
} else {
echo 'not';
}
}
}
elseif($realP==7 && $numberDays >= 210){
$output .= "<tr style='background-color: red; color: #fff'>
<td> $user_data->ID</td>
<td> R$user_data->amount</td>
<td> $user_data->deposit_date</td>
<td> $realP</td>
<td> $numberDays</td>
<td> R$amount_growth</td>
<td> R$roi</td>
<td> <form method='post'> <input type='submit' name='submit' value='Pay' /> </form> </td>
</tr>";
//Add Withdrawal
if($_POST['submit']) {
$todate = date('Y-m-d H:i:s');
$success = $wpdb->insert("wp_member_history", array(
"ID" => $user_data->ID,
"amount" => $user_data->amount,
"wit_date" => $todate
));
//Update Member Status
$status = "Approved";
$where = array('ID' => $user_data->ID);
$subs = array('approve' => $status);
$table_name = $wpdb->prefix."member_deposit";
$wpdb->update($table_name, $subs, $where);
if($success) {
echo "<script> alert('Member order has been approved'); </script>";
header("Refresh:0");
exit;
} else {
echo 'not';
}
}
}
else {
$output .= "<tr>
<td> $user_data->ID</td>
<td> R$user_data->amount</td>
<td> $user_data->deposit_date</td>
<td> $realP</td>
<td> $numberDays</td>
<td> R$amount_growth</td>
<td> R$roi</td>
</tr>";
}
}
$output .= '</table>';
return $output;
}
add_shortcode('views', 'lue_custom_view');
//example usae with php
echo do_shortcode( $content, '[lue_custom_view]'
);