How do I make a pagination system with PHP? [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a PHP page that displays my MySQL table's data four rows at a time. If there are, for example, nine rows, the first page would be http://webste.ca/usedunits.php?page=1 and would display the first four rows. The second page would be http://webste.ca/usedunits.php?page=2 and would display the fifth to the eigth rows, and the third page would be http://webste.ca/usedunits.php?page=3 and would display the ninth row.
I am trying to make a link that appears as an arrow that will send the user to http://webste.ca/usedunits.php?page=2 if they are on http://webste.ca/usedunits.php?page=1. If they are on page two there will be a link to page 3, and so on.
I am also trying to make a link that appears as a fast forward button that will send the user to the very last page. My problem is actually making this function.
Here is my full PHP page code:
<?php
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$dbhost = 'dnam';
$dbuser = 'kabm';
$dbpass = 'Kazer';
$dbname = 'kam';
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $connection )
{
die('Could not connect: ' . mysqli_error());
}
$start_from = ($page-1) * 4;
$sql = "SELECT * FROM `used_trailers` ORDER BY `orderid` ASC LIMIT $start_from,4";
$rs_result = mysqli_query ($connection, $sql);
echo mysqli_error( $connection );
?>
<!doctype html>
<!--[if lt IE 7]> <html class="ie6 oldie"> <![endif]-->
<!--[if IE 7]> <html class="ie7 oldie"> <![endif]-->
<!--[if IE 8]> <html class="ie8 oldie"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="">
<!--<![endif]-->
<head>
<meta name="author" content="Kelsey Nealon(Kelseynealon#gmail.com), Contract Web Developer" />
<meta name="description" content="GBM Trailer Service Ltd. Calgary-based, proudly serving the tanker and bulk goods transportation industry for over 25 years." />
<meta name="keywords" content="Tanker, Barrel, Parts, Betts, Camloc, Scully, Lubecore, Dixon, GBM, Flotech" />
<title>GBM Trailer Service Ltd. ::: Service</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/mobilemenu.css"/>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="boilerplate.css" rel="stylesheet" type="text/css">
<link href="responsiveused.css" rel="stylesheet" type="text/css">
<link href="perfect-scrollbar.css" rel="stylesheet" type="text/css" />
<!--
To learn more about the conditional comments around the html tags at the top of the file:
paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/
Do the following if you're using your customized build of modernizr (http://www.modernizr.com/):
* insert the link to your js here
* remove the link below to the html5shiv
* add the "no-js" class to the html tags at the top
* you can also remove the link to respond.min.js if you included the MQ Polyfill in your modernizr build
-->
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- jQuery library (served from Google) -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!-- include Cycle2 -->
<script src="jquery.mousewheel.js"></script>
<script src="perfect-scrollbar.js"></script>
<script src="jquery.cycle2.min.js"></script>
<script>
$(document).ready(function(){
$(".nav-button").click(function () {
$(".nav-button,.primary-nav").toggleClass("open");
});
});
</script>
<script>
jQuery(document).ready(function ($) {
"use strict";
$('.Default').perfectScrollbar();
});
</script>
<script src="respond.min.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-45342007-3', 'gbmtrailer.ca');
ga('send', 'pageview');
</script>
</head>
<body bgcolor="#102540">
<div class="gridContainer clearfix">
<div id="div1" class="fluid">
<div id="navcontain"></div>
<div id="wrapper">
<div id="cover"></div>
<div id="mainnaviphone">
<img id="logo" src="images/gbmlogo.jpg" alt="G.B.M. Trailer Service LTD. Logo" title="G.B.M. Trailer Service LTD. Logo"/>
<img src="images/newunitsbutton.png" alt="New Units Button" title="New Units Button" id="buto1">
<img src="images/usedunitsbutton.png" alt="Used Units Button" title="Used Units Button" id="buto2">
<img src="images/homebutton.png" alt="home" title="Home" id="homebuto"/>
<img src="images/previousbutton.png" alt="previous" title="Previous" id="prevbuto"/>
<img src="images/nextbutton.png" alt="next" title="Next" id="nextbuto"/>
</div>
<div id="background2">
<div id="textcontrol">
<?php
while ($row = mysqli_fetch_assoc($rs_result)) {
?>
<div id='used1'>
<div id='rigtitle' class="contentHolder Default">
<? echo $row["title"]; ?>
</div>
<table class="infotabe1"><tr><td class="desctde">Description: </td><td rowspan="2" id="content_1" class="infotde contentHolder Default"><? echo $row["description"]; ?></td></tr></table>
<table class="infotabe"><tr><td class="desctd">Make: </td><td id="content_1" class="infotd contentHolder Default"><? echo $row["make"]; ?></td></tr></table>
<table class="infotabe"><tr><td class="desctd">Model: </td><td id="content_1" class="infotd contentHolder Default"><? echo $row["model"]; ?></td></tr></table>
<table class="infotabe"><tr><td class="desctd">Year: </td><td id="content_1" class="infotd contentHolder Default"><? echo $row["year"]; ?></td></tr></table>
<table class="infotabe"><tr><td class="desctd">Price: </td><td id="content_1" class="infotd contentHolder Default"><? echo $row["price"]; ?></td></tr></table>
<!-- Unit Picture -->
<img src='images/<? echo $row["photo"]; ?>' id='mainimage'>
<a href='#'><img src='images/picturesandspecsbutton.png' alt='Pictures and Specs' title='Pictures and Specs' id='picsandspecsbuto'></a>
</div>
<?php
};
?>
</div>
<div id="description" class="resize">
<p><u>Click</u> or call for more information or enquiries.</p>
</div>
<div id="bottomquote">
Your Certified Tanker Trailer Specialist
</div>
<div id="bottomcontactinfo">
<em>Phone <a style="color:white; text-decoration:none;" href="tel:1-4032799717">403 279 9717</a> or <a style="color:white; text-decoration:none;" href="tel:1-888-426-9717">1 888 426 9717</a> 9300 Endeavor Dr. SE, Calgary Alberta, T3S 0A1<br /><a style="color:white; text-decoration:none;" href="mailto:info#gbmtrailer.ca">info#gbmtrailer.ca</a>
</div>
<img src="images/visamastercardlogos.gif" id="visamastercardlogos" alt="Visa and Mastercard Logo" title="Visa and Mastercard Logo">
<img id="mycart" src="images/mycart.gif" alt="My Cart" title="My Cart">
</div>
</div>
</div>
</div>
<script>
<!--
var autoSizeText;
autoSizeText = function() {
var el, elements, _i, _len, _results;
elements = $('.resize');
console.log(elements);
if (elements.length < 0) {
return;
}
_results = [];
for (_i = 0, _len = elements.length; _i < _len; _i++) {
el = elements[_i];
_results.push((function(el) {
var resizeText, _results1;
resizeText = function() {
var elNewFontSize;
elNewFontSize = (parseInt($(el).css('font-size').slice(0, -2)) - 1) + 'px';
return $(el).css('font-size', elNewFontSize);
};
_results1 = [];
while (el.scrollHeight > el.offsetHeight) {
_results1.push(resizeText());
}
return _results1;
})(el));
}
return _results;
};
autoSizeText(); // here is where we call the function.
</script>
</body>
</html>
<?php
$sql = "SELECT COUNT(`orderid`) FROM `used_trailers`";
$rs_result = mysqli_query($connection, $sql);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 4);
$sqlef = 'SELECT * FROM `used_trailers`';
$rs_resultt = mysqli_query($connection, $sqlef);
$rowcount = mysqli_num_rows($rs_resultt);
$record_start = $start_from + 1;
$record_end = $record_start + 3;
// Remember to check against the max record
if ($record_end > $total_records) {
$record_end = $total_records;
}
echo "Showing ".$record_start." - ".$record_end." of ".$rowcount." results ";
echo "asd<a href='usedunits.php?page=".$i."'>".$i."</a>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='usedunits.php?page=".$i."'>".$i."</a> ";
};
?>

