php pass id into modal - php

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>
?>

Related

Display all records from list of array in php

I have a script which runs pretty well, it displays 2 records at a time. I want to actually display all the records from the array data. I am not very familiar with jquery and the script uses jquery.
Basically it works like this... the user submits a question on one page and then the question submitted displays on the a second page. You can try it here:
Submit questions: http://www.webcastviewer.com/post/submit.php
Lists questions: http://www.webcastviewer.com/post/list.php
On the "List" page: Currently it lists the two most recent questions. The most recent question is in blue/larger font at the top. The change I want is that I want it to list ALL questions with the most recent at the top (as it is now). Currently older questions disappear after two entries. I want it to show all submissions.
App.js (jquery code )
http://www.webcastviewer.com/post/js/app.js
Page code for displaying 2 records.
<?php
//read questions
$question_file_path = 'server/question.list';
if(!file_exists($question_file_path)){
$questions = array();
}
else {
$questions = json_decode(file_get_contents($question_file_path), true);
}
?>
<body>
<div class="p-3" id="page_list">
<?php
if(count($questions) > 0){
?>
<div class="questions-list">
<?php
}
else{
?>
<div class="questions-list questions--empty">
<?php
}
?>
<div class="alert alert-primary questions-alert">
<h1 id="question_new"><?php echo isset($questions[0]) ? $questions[0]: ' ' ?></h1>
</div>
<div class="alert alert-info questions-alert">
<h4 id="question_old"><?php echo isset($questions[1]) ? $questions[1] : ' ' ?></h4>
</div>
<div class="alert alert-danger questions-alert-empty">
<h1 id="question_new text-danger">There isn't question.</h1>
</div>
</div>
</div>
Please look at how you can do it.
I have improved some part of code and commented on them as well.
Note: Try to improve your mixing of PHP with HTML and avoid using it.
<div class="p-3" id="page_list">
//Added ternary operator instead of typical if else.
<div class="<?= count($questions) > 0 ? 'questions-list' : 'questions--empty' ?>">
<?php
//checking if has records else show message
if(count($questions) > 0){
?>
<?php foreach($questions as $question) ?>
<div class="alert alert-primary questions-alert">
<h1 id="question_new"><?php echo isset($question) ? $question: ' ' ?></h1>
</div>
<?php }
}else{ ?>
<div class="alert alert-danger questions-alert-empty">
<h1 id="question_new text-danger">There isn't question.</h1>
</div>
<?php } ?>
</div>
</div>

Loading data from database with pagination in PHP PDO

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

Get an id to another page

