Loading data from database with pagination in PHP PDO - php

I have a website developed using PHP PDO programming. In this website there is one page where the details of holiday packages is listed from database along with pagination. My problem is only 6 records from database is loading in the page, but the pagination shows more than one page. Actually I am using PDO for the first time and I can't manage this problem.
Here is my code
index.class (all database functions are written here)
<?php
include_once "class/common.php";
class INDEX {
var $doAction;
var $msg;
var $objScr;
var $objCommonFunc; // This is common function object.
var $rsltInfo; // A common result set used by this class in various queries.
var $rsltOp; // A common result set used by this class in various queries.
var $arrSysVar; // Array that stores the System Variable those are used in screen.
// Other variables
var $adminEmail;
var $arrMetaContent; // For storing content and meta information.
var $leftMenuId; // Recid of 'menugroup' table for managing leftbar.
var $rightMenuId; // Recid of 'menugroup' table for managing rightbar.
var $menuPos;
function getHolydays($rgnId,$start_from, $record_per_page){
$qrySel = "SELECT h.recid recid, h.regionid regionid, h.name name, h.subtitle subtitle, h.txtDays txtDays, h.txtNights txtNights, h.pricingfrom pricingfrom, h.thumb_img thumb_img
FROM holydays h";
if($rgnId!=0){ $qrySel.= " WHERE h.regionid=".$rgnId; }
$qrySel .=" LIMIT $start_from,$record_per_page";
$objDbResult = $this->rsltInfo->getResult($qrySel);
return $objDbResult;
}
function getHolydaysPagntnCount($rgnId){
$qrySel = "SELECT h.recid recid, h.regionid regionid, h.name name, h.subtitle subtitle, h.txtDays txtDays, h.txtNights txtNights, h.pricingfrom pricingfrom, h.thumb_img thumb_img
FROM holydays h";
if($rgnId!=0){ $qrySel.= " WHERE h.regionid=".$rgnId; }
//echo $qrySel ;
$objDbResult = mysql_query($qrySel);
return $objDbResult;
}
}
$objScr = new INDEX();
?>
international-holidays.php
<?php $active_menu = 'Holidays';
include_once("index.class.php");
include_once 'header.php';
$objScr = new INDEX();
//$region_name="";
//if($_REQUEST['mcId'])$mcId=$_REQUEST['mcId'];
$rgnId=0;
if($_REQUEST['rgnId'])$rgnId=$_REQUEST['rgnId'];
if($_REQUEST['pgId'])
$pgId=$_REQUEST['pgId'];
$start_from=!empty($_REQUEST['page'])?$_REQUEST['page']:0;
//echo $start_from;
$record_per_page=6;
$rsltrgn = $objScr->getRegionname();
$rsltsdrgn = $objScr->getRegionname();
$rsltholyDetails = $objScr->getHolydays($rgnId,$start_from, $record_per_page);
$holydetailscount = mysql_numrows($objScr->getHolydayscount());
$holydaycount = mysql_numrows($objScr->getHolydaysPagntnCount($rgnId));
$total_pages = ceil($holydaycount/$record_per_page);
$rsltbannerDets= $objScr->getbannerImg($rgnId);
$rowbannerdts = $rsltbannerDets->fetchAssoc();
?>
<div class="col-md-8 col-sm-12 col-8">
<div class="right">
<div class="pagination-wrp clearfix">
<nav class="nav-pagination">
<?php //Here is the code for pagination
if($total_pages>0){?>
<ul class="pagination">
<?php if($pgId>1){?>
<li class="active"> <?php $prvpages=$start_from-6; $prev = ($pgId - 1); ?>
<a href="international-holidays-details.php?<?php echo "page=".$prvpages."&rgnId=".$rgnId."&pgId=".$prev; ?>" aria-label="Previous">
<img src="images/left-arrow.svg" alt=""></a></li>
<?php }?>
<?php
if($pgId!="" && $pgId!=1){ $spage=$pgId-1; }else{ $spage=1; $pgId=1; }
$maxPages=5;
$maxDisplayPage = $pgId + $maxPages - 1;
if($total_pages-$spage<5){ $spage=$total_pages-5; }
if($spage<=0){ $spage=1; }
for($i=$spage;$i<=min($maxDisplayPage, $total_pages);$i++){
if($i==1){ $st=0; }
else{
$st = ($i - 1) * $record_per_page;
}
?>
<li <?php if($i==$pgId){?> class="active" <?php }?>>
<a href="international-holidays-details.php?<?php echo "page=".$st."&rgnId=".$rgnId."&pgId=".$i; ?>" >
<?php echo $i; ?></a>
</li>
<?php
}
?>
<li class="active"><?php $nextpages=$start_from+6; if($pgId<$total_pages){ $next = ($pgId + 1); ?>
<a href="international-holidays-details.php?<?php echo "page=".$nextpages."&rgnId=".$rgnId."&pgId=".$next; ?>" aria-label="Next">
<img src="images/right-arrow.svg" alt=""> </a></li>
<?php }?></ul><?php }?>
</nav>
</div>
<?php
if($holydetailscount > 0) {
while($rowhly = $rsltholyDetails->fetchAssoc())
{
//$catgryname=str_split("Hello",3);
?>
<div class="col-sm-6 wow fadeInUp">
//Here the holiday packages as shown in boxes.
<a href="international-holidays-details.php?holyId=<?php echo $rowhly['recid']; ?>" class="offer-thumb">
<div class="offer-img"><img src="../sysimages/origimages/<?php echo $rowhly['thumb_img']; ?>" class="w-100" alt=""></div>
<div class="details clearfix">
<div class="title"><?php echo $rowhly['name']; ?><span><?php echo $rowhly['subtitle']; ?> </span></div>
<div class="price"><span>aed <?php echo $rowhly['pricingfrom']; ?></span>
<?php echo $rowhly['txtDays']; ?> days <?php echo $rowhly['txtNights']; ?> night</div>
</div>
</a>
</div>
<?php } }?>
</div>
</div>
The same code works perfectly in other page. The thing is the pagination shows 5 pages, but when i click on any page for example 2, it doesn't load the 2nd page rather it shows the first page itself.
I didn't understand what is happening. This code has been developed by someone else and my task is to do some updates on this.
Can anyone please help me.. I really needed to get this done.