If you want to put a link to the last page, you need to know what the last page will be.
The way to do that is to do an additional query to your database asking for the total number of items matching the criterias, and with that and your step (i.e. number of items per page) calculating the number of pages.
In your example that query would be
SELECT count(*) FROM `used_trailers`
P.S. assuming you put the result of that query in a $count variable, the last page (counting from 1) would then be ceil($count / 4) (with a corner case for $count == 0), 4 being the number of items you show on each page according to your code.

Related

Fetch company details on a click on button using jquery

I created a table as company_list. This table consists of following fields
1)Company Name 2) Country 3) Vertical
So, lets assume there are 5 companies as following
Company Name: Water, Earth, Sun, Moon, Star
Countries: Water= UK, Earth=UK, Sun=Australia, Moon, =India, Star=Germany
Vertical:Water, Earth, Sun = WWF and Moon, Star=Social Welfare
First HTML is a select option to filter by
1st Country 2nd Vertical
So, if I chose UK country, vertical should auto select WWF (exclude AUS country company name)
Now, post this I have button to FETCH single COMPANY DETAILS first that matches the above condition.
Code, I have designed is not adding a filter and does fetch every company.
Kindly help with this.
1) index page
<?php
$conn = mysqli_connect("localhost","root","","accounts");
$sql = "SELECT * FROM company_list";
$res= mysqli_query($conn,$sql);
$res1=mysqli_query($conn,$sql);
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<!-- Bootstrap 3.3.7 -->
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
<!-- Font Awesome -->
<link rel="stylesheet" href="bower_components/font-awesome/css/font-awesome.min.css">
<!-- Ionicons -->
<link rel="stylesheet" href="bower_components/Ionicons/css/ionicons.min.css">
<!-- DataTables -->
<link rel="stylesheet" href="bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css">
<!-- Theme style -->
<link rel="stylesheet" href="dist/css/AdminLTE.min.css">
<!-- AdminLTE Skins. Choose a skin from the css/skins
folder instead of downloading all of them to reduce the load. -->
<link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">
<style media="screen">
.navbar-inverse {
background-color: #4a8cbb;
border-color: #4a8cbb;
}
#row1{
margin-left: 100px;
}
</style>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<select class="form-control">
<?php while ($row=mysqli_fetch_array($res)):; ?>
<option value="">SELECT COUNTRY</option>
<option value=""><?php echo $row['country']; ?></option>
<?php endwhile; ?>
</select>
</div>
<div class="navbar-header" id="row1">
<select class="form-control">
<?php while ($row1=mysqli_fetch_array($res1)):; ?>
<option value="">SELECT VERTICAL</option>
<option value=""><?php echo $row1['vertical']; ?></option>
<?php endwhile; ?>
</select>
</div>
<div class="navbar-header" id="row1">
<button id="fetch_company" class="btn btn-success btn-lg btn-block btn-huge">FETCH COMPANY DETAILS</button>
</div>
</div>
</nav>
<div class="row">
<div class="col-md-3">
Company Name:<span id="com_get_data"></span>
</div>
</div>
</body>
<script type="text/javascript" src="bower_components/jquery/dist/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var count = 0;
$("#fetch_company").click(function(){
//alert("Working");
count = count + 1;
$("#com_get_data").load("get_data.php",{countNew:count});
});
});
</script>
</html>
2)Get_data.php
<?php
$conn = mysqli_connect("localhost","root","","accounts");
$countNew = $_POST['countNew'];
$sql = "SELECT * FROM company_list LIMIT $countNew";
$res = mysqli_query($conn,$sql);
if (mysqli_num_rows($res) > 0) {
while ($row = mysqli_fetch_array($res)) {
echo $row['company_name'];
}
}
?>
Please check using $.ajax
Jquery Code
<script type="text/javascript">
$(document).ready(function(){
var count = 0;
$("#fetch_company").click(function(){
//alert("Working");
count = count + 1;
$.ajax({
type:"POST",
url: "get_data.php",
data:{countNew:count},
cache: false,
success: function(result){
$("#com_get_data").html(result);
}
});
});
});
</script>
Php Code
<?php
$conn = mysqli_connect("localhost","root","","accounts");
$countNew = $_POST['countNew'];
$sql = "SELECT * FROM company_list LIMIT $countNew";
$res = mysqli_query($conn,$sql);
if (mysqli_num_rows($res) > 0) {
while ($row = mysqli_fetch_array($res)) {
echo $row['company_name'];
}
}
else
{
echo 'No result found';
}
?>
Note: here you loop trough fetch data you need to add data in string array after that return data in JSON
Please check here for more details Click Here

