I have a view containing a html table that display data retrieve from the database. Is it possible to go about doing a pagination for it?
Code for the view
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div class="row" ng-app="appcolor">
<div class="col-lg-12">
<!-- Section for tabs-->
<div class="tabbable">
<ul class="nav nav-tabs">
<li class="active">Home Banner</li>
</ul>
</div>
<!-- Section for tab content-->
<div class="tab-content">
<div class="tab-pane active" id="home_banner">
<fieldset>
<!-- Section for managing home banner -->
<legend>Manage Home Banner</legend>
<?php if($this->session->flashdata('notification')) {echo '<div class="alert alert-success">'.$this->session->flashdata('notification').'</div>'; } ?>
<?php if($banners != NULL) { ?>
<form action="<?php echo base_url().'backendBanner/update_banners'; ?>" method="POST">
<table style="" class="table table-striped">
<tr>
<th>Banner Image</th>
<th>Title</th>
<th>Description</th>
<th>Is Active</th>
<th>Seq Num</th>
<th>Viewed By</th>
<th>Link</th>
<th>Mobile Link</th>
<th>Delete</th>
</tr>
<?php } ?>
<?php foreach($banners as $banner) { ?>
<tr>
<input type="hidden" name="banner_path[]" value="<?php echo $banner['banner_path']; ?>">
<!-- Display the banner image -->
<td style="width:170px;"><?php echo "<img style='width:150px; height:100px'class='gc_thumbnail' src='".base_url()."../home_banner_images/".$banner['banner_path']."' style='padding:5px; border:1px solid #ddd'/>"; ?></td>
<!-- Display the banner title -->
<td style="width:170px;">
<input name="banner_title[]" type="text" value="<?php echo $banner['banner_title']; ?>" style="width:180px;">
</td>
<!-- Display the banner desc -->
<td style="width:170px;">
<input name="banner_desc[]" type="text" value="<?php echo $banner['banner_desc']; ?>" style="width:180px;">
</td>
<!-- Drop down list to select whether the banner is active -->
<td style="width:60px;">
<select name="is_active[]" style="width:60px;">
<?php
echo "<option value='0'"; if($banner['is_active'] == 0) {echo "selected";}; echo ">".'0'."</option>";
echo "<option value='1'"; if($banner['is_active'] == 1) {echo "selected";}; echo ">".'1'."</option>";
echo "</select>";
?>
</td>
<!-- Drop down list to select the sequence number -->
<td style="width:75px;">
<?php echo '<select name="seq[]" style="width:60px;">';
for($count =1 ; $count <= count($banners); $count ++)
{
echo "<option value='$count'"; if($banner['seq'] == $count) {echo "selected";}; echo ">".$count."</option>";
}
?>
</select>
</td>
<!-- Drop down list to select the banner can be view by which group of users-->
<td style="width:130px;">
<select name="viewed_by[]" style="width:120px;">
<?php
echo "<option value='members'"; if($banner['viewed_by'] == 'members') { echo "selected"; } echo ">".'Members'."</option>";
echo "<option value='non-members'"; if($banner['viewed_by'] == 'non-members') { echo "selected"; } echo ">".'Non-Members'."</option>";
echo "<option value='both'"; if($banner['viewed_by'] == 'both') { echo "selected"; } echo ">".'Both'."</option>";
echo "</select>"; ?>
</td>
<!-- Text field for user to input the banner url link -->
<td style="width:230px;">
<input name="url[]" type="text" value="<?php echo $banner['url']; ?>" style="width:180px;">
</td>
<!-- Text field for user to input the banner mobile url link -->
<td style="width:230px;">
<input name="murl[]" type="text" value="<?php echo $banner['murl']; ?>" style="width:180px;">
</td>
</tr>
<?php } ?>
<?php if($banners != NULL) { ?>
</table>
</fieldset>
</div>
</body>
</html>
Code for controller
class backendBanner extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('backend_banner_model');
$this->load->helper('url');
//$this->load->model('product_model');
}
public function index()
{
$this->landing_banners_upload();
}
function escapeQuote($var)
{
if (isset($var)){
$string = str_replace("'","''",$var);
}
return $string;
}
public function landing_banners_upload()
{
/*if ($this->session->userdata('admin_logged_in') == 1)
{*/
$config['allowed_types'] = 'gif|jpg|png';
$config['upload_path'] ='../home_banner_images';//This directory is under base url,
$config['overwrite'] = true;
$directory = '../home_banner_images/';
if (!file_exists($directory))
{
mkdir($directory, 0777, true);
}
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
if($this->input->post('userfile'))
{
$data['error']= $this->upload->display_errors();
}
$data['banners'] = $this->backend_banner_model->get_landing_banners();
$this->load->view('layout/header');
$this->load->view('backendBanner/landing_banners_upload_view', $data);
$this->load->view('layout/footer');
}
else
{
$data['upload_data'] = $this->upload->data();
$file_name = $data['upload_data']['file_name'];
if(!$this->backend_banner_model->landing_banner_exist($file_name))
{
$this->backend_banner_model->insert_banner_image($this->upload->data());
}
redirect(base_url()."backendBanner/landing_banners_upload");
}
}}
Code for the model
class backend_banner_model extends CI_Model {
public function __construct ()
{
parent::__construct();
$this->db = $this->load->database('online', TRUE);
}
//Section for home banner (category)
/* Get the list of banner from the category of "home_banner */
public function get_landing_banners()
{
$this->db->select('*')->from('banner')->where('banner_category', 'home_banner');
$data = $this->db->get();
if ($data->num_rows() > 0) {
return $data->result_array();
}
}
}
Related
Hi im working on my workshop tools project.
Now i have some strange action sometimes when i pressed the add to cart button (mine was named pinjam) it just going back to the login page because the session destroyed. Anyone has this strange thing happened? or Can you help me?
The scenario is like this..
I pressed the add to cart button on one of much tools that can i borrow, then it post to the add_to_cart function on my controller and reload the #cart_content div on that page with new added data with jquery / ajax
Controller Siswa
function index(){
$data['guru'] = $this->model_akun->getnamaguru();
$data['data']=$this->model_alat->getAll();
$this->load->view('siswa/dashboard',$data);
}
function add_to_cart(){
if($this->model_alat->validate_add_cart_item() == TRUE){
// Check if user has javascript enabled
if($this->input->post('ajax') != '1'){
redirect('siswa'); // If javascript is not enabled, reload the page with new data
}else{
echo 'true'; // If javascript is enabled, return true, so the cart gets updated
}
}
}function show_cart(){
$this->load->view('siswa/cart');
}
Model_Alat
function validate_add_cart_item(){
$id = $this->input->post('id_alat'); // Assign posted id_alat to $id
$cty = $this->input->post('quantity'); // Assign posted quantity to $cty
//$this->db->where('id_alat', $id); // Select where id matches the posted id
$query = $this->db->query('select * from alat where id_alat="'.$id.'"')->result(); // Select the products where a match is found and limit the query by 1
//$ss = $this->input->post('ajax');
//echo $query['id_alat'];
//echo "<script>alert(".$cty.");
// </script>";
// Check if a row has been found
if($query > 0){
foreach ($query as $row){
$data = array(
'id' => $id,
'qty' => $cty,
'price' => '0',
'name' => $row->nama_alat
);
$this->cart->insert($data);
return TRUE;
}
// Nothing found! Return FALSE!
}else{
echo "<script>alert('FALSE no rows :(');</script>";
return FALSE;
}
}
JQUERY
<script>
$(document).ready(function() {
/*place jQuery actions here*/
var link = "/weteies/index.php/"; // Url to your application (including index.php/)
$("div.subalat form").submit(function() {
// Get the product ID and the quantity
var id = $(this).find('input[name=id_alat]').val();
var qty = $(this).find('input[name=quantity]').val();
$.post(link + "siswa/add_to_cart", { id_alat: id, quantity: qty, ajax: '1' },
function(data){
// Interact with returned data
if(data == 'true'){
$.get(link + "siswa/show_cart", function(cart){ // Get the contents of the url cart/show_cart
$("#cart_content").html(cart); // Replace the information in the div #cart_content with the retrieved data
});
}else{
alert("Product does not exist");
}
});
});
});
siswa/cart (for viewing the cart list)
##siswa/cart view
<div class="table-responsive">
<?php echo form_open('siswa/update_cart'); ?>
<table class="table table-bordered">
<thead >
<tr>
<th>Alat</th>
<th>Jumlah</th>
<!--<th>Total</th>-->
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php $a=0; $b=0;foreach($this->cart->contents() as $items): ?>
<tr>
<!--<input type="hidden" id="id_peminjam" name="id_peminjam" value="'.$this->session->userdata('ses_id').'"/>-->
<td><?php echo form_hidden('rowid['.$a.']', $items['rowid']);
echo $items['name']; ?></td>
<td><?php echo form_input(array('name' => 'qty['.$a.']', 'value' => $items['qty'], 'maxlength' => '2'), '', 'class="form-control form-control-sm"'); ?></td>
<!--<td><button type="button" id="'.$items['rowid'].'" class="romove_cart btn btn-danger btn-sm">Cancel</button></td>-->
<td><?php echo form_submit('', 'Update', 'class="btn btn-accept btn-sm"');
$b=$a+1;?>
</tr>
<?php $a++; endforeach; ?>
</tbody>
</table>
<?php echo form_hidden('jumlah_alat', $b);
echo form_close();?>
<?php echo form_open('siswa/sub_cart'); ?>
<table class="table">
<tr>
<td >
<select name="id_guru" class="form-control form-control-sm">
<option>Pilih Guru</option>
<?php foreach($guru as $gurus): echo '<option value='.$gurus->id_akun.'>'. $gurus->nama .'</option>'; endforeach;?>
</select>
</td>
<td>
<?php $hitung=0; $b=0; foreach($this->cart->contents() as $items): ?>
<?php echo form_hidden('id_alat'.$hitung.'', $items['id']);
echo form_hidden('jumlah_pinjam'.$hitung.'', $items['qty']);
echo form_hidden('id_siswa', $this->session->userdata('ses_id'));
$b=$hitung+1;
$hitung++;
endforeach;
echo form_hidden('jumlah_alat', $b);
echo anchor('siswa/empty_cart', 'Hapus Semua', 'class="btn btn-danger btn-sm col-sm-5"'); ?>
<?php echo form_submit('', 'Pinjam!', 'class="btn btn-primary btn-sm col-sm-5"');?>
</td>
</tr>
</table>
<?php echo form_close(); ?>
</div>
FORM siswa/dashboard
<!DOCTYPE html>
<head>
<?php $this->load->view("admin/_partials/head.php") ?>
</head>
<body id="page-top">
<!-- Navbar-->
<?php $this->load->view("siswa/_partials/navbar.php") ?>
<div id="wrapper">
<!-- Sidebar-->
<?php $this->load->view("siswa/_partials/sidebar.php") ?>
<div id="content-wrapper">
<div class="container-fluid">
<div class="row ">
<div class="col-md-6">
<div class="card mb-3">
<h4 class="card-header">Alat Tersedia</h4>
<br>
<div class="row card-body">
<?php foreach ($data as $row) : ?>
<div class="col-md-6">
<div class="card mb-3">
<div class="card-header text-center"><?php echo $row->nama_alat;?>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-7">
Tersedia
</div>
<div class="col-md-5">
<input value="<?php echo $row->jumlah;?>" class="form-control form-control-sm" readonly>
</div>
</div>
<div class="subalat">
<?php echo form_open(''); ?>
<div class="row">
<div class="col-md-7">
Mau Brp?
</div>
<div class="col-md-5">
<?php echo form_hidden('id_alat', $row->id_alat);
echo form_input('quantity', '1', 'maxlength="2" class="form-control form-control-sm"'); ?>
</div>
</div>
<br>
<?php echo form_submit('add', 'Pinjam','class="btn btn-success btn-block col-md"'); ?>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div>
<?php endforeach;?>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card mb-3">
<h4 class="card-header">List Pinjam</h4>
<!--<div class="card-body">-->
<div id="cart_content">
<?php $this->load->view('siswa/cart'); ?>
</div>
<!--</div>-->
<div class="card-footer small text-muted">
Jika jumlah alat nya 0(nol), alat akan dihapus. Page rendered in <strong>{elapsed_time}</strong> seconds.
</div>
</div>
</div>
</div><!-- /.row -->
</div><!-- /.content-fluid -->
<!-- Sticky Footer -->
<?php $this->load->view("siswa/_partials/footer.php") ?>
</div>
<!-- /.content-wrapper -->
</div>
<!-- /#wrapper -->
<?php $this->load->view("siswa/_partials/scrolltop.php") ?>
<!-- Modal-->
<?php $this->load->view("siswa/_partials/modal.php") ?>
<!-- Js-->
<?php $this->load->view("siswa/_partials/js.php") ?>
</body>
<script>
$(document).ready(function() {
/*place jQuery actions here*/
var link = "/weteies/index.php/"; // Url to your application (including index.php/)
$("div.subalat form").submit(function() {
// Get the product ID and the quantity
var id = $(this).find('input[name=id_alat]').val();
var qty = $(this).find('input[name=quantity]').val();
$.post(link + "siswa/add_to_cart", { id_alat: id, quantity: qty, ajax: '1' },
function(data){
// Interact with returned data
if(data == 'true'){
$.get(link + "siswa/show_cart", function(cart){ // Get the contents of the url cart/show_cart
$("#cart_content").html(cart); // Replace the information in the div #cart_content with the retrieved data
});
}else{
alert("Product does not exist");
}
});
});
});
</script>
</html>
Login controller
class Login extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model("model_login");
}
function index(){
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
if($this->form_validation->run()==FALSE){
$session = $this->session->userdata('isLoggedin');
if($session == FALSE){
$this->load->view('login');
}
else{
redirect('');
}
}
else{
$email=$this->input->post('email');
$password=$this->input->post('password');
$cek_akun=$this->model_login->auth_akun($email,$password);
if($cek_akun->num_rows() > 0){
$user_data=$cek_akun->row_array();
$this->session->set_userdata('isLoggedin',TRUE);
$this->session->set_userdata('ses_id',$user_data['id_akun']);
$this->session->set_userdata('ses_nama',$user_data['nama']);
if($user_data['level']=='admin'){ //level admin
$this->session->set_userdata('level','admin');
redirect('admin');
}
else if($user_data['level']=='aspiran'){ //level aspiran
$this->session->set_userdata('level','aspiran');
redirect('aspiran');
}
else if($user_data['level']=='guru'){ //level guru
$this->session->set_userdata('level','guru');
redirect('guru');
}
else if($user_data['level']=='siswa'){ //level siswa
$this->session->set_userdata('level','siswa');
redirect('siswa');
}
}
else{
$this->load->view('login');
echo "<script>alert('Failed Login: Check your username and password!');
</script>";
}
}
}
function logout(){
$this->session->sess_destroy();
$url=base_url('');
redirect($url);}}
Hello so I have a site setup to where users can submit projects and it adds it to a upload folder and the database for user download anyway I want to have it so once a user submits a new project it creates a URL like “sitename.com/projects.html?projectname” or something like that. My code is below.
P.S Everything on my site works just need to learn how to create the url.
projects.html: Mainly used to display the recent projects.
<?php include("includes/header.php"); ?>
<?php
include_once 'dbconnect.php';
// fetch files
$sql = "select filename, title, description from tbl_files LIMIT 4";
$result = mysqli_query($con, $sql);
?>
<div id="container">
<div class="wrapper">
<div id="content">
<h2>Recent Projects <button style="float: right;">New Project</button></h2>
<p><table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>File Name</th>
<th>Description</th>
<th>Download</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
while($row = mysqli_fetch_array($result)) { ?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><a href="uploads/<?php echo $row['filename']; ?>" download>Download</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</p>
<div id="column">
<div class="holder">
<h2>Welcome!</h2>
<ul id="latestnews">
<li class="last"> <p><?php
session_start();
include_once "vendor/autoload.php";
$page = new membership\Page(1);
if ($page->isValid() == true){
echo "Hello " . $_SESSION["username"] . "!<br /><br />
<a href='logout.html'>Logout</a>\n";
} elseif ($page->isValid() == false) { echo "<center>Please <a href='login.php'>Log in</a> to share projects.<br /> <a href='register.php'>Need A Account?</a></center>";}
?><br />
</p>
<br /></p>
</li>
</ul>
</div>
</div>
<br class="clear" />
</div>
</div>
<?php
error_reporting(E_ALL & ~E_NOTICE);
include('includes/memberlistconfig.php');
// call this file only after database connection
require_once 'functions.php';
?>
<div id="container">
<div class="wrapper">
<div id="content">
<h2>Categories</h2>
<p>
<div class="height20"></div>
<?php echo $emsg; ?>
<article>
Click on one of the categories to see what’s inside.
<ul>
<?php
$res = fetchCategoryTreeList();
foreach ($res as $r) {
echo $r;
}
?>
</ul>
</article>
</div></p>
<br class="clear" />
</div>
</div>
<?php include("includes/footer.php"); ?>
new-project.html: Allows used to upload a new project.
<?php
include_once('includes/header.php'); ?>
<?php
include_once 'dbconnect.php';
// fetch files
$sql = "select filename from tbl_files";
$result = mysqli_query($con, $sql);
?>
<?php
session_start();
include_once "vendor/autoload.php";
$page = new membership\Page(1);
if ($page->isValid() == true) {
?>
<div id="container">
<div class="wrapper">
<div id="content">
<h2>New Project</h2>
<p><center>
<form action='upload.php' method='post' enctype='multipart/form-data'>
<legend>Select File to Upload:</legend>
<div class='form-group'>
Title: <br /><input type='text' name='title' maxlength="255"/><br /><br />
Description: <br /><textarea type='text' name='description' maxlength="2000"></textarea><br /><br />
<input type='file' name='file1' />
</div>
<div class='form-group'><br />
<input type='submit' name='submit' value='Upload' class='btn btn-info'/>
</div>
<?php if (isset($_GET['st'])) { ?>
<div class='alert alert-danger text-center'>
<?php
if ($_GET['st'] == "success") {
echo "File Uploaded Successfully!";
} else {
echo 'Invalid File Extension!';
}
?>
</div>
<?php } ?>
</form></center>
</p><?php } ?>
<br /></div>
</p>
<div id="column">
<div class="holder">
<h2>Project Upload Rules</h2>
<ul id="latestnews">
This is this rules you must follow for uploading a project.<br /><br />
- You must own the project / script.<br />
- Must be 100% clean / safe.<br />
- Code must be easy to read.<br />
- No outdated code.<br />
<br />
If you don’t follow the rules your account who be banned.
<br />
</p>
<br /></p>
</li>
</ul>
</div>
</div>
<br class="clear" />
</div>
</div>
<?php include_once('includes/footer.php'); ?>
upload.php: This file uploads the info to the database.
<?php include('dbconnect.php'); ?>
<?php
//check if form is submitted
if (isset($_POST['submit']))
{
$filename = $_FILES['file1']['name'];
//upload file
if($filename != '')
{
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$allowed = ['zip', 'rar', 'php', 'html', 'sql'];
//check if file type is valid
if (in_array($ext, $allowed))
{
// get last record id
$sql = 'select max(id) as id from tbl_files';
$result = mysqli_query($con, $sql);
if (count($result) > 0)
{
$row = mysqli_fetch_array($result);
$filename = ($row['id']+1) . '-' . $filename;
}
else
$filename = '1' . '-' . $filename;
//set target directory
$path = 'uploads/';
$created = #date('Y-m-d H:i:s');
move_uploaded_file($_FILES['file1']['tmp_name'],($path . $filename));
$title = '';
if(!empty($_POST['title']))
{
$title = mysqli_real_escape_string($con, $_POST['title']);
}
$description = '';
if(!empty($_POST['description']))
{
$description = mysqli_real_escape_string($con, $_POST['description']);
}
// insert file details into database
$sql = "INSERT INTO tbl_files(filename, created, title, description) VALUES('$filename', '$created', '$title', '$description')";
mysqli_query($con, $sql);
header("Location: new-project.html?st=success");
}
else
{
header("Location: new-project.html?st=error");
}
}
else
header("Location: new-project.html");
}
?>
I already use if(isset($_POST['sub'])) in my code. But it show Notice: Undefined index: category in C:\xampp\htdocs\online shop\controlar\product.php on line 100. This is a simple product upload page. When I try to upload a product it works fine but doesn't work in the category, I mean category not add in the database.
product.php
<?php
require_once("/../dataAccessLayer/dalSession.php");
session::checkA();
require_once("/../dataAccessLayer/dalProduct.php");
require_once("/../dataAccessLayer/dalSub-Category.php");
require_once("/../dataAccessLayer/dalCategory.php");
require_once("/../dataAccessLayer/dalSize.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>Add Product</title>
<link rel="stylesheet" href="../css/contact.css">
<link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Roboto'>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="../js/jquery-1.9.1.min.js"></script>
<script src='../js/jquery.scrollto.js'></script>
</head>
<body>
<?php
if(isset($_GET['action']) && ($_GET['action']== "logout")){
Session::StopA();
}
?>
<nav class="navbar navbar-default navbar-static-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">
Add Product
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li>Visit Site </li>
<li>Logout</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<div class="container-fluid main-container">
<div class="col-md-2 sidebar">
<ul class="nav nav-pills nav-stacked">
<li> Home</li>
<li >Category Details</li>
<li>Sub-Category Details</li>
<li>Size Details</li>
<li class="active">Product Details</li>
<li>Delivery Charge Details</li>
<li>Sell Report</li>
<li>Order View</li>
<li>Member List</li>
</ul>
</div>
<div class="col-md-10 content">
<div class="panel panel-default">
<div class="panel-heading">
<h2 align="center"><b><u>Add New Product</u></b></h2>
</div>
<div class="panel-body">
<h3>View Product</h3>
<?php
if (isset($_POST['sub'])){
$pro = new Product();
$pro->p_name = $_POST['name'];
$pro->price = $_POST['price'];
$pro->vat = $_POST['vat'];
$pro->discount = $_POST['discount'];
$pro->picture = $_FILES['pic']['name'];
$pro->details = $_POST['details'];
$pro->quantity = $_POST['quantity'];
$pro->subcat_id = $_POST['subcatagory'];
$pro->cat_id = $_POST['category']; //100 line
$pro->size_id = $_POST['size'];
if ($pro->Insert()) {
echo "Insert Sucessfully";
$image_type = $_FILES['pic']['type'];
if ($image_type=='image/jpeg' || $image_type=='image/jpg' || $image_type=='image/png')
{
move_uploaded_file($_FILES['pic']['tmp_name'],'../image/'.$_FILES['pic']['name']);
}
}
else {
echo "Data Failed";
//echo $_POST['size'];
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<table align="center">
<tr>
<td>Name: </td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" /></td>
</tr>
<tr>
<td>Vat:</td>
<td><input type="text" name="vat" /></td>
</tr>
<tr>
<td>Discount:</td>
<td><input type="text" name="discount" /></td>
</tr>
<tr>
<td>Picture:</td>
<td><input type="file" name="pic" /></td>
</tr>
<tr>
<td>Details:</td>
<td><input type="text" name="details" /></td>
</tr>
<tr>
<td>Quantity:</td>
<td><input type="text" name="quantity" /></td>
</tr>
<tr>
<td>Sub-Category:</td>
<td>
<select name="subcatagory" id="siz">
<?php
$ser = new SubCategory();
$load = $ser->Load();
echo $load;
?>
</select>
</td>
</tr>
<tr>
<td>Category:</td>
<td><div id="cos" name="category"></div></td>
</tr>
<tr>
<td>Size:</td>
<td>
<select name="size">
<?php
$size = new Size();
$size->View_dd();
?>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sub" value="Save" /></td>
</tr>
</table>
</form>
</div>
</div>
</div>
<hr>
</body>
</html>
<script>
$(document).ready(function(){
$("#siz").change(function(){
var ser_id = $(this).val();
$.ajax({
url:"show_category_delivery.php",
method:"POST",
data:{ser_Id:ser_id},
dataType:"HTML",
success: function(data)
{
$("#cos").html(data);
}
});
});
});
</script>
dalProduct.php
<?php
class Product {
public $product_id;
public $p_name;
public $price;
public $vat;
public $discount;
public $picture;
public $details;
public $quantity;
public $subcat_id;
public $cat_id;
public $size_id;
public function DB()
{
$connection = mysqli_connect("localhost","root","","project");
return $connection;
}
public function SelectById()
{
$sql = "SELECT product.product_id, product.p_name, product.price, product.vat, product.discount, product.picture, product.details, product.quantity, subcategory.name, size.size_name
FROM product,subcategory,size
WHERE product.subcategory_id = subcategory.subcategory_id
AND product.size_id = size.size_id
AND product.product_id = '".$this->product_id."'";
$sql = mysqli_query($this->DB(),$sql);
while($d = mysqli_fetch_row($sql))
{
return $d;
}
}
public function Insert()
{
$sql = "INSERT INTO product (p_name, price, vat, discount, picture, details, quantity, subcategory_id, category_id, size_id)
VALUES ('".$this->p_name."',
'".$this->price."',
'".$this->vat."',
'".$this->discount."',
'".$this->picture."',
'".$this->details."',
'".$this->quantity."',
'".$this->subcat_id."',
'".$this->cat_id."',
'".$this->size_id."');";
if(mysqli_query($this->DB(), $sql))
{
return true;
}
return false;
}
public function Update()
{
$sql = "UPDATE product SET p_name = '".$this->p_name."',
price = '".$this->price."',
vat = '".$this->vat."',
discount = '".$this->discount."',
picture = '".$this->picture."',
details = '".$this->details."',
quantity = '".$this->quantity."',
subcategory_id = '".$this->subcat_id."',
size_id = '".$this->size_id."'
WHERE product_id = '".$this->product_id."'";
if(mysqli_query($this->DB(),$sql))
{
return true;
}
return false;
}
public function View()
{
$sql = "SELECT product.product_id, product.p_name, product.price, product.vat,
product.discount, product.picture, product.details, product.quantity,
subcategory.name, size.size_name
FROM product,subcategory,size
WHERE product.subcategory_id = subcategory.subcategory_id AND
product.size_id = size.size_id";
$sql_connect = mysqli_query($this->DB(),$sql);
while($d = mysqli_fetch_row($sql_connect))
{
$arr[] = $d;
}
return $arr;
}
public function Delete()
{
$sql = "DELETE FROM product WHERE product_id = '".$this->product_id."'";
if(mysqli_query($this->DB(),$sql))
{
return true;
}
return false;
}
}
?>
I give you alternative suggestion:
Just add below code and write category id manually.
<td>Category:</td>
<td><div id="cos"></div> <input type="text" id="category" name="category" /></td>
UPDATE: The category is automatically selected when sub-category is select
in line number 169 you should use
<td><div id="cos" name="divCategory"></div> <input type="hidden" id="category" name="category" /></td>
changed name of div to divCategory
It always drops the first checkbox, but not the actual part of the record the checkbox represents. If I keep clicking 'save' it drops off the next checkbox, and the first record. For instance, if the record is 'DMG, SCR, KLS, AST' when I click 'save' and the page refreshed, the DMG checkbox is not checked but the record still says 'DMG, SCR, KLS, AST' If I click it again, the record becomes 'SCR, KLS, AST' and the SCR checkbox is unchecked. The checkbox part of the form is near the bottom.
Thanks!
// delete
if(isset($_GET['id']) && $_GET['x'] == "d") {
$eventType = EventType::find_by_id($_GET['id']);
if($eventType && $eventType->delete()) {
$message = "The Event Type '{$eventType->name}' was deleted.";
} else {
$message = "The Event Type could not be deleted.";
}
}
// save
if(isset($_POST['submit'])) {
$eventType = new EventType();
if(isset($_POST['id'])) {
$eventType->id = mysql_prep($_POST['id']);
}
$eventType->name = mysql_prep($_POST['name']);
$scoreTypes = ScoreType::find_all();
$score_set = array();
foreach($scoreTypes as $scoreType) {
if(isset($_POST[$scoreType->name]) && $_POST[$scoreType->name] == $scoreType->name) {
array_push($score_set, mysql_prep($_POST[$scoreType->name]));
}
}
if(!empty($score_set)) {
$scores = implode(", ", $score_set);
} else {
$scores = "empty";
}
if(isset($scores)) { $eventType->score_types = $scores; } else { $eventType->score_types = ""; }
if(isset($_POST['is_disp'])) { $eventType->is_disp = mysql_prep($_POST['is_disp']); }
$eventType->save();
}
// edit
if(isset($_GET['id']) && ($_GET['x'] == "e")) {
$eventType = EventType::find_by_id($_GET['id']);
}
//view
$eventTypes = EventType::find_all($ord="name");
$scoreTypes = ScoreType::find_all();
?>
<div id="admin_table">
<div id="admin_thead">
<table>
<tr>
<td width="35%">Name</td>
<td width="40%">Score Types</td>
<td width="5%">Display</td>
<td width="7%">Actions</td>
<td width="12"></td>
</tr>
</table>
</div>
<div id="admin_tdata">
<table>
<colgroup></colgroup>
<colgroup class="odd"></colgroup>
<colgroup></colgroup>
<colgroup class="actions"></colgroup>
<?php foreach($eventTypes as $eventType_v): ?>
<tr>
<td width="35%"><?php echo $eventType_v->name; ?></td>
<td width="40%" title="<?php echo $eventType_v->score_types; ?>"><?php echo $eventType_v->score_types; ?></td>
<td width="5%" class="bool"><img src="../images/<?php echo ($eventType_v->is_disp == 1) ? "x_yes.png" : "x_no.png"; ?>"></td>
<td width="7%" class="but_admin"><a class="but_admin_edit" title="edit" href="index.php?con=event_types&id=<?php echo $eventType_v->id; ?>&x=e">edit</a><a class="but_admin_delete" title="delete" href="index.php?con=event_types&id=<?php echo $eventType_v->id; ?>&x=d" onclick="return confirm('Are you sure you want to delete the Event Type <?php echo "\'{$eventType_v->name}\'"; ?>?')">delete</a></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div>
<div id="admin_ops_cont">
<div id="admin_lt_panel">
<div id="admin_msg">
<h2>Messages</h2>
<p><?php if(!$message) { echo "no message"; } else { echo $message; } ?></p>
</div>
</div>
<div id="admin_form">
<?php if(isset($_GET['id'])) { if(isset($_GET['x'])) { if($_GET['x'] == "e") { $edit = 1; } else { $edit = 0; }}} else { $edit = 0; } ?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>?con=event_types<?php if(isset($_GET['id'])) { echo "&x=e&id={$eventType->id}"; } ?>" method="post">
<fieldset>
<legend><?php if($edit == 1) { echo "Edit: <span>{$eventType->name}</span>"; } else { echo "New EventType"; } ?></legend>
<?php if($edit == 1): ?>
<input type="hidden" name="id" id="id" value="<?php echo $eventType->id; ?>">
<?php endif; ?>
<p>
<label for="name">Name</label>
<input name="name" id="name" type="text"<?php if($edit == 1): ?> value="<?php echo $eventType->name; ?>"<?php endif; ?>>
</p>
<p>
<?php foreach($scoreTypes as $scoreType): ?>
<label for="<?php echo $scoreType->name; ?>"><?php echo $scoreType->description; ?></label>
<input type="checkbox" id="<?php echo $scoreType->name; ?>" name="<?php echo $scoreType->name; ?>" value="<?php echo $scoreType->name; ?>"
<?php if($edit == 1): if(strpos("{$eventType->score_types}", "{$scoreType->name}")): ?> checked<?php endif; endif; ?>>
<?php endforeach; ?>
</p>
<p>
<label for="is_disp">Display?</label>
<input type="checkbox" id="is_disp" name="is_disp" value="1"<?php if($edit == 1) { if($eventType->is_disp == 1) { echo " checked"; }} ?>>
</p>
<p>
<button type="submit" name="submit">Save</button><?php if($edit == 1): ?><a class="btn_cancel" href="index.php?con=event_types">done</a><?php endif; ?>
</p>
</fieldset>
</form>
</div><!-- end .admin_form -->
</div><!-- end #admin_ops_con -->
I think the problem is in the line
<?php if($edit == 1): if(strpos("{$eventType->score_types}", "{$scoreType->name}")): ?> checked<?php endif; endif; ?>>
if the entry is in the first position(index in the String is 0) strpos returns 0. PHP assumes that 0 equals false, so the first entry with the match with the index 0 will not be "checked".
this code should solve the problem (checking for false !==)
<?php if($edit == 1): if(strpos("{$eventType->score_types}", "{$scoreType->name}")!==false): ?> checked<?php endif; endif; ?>>
// Not tested since i dont have PHP installed on this machine. but will try to confirm this asap
Update:
Info strpos(...) return the index in the string. Counting starts by 0 so you would have to check explicitly for false (===false) oder (!==false) to be sure, that PHP doesnt treat the index 0 as false. here is a link to the manual (Link)
I have a script that is reading my wheel brands. If I select a wheel it show all the wheels from that brand...
But because I did GROUP BY merk_naam so in my dropdown there wont be 100 same brands I cant show them all...
Can somebody help me with the following:
I have two tables now:
Table 1: it_merken
Table 1
Table 2: it_merken2
Table 2
I want the merk_naam in a dropdown list
But when I do that I get a lot of double ones like AS 2 time in this example...
I want to JOIN in table 1 and table 2 the merk_naam in one query...
I cant figure out how...
Can somebody help me out?
This is the class I got now with the GROUP BY:
<?php
class merken extends connect
{
/**
* Merken lijst
*
* #param object
*/
private $merkenlijst;
/**
* Vraag merken op uit database
*
* #param int $id
* #return array
*/
public function getMerken($database, $id = NULL)
{
$sql = "SELECT merk_code, merk_logo, merk_naam, wiel_foto, wiel_nummer, wiel_info FROM ".$database."_merken";
if(!empty($id))
{
$sql .= " WHERE merk_code=:id";
}
else
{
$sql .= " GROUP BY merk_naam";
}
try
{
$stmt = $this->db->prepare($sql);
if(!empty($id)){ $stmt->bindParam(":id", $id, PDO::PARAM_STR); }
$stmt->execute();
$this->merkenlijst = $stmt->fetchAll(PDO::FETCH_OBJ);
$stmt->closeCursor();
return $this->merkenlijst;
}
catch (Exception $e)
{
die ( $e->getMessage() );
}
}
/**
* Maak een database object en sla de data op
*
* #param object $dbo
* #return void
*/
public function __construct($dbo = NULL)
{
/*
* Roep de parent constructor aan om te controleren op
* een database object
*/
parent::__construct($dbo);
}
}
?>
That is given me each brand one time in my dropdown but when I select nothing, I want to show all brands and because I use GROUP BY now I only see one wheel per brand after submit.
This is my PHP and HTML:
<div class="bandwielkolom">
<form name="wielenzoeken" action="index.php?lang=<?php echo $lang; ?>&p=<?php echo $_GET['p']; ?>#keuze" method="post">
<table class="wielentabel">
<tr>
<th colspan="2"><?php echo $searchwheelsbybrand; ?><a name="wiel"></a></th>
</tr>
<tr>
<td><?php echo $brand; ?>:</td>
<td>
<select name="wiel_merk" onchange="enable()">
<option value="0">- <?php echo $brand; ?> -</option>
<?php
$merken = $merkclass->getMerken($website, $merk);
foreach($merken as $merk)
{
echo "\t\t\t\t\t\t\t\t\t\t\t<option value=\"".$merk->merk_code."\"";
if(isset($_GET['search']) && $_GET['search'] == "wiel" && isset($_GET['merk']) && $_GET['merk'] != "0" || isset($_POST['wiel_submit']) && $_POST['wiel_merk'] != "0") { echo " selected=\"selected\""; }
echo ">".$merk->merk_naam."</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="wiel_submit" value="<?php echo $search; ?>"/></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
</form>
</div>
</div>
<div class="clearboth"></div>
<div id="main-right">
<?php
foreach($teksten as $tekst)
{
?>
<h1><strong><?php echo $tekst->tekst_header; ?></strong></h1>
<?php echo $tekst->tekst_content."\n"; ?>
<?php
if(!empty($tekst->tekst_img))
{
?>
<div class="imgcontainer">
<div class="imgcontent">
<img src="<?php echo $tekst->tekst_img; ?>" class="content_image" alt="<?php echo $tekst->tekst_header; ?>" />
</div>
</div>
<?php
}
?>
<?php
}
?>
</div>
</div>
<div class="clearboth"></div>
<div id="main-left">
<ul class="wheels">
<?php
if(isset($_POST['wiel_submit']) && $_POST['wiel_submit'] == $search || isset($_GET['merk']))
{
$merk = NULL;
if(isset($_POST['wiel_submit']) && $_POST['wiel_submit'] == $search)
{
echo "\t\t\t\t\t<h2>".$brandselection."</h2>
\t\t\t\t\t<br />\n";
$merk = $_POST['wiel_merk'];
}
?>
<?php
$merken = $merkclass->getMerken($website, $merk);
foreach($merken as $merk)
{
?>
<li><span class="title"><?php echo $merk->wiel_info; ?></span>
<a href="http://www.website.nl/website/images/w3/<?php echo $merk->wiel_foto; ?>" class="preview" title="Fotonummer: <?php echo $merk->wiel_foto; ?>">
<img src="http://www.website.nl/website/images/w3/<?php echo $merk->wiel_foto; ?>" alt="Fotonummer: <?php echo $merk->wiel_foto; ?>" class="wheelImg"/>
</a>
<span class="descr">
<img src="http://www.website.net/preview/bnet/logos/<?php echo str_replace(".png", "_100.png", $merk->merk_logo); ?>" style="padding-left:45px;" width="100" class="logo-merk" alt="<?php echo $merk->merk_naam; ?>"/>
</span>
</li>
<?php
}
?>
</ul>
<?php
}
?>
</div>
Any help is welcome because I can't figure it out!
You are getting doubles because some some of the fields in your SQL do not match, so you get a record for each.