Okay, I'm building (trying) a coupon code system for my admin panel, what I want to achieve is allow admin to manually create coupons by setting its coupon_code and coupon_discount (1% TO 100%). When he will submit, it will be stored in two different tables of two dbs.
TABLE coupons:
I checked lots of examples on the net and this is my probably poorly written class:
class ProductDiscount {
static public function validate($coupon_code);
private $_coupon_code; // string
private $_coupon_discount; // integer??
private $_expire; // null cause unlimited
private $_count; // null
private function __construct(); //for this line I got an error
public function getCouponCode(); // return string
public function getCouponDiscount(); // return
public function getCount(); // returns null unlimited
public function getExpireDate();// null
public function useDiscount(); // apply this discount now
public function useAllDiscount(); // invalidate this discount for future use
COUPONS.PHP - new coupon creation
On the admin side, I am totally lost about how to pass the coupon_code and coupon_discount to the database...How to utilize the functions that I wrote in the class...This is what I did:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$coupon_code = $_POST['coupon_code'];
$coupon_discount = $_POST['coupon_discount'];
//insert into db for admin
$connect = new mysqli("localhost","-----","-------","--------");
$stmt = $connect->prepare("INSERT INTO `coupons` (`coupon_code`, `coupon_discount`) VALUES (?,?)");
$stmt->bind_param('si', $coupon_code, $coupon_discount);
$stmt->execute();
$connect->close();
}
?>
I'm getting undefined index errors for both the coupon_code and coupon_discount..
If I require_once my class file here, I'm getting Non-abstract method ProductDiscount::create() must contain body error.
<form class="form-horizontal" role="form" action="createcoupon.php">
<div class="form-group">
<label class="col-sm-2 control-label">Coupon Code</label>
<div class="col-sm-2">
<input type="text" class="form-control" name="coupon_code" id="couponCode" placeholder="e.g SAVE10">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Coupon Discount</label>
<div class="col-sm-2">
<input type="number" class="form-control" name="coupon_discount" id="couponDiscount" placeholder="e.g 10 for 10%">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-3">
<input type="submit" style="background-color:#7575DD; padding:0 !important; color:white;" name="create_coupon" value="Create Coupon" class="btn btn-default"/>
</div>
</div>
</form>
Here is my createcoupon.php:
<?php
//Connect to the 1st db
$coupon_code = $_POST['coupon-code'];
$coupon_discount = $_POST['coupon-discount'];
//the prepared stmt to insert into db1
//Connect to the 2nd tb
$coupon_code = $_POST['coupon-code'];
$coupon_discount = $_POST['coupon-discount'];
//prepared stmt to insert into db2
header("location: http://example.com/admin/coupons");
?>
It's 4am and been hours that I'm trying to learn these.
So my question is: "How can I save the necessary form data to the table in the correct way and save the coupon into the table?"
(edited and specified my current problem only, can you reopen?)
(commented the long/unnecessary parts to clarify)
Thank you so much for your time!
Okay so far, I managed to solve it. Using bootstrap table.
With an "add coupon" button:
<button data-toggle='modal' data-target='#myModal' type='button' class='btn btn-primary' style="margin: 20px;" name='add-btn' id='add-btn'>Add Coupon</button>
data-toggle='modal'
and data-target='#myModal'
to link the modal div to the button
SELECT statement to get the coupon table and pull the data into a bootstrap table.
<div style="padding: 20px;">
<?php
$con = new mysqli("------","--------","--------","-----");
$query = "SELECT * FROM coupons WHERE expire > NOW() OR expire IS NULL OR expire = '0000-00-00 00:00:00'";
if ($result = $con->query($query))
{
echo "<table border='1' class='table table-hover' data-toggle='table'>
<tr>
<th>Coupon Code</th>
<th>Amount</th>
<th>Expiry</th>
<th></th>
</tr>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['coupon_code'] . "</td>";
echo "<td>" . $row['coupon_discount'] . "</td>";
echo "<td>" . $row['expire'] . "</td>";
echo "<td><button data-toggle='modal' data-target='#myModal' type='button' class='btn btn-primary' name='submit-btn' data-id='" . $row["coupon_id"] ."'>Edit</button></td>";
echo "</tr>";
}
echo "</table>";
}
mysqli_close($con);
?>
These are the modal divs and form to post the inputs and save it to the table.
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Coupon Information</h4>
</div>
<div class="modal-footer">
<form action='createcoupon.php' method='POST'>
<input type='hidden' name='coupon_id' id='couponId' value='0'/>
<input type="text" class="form-control" style="margin-bottom:10px;" name="coupon_code" id="couponCode" placeholder="e.g SAVE10">
<input type="number" class="form-control" style="margin-bottom:10px;" name="coupon_discount" id="couponDiscount" placeholder="e.g 10 for 10%">
<input type="text" class="form-control" style="margin-bottom:10px;" name="coupon_expire" id="couponExpire" placeholder="e.g 01/01/2015">
<input class="btn btn-primary" type='submit' name='submit-btn' value='Save' />
</form>
</div>
</div>
</div>
</div>
</div>
This is the javascript
<script>
// Function added to each edit button. Sets the ID to the button "data-id" value.
// Finds the parent (td) of the button, then goes to each sibling with the correct class name
// and changes the modal input value to the inner text of the TD
var editfunction = function() {
$('#couponId').val($(this).attr('data-id'));
var parent = $(this).parent();
$('#couponCode').val($(parent).siblings('.coupon_code').text());
$('#couponDiscount').val($(parent).siblings('.coupon_discount').text());
$('#couponExpire').val($(parent).siblings('.coupon_expire').text());
};
// Called on the ADD button only
// Sets the ID to 0 (to INSERT the record on the form submit)
// Sets the other inputs to blank values
function addNewCoupon()
{
$('#couponId').val('0');
$('#couponCode').val('');
$('#couponDiscount').val('');
$('#couponExpire').val('');
}
// When the page has loaded...
// Attach the edit function call to the click event of each edit button
// Attach the add new coupon to the click event of the add button
$(document).ready(function() {
$('button[name="submit-btn"]').click(editfunction);
$('#add-btn').click(function() { addNewCoupon(); });
});
</script>
Then on the createcoupon.php I did this:
// Check connection
$coupon_id = intval($_POST['coupon_id']);
$coupon_code = $_POST['coupon_code'];
$coupon_discount = $_POST['coupon_discount'];
and after with mysqli connection I inserted/updated (in an if else statement, "if is 'add coupon' or 'edit coupon') them.
I solved this in the end, If you have any ideas about it, please share with me so that I can improve.
I hope these codes can be useful for someone!
Thanks for your time.