Updating database on bootstrap button click

I am trying to send a value to my mysql database, I am very new to php/bootstrap programming and I've tried everything but most of the guides are beyond my understanding. Please guide me in the right path so on the button click the button will update the specific database.
Here's what the button click will do:
on Uninstall button click it will send update the database like
UPDATE activate="No" FROM software WHERE uid(or id)="same id of the row where the button is clicked" [I know this mysql query is wrong]
Here's my code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Control Panel</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Font Awesome -->
<link href="css/font-awesome.min.css" rel="stylesheet">
<!-- NProgress -->
<link href="css/nprogress.css" rel="stylesheet">
<!-- iCheck -->
<link href="css/green.css" rel="stylesheet">
<!-- Datatables -->
<link href="css/dataTables.bootstrap.min.css" rel="stylesheet">
<link href="css/buttons.bootstrap.min.css" rel="stylesheet">
<link href="css/fixedHeader.bootstrap.min.css" rel="stylesheet">
<link href="css/responsive.bootstrap.min.css" rel="stylesheet">
<link href="css/scroller.bootstrap.min.css" rel="stylesheet">
<!-- Custom Theme Style -->
<link href="css/custom.min.css" rel="stylesheet">
</head>
<body class="nav-md">
<div class="container body">
<div class="main_container">
<div class="col-md-12 left_col">
<div class="left_col scroll-view">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_content">
<table id="datatable-buttons" class="table table-striped table-bordered">
<thead>
<tr>
<th>S/N</th>
<th>Date</th>
<th>IP Address</th>
<th>UID</th>
<th>Activation</th>
<th>Command</th>
</tr>
</thead>
<tbody>
<?php
$count_rows = 0;
include 'database_connection.php';
$sql = "SELECT `date`, `ip`, `id`, `activate` FROM `software`";
$result = $conn->query($sql);
while ($row = mysqli_fetch_array($result)):
$count_rows++;
?>
<tr>
<td><?php echo $count_rows ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['ip']; ?></td>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['activate']; ?> <td><button type="button" id="btn-uninstall" class="btn btn-danger btn-xs">Uninstall</button> <button type="button" id="btn-install" class="btn btn-success btn-xs">Install</button> <button type="button" id="btn-upgrade" class="btn btn-warning btn-xs">Upgrade</button></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<!-- jQuery -->
<script src="js/jquery.min.js"></script>
<!-- Bootstrap -->
<script src="js/bootstrap.min.js"></script>
<!-- FastClick -->
<script src="js/fastclick.js"></script>
<!-- NProgress -->
<script src="js/nprogress.js"></script>
<!-- iCheck -->
<script src="js/icheck.min.js"></script>
<!-- Datatables -->
<script src="js/jquery.dataTables.min.js"></script>
<script src="js/dataTables.bootstrap.min.js"></script>
<script src="js/dataTables.buttons.min.js"></script>
<script src="js/buttons.bootstrap.min.js"></script>
<script src="js/buttons.flash.min.js"></script>
<script src="js/buttons.html5.min.js"></script>
<script src="js/buttons.print.min.js"></script>
<script src="js/dataTables.fixedHeader.min.js"></script>
<script src="js/dataTables.keyTable.min.js"></script>
<script src="js/dataTables.responsive.min.js"></script>
<script src="js/responsive.bootstrap.js"></script>
<script src="js/dataTables.scroller.min.js"></script>
<script src="js/jszip.min.js"></script>
<script src="js/pdfmake.min.js"></script>
<script src="js/vfs_fonts.js"></script>
<!-- Custom Theme Scripts -->
<script src="js/custom.min.js"></script>
</body>
</html>
Here's screenshot of the page: https://image.ibb.co/eToeaz/Capture.png
You can send request to server by this code.
$con = new mysqli_connect("servername", "username", "password", "name");
if(!$con)
{
die("DB CONNECTION ERROR");
}
$result=mysqli_query($con, "SELECT * FROM 'table' WHERE 'something'");
$row=$result->fetch_array();
$count = mysqli_num_rows($result);
mysqli_close($con);
if( $count == 1 )
If you want it to happen instantly then use AJAX to send request to the php server, see https://www.w3schools.com/php/php_ajax_database.asp for more info.
if you want it with redirecting then use
$conn = new mysqli("DBservername", "DBusername", "DBpassword", "DBname");
if ($conn->connect_errno) {
$errormsg = ("Database Connection failed: " . $conn->connect_error );
return $errormsg;
}
$res=mysqli_query($conn, "SELECT * FROM 'table' WHERE 'something'");
$row=$res->fetch_array();
$count = mysqli_num_rows($res); // if fetch correct it returns must be 1 row
mysqli_close($conn);
if( $count == 1 ) etc
send query, update, delete etc.
$sql = "DELETE FROM 'table' WHERE 'something';
mysqli_query($conn,$sql);
Rough and bad php code but should work.
Edit after i understand what you actually asked:
When rendering results from database, anchor each button to redirect to a page /update/{rowid} or /update.php?id={rowid}
and in that link execute the query with conditions and etc.

