How to apply sorting with paging? - php

I tried applying sorting with paging but it is only sorting first page because the page number is one which is fine by me but the records of second and third page are not sorted.
Here is the FIRST PAGE CODE
include_once("session.php");
include_once("functions.php");
include_once("connection.php");
$username = $_SESSION["username"];
$orderby = "job_name";
$order = "asc";
//$conn2 = mysqli_connect("localhost","root","","jobportal");
//$conn3 = mysqli_connect("localhost","root","","jobportal");
//$conn4 = mysqli_connect("localhost","root","","jobportal");
$sql3 = "SELECT COUNT(*) FROM job_info WHERE username='$username' AND approved=1";
$sql4 = "SELECT COUNT(*) FROM job_info WHERE username='$username' AND approved=0";
$stmt3 = $conn->query($sql3);
$stmt4 = $conn->query($sql4);
$approved = $stmt3->fetch_array();
$pending = $stmt4->fetch_array();
$total_rows_approved = $approved[0];
$total_rows_pending = $pending[0];
$limit = 2;
$total_pages_apporved = ceil($total_rows_approved/$limit);
$total_pages_pending = ceil($total_rows_pending/$limit);
$page_approved = 0;
$page_pending = 0;
if(isset($_GET["page_approved"])){
$page_approved = $_GET["page_approved"];
}
if($page_approved){
$start_approved = ($page_approved-1)*$limit;
}
else{
$start_approved = 0;
}
if(isset($_GET["page_pending"])){
$page_pending = $_GET["page_pending"];
}
if($page_pending){
$start_pending = ($page_pending-1)*$limit;
}
else{
$start_pending = 0;
}
$sql1 = "select * from job_info where username='$username' and approved=1 order by ".$orderby." ".$order." limit $start_approved,$limit";
$stmt1 = $conn->query($sql1);
$sql2 = "select * from job_info where username='$username' and approved=0 order by ".$orderby." ".$order." limit $start_pending,$limit";
$stmt2 = $conn->query($sql2);
<script>
var page_no_approved=1;
var page_no_pending=1;
function order_column1(column_name,column_order){
$.ajax({
url: "get_job_list.php",
data: "orderby1="+column_name+"&order1="+column_order+"&page_no_approved="+page_no_approved,
type: "POST",
success: function(data){
$("#showresults").html(data);
}
});
}
function order_column2(column_name,column_order){
$.ajax({
url: "get_job_list.php",
data: "orderby2="+column_name+"&order2="+column_order+"&page_no_pending="+page_no_pending,
type: "POST",
success: function(data){
$("#showresults").html(data);
}
});
}
</script>
and here is the SECOND PAGE CODE(ajax)
include_once("session.php");
include_once("functions.php");
include_once("connection.php");
$username = $_SESSION["username"];
$sql3 = "SELECT COUNT(*) FROM job_info WHERE username='$username' AND approved=1";
$sql4 = "SELECT COUNT(*) FROM job_info WHERE username='$username' AND approved=0";
$stmt3 = $conn->query($sql3);
$stmt4 = $conn->query($sql4);
$approved = $stmt3->fetch_array();
$pending = $stmt4->fetch_array();
$total_rows_approved = $approved[0];
$total_rows_pending = $pending[0];
$limit = 2;
$total_pages_apporved = ceil($total_rows_approved/$limit);
$total_pages_pending = ceil($total_rows_pending/$limit);
$page_approved = 0;
$page_pending = 0;
if(isset($_POST["page_no_approved"])){
$page_approved = $_POST["page_no_approved"];
}
if($page_approved){
$start_approved = ($page_approved-1)*$limit;
}
else{
$start_approved = 0;
}
if(isset($_POST["page_no_pending"])){
$page_pending = $_POST["page_no_pending"];
}
if($page_pending){
$start_pending = ($page_pending-1)*$limit;
}
else{
$start_pending = 0;
}
$orderby1 = "job_name";
$order1 = "asc";
$orderby2 = "job_name";
$order2 = "asc";
if(!empty($_POST["orderby1"])){
$orderby1 = $_POST["orderby1"];
}
if(!empty($_POST["order1"])){
$order1 = $_POST["order1"];
}
if(!empty($_POST["orderby2"])){
$orderby2 = $_POST["orderby2"];
}
if(!empty($_POST["order2"])){
$order2 = $_POST["order2"];
}
$date_order1 = "asc";
$job_name_order1 = "asc";
$industry_order1 = "asc";
$date_order2 = "asc";
$job_name_order2 = "asc";
$industry_order2 = "asc";
if($orderby1 == "date" && $order1=="asc"){
$date_order1 = "desc";
}
if($orderby1 == "job_name" && $order1=="asc"){
$job_name_order1 = "desc";
}
if($orderby1 == "industry" && $order1 == "asc"){
$industry_order1 = "desc";
}
if($orderby2 == "date" && $order2=="asc"){
$date_order2 = "desc";
}
if($orderby2 == "job_name" && $order2=="asc"){
$job_name_order2 = "desc";
}
if($orderby2 == "industry" && $order2 == "asc"){
$industry_order2 = "desc";
}
$sql1 = "select * from job_info where username='$username' and approved=1 order by ".$orderby1." ".$order1." limit $start_approved,$limit";
$result1 = $conn->query($sql1);
$sql2 = "select * from job_info where username='$username' and approved=0 order by ".$orderby2." ".$order2." limit $start_pending,$limit";
$result2 = $conn->query($sql2);
EDIT: I didn't included the code to display data because it will make the already large code to be more longer and possibly unreadable. It would help if someone could point me to the place where the concept of sorting with pagination is applied or if someone could explain the concept with small example.