Hello this is my first post here, I need your help.
First of all, I want to say sorry for my bad English. I hope you will understand me :D
So, I'm a newbie in php and I have a problem.
I want to take the id from a file, to another file, but I don't know how. I will show you my code below.
while($array = $db->get_row($row))
{
$id = $array['id'];
$Nume = $array['user_name'];
$date = date("Y-m-d H:i:s", $array['date']);;
$stitle = $array['shorttext'];
$text = $array['text'];
$logs .= '
<div class="post">
<div class="post-meta clearfix">
<h4 class="post-title left">
'.$stitle.'
</h4>
<p class="post-info right">
<span>'.$Nume.'</span>
'.$date.'
</p>
</div>
<div class="post-box">
<div class="post-content">
<div class="post-intro">
'.$text.'
</div>
</div>
<div class="post-footer clearfix">
<div class="continue-reading">
</div>
</div>
</div>
</div>
<br>
';
}
Here where is ID, I want to take that id to the news page. (the code is in index.php)
'.$stitle.'
In news.php have to select from mysql all but only the table with the ID from index.php
if(isset($_GET['id']))
{
$id = (int)$_GET['id'];
$row = $db->sql("SELECT * FROM `ucp_news` WHERE `id` = '".$id."' ");
$array = $db->get_array($row);
Can you help me, please?
Your PHP code is correct, although you should be using htmlspecialchars() when adding the variables to the output. This to prevent XSS attacks, and the like.
Though, your problem lies most likely in this line:
<a href="/index.php?page=news id='.$id.'">
More specifically, the missing ampersand (&) between news and id. Try to look at the value of $_GET['page'] when clicking on the link, then you should see what the problem is.

while($query->fetch()) both working and not working

I'm working on a site which displays a blog and a photo gallery, which both get all their data from a MySQL-database using the same code (see below).
<?php
include_once("sql/db_connect.php");
$query1 = $db->prepare("SELECT post_id, title, body, img, timeposted FROM blogposts ORDER BY post_id DESC");
$query2 = $db->prepare("SELECT img_id, imgsrc FROM gallery ORDER BY img_id DESC");
$query1->execute();
$query2->execute();
$query1->bind_result($post_id, $title, $body, $img, $timeposted);
$query2->bind_result($img_id, $imgsrc);
?>
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<header>...</header>
<main>
<div class="container">
<div>
<!-- Choose between tabs -->
</div>
<div class="wrapper1">
<?php
while($query1->fetch()):
?>
<div class="w3-col s12 m6">
<div class="card w3-depth-1">
<div class="cardheader">
<?php echo $timeposted ?>
<span></span>
</div>
<div class="cardmain">
<div class="cardtext">
<h1><?php echo $title ?></h1>
<p><?php echo $body ?></p>
</div>
<?php
if($img !== "NOPE") {
echo "<img src='uploads/" . $img . "' class='blogimage' />";
}
?>
</div>
</div>
</div>
<?php endwhile?>
</div>
<div class="wrapper2">
<?php
while($query2->fetch()):
?>
<div class="w3-col s12 m6">
<?php echo "<img src='uploads/" . $imgsrc . "' class='galleryphoto' />"; ?>
</div>
<?php endwhile?>
</div>
</div>
</main>
<footer>...</footer>
</body>
The div "wrapper1" is used for the blog, "wrapper2" for the gallery and at the top of the page there is a button which lets you switch between views (using jQuery). So on default, wrapper1 is displayed, but on clicking the button it will switch to wrapper2.
Now, my problem: in wrapper1, all blogposts are displayed, but in wrapper2, none of the pictures stored in the database are displayed at all.
And, before you ask, yes, the table is actually called "gallery" and the values are actually called "img_id" and "imgsrc". I guess I've checked that about ten times already.
When I placed "echo 'Hello World';" before the second while loop, the page said Hello World, but when I placed it in the while loop, the page still didn't display anything; that said, it shouldn't be the jQuery, I guess.
I hope that anybody can point out what I'm doing wrong. Thanks in advance! If there's any additional data / code you need to have, I'd be happy to provide that.

"if message/text exists in database", then show the message that the user created from latest to oldest

I'm working on this message system. The user has a form presented to him, where the user will type notes, in this case called a "message". When the user visits his profile page, he is shown the message that he had typed in the form earlier and pressed saved.
Screenshot:
http://i.stack.imgur.com/VEsDg.png
My database has a table called "messages" where I have columns pid, uid, message, time, and picture where the uid is the uid in my "users" table.
So the pid is a auto-incrementing and the uid is the user id of the user who posted the message.
(The user posts a URL to his picture in the form which is later saved to the picture column in the database.
My PHP code where I get data from the database:
<?php
$uid = $this->session->userdata('uid');
$this->db->limit(10); //Use this to limit the entires while you show the entires on the front page.
$query = $this->db->query("SELECT pid, message, time, picture FROM messages WHERE uid = '$uid';");
foreach ($query->result_array() as $row)
{
$message = $row['message'];
$time = $row['time'];
$picture = $row['picture'];
}
?>
My html code in my profile_body.php page in my views folder where I have the above's code included.
<div class="list-group list-group-breakout">
<a class="list-group-item" href="https://assembly.com/assemblycoins">
<div class="chip">
<div class="chip-icon">
<img alt="Coins" width="48" height="48" class="app-icon" src="<?php echo $picture ?>"/>
</div>
<div class="row">
<div class="col-sm-12">
<p class="omega"><?php echo $message;?></p>
</div>
<div class="col-sm-12 right-align">
<p class="gray-2 small omega">
Feb. 21, 2015 <!-- Not from database -->
</p>
</div>
</div>
</div>
How would I get only 10 results for the messages that the users typed in the database and display it? I have tried to paste the same html code again but it displays the same message and picture.
Thanks.
Here is some improvements to do :
$uid = $this->session->userdata('uid');
$result = $this->db->select('pid, message, time, picture')
->where(array('uid' => $uid))
->limit(10)
->get('messages')
->result_array();
$this->load->view('profile_body', $result);
In your view profile_body.php file :
<?php foreach($result as $row): ?>
<div class="list-group list-group-breakout">
<a class="list-group-item" href="https://assembly.com/assemblycoins">
<div class="chip">
<div class="chip-icon">
<img alt="Coins" width="48" height="48" class="app-icon" src="<?php echo $row['picture'] ?>"/>
</div>
<div class="row">
<div class="col-sm-12">
<p class="omega"><?php echo $row['message'];?></p>
</div>
<div class="col-sm-12 right-align">
<p class="gray-2 small omega">
Feb. 21, 2015 <!-- Not from database -->
</p>
</div>
</div>
</div>
<?php endforeach; ?>

Categories