Issue with foreach loop updating every record, rather than the ones just selected in a checkbox

I have a page with a dropdown box at the top populated from the database, when I select an item from the dropdown it gives a list of results with a checkbox at the end of each line, I want to be able to select, using each checkbox, any number of results, and submit a value back into a field in the database for each result selected.
I sort of have this working, but it submits the value to every field in the database, rather than just the ones selected
<?php
require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="favicon.ico">
<title>Stock Items</title>
<!-- Bootstrap CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- bootstrap theme -->
<link href="css/bootstrap-theme.css" rel="stylesheet">
<!--external css-->
<!-- font icon -->
<link href="css/elegant-icons-style.css" rel="stylesheet" />
<link href="css/font-awesome.min.css" rel="stylesheet" />
<!-- full calendar css-->
<link href="assets/fullcalendar/fullcalendar/bootstrap-fullcalendar.css" rel="stylesheet" />
<link href="assets/fullcalendar/fullcalendar/fullcalendar.css" rel="stylesheet" />
<!-- easy pie chart-->
<link href="assets/jquery-easy-pie-chart/jquery.easy-pie-chart.css" rel="stylesheet" type="text/css" media="screen"/>
<!-- owl carousel -->
<link rel="stylesheet" href="css/owl.carousel.css" type="text/css">
<link href="css/jquery-jvectormap-1.2.2.css" rel="stylesheet">
<!-- Custom styles -->
<link rel="stylesheet" href="css/fullcalendar.css">
<link href="css/widgets.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<link href="css/style-responsive.css" rel="stylesheet" />
<link href="css/xcharts.min.css" rel=" stylesheet">
<link href="css/jquery-ui-1.10.4.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 -->
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<script src="js/respond.min.js"></script>
<script src="js/lte-ie7.js"></script>
<![endif]-->
</head>
<body>
<!-- container section start -->
<section id="container" class="">
<?php include("navigation.php"); ?>
<!--main content start-->
<section id="main-content">
<section class="wrapper">
<!--overview start-->
<div class="row">
<div class="col-lg-12">
<h3 class="page-header"><i class="fa fa-lightbulb-o"> </i>Stock</h3>
<ol class="breadcrumb">
<li><i class="fa fa-home"></i>Home</li>
<li><i class="fa fa-lightbulb-o"></i>Stock</li>
</ol>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<section class="panel">
<header class="panel-heading">
All Stock
</header>
<div class="panel-body">
<form class="form-horizontal" method="post" action="">
<div class="form-group">
<div class="col-lg-8">
<select name="search" class="form-control" required >
<?php
// connect to the database
require_once('models/db-settings.php');
$conn = mysql_connect($db_host, $db_user, $db_pass, $db_name) or die("Error " .mysql_error($conn));
mysql_select_db($db_name);
$query = "SELECT `id`, `description` FROM `stock_templates`";
$stock_templates = mysql_query($query);
echo "<option value=''>Select Stock Template</option>";
while ($description=mysql_fetch_assoc($stock_templates)) {
echo "<option value='" . $description['id'] . "'>" . $description['description'] . "</option>";
}
?>
</select>
</div>
<div class="col-lg-1">
<input type="submit" name="filter" value="Search" class="btn btn-success" />
</div>
</div>
</form><br><br>
<?php
if(isset($_POST['formSubmit']))
{
$aDoor = $_POST['check_list'];
if(empty($aDoor))
{
echo("<p>You didn't select any items to add to lease.</p>\n");
}
else
{
$N = count($aDoor);
echo("<p>You selected $N item(s) to add to lease: ");
for($i=0; $i < $N; $i++)
{
echo($aDoor[$i] . " ");
}
echo("</p>");
}
}
function IsChecked($chkname,$value)
{
if(!empty($_POST[$chkname]))
{
foreach($_POST[$chkname] as $chkval)
{
if($chkval == $value)
{
return true;
}
}
}
return false;
}
?>
<?php
ob_start( );
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
$query = mysql_query("UPDATE stock SET lease_id = $lease_id");
$result2 = mysql_query($query);
// check if sent
if ($result2) {
?>
<div class="alert alert-success fade in">
<button data-dismiss="alert" class="close close-sm" type="button">
<i class="icon-remove"></i>
</button>
<strong>Well done!</strong> Your lease items have been successfully saved.
</div>
<?php
} else {
?>
<div class="alert alert-block alert-danger fade in">
<button data-dismiss="alert" class="close close-sm" type="button">
<i class="icon-remove"></i>
</button>
<strong>Oh snap!</strong> We could not save your lease items.
</div>
<?php
}
}
}
?>
<?php
// connect to the database
require_once('models/db-settings.php');
$conn = mysql_connect($db_host, $db_user, $db_pass, $db_name) or die("Error " .mysql_error($conn));
mysql_select_db($db_name);
// Extract filter information
$count = 0;
$search = mysql_escape_string(#$_POST['search']);
// select data from the database
$query2 = "SELECT * FROM `stock_templates` ORDER BY `stock_templates`.`id` DESC LIMIT 0";
// Perform Logic
if (array_key_exists("filter", $_POST)) {
// query based on search term
$query2 = "SELECT * FROM `stock` WHERE $search=stocktemplate_id AND lease_id=0";
}
$result2 = mysql_query($query2);
// Result
if (mysql_num_rows($result2) < 1) {
echo "<div align='center'><h2>Please select a stock template above and click search</h2></div>";
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" class="form-horizontal" name="check_list[]">
<div class="form-group">
<label class="control-label col-lg-1" for="lease_id">Lease Number *</label>
<div class="col-lg-10">
<select name="lease_id" class="form-control">
<?php
// connect to the database
require_once('models/db-settings.php');
$conn = mysql_connect($db_host, $db_user, $db_pass, $db_name) or die("Error " .mysql_error($conn));
mysql_select_db($db_name);
$query = "SELECT `id`, `leasenumber` FROM `lease`";
$leases = mysql_query($query);
echo "<option value=''>Select Lease..........</option>";
while ($lease=mysql_fetch_assoc($leases)) {
echo "<option value='" . $lease['id'] . "'>" . $lease['leasenumber'] . "</option>";
}
?>
</select>
</div>
</div>
<table class="table table-hover">
<thead>
<tr>
<th>Item ID</th>
<th>Description</th>
<th>Barcode</th>
<th>Serial</th>
<th>Add To Lease</th>
</tr>
</thead>
<?php
while ($row = mysql_fetch_array($result2))
{
$id = $row["id"];
$lease_id = $row["lease_id"];
$barcode = $row["barcode"];
$serial = $row["serial"];
$stocktemplate_id = $row["stocktemplate_id"];
$qa = 0;
?>
<tbody>
<tr>
<td><?php print $id ?></td>
<td><?php $q = mysql_query("SELECT description FROM stock_templates WHERE id = '$stocktemplate_id'"); while ($row = mysql_fetch_array($q)){$qa = $row["description"];} print $qa ?></td>
<td><?php print $barcode ?></td>
<td><?php print $serial ?></td>
<td><input type="checkbox" name="check_list[]" value="<?php print $id ?>" /></td>
<td></td>
</td>
</tr>
</tbody>
<?php
}
?>
</table>
<input class="btn btn-danger" type="submit" name="formSubmit" value="Add Selected To Lease" />
</div>
</form>
</section>
</div>
</div>
</section>
</section>
<!--main content end-->
</section>
<!-- container section end -->
<!-- javascripts -->
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<!-- nice scroll -->
<script src="js/jquery.scrollTo.min.js"></script>
<script src="js/jquery.nicescroll.js" type="text/javascript"></script>
<!-- jquery ui -->
<script src="js/jquery-ui-1.9.2.custom.min.js"></script>
<!--custom checkbox & radio-->
<script type="text/javascript" src="js/ga.js"></script>
<!--custom switch-->
<script src="js/bootstrap-switch.js"></script>
<!--custom tagsinput-->
<script src="js/jquery.tagsinput.js"></script>
<!-- colorpicker -->
<!-- bootstrap-wysiwyg -->
<script src="js/jquery.hotkeys.js"></script>
<script src="js/bootstrap-wysiwyg.js"></script>
<script src="js/bootstrap-wysiwyg-custom.js"></script>
<!-- ck editor -->
<script type="text/javascript" src="assets/ckeditor/ckeditor.js"> </script>
<!-- custom form component script for this page-->
<script src="js/form-component.js"></script>
<!-- custome script for all page -->
<script src="js/scripts.js"></script>
</body>
</html>
No worries. Just put the code of insert query inside foreach() loop. Like this:
foreach($_POST['check_list'] as $item)
{
$sql="INSERT/UPDATE Query";
//for example
$sql = "INSERT INTO table_demo (field_1, TARGET_FIELD, field_2, field_3) VALUES (val_1, $item, val_2, val_3)";
$insert = mysqli_query($connection,$sql);
}
//next code of your choice
That is really Easy to give a go.
See this is same as you want.
In this link the first answer by Sean Valsh is your solution.
Giving the array as name of every checkbox will give you only chacked option's id in array while submitted.
https://stackoverflow.com/a/4997271/6834980
Ask if still have the problem. Happy to help.

How to get the First and Last Row Numbers of Four Rows to be Displayed

I have a PHP page that displays all the rows in my MySQL table. The PHP page only shows the first four rows on the page http://mywebsite.ca/usedunits.php?page=1, and then creates a link to http://mywebsite.ca/usedunits.php?page=2. The link is the same page with the next four rows on it. For example, the first page would have rows one to four, then the second has five to eight. If there is 9 rows, the PHP creates another link that goes to http://mywebsite.ca/usedunits.php?page=3 and will display the ninth row there. My problem is that I am trying to display the links like this:
So far, the of 20 results part shows up properly and so does the pages 1 2 3 4 5 6 7 8 9 10 > >>> but I can't seem to figure out how to get the 1-4 part for each page.
Here is my full PHP page code:
<?php
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$dbhost = 'dam';
$dbuser = 'sbm';
$dbpass = 'Kis';
$dbname = 'ksbm';
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $connection )
{
die('Could not connect: ' . mysqli_error());
}
$start_from = ($page-1) * 4;
$sql = "SELECT * FROM `used_trailers` ORDER BY `orderid` ASC LIMIT $start_from,4";
$rs_result = mysqli_query ($connection, $sql);
echo mysqli_error( $connection );
?>
<!doctype html>
<!--[if lt IE 7]> <html class="ie6 oldie"> <![endif]-->
<!--[if IE 7]> <html class="ie7 oldie"> <![endif]-->
<!--[if IE 8]> <html class="ie8 oldie"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="">
<!--<![endif]-->
<head>
<meta name="author" content="Kelsey Nealon(Kelseynealon#gmail.com), Contract Web Developer" />
<meta name="description" content="GBM Trailer Service Ltd. Calgary-based, proudly serving the tanker and bulk goods transportation industry for over 25 years." />
<meta name="keywords" content="Tanker, Barrel, Parts, Betts, Camloc, Scully, Lubecore, Dixon, GBM, Flotech" />
<title>GBM Trailer Service Ltd. ::: Service</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/mobilemenu.css"/>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="boilerplate.css" rel="stylesheet" type="text/css">
<link href="responsiveused.css" rel="stylesheet" type="text/css">
<link href="perfect-scrollbar.css" rel="stylesheet" type="text/css" />
<!--
To learn more about the conditional comments around the html tags at the top of the file:
paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/
Do the following if you're using your customized build of modernizr (http://www.modernizr.com/):
* insert the link to your js here
* remove the link below to the html5shiv
* add the "no-js" class to the html tags at the top
* you can also remove the link to respond.min.js if you included the MQ Polyfill in your modernizr build
-->
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- jQuery library (served from Google) -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!-- include Cycle2 -->
<script src="jquery.mousewheel.js"></script>
<script src="perfect-scrollbar.js"></script>
<script src="jquery.cycle2.min.js"></script>
<script>
$(document).ready(function(){
$(".nav-button").click(function () {
$(".nav-button,.primary-nav").toggleClass("open");
});
});
</script>
<script>
jQuery(document).ready(function ($) {
"use strict";
$('.Default').perfectScrollbar();
});
</script>
<script src="respond.min.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-45342007-3', 'gbmtrailer.ca');
ga('send', 'pageview');
</script>
</head>
<body bgcolor="#102540">
<div class="gridContainer clearfix">
<div id="div1" class="fluid">
<div id="navcontain"></div>
<div id="wrapper">
<div id="cover"></div>
<div id="mainnaviphone">
<img id="logo" src="images/gbmlogo.jpg" alt="G.B.M. Trailer Service LTD. Logo" title="G.B.M. Trailer Service LTD. Logo"/>
<img src="images/newunitsbutton.png" alt="New Units Button" title="New Units Button" id="buto1">
<img src="images/usedunitsbutton.png" alt="Used Units Button" title="Used Units Button" id="buto2">
<img src="images/homebutton.png" alt="home" title="Home" id="homebuto"/>
<img src="images/previousbutton.png" alt="previous" title="Previous" id="prevbuto"/>
<img src="images/nextbutton.png" alt="next" title="Next" id="nextbuto"/>
</div>
<div id="background2">
<div id="textcontrol">
<?php
while ($row = mysqli_fetch_assoc($rs_result)) {
?>
<div id='used1'>
<div id='rigtitle' class="contentHolder Default">
<? echo $row["title"]; ?>
</div>
<table class="infotabe1"><tr><td class="desctde">Description: </td><td rowspan="2" id="content_1" class="infotde contentHolder Default"><? echo $row["description"]; ?></td></tr></table>
<table class="infotabe"><tr><td class="desctd">Make: </td><td id="content_1" class="infotd contentHolder Default"><? echo $row["make"]; ?></td></tr></table>
<table class="infotabe"><tr><td class="desctd">Model: </td><td id="content_1" class="infotd contentHolder Default"><? echo $row["model"]; ?></td></tr></table>
<table class="infotabe"><tr><td class="desctd">Year: </td><td id="content_1" class="infotd contentHolder Default"><? echo $row["year"]; ?></td></tr></table>
<table class="infotabe"><tr><td class="desctd">Price: </td><td id="content_1" class="infotd contentHolder Default"><? echo $row["price"]; ?></td></tr></table>
<!-- Unit Picture -->
<img src='images/<? echo $row["photo"]; ?>' id='mainimage'>
<a href='#'><img src='images/picturesandspecsbutton.png' alt='Pictures and Specs' title='Pictures and Specs' id='picsandspecsbuto'></a>
</div>
<?php
};
?>
</div>
<div id="description" class="resize">
<p><u>Click</u> or call for more information or enquiries.</p>
</div>
<div id="bottomquote">
Your Certified Tanker Trailer Specialist
</div>
<div id="bottomcontactinfo">
<em>Phone <a style="color:white; text-decoration:none;" href="tel:1-4032799717">403 279 9717</a> or <a style="color:white; text-decoration:none;" href="tel:1-888-426-9717">1 888 426 9717</a> 9300 Endeavor Dr. SE, Calgary Alberta, T3S 0A1<br /><a style="color:white; text-decoration:none;" href="mailto:info#gbmtrailer.ca">info#gbmtrailer.ca</a>
</div>
<img src="images/visamastercardlogos.gif" id="visamastercardlogos" alt="Visa and Mastercard Logo" title="Visa and Mastercard Logo">
<img id="mycart" src="images/mycart.gif" alt="My Cart" title="My Cart">
</div>
</div>
</div>
</div>
<script>
<!--
var autoSizeText;
autoSizeText = function() {
var el, elements, _i, _len, _results;
elements = $('.resize');
console.log(elements);
if (elements.length < 0) {
return;
}
_results = [];
for (_i = 0, _len = elements.length; _i < _len; _i++) {
el = elements[_i];
_results.push((function(el) {
var resizeText, _results1;
resizeText = function() {
var elNewFontSize;
elNewFontSize = (parseInt($(el).css('font-size').slice(0, -2)) - 1) + 'px';
return $(el).css('font-size', elNewFontSize);
};
_results1 = [];
while (el.scrollHeight > el.offsetHeight) {
_results1.push(resizeText());
}
return _results1;
})(el));
}
return _results;
};
autoSizeText(); // here is where we call the function.
</script>
</body>
</html>
<?php
$sql = "SELECT COUNT(`orderid`) FROM `used_trailers`";
$rs_result = mysqli_query($connection, $sql);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 4);
$sqlef = 'SELECT * FROM `used_trailers`';
$rs_resultt = mysqli_query($connection, $sqlef);
$rowcount = mysqli_num_rows($rs_resultt);
$record_start = $start_from + 1;
$record_end = $record_start + 4;
// Remember to check against the max record
if ($record_end > $max_record) {
$record_end = $max_record;
}
echo "Showing ".$record_start." - ".$record_end." of ".$rowcount." results ";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='usedunits.php?page=".$i."'>".$i."</a> ";
};
?>
Any help is greatly appreciated. Thank you for any help.
Let's take a look at your code:
$start_from = ($page-1) * 4;
/*
Page $start_from What you want
1 0 Showing 1 - 4
2 4 Showing 5 - 8
3 8 Showing 9 - 12
etc.
*/
If you notice the pattern for what you want, the record will start at $start_from + 1. The record will end at that value plus 4.
So, you can get what you want with this:
$record_start = $start_from + 1;
$record_end = $record_start + 3;
// Remember to check against the max record
if ($record_end > $total_records) {
$record_end = $total_records;
}
The "1" would be:
$start_from + 1
The "4" would be:
Math.min($start_from + $rowcount, $total_records);
Math.min returns the smallest parameter passed to it. In this case, we want to display start_from + row count until that number is bigger than the total records.

Get userid in Popup

<head>
<link type='text/css' href='css/osx.css' rel='stylesheet' media='screen' />
<script type='text/javascript' src='js/jquery.js'></script>
<script type='text/javascript' src='js/jquery.js'></script>
<script type='text/javascript' src='js/jquery.simplemodal.js'></script>
<script type='text/javascript' src='js/osx.js'></script>
</head>
<?php
$myuser = 3;
$a = mysql_query('SELECT cmc.coursemoduleid, cma.sourcecmid, cmc.userid
FROM course_modules_completion, course_modules_availability cma
WHERE cmc.userid = '.$myuser.'');
echo '<table>
<tr><th>Course Module ID</th><
<th>Course Module ID</th>
</tr>';
foreach($a as $aa)
{
$mid = $aa->coursemoduleid;
$sid = $aa->sourcecmid;
$user = $aa->userid;
<tr><td>'.$user.'</td>
<td>'.$mid.'</td>
<td>'.$sid.'</td>
<td>
<div id="container">
<div id="content"><div id="osx-modal">View More
</div></div>
<div id="osx-modal-content">
<div class="close">X</div>
<div id="osx-modal-data">';
echo $user.'Welcome Here';
</div></div>
</td>
</tr>';
}
echo '</table>';
?>
I want to get $user value in popup (If suppose If the $myuser is 6 in a table then even i should get 6 inside the popup when I click viewmore). My popup was working exactly no errors in jquery just I need to pass $user value inside popup.
Right now, it displays only the first userid 9 in all popups.
Could anyone suggest me?
You need to give the user id to the popup.
<script type='text/javascript'>
function showUser(userID)
{
var userWindow = window.open(url, "User", "width=600,height=400");
}
</script>
<a href="#" class="osx" onclick="showUser(<?= $user ?>)">

Categories