What about if you use this pluggin? In my case it was very helpfull.
https://mottie.github.io/tablesorter/beta-testing/example-pager-custom-controls.html
Or you can just do it manually using mysql.
Pagination using MySQL LIMIT, OFFSET

Related

set active class for dynamically generated links in php

In my application there is a page where I generate dynamic links based on the data from mysql DB. My task is to set a class="active" for the selected link. If the links where static I can give active class using page name like this. But what to do in my case? Is there any way to get this done?
Here is my code:
<div class="select-region clearfix">
<input type="hidden" id="rgnId" name="rgnId">
<input type="hidden" id="hltypId" name="hltypId">
<ul class="nav">
<li>Select your region</li>
<li value="0"><a class="active" id="show_0" onClick="UAEHolydayDetails(0)">All<span class="badge"><?php echo $uaedetailscount?></span></a></li>
<?php
$i=0;
while($rowrgn = $rsltrgn->fetchAssoc())
{
?>
<li><a id="show_<?php echo $rowrgn['rgnId']; ?>" onClick="UAEHolydayDetails(<?php echo $rowrgn['rgnId']; ?>)"><?php echo $rowrgn['rgnName']; ?><span class="badge">
<?php echo $rowrgn['regioncount'];?></span></a></li>
<?php } ?>
</ul>
</div>
can anyone please help me
Edit 1
when I click any of these dynamic links it passes an id to the same page through js and the page will be filled with data from DB based on this paseed id like this:
onClick="UAEHolydayDetails(<?php echo $rowrgn['rgnId']; ?>)"
js:
<script>
function HolytypeDetails(id){
$('#hltypId').val(id);
window.location='uae-holidays.php?hltypId='+id;
}
</script>
in script the page in window.location is the page where the links are generated itself
Of course. It's the same thing. You need to know your active item id and based on this information just chose the item you want.
<?php
$activePageId = 5;
$i=0;
while($rowrgn = $rsltrgn->fetchAssoc())
{
?>
<li <?php if($rowrgn['rgnId'] === $activePageId){ ?>class="active"<?php } ?>><a id="show_<?php echo $rowrgn['rgnId']; ?>" onClick="UAEHolydayDetails(<?php echo $rowrgn['rgnId']; ?>)"><?php echo $rowrgn['rgnName']; ?><span class="badge">
<?php echo $rowrgn['regioncount'];?></span></a></li>
<?php } ?>
You may just use a selector to grab it via the href
Example with jquery:
$('nav a[href^="/' + location.pathname.split("/")[1] + '"]').addClass('active');

