I am new to php, i want to create my own e-commerce website. i stuck at the very final stage , to create cash on delivery database.
i want to insert all selected product title or id with quantity in one column
my database table
order_id
order_product ----here i want all selected product_id or title with quantity
Please help
<?php require_once("config.php"); ?>
<?php
if(isset($_GET['add'])) {
$query = query("SELECT * FROM products WHERE product_id=".escape_string($_GET['add'])." ");
confirm($query);
while($row = fetch_array($query)) {
if($row['product_quantity'] != $_SESSION['product_' . $_GET['add']]) {
$_SESSION['product_' . $_GET['add']] +=1;
redirect("../public/checkout.php");
}else{
set_message("We only have" . $row['product_quantity'] . " " . " available");
redirect("../public/checkout.php");
}
}
}
if(isset($_GET['remove'])) {
$_SESSION['product_' . $_GET['remove']]--;
if($_SESSION['product_' . $_GET['remove']] < 1) {
unset($_SESSION['item_total']);
redirect("../public/checkout.php");
}else{
redirect("../public/checkout.php");
}
}
if(isset($_GET['delete'])) {
$_SESSION['product_' . $_GET['delete']] = '0';
unset($_SESSION['item_total']);
redirect("../public/checkout.php");
}
function cart() {
$total = 0;
$item_quantity = 0;
$item_name = 1;
$item_number = 1;
$amount = 1;
$quantity = 1;
foreach ($_SESSION as $name => $value) {
if($value > 0 ) {
if(substr($name, 0, 8) == "product_"){
$length = strlen($name - 8);
$id = substr($name, 8 , $length);
$query = query("SELECT * FROM products WHERE product_id = " . escape_string($id)." ");
confirm($query);
while($row = fetch_array($query)) {
$sub = $row['product_price']*$value;
$item_quantity +=$value;
$product = <<<DELIMETER
<tr>
<td data-th="Product">
<div class="row">
<div class="col-sm-2 hidden-xs"><img src="http://placehold.it/100x100" alt="..." class="img-responsive"/></div>
<div class="col-sm-10">
<h4 class="nomargin">{$row['product_title']}</h4>
</div>
</div>
</td>
<td data-th="Price">₹{$row['product_price']}</td>
<td data-th="Quantity">
<button class="btn btn-info btn-sm"><i class="fa fa-minus"></i></button>
{$value} <button class="btn btn-info btn-sm"><i class="fa fa-plus"></i></button>
</td>
<td data-th="Subtotal" class="text-center">₹{$sub}</td>
<td class="actions" data-th="">
<button class="btn btn-info btn-sm"><i class="fa fa-trash-o"></i></button>
</td>
</tr>
<input type="hidden" name="item_name_{$item_name}" value="{$row['product_title']}">
<input type="hidden" name="item_number_{$item_number}" value="{$row['product_id']}">
<input type="hidden" name="amount_{$amount}" value="{$row['product_price']}">
<input type="hidden" name="quantity_{$quantity}" value="{$value}">
DELIMETER;
echo $product;
$item_name++;
$item_number++;
$amount++;
$quantity++;
}
$_SESSION['item_total'] = $total += $sub;
$_SESSION['item_quantity'] = $item_quantity;
}
}
}
}
function show_checkout(){
if(isset($_SESSION['item_quantity'])){
$query = query(" SELECT * FROM products");
confirm($query);
while ($row = fetch_array($query)) {
$checkout_button = <<<DELIMETER
<td><a name="checkout" href="thank_you.php?pi={$row['product_title']}" type="submit" class="btn btn-success btn-block">Checkout<i class="fa fa-angle-right"></i></a></td>
DELIMETER;
return $checkout_button;
}
}
}
?>
Related
How do I make my added products to total price?
Meaning I did some code to sub some products prices but it doesn't work?!
It just print on last product the total which I want to be sub all products
Here's the code:
function.php
function display_payers(){
$total = 0;
$category_query = query("SELECT * FROM payers WHERE payers_id " . escape_string($id) . " ");
confirm($category_query);
while ($row = fetch_array($category_query)) {
$sub = $row['price'];
$payers_id = $row['payers_id'];
$name = $row['name'];
$serial_number = $row['serial_number'];
$mobile_number = $row['mobile_number'];
$email = $row['email'];
$pass = $row['pass'];
$price = $row['price'];
$payer_photo = $row['payer_photo']; //**** */
$user_show = $row['user_show']; //**** */
$payer = <<<DELIMETER
<tr>
<td>{$payers_id}</td>
<td>{$name}</td>
<td>{$serial_number}</td>
<td>{$mobile_number},<br>{$email},<br>{$pass}</td>
<td>{$user_show}<br>
<a>
<img style="width: 80px; height="80px" src="../../resources/uploads/$payer_photo" alt="">
</a>
</td>
<td>{$price}SR<br>
</td>
<td><a class="btn btn-default btn-number" href="index.php?edit_payer&id={$row['payers_id']}"><span class="glyphicon glyphicon-edit"></span></a></td>
<td><a class="btn btn-default" href="index.php?delete_payers_id={$row['payers_id']}"><span class="glyphicon glyphicon-remove"></span></a></td>
</tr>
DELIMETER;
echo $payer;
}
$_SESSION['item_totals'] = $total += $sub;
}
Use this to add subtotal
$_SESSION['item_totals'] += $sub;
To subtract
$_SESSION['item_totals'] -= $sub_value;
I would like to see if the value is over the current time, multiply by st-ads-time 3600 number as in the picture, how can I do this?
My Code:
<?php
$advertisement_query = mysql_query("SELECT * FROM andesite_advertisement ORDER BY advertisement_id DESC");
$num_log = mysql_num_rows($advertisement_query);
if ($num_log > 0) {
while ($ads_row = mysql_fetch_array($advertisement_query)) {
$ads_ase = $ads_row['advertisement_base'];
$is_buyed_query = mysql_query("SELECT * FROM andesite_adsrice WHERE adsrice_adsase = '$ads_ase' AND adsrice_username = '$user_nickname'");
while ($is_row = mysql_fetch_array($is_buyed_query)) {
$timeA = $is_row['adsrice_time'];
$timeB = date("d.m.Y H:i");
$timeDiff = strtotime($timeB) - strtotime($timeA);
if ($timeDiff > $ads_time) {
echo '
<tr id="'.$ads_row['advertisement_base'].'">
<td>'.$ads_row['advertisement_id'].'</td>
<td>'.$ads_row['advertisement_title'].'</td>
<td><a class="btn btn-success btn-success flat btn-xs" href="Surf/'.$ads_row['advertisement_base'].'" target="_blank"><i class="fa fa-eye"></i> '.ls_view.'</a></td>
</tr>
';
}
}
}
} else {
echo '
<tr>
<td>'.ls_nothata.'</td>
<td>'.ls_nothata.'</td>
<td>'.ls_nothata.'</td>
</tr>
';
}
?>
The query shows only those ads which have a matching advertisement_base and only when the current time is after their adsrise_time but before adsrise_time plus st-ads-time hours.
<?php
$query = "SELECT * FROM andesite_advertisement ORDER BY advertisement_id DESC";
$advertisement_query = mysql_query($query);
$num_log = mysql_num_rows($advertisement_query);
if ($num_log > 0)
{
while ($ads_row = mysql_fetch_array($advertisement_query))
{
$ads_ase = $ads_row['advertisement_base'];
$query = "SELECT * FROM andesite_adsrice
WHERE adsrice_adsase = '$ads_ase'
AND adsrice_username = '$user_nickname'
AND NOW() BETWEEN adsrise_time AND ADDTIME(adsrise_time, INTERVAL
(SELECT setting_value
FROM settings
WHERE setting_name = 'st-ads-time'
))";
$is_buyed_query = mysql_query($query);
while ($is_row = mysql_fetch_array($is_buyed_query))
{
echo '<tr id="'.$ads_row['advertisement_base'].'">
<td>'.$ads_row['advertisement_id'].'</td>
<td>'.$ads_row['advertisement_title'].'</td>
<td>
<a class="btn btn-success btn-success flat btn-xs" href="Surf/' . $ads_row['advertisement_base'] . '" target="_blank">
<i class="fa fa-eye"></i> '.ls_view.'
</a>
</td>
</tr>';
}
}
}
}
else
{
echo '
<tr>
<td>'.ls_nothata.'</td>
<td>'.ls_nothata.'</td>
<td>'.ls_nothata.'</td>
</tr>
';
}
?>
I am trying to get the course attempt details where user attempted 2 times, so whenever I try to show 2 results its showing 2 results are same.
For example in first attempt if i failed and second attempt if i passed it showing 2 records data are passed only I don't know why it is happening like that.
Here is my code for that page:
<?php
global $DB, $USER, $CFG;
include("include/header.php");
date_default_timezone_set('Asia/Kolkata');
?>
<body>
<div style="
margin: 50px 30px 5px 0px;
float: right;
">
<?php
if ($_SESSION['idnumber'] == 3) {
?>
<button type="submit" name="linkcourses" value="linkcourses" class="btn btn-primary">Link Exam</button>
<button type="submit" name="accesscode" value="accesscode" class="btn btn-primary">Access Code</button>
<button type="submit" name="accesscode" value="accesscode" class="btn btn-primary">Create Single User</button>
<button data-toggle="modal" data-target="#contact_dialog" type="submit" name="accesscode" value="accesscode" class="btn btn-primary" style="cursor: pointer;">Max No. of Users</button>
<button data-toggle="modal" data-target="#bulk_upload" type="submit" name="accesscode" value="accesscode" class="btn btn-primary" style="cursor: pointer;">Bulk User Upload</button>
<button type="submit" name="accesscode" value="accesscode" class="btn btn-primary">Report</button>
<button type="submit" name="accesscode" value="accesscode" class="btn btn-primary">Groups</button>
<?php
}
if ($_SESSION['idnumber'] == 2) {
?>
<button type="submit" name="accesscode" value="accesscode" class="btn btn-primary">Report</button>
<?php
}
?>
</div>
<div class="container-fluid"style="background: #dddadd">
<div style="
margin: 50px 15px 5px 0px;
float: right;
">
<?php
if ($_SESSION['idnumber'] == 3) {
?>
<?php
}
?>
</div>
<div class="row" style=" margin-top: 20px;">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-body" id="report_grid">
<div class="table-responsive" style="font-family: myFirstFont;">
<table id="datatable2" class="table">
<thead>
<tr>
<th style="color: #005EB8">Course</th>
<th style="color: #005EB8">Username</th>
<th style="color: #005EB8">First Name</th>
<th style="color: #005EB8">Last Name</th>
<th style="color: #005EB8">Total Time</th>
<th style="color: #005EB8">Start Date</th>
<th style="color: #005EB8">Completion Date</th>
<th style="color: #005EB8">Attempt</th>
<th style="color: #005EB8">Status</th>
<th style="color: #005EB8">Exam score</th>
</tr>
</thead>
<tbody>
<?php
require_once($CFG->dirroot . '/mod/scorm/lib.php');
require_once($CFG->dirroot . '/mod/scorm/locallib.php');
require_once($CFG->dirroot . '/course/lib.php');
global $DB;
//course table fetch
$sql1 = "SELECT cm.id as cmid,c.id,c.fullname,c.startdate,c.startdate,instance FROM {course} c LEFT JOIN {course_modules} cm ON c.id=cm.course where c.category != 0 and requested='0' AND cm.id!=''";
$courses = $DB->get_records_sql($sql1);
if (sizeof($courses)) {
foreach ($courses as $row) {
$scorm = $DB->get_record("scorm", array("id" => $row->instance));
$scoid = 0;
$orgidentifier = '';
if ($sco = scorm_get_sco($scorm->launch, SCO_ONLY)) {
if (($sco->organization == '') && ($sco->launch == '')) {
$orgidentifier = $sco->identifier;
} else {
$orgidentifier = $sco->organization;
}
$scoid = $sco->id;
}
$cmid = $row->cmid;
$coursename = $row->fullname;
$useridQry = '';
$userIds = '';
$login_id = $_SESSION['USER']->id;
if ($_SESSION['idnumber'] == 3) {
} elseif ($_SESSION['idnumber'] == 2) {
$records = $DB->get_records_sql("select * from {user} where maildigest=$login_id");
if (count($records)) {
foreach ($records as $row) {
$userIds[] = "'" . $row->id . "'";
}
if ($userIds != '') {
$userIds = implode(',', $userIds);
$useridQry = " and st.userid IN ($userIds)";
}
}
} else {
$useridQry = "and st.userid = $login_id";
}
$scom_id = $row->cmid;
$cm = get_coursemodule_from_id('scorm', $scom_id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$scorm = $DB->get_record('scorm', array('id' => $cm->instance), '*', MUST_EXIST);
$query = "SELECT DISTINCT CONCAT(u.id, '#', COALESCE(st.attempt,0)) AS uniqueid, st.scormid AS scormid,st.value, st.attempt AS attempt, u.id AS userid,u.picture,u.firstname,u.lastname,u.username,u.firstnamephonetic,u.lastnamephonetic,u.middlename,u.alternatename,u.imagealt,u.email,u.idnumber FROM mdl_user u LEFT JOIN mdl_scorm_scoes_track st ON st.userid = u.id AND st.scormid = $scorm->id WHERE (st.userid IS NOT NULL) and idnumber != 0 $useridQry";
$attempts = 0;
$attempts = $DB->get_records_sql($query);
//print_r($attempts);
$row = array();
if (isset($attempts)) {
foreach ($attempts as $scouser) {
//print_r($scouser);
$type = '';
$status = '';
$totaltime = '';
$score = '';
if (!empty($scouser->attempt)) {
$timetracks = scorm_get_sco_runtime($scorm->id, false, $scouser->userid, $scouser->attempt);
} else {
$timetracks = '';
}
//echo $timetracks;
global $DB;
$tracks = $DB->get_records_sql("SELECT id, element, value FROM {scorm_scoes_track} WHERE scormid = ? AND userid = ? AND element IN('cmi.core.lesson_status','cmi.core.total_time','cmi.core.score.raw')", array($scorm->id, $scouser->userid));
foreach ($tracks as $trac) {
if ($trac->element == 'cmi.core.lesson_status') {
$status = $trac->value;
}
if ($trac->element == 'cmi.core.total_time') {
$totaltime = $trac->value;
}
if ($trac->element == 'cmi.core.score.raw') {
$score = $trac->value;
}
$scouser->times = $timetracks;
$scouser->status = $status;
$scouser->totaltime = $totaltime;
$scouser->score = $score;
$row[] = $scouser;
$start_date = $finish_date = '';
if (isset($scouser->times)) {
if (isset($scouser->times->start))
$start_date = date('Y-M-d H-i-s', $scouser->times->start);
if (isset($scouser->times->finish))
$finish_date = date('Y-M-d H-i-s', $scouser->times->finish);
}
} //foreach score track ends
?>
<tr class="gradeX">
<td ><?php echo $coursename; ?></td>
<td ><?php echo $scouser->username; ?></td>
<td ><?php echo $scouser->firstname; ?></td>
<td ><?php echo $scouser->lastname; ?></td>
<td ><?php echo $totaltime; ?></td>
<td ><?php echo $start_date; ?></td>
<td ><?php echo $finish_date; ?></td>
<td ><?php echo $scouser->attempt; ?></td>
<td>
<?php
//print_r($tracks);
//echo $status;
if (trim($status) == "completed") {
echo " Completed";
} elseif (trim($status) == "incomplete") {
echo "Incomplete";
} elseif (trim($status) == "failed") {
echo "Failed";
} elseif (trim($status) == "passed") {
echo "Passed";
} else {
echo "Not Started";
}
?>
</td>
<td>
<?php
if ($score == "") {
} else {
echo $score;
echo '%';
}
//echo $score;
?>
</td>
</tr>
<?php
} //foreach 104 ends
}//attempts if ends
} //course foreach ends
}//course if ends
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</body>
<?php
include("include/footer.php");
?>
</html>
I wrote this code:
<div class="container mx-auto">
<!--Add class table-responsive for responsive table -->
<div class="row">
<div class="col-md-6">
<form method="post">
<div class="input-group">
<input size='14' type="text" class="form-control" placeholder="Search for..." name="searchValue">
<span class="input-group-btn">
<button class="btn btn-secondary" name='search' type="submit"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
</div>
</div>
<table class="table mx-auto" id="'table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Phone</th>
<th>Company</th>
<th>More</th>
</tr>
</thead>
<tbody>
<?php
$pagination = new Pagination(7);
$page_max = $pagination->getPageMax();
$start = $pagination->getStart();
if(!isset($_POST['search'])) {
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers"));
$customers = $database->getDataAsArray("SELECT * FROM customers LIMIT $start, $page_max");
}
else{
$searchValue = '%'. $_POST['searchValue'] . '%';
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers WHERE name LIKE '$searchValue' "));
$customers = $database->getDataAsArray("SELECT * FROM customers WHERE name LIKE '$searchValue' LIMIT $start, $page_max");
}
foreach($customers as $customer){
$name = $customer ['name'];
$surname = $customer['surname'];
$email = $customer['email'];
$phone = $customer['phone'];
$company = $customer['company'];
$id = $customer['id'];
echo "<tr>
<td>$name</td>
<td>$surname</td>
<td>$email</td>
<td>$phone</td>
<td>$company</td>
<td><a href='customerInfo.php?id=$id' class='btn btn-sm btn-info'><i class='fa fa-info'></i></a></td>
</tr>";
}
?>
</tbody>
</table>
<ul class="pagination">
<?php
echo $pagination->previous($numberOfPages);
for($i = 0; $i < $numberOfPages; $i++){
echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
echo $pagination->next($numberOfPages);
?>
</ul>
</div>
The getDataAsArray function is like this:
public function getDataAsArray($myQuery){
$this->connection = mysqli_connect($this->host, $this->dbUsername, $this->dbPassword, 'portal');
$query = mysqli_query($this->connection, $myQuery);
$results = array();
while($line = mysqli_fetch_array($query)){
$results[] = $line;
}
return $results;
}
I know I should use :[name] or something to set a parameter in my query,the question is not about that and ofcourse I should use a prepared statment. Will do that later.
The question:
I wrote the code to search in the db records. I works fine but when I search it creates a new pagination with all the new records and when I click on the second page. The page builds the pagination with all the records from the database so I won't use it's searchValue anymore.
I think this issue will be solved with a $_GET parameter like search in the URL or Ajax but I don't know how.
Could anyone help me out?
In my database i have a table called publi inside this table i have two columns pub_year and pub_publi
Example of the table publi content :
<table border="1">
<tr>
<td>2016</td>
<td>content_2016_1</td>
</tr>
<tr>
<td>2016</td>
<td>content_2016_2</td>
</tr>
<tr>
<td>2016</td>
<td>content_2016_3</td>
</tr>
<tr>
<td>2015</td>
<td>content_2015_1</td>
</tr>
<tr>
<td>2015</td>
<td>content_2015_2</td>
</tr>
</table>
and i want this output
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2016">2016</button>
<div id="2016" class="collapse">
content_2016_1 <br>
content_2016_2 <br>
content_2016_3 <br>
</div>
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2015">2015</button>
<div id="2015" class="collapse">
content_2015_1 <br>
content_2015_2 <br>
</div>
and this is my code :
<?php
$query = "SELECT * FROM publi where pub_type=0 order by pub_year DESC, pub_publi ";
$result = mysqli_query($connection, $query);
$previous =0;
while ($val = mysqli_fetch_array($result))
{
if ($previous <> $val['pub_year'])
{
$previous = $val['pub_year'];
$year = $previous;
echo '<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#';
echo $year;
echo '">';
echo $year;
echo '</button>';
echo '<div id="';
echo $year;
echo '" class="collapse">';
$Temp = highlight("person1",$val['pub_publi'],"0000FF");
$Temp = highlight("person2",$Temp,"0000FF");
$Temp = highlight("person3",$Temp,"0000FF");
$Temp = highlight("person4",$Temp,"0000FF");
$Temp = highlight("person5",$Temp,"0000FF");
$Temp = highlight("person6",$Temp,"0000FF");
$Temp = highlight("person7",$Temp,"0000FF");
$Temp = highlight("person8",$Temp,"0000FF");
$Temp = highlight("person9",$Temp,"0000FF");
$Temp = highlight("person10",$Temp,"0000FF");
echo '<a target=blank href="http://www.test.com/query.f?term=' . $val['pub_pubmed'] . '";)><img border="0" src="img/test.gif" align=MIDDLE alt="Search in for ' . $val['pub_publi'] . '"></a>';
echo $Temp;
echo '</div>';
}
}
?>
but the result that i get is:
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2016">2016</button>
<div id="2016" class="collapse">
content_2016_1 <br>
</div>
content_2016_2
content_2016_3
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2015">2015</button>
<div id="2015" class="collapse">
content_2015_1 <br>
</div>
content_2015_2 <br>
The way you are grouping the data by pub_year is correct but from your example, you are only outputting information when group is changing.
I would propose something similar:
<?php
$previous = false;
while ($val = mysqli_fetch_array($result)) {
// when group is changing, end previous and start new
if ($previous <> $val['pub_year']) {
// end previous group html markup
if ($previous !== false) {
echo '</div>';
}
$previous = $val['pub_year'];
$year = $previous;
echo '<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#' . $year . '>' . $year . '</button>';
echo '<div id="' . $year . '" class="collapse">';
}
// always output data inside group
$Temp = highlight("person1",$val['pub_publi'],"0000FF");
$Temp = highlight("person2",$Temp,"0000FF");
$Temp = highlight("person3",$Temp,"0000FF");
$Temp = highlight("person4",$Temp,"0000FF");
$Temp = highlight("person5",$Temp,"0000FF");
$Temp = highlight("person6",$Temp,"0000FF");
$Temp = highlight("person7",$Temp,"0000FF");
$Temp = highlight("person8",$Temp,"0000FF");
$Temp = highlight("person9",$Temp,"0000FF");
$Temp = highlight("person10",$Temp,"0000FF");
echo $Temp . '<br>';
}
// end last group html markup
if ($previous !== false) {
echo '</div>';
}
?>