Related

How to use three conditions with AND operator in while loop in PHP

How to use three conditions with AND operator in while loop in PHP
Here is my code:
$query = "select * from agent_profile where status = 1";
$run = mysqli_query($con, $query);
$pricequery = "select * from airport_parking where airport_name = '$airport' AND '$parking_start_time' >= start_time ";
$resultprice = mysqli_query($con, $pricequery);
$squery = "select * from agent_services where agent_id = '$crnt_id' AND service_type = '$service_type' ";
$srun = mysqli_query($con, $squery);
if ($run && $resultprice && $squery) {
while (($datarow = mysqli_fetch_array($run)) AND ($getdata = mysqli_fetch_array($resultprice)) AND ($sdata = mysqli_fetch_array($srun))) {
}
Try This
$query = "select * from agent_profile where status=1
UNION
select * from airport_parking where airport_name = '$airport' AND '$parking_start_time' >= start_time
UNION
select * from agent_services where agent_id = '$crnt_id' AND service_type='$service_type';";
$run = mysqli_query($con,$query);
if(strlen($run) <> ''){
while (($datarow = mysqli_fetch_array($run)))
{
$column_name = $datarow['column_name'];
}
}

PHP SQL Result stored as var in new sql query

So its a php game etc...
This is the main part of my script;
$sql = "SELECT * from search1 WHERE username='Hitachi'";
$result = mysqli_query($connection, $sql);
while ($rows = mysqli_fetch_array($result)) {
$timeleft = $rows['time'];
$target55 = $rows['target'];
$id = $rows['id'];
$status = $rows['status'];
$hours = $rows['hours'];
$started = $rows['started'];
$deletetime = $started - 86400;
mysqli_query($connection, "DELETE FROM search WHERE started<'$deletetime'");
$starttime = $started;
$time1 = $starttime + 10800;
$a_query = mysqli_query($connection, "SELECT * FROM BG WHERE username='$target55' AND status='Ready'");
$check_found = mysqli_num_rows($a_query);
if ($check_found >= 1) {
$bg = mysqli_query($connection, "SELECT * FROM BG WHERE status='Ready' AND username='$target55' ORDER by slot DESC LIMIT 1");
while ($i = mysqli_fetch_object($bg)) {
$bg1 = $i->bodyguard;
}
echo "This user has a bodyguard called <b>$bg1</b>!";
if ($time1 < time() && $hours >= 3 || $userlevel >= 14) {
$sql2 = "SELECT * from users WHERE username='$target55'";
$result2 = mysqli_query($connection, $sql2);
while ($rows2 = mysqli_fetch_array($result2)) {
$target_country = $rows2['country'];
}
mysqli_query($connection, "UPDATE search1 SET status='2', location='$target_country', found='$target_country' WHERE id='$id'");
}
mysqli_query($connection, "DELETE FROM BG WHERE username='$target55'");
This does nothing at all regardless if there is a bodyguard or not.
If i remove the check for the bodyguard etc then it works however i need it to check?

delete not working properly in while loop

here i am trying to delete from detail table here is one problem with my code if detail table have three records than only delete one record from detail table.and also it does not effect other two records in stock table..
Only one first record this code work properly after that it does not work in for delete query from detail table and not effected in stock table...
<?php
include("include/config.inc.php");
$purchaseMasterId = isset($_REQUEST['purchaseMasterId']) ? $_REQUEST['purchaseMasterId'] : 0;
if($purchaseMasterId > 0) {
$k = 0;
$selectMaster = "SELECT purchasedetail.purchaseMasterId, colorId,
totalkg, purchaseDetailId, partyId
FROM purchasedetail
JOIN purchasemaster ON
purchaseMaster.purchaseMasterId = purchasedetail.purchaseMasterId
WHERE purchasedetail.purchaseMasterId = ".$_REQUEST['purchaseMasterId'];
$selectMasterRes = mysql_query($selectMaster);
while($purRow = mysql_fetch_array($selectMasterRes)) {
$purchaseDetailId = $purRow['purchaseDetailId'];
$purchaseMasterId = $purRow['purchaseMasterId'];
$colorId = $purRow['colorId'];
$totalkg = $purRow['totalkg'];
$partyId = $purRow['partyId'];
$select = "SELECT qty
FROM stock
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$selectRes = mysql_query($select);
if($stockRow = mysql_fetch_array($selectRes)) {
$current = $stockRow['qty'];
}
$updateStock = "UPDATE stock
SET qty = ".$current." - ".$totalkg."
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$updateStockRes = mysql_query($updateStock) or die(mysql_error());
if($updateStockRes) {
$deleteDt = "DELETE FROM purchasedetail
WHERE purchaseDetailId = ".$purchaseDetailId;
$deleteRes = mysql_query($deleteDt);
if($deleteRes){
$deleteMst = "DELETE FROM purchasemaster
WHERE purchaseMasterId = ".$_REQUEST['purchaseMasterId'];
$deleteMstRes = mysql_query($deleteMst) or die(mysql_error());
if(!$deleteMstRes) {
echo "Purchase Master Delete Fail";
} else {
header("Location:purdetail.php");
exit();
}
}
}
}
}
?>
Here is my updated code with perfect code working
<?php
include("include/config.inc.php");
$purchaseMasterId = isset($_REQUEST['purchaseMasterId']) ? $_REQUEST['purchaseMasterId'] : 0;
if($purchaseMasterId > 0)
{
$k = 0;
$selectMaster = "SELECT purchasedetail.purchaseMasterId,colorId,totalkg,purchaseDetailId,partyId
FROM purchasedetail
JOIN purchasemaster ON purchasemaster.purchaseMasterId = purchasedetail.purchaseMasterId
WHERE purchasedetail.purchaseMasterId = ".$_REQUEST['purchaseMasterId'];
$selectMasterRes = mysql_query($selectMaster) or die(mysql_error());
while($purRow = mysql_fetch_array($selectMasterRes))
{
$purchaseDetailId = $purRow['purchaseDetailId'];
$purchaseMasterId = $purRow['purchaseMasterId'];
$colorId = $purRow['colorId'];
$totalkg = $purRow['totalkg'];
$partyId = $purRow['partyId'];
$select = "SELECT qty
FROM stock
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$selectRes = mysql_query($select);
if($stockRow = mysql_fetch_array($selectRes))
{
$current = $stockRow['qty'];
}
$updateStock = "UPDATE stock
SET qty = ".$current." - ".$totalkg."
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$updateStockRes = mysql_query($updateStock) or die(mysql_error());
if($updateStockRes)
{
$deleteDt = "DELETE FROM purchasedetail
WHERE purchaseDetailId = ".$purchaseDetailId;
$deleteRes = mysql_query($deleteDt);
if($deleteRes)
{
$selectid = "SELECT purchaseDetailId
FROM purchasedetail
WHERE purchaseMasterId = ".$purchaseMasterId;
$selectidRes = mysql_query($selectid);
if(mysql_affected_rows() == 0)
{
$mst = "DELETE FROM purchasemaster
WHERE purchaseMasterId = ".$purchaseMasterId;
$mstRes = mysql_query($mst) or die(mysql_error());;
if($mstRes)
{
header("Location:purdetail.php");
exit();
}
else
{
}
}
}
}
}
}
?>

php home posts system using Mysql

This is my code to get there posts on their profile
<?php
$getposts = mysql_query("SELECT * FROM videos_content WHERE username ='$username' ORDER BY id DESC LIMIT 10") or die(mysql_error());
while ($row = mysql_fetch_assoc($getposts)) {
$id = $row['id'];
$video_title = $row['video_title'];
$video_url = $row['video_url'];
$date_posted = $row['date_posted'];
$added_by = $row['username'];
$get_user_info = mysql_query("SELECT * FROM users WHERE username='$added_by'");
$get_info = mysql_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./image/default_pic.jpg";
}
else
{
$profilepic_info = "./userdata/profile_pics/".$profilepic_info;
}
?>
My sql user table is like this
[id,username,followers,following,account_active]
-> My following and followers columns are arrays with comma separated values.
My sql posts table is like this
[id,username,video_title,video_url,date_posted,removed]
I want to display posts of people whom a user(i mean a logged in user) is following on his/her home page.
can anyone suggest me some solution ?
Thanks.
I tried this code
<?php
$followingArray = "";
$countFollowing = "";
$followingArray12 = "";
$selectFollowingQuery = mysql_query("SELECT following FROM users WHERE username = '$user' ");
$followingRow = mysql_fetch_assoc($selectFollowingQuery);
$followingArray = $followingRow['following'];
if ($followingArray != "") {
$followingArray = explode(",","$followingArray");
$countFollowing = count($followingArray);
$followingArray12 = array_slice($followingArray, 0, 12);
$getposts = mysql_query("SELECT * FROM videos_content WHERE username ='$username' ORDER BY id DESC LIMIT 10") or die(mysql_error());
while ($row = mysql_fetch_assoc($getposts)) {
$id = $row['id'];
$video_title = $row['video_title'];
$video_url = $row['video_url'];
$date_posted = $row['date_posted'];
$added_by = $row['username'];
$get_user_info = mysql_query("SELECT * FROM users WHERE username='$added_by'");
$get_info = mysql_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./image/default_pic.jpg";
}
else
{
$profilepic_info = "./userdata/profile_pics/".$profilepic_info;
}
}
if (in_array($username, $followingArray) {
echo '(posts)'; /*html to echo those posts, its a very long chain of divs, that is the reason i didn't include that code.*/
}
else{
echo ''; /*nothing*/
}
}
?>
Firstly I would suggest splitting your users table into 3 tables.
users : [id,username,account_active]
followers : [id,followerId]
following : [id,followingId]
This will prevent columns with comma-separated values, and make your problems much simpler.
From there its easy:
SELECT *
FROM posts
WHERE id IN (
SELECT followerId
FROM followers
WHERE id = $userID
);
etc..

PHP, using Joomla, algorithm, networking if else,

Hi, I have a question, when i ever insert this code
if ($pointsreach>=$tempunilevel ){
mysql_query("UPDATE `incentives_table` SET pointsreach = pointsreach +500 where userid = '$newsponsorid' ");
the if statement does not work anymore
if (count($items) >0 && $lev <= 10 ){ // found get sponsor id for the next computation
$sponsorid = $items->upline; //sponsor id
but when i remove the
if ($pointsreach>=$tempunilevel ){
mysql_query("UPDATE `incentives_table` SET pointsreach = pointsreach +500 where userid = '$newsponsorid' ");
the statement works perfectly ...
$query = " UPDATE `#__eds_incentives_table` SET `temp` = `temp` + 25 where `userid` = '$sponsorid' ";
$db->setQuery($query);
$db->query();
$mote = $this->entryuni($sponsorid, $lev + 1, +25);
}
else {
return 'ok';
}
This is the code
public function entryuni($newsponsorid = null, $lev = 2, $fpv = 0)
{
$db = & JFactory::getDBO();
$query = "SELECT upline,fslot FROM `table` where userid = '$newsponsorid' ";
$db->setQuery($query);
$items = $db->loadObject();
$items = (!empty($items)) ? $items : array();
$queryreach = mysql_query("SELECT * FROM incentives_table WHERE userid = '$newsponsorid' ");
$fetchreach = mysql_fetch_array($queryreach);
$pointsreach=$fetchreach['pointsreach'];
$tempunilevel=$fetchreach['temp_unilevel'];
if ($pointsreach>=$tempunilevel ){
mysql_query("UPDATE `incentives_table` SET pointsreach = pointsreach +500 where userid = '$newsponsorid' ");
if (count($items) >0 && $lev <= 10 ){ // found get sponsor id for the next computation
$sponsorid = $items->upline; //sponsor id
$query = " UPDATE `#__eds_incentives_table` SET `temp` = `temp` + 25 where `userid` = '$sponsorid' ";
$db->setQuery($query);
$db->query();
$mote = $this->entryuni($sponsorid, $lev + 1, +25);
}
else {
return 'ok';
}

Categories