php pass id into modal

I'm stuck again! So hopefully you awesome people can help me...
So far what I have is a search function that queries the database, and returns results, which works fine, and then the user can click one of the results, which takes them through to a new detailed page of what they clicked, for example a brand. Then on the detailed page, it returns all of that brands details (using $_GET['id']) and a new set of data, from a relational table, with a list of all the offers corresponding to that brand, which also works. But then I get stuck, I want the user to be able to click on each offer, and have the details of that offer load into a modal... I've tried using the $_GET['id'] for the offers id, but obviously, with it being on the same page (i presume), it passes the id of the brand / parent instead of the offer.
This is the code that calls back all the offers relating to the brand
$offers_sql = 'SELECT * FROM codes WHERE client_id LIKE :client_id AND CURDATE() between start AND expiry';
$offersq = $db->prepare($offers_sql);
$offers_params = array(':client_id' => $id);
$offersq->execute($offers_params);
$results = $offersq->fetchAll(PDO::FETCH_ASSOC);
if(count($results) < 1) {
$output = "<h4>no current offers available</h4>";
}
else {
foreach($results as $r) {
$title = $r['title'];
$code = $r['textcode'];
$offer_id = $r['id'];
$expiry = $r['expiry'];
$date = new DateTime($expiry);
$output .= '<h4>' . $title . '</h4><hr>';
}
}
} catch (PDOException $e) {
echo "failed to load offers";
}
?>
and this is the code i was using for the modal
require('inc/connect/config.php');
include('inc/result.php');
$page_title = "Title - " . $name . " Offers";
include('inc/style.php');
include('inc/header.php');
include('inc/offers.php');
?>
<!-- Intro Section -->
<section class="intro-section">
<div class="container">
<div class="row">
<div class="col-lg-12">
<h3 class="navbar-fixed-top">Latest Deals</h3>
<h1><?php echo $name; ?></h1>
</div>
</div>
</div>
</section>
<section class="offer-section">
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-6 col-sm-offset-3 col-md-6 col-md-offset-3 col-lg-6 col-lg-offset-3">
<?php echo $output; ?>
</div>
</div>
</div>
</section>
<div class="md-modal md-effect-flip" id="offer">
<div class="md-content">
<h4><?php $offer = (int) $_GET['id'];
echo $offer ?></h4>
<button type="submit" class="modal-btn md-close navbar-fixed-bottom" value="submit"><p>close</p></button>
</div>
</div>
<?php include('inc/footer.php'); ?>
The code that returns the details of the brand is on a separate page, but i can include that if necessary.
What am i doing wrong? Please go easy on me, I am relatively new to php.
Thanks in advance
Kaylee
Make Changes, Where Output is declared in else condition. (Changes Done)
$output='<h4><a class="OfferIdClass" data-OfferID="<?echo $offer_id;?>" href="#offer" data-modal="offer" data-toggle="modal">'.$title.'</a></h4>';
// Add This to footer.php (Changes Done)
<div class="md-modal md-effect-flip" id="offer">
<div class="md-content">
</div>
</div>
//Add this below in that page, where you are clicking Modal.
//Changes Done
<script>
$('.OfferIdClass').click(function(){
var OfferID=$(this).attr('data-OfferID');
$.ajax({url:"Open-Offer.php?OfferID="+OfferID,cache:false,success:function(result){
$(".md-content").html(result); //Here, Changes Done
}});
});
</script>
//Create one Open-Offer.php page. (Changes Done)
Open-Offer.php
<?
extract($_GET);
<h4><?php $offer = (int) $_GET['id'];
echo $offer ?></h4>
<button type="submit" class="modal-btn md-close navbar-fixed-bottom" value="submit"><p>close</p></button>
?>

Session variables not working correctly

In my code i use a for each and for some reason it takes for ever to load and it screws up half way through with not displaying the name or it just does show up.
Code, http://hastebin.com/axutiqaqev.php (Not displaying correctly here)
<?php
$griditems = json_decode(file_get_contents($url));
foreach ($griditems->rgInventory as $index=>$items) {
$_SESSION['itemdesc'] = json_decode(file_get_contents($url));
$_SESSION['itemnamevalue'] = strval($items->classid) . "_" . strval($items->instanceid);
$_SESSION['itemname'] = $_SESSION['itemdesc']->rgDescriptions->$_SESSION['itemnamevalue']->market_name;
//echo $index; // will get index
?>
<div class="col-lg-2 col-sm-3 col-xs-4">
<a href="#">
<p><a class="hovertext" href="#" title="<?php echo $_SESSION['itemname']; ?>"><img src="https://steamcommunity-a.akamaihd.net/economy/image/class/730/<?= $items->classid ?>/150fx125f" class="thumbnail img-responsive"></a></p>
</a>
</div>
<?php
}
session_unset();
?>
Website: csgohighroller.com
Everything is working fine but it stops working after i use it once and when a player has too many items.
Example inventory http://www.csgohighroller.com/backpack.php?id=baconcraze

how to retrieve data from mysql database and populate in list view in jquery mobile

please someone help me to find the solution for how to retrieve data from mysql database and populate in list view in jquery mobile.
am having the php code as follows
<? php
include('libraries/config.php');
$result = mysql_query("SELECT * from category") or die(mysql_error());
while ($obj = mysql_fetch_object($result)) {
$arr[] = $obj;
}
echo json_encode($data);
echo '{"Deals":'.json_encode($arr).'}';
?>
here am getting the data from mysql in json format but i was not known how to populate this in listview, my html page is as follows
<div id="content-area" style="height:auto;">
<br/>
<ul data-role="listview">
<li>
<a href="comfort_list.html">
<div class="content-home-tab1">
<div class="img-content">
<img id="ic_home" src="images/next_btn.png" style="margin-top:37px; margin-left:448px;" width="22" height="28" />
</div>
<div class="content-home-p">
<b>Comfort</b>
</div>
</div>
</a>
</li>
<li>
<a href="#">
<div class="content-home-tab1">
<div class="img-content">
<img id="ic_home" src="images/next_btn.png" style="margin-top:37px; margin-left:448px;" width="22" height="28" />
</div>
<div class="content-home-p">
<b>Handling Your Lenses</b>
</div>
</div>
</a>
</li>
</ul>
</div>
Here in my code am giving static data to list that is comfort and handling your lens, am having this data in db and i need to get that and to be placed here.
in the page i am posting in a form like this
<form method="post" action="help.html">
<ul data-role="listview" >
<?php while ($row = $stmt->fetch()){
?>
<li>
<a href="help1.php?id=<?php echo $row['categoryID']; ?>">
<div class="content-home-tab1">
<div class="img-content">
<img id="ic_home" src="images/next_btn.png" style="margin-top:37px; margin-left:448px;" width="22" height="28" />
</div>
<div class="content-home-p">
<b><?php echo $row['title'];?>
</b>
</div></div></a>
</li>
<?php }?>
</ul>
</form>
and in another page am trying to get the id as follows
<?php
include('libraries/config.php');
$getID = $_GET['id'];
echo $getID;
$stmt = $db->prepare("SELECT * from category where categoryID ='$_GET['id']'");
$stmt->execute();
?>
but when going to that page it is showing nothing and when refreshed then it is showing so that only am asking how to get it to fetch data and print it when the page is loaded. thanks.
As C.S. says, you don't need to use JSON, just loop through your array and add your list items.
Edited to show basic use of PDO, and demo how to access query results.
The mysql_query extension is deprecated as of PHP 5.5.0, so you I would suggest you use PDO_MySQL
First set up your database connection:
$dbName = "your_database_name_here";
$dbUserName = "your_username_here";
$dbPassword = "your_password_here";
$db = new PDO( 'mysql:host=localhost;dbname='.$dbName, $dbUserName, $dbPassword );
Then run your query, and echo your list (or whatever you need from the database by accessing the $row array within the while loop):
$stmt = $db->prepare("SELECT * from category");
$stmt->execute();
while ($row = $stmt->fetch()){
echo "<li>". $row['title'] ."</li>";
}
p.s. you should get rid of that inline css, it's a bad habit to get into.

Echoing forum Id to new page to display content?

can someone please help me i am having problems creating my forum.
At the moment users can create posts, the post title is listed down the page and then the user is suppose to be able to click the title link and be taken to read_post.php and then this should take the user to another page where the post content can be viewed, i am trying to do this by echoing the forum post id but it doesnt seem to want to work, instead i get this error:
Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
can someonee please show me where im going wrong.
here is my sql function:
function read_forum() {
global $connection;
global $forum_id;
$query = "SELECT *
FROM ptb_forum, ptb_profiles
WHERE ptb_forum.id = $forum_id ";
$forum_set = mysql_query($query, $connection);
confirm_query($forum_set);
return $forum_set;
}
here is the link code that takes the user to read_post.php which suppose to echo the forum id and display the content for each individual post.
<?
$forum_set = get_forum();
while ($forum = mysql_fetch_array($forum_set)) {
?>
<div class="forumcase" id="forumcase">
<div class="pend-forum-content">
<?php echo "<strong>{$forum['title']}</strong> - Posted by {$user['first_name']}"; ?>
</div>
here's my code for read_post.php:
<?php
$page_title = "Read Post";
include('includes/header.php');
include ('includes/mod_login/login_form2.php'); ?>
<?php
confirm_logged_in();
if (isset ($_GET['frm'])) {
$forum_id = $_GET['frm'];
}
?>
<?php include('includes/copyrightbar.php'); ?>
<div class="modtitle">
<div class="modtitle-text">Messages Between <?php echo "{$forum['display_name']}"; ?> & You</div>
</div>
<div class="modcontent57">
<br /><br /><br/><br/>
<div class="forum">
<div class="forum-pic"><?php echo "<img src=\"data/photos/{$_SESSION['user_id']}/_default.jpg\" width=\"100\" height=\"100\" border=\"0\" align=\"right\" class=\"img-with-border-forum\" />";?>
</div>
<div class="message-links">
<strong><< Back to Forum
</div>
<br /><br /><br/><br/>
<?php
$datesent1 = $inbox['date_sent']; ?>
<?php
$forum_set = read_forum();
while ($forum = mysql_fetch_array($forum_set)) {
$prof_photo = "data/photos/{$message['user_id']}/_default.jpg";
$result = mysql_query("UPDATE ptb_forum SET ptb_forum.read_forum='1' WHERE ptb_forum.id='$forum_id'")
or die(mysql_error());
?>
<div class="message-date">
<?php echo "".date('D M jS, Y - g:ia', strtotime($message['date_sent'])).""; ?></div>
<div class="img-with-border-msg-read"><?php echo "<img width=\"60px\" height=\"60px\" src=\"{$prof_photo}\"><br />"; ?></div>
<div class="conversation-text">
<?php echo "<i>Conversations between you and </i>{$forum['display_name']}.<br /> "; ?></div>
<div class="message-content">
<?php echo "<strong>Message Subject: </strong><i>{$forum['subject']}</i>"; ?>
<br/>
<br/>
<br/>
<br/>
<?php echo "<strong>Message:<br/></strong></br ><i>{$forum['content']}</i>"; ?>
</div>
<div class="reply-box">
<? include ('message_reply.php'); ?>
</div>
<?php
}
?>
<br/>
<br/>
<br/>
</div>
</div>
<?php include('includes/footer.php'); ?>
</div>
You have an error in your query... Your parameter is not quoted...
$query = "SELECT *
FROM ptb_forum, ptb_profiles
WHERE ptb_forum.id = '$forum_id'";
However... I suggest that you refrain from using the mysql_ family of functions. They are deprecated and due to be removed from PHP in a future release. You should be using parameterized queries using MySQLi or PDO.
Also, global is evil. I've never had a need to use it in 10 years of PHP programming. Neither should you.

Categories