PHP echo display outside the div - php

My project is to display a calendar with the number of the day on top of a "card" from the bootstrap library.
Under the number of the day I display the name of the day, what I want is just to echo a number or character on the right of the name of the day which come from a function.
The function return a value from a SQL query.
Screenshots:
The php with html
My function in php
My app in chrome
The code:
<?php
session_start();
include 'Cadre.php';
$date = new DateTime('2018-01-01');
$idEleve = 1;
$idClasse = 1;
try
{
$bdd = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', '');
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
function devoir($result, $idClasse, $idEleve, $bdd) {
try
{
$req = $bdd->prepare("SELECT `idMatiere` FROM devoir WHERE `idEleve` = :idEleve AND `dateDevoir` = :date_devoir OR `idClasse` = :id_classe AND `dateDevoir` = :date_devoir");
$req->execute(array(
':id_classe' => $idClasse,
':idEleve' => $idEleve,
':date_devoir' => $result));
while($devoir = $req->fetch()){
if($devoir['idMatiere']== 1){
echo $devoir['idMatiere'];
};
}
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
}
?>
<br>
<h1 class="display-4">Agenda</h1><p class="lead"> <?php echo $date->format('F'); ?></p>
<br>
<?php
echo '
<div class="row">
<div class="col-1"></div> ';
while(date_format($date, 'd') <= '30'){
if($date->format('l') != 'Saturday' AND $date->format('l') != 'Sunday'){
$result = $date->format('Y-m-d');
echo '
<div class="col-sm-2"><a href="https://google.com">
<div class="card">
<div class="card-body">
<h3 class="card-title">'.date_format($date, 'd').'</h3>
<h6 class="card-subtitle mb-2 text-muted">'. $date->format('l').devoir($result, $idClasse, $idEleve, $bdd).'</h6>
</div>
</div>
</a></div>';
}
else{
echo '</div><br>';
echo '
<div class="row">
<div class="col-1"></div>';
}
$date->modify('+1 day');
}
echo '</div>
<br>';
?>
<br>
<div class="row">
<div class="col-1"></div>
<div class="col-4">
<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item"><a class="page-link" href="#">Mois précédent</a></li>
<li class="page-item"><a class="page-link" href="#">Mois courrant</a></li>
<li class="page-item"><a class="page-link" href="#">Mois suivant</a></li>
</ul>
</nav>
</div>
<div class="col-7"></div>
</div>
</div>
</body>
Thanks a lot for taking the time to read and sorry for my english <3

remove the h6 element and replace it with span inside h3
since the heading elements like H3 , H6 display as block .
<div class="card-body">
<h3 class="card-title">'.date_format($date, 'd').' <span> class="card-subtitle mb-2 text-muted">'. $date->f
ormat('l'). devoir($result, $idClasse, $idEleve, $bdd).'</span>
</h3>
</div>

Related

Holding PHP/AJAX filter selections during pagination

I have a page which allows you to filter results using an AJAX call which works fine, I have added pagination which work fine initially but as soon as you move to another page, the checkbox becomes unchecked and it just shows all results again. I assume this is because the page is reloading when it moves to page 2, is there a way of keep the filter setting set and continue to show the results from the filter AJAX. The pagination obvisouly works fine when no filter is selected but my brain just doesn't seem to be working and can't work this out.
Any help would be appreciated!
My code is below, I am also aware that currently my code is open to sql injection but just trying to get everything to work and then will go back through it:
<body>
<?php include("PHP/header.php"); ?>
<div class="container-fluid">
<div class="container" style="margin-top: 2%; text-align: center;">
<h1> Reviews</h1>
On This page you will find our reviews on music tech and software
<br/>
<br/>
<br/>
Filter Reviews:
<ul class="list-group">
<?php
$search = $conn->prepare("SELECT DISTINCT reviewcat FROM review_db ORDER BY reviewcat");
$search->execute();
while ($row = $search->fetch(PDO::FETCH_ASSOC)) {
?>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="<?=$row['reviewcat'];?>" id="reviewcat"> <?=$row['reviewcat']; ?>
</label>
</div>
</li>
<?php } ?>
</ul>
</div>
<br/><br/>
<div class="row-fluid ">
<h5 class="text-center" id="textChange"> All Reviews </h5>
<hr>
<div class="text-center">
<img src="Images/loader.gif" id="loader" width="100" style="display: none">
</div>
<?php
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$no_of_records_per_page = 8;
$offset = ($pageno-1) * $no_of_records_per_page;
// Prev + Next
$prev = $pageno - 1;
$next = $pageno + 1;
?>
<div id="result" class="card-deck card_group_style pt-4" >
<?php
$stmt = $conn->prepare("SELECT COUNT(*) FROM review_db");
$stmt->execute();
$total_rows = $stmt->fetchColumn();
$total_pages = ceil($total_rows / $no_of_records_per_page);
$result = $conn->prepare("SELECT * FROM review_db ORDER BY reviewsub DESC LIMIT $offset, $no_of_records_per_page ");
$result->execute();
?>
<?php while ($row = $result->fetch(PDO::FETCH_ASSOC)) {// Important line !!! Check summary get row on array .. ?>
<?php
$my_date = $row['reviewsub'];
$date = DATE("d/m/Y",strtotime($my_date));
?>
<div class="col-sm-6 col-lg-3 py-2">
<div class="card mb-4">
<img class="card-img-top card-images " src="Images/Reviews/<?php echo $row['reviewimage1'];?>" alt="<?php echo $row['reviewtitle'];?>" >
<div class="card-body">
<h5 class="card-title"><?php echo $row['reviewtitle'];?></h5>
<p class="card-text"><?php echo $row['reviewsynop'];?></p>
<a href="Reviews/review-content.php?id=<?php echo $row['id'];?>&reviewtitle=<?php echo $row['reviewtitle'];?>" class="btn btn-primary my-4" >Read More</a>
<div class="card-footer" style="padding: 1%;">
<small class="text-muted">Submitted: <?php echo $date; ?></small>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
<div class="container">
<!-- Pagination Controller -->
<?php
if($total_pages <= 1){
$hidepage = 'none';
}else{
$hidepage = 'flex';
}
?>
<ul class="pagination justify-content-center pagination-mb" style="display: <?php echo $hidepage; ?>">
<li><a class="page-link" href="?pageno=1">First</a></li>
<li class="page-item <?php if($pageno <= 1){ echo 'disabled'; } ?>">
<a class="page-link" href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".($pageno - 1); } ?>">Prev</a>
</li>
<?php for($i = 1; $i <= $total_pages; $i++ ): ?>
<li class="page-item <?php if($pageno == $i) {echo 'active'; } ?>">
<a class="page-link" href="?pageno=<?= $i; ?>"> <?= $i; ?> </a>
</li>
<?php endfor; ?>
<li class="page-item <?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
<a class="page-link" href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?pageno=".($pageno + 1); } ?>">Next</a>
</li>
<li><a class="page-link" href="?pageno=<?php echo $total_pages; ?>">Last</a></li>
</ul>
<!-- Pagination end -->
</div>
</div>
</div>
</div>
<?php include("PHP/footer.php"); ?>
</div>
</body>
<?php include("PHP/js.php"); ?>
<script>
$(document).ready(function(){
$('#link-review,#link-footer-review').addClass('active');
});
</script>
<script type="text/javascript">
$(document).ready(function(){
function get_filter_text(text_id){
var filterData = [];
$('#'+text_id+':checked').each(function(){
filterData.push($(this).val());
});
return filterData;
}
$(".product_check").click(function(){
if ($(this).prop('checked')) {
$("#loader").show();
var action = 'data';
var reviewcat = get_filter_text('reviewcat');
$.ajax({
method:'POST',
url:'reviewaction.php',
data:{action:action,reviewcat:reviewcat},
success:function(response){
$("#result").html(response);
$("#loader").hide();
$("#textChange").text("Filtered Reviews");
}
});
} else {
$("#loader").show();
var action = 'data';
var reviewcat = get_filter_text('reviewcat');
$.ajax({
method:'POST',
url:'reviewaction.php',
data:{action:action,reviewcat:reviewcat},
success:function(response){
$("#result").html(response);
$("#loader").hide();
$("#textChange").text("All Reviews");
}
});
}
});
});
</script>
reviewaction.php:
<?php
if(isset($_POST['action'])){
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$no_of_records_per_page = 8;
$offset = ($pageno-1) * $no_of_records_per_page;
// Prev + Next
$prev = $pageno - 1;
$next = $pageno + 1;
$checksql = "SELECT COUNT(*) FROM review_db WHERE reviewcat !=''";
$sql = "SELECT * FROM review_db WHERE reviewcat !=''";
if(isset($_POST['reviewcat'])){
$reviewcat = implode("','", $_POST['reviewcat']);
$checksql .="AND reviewcat IN('".$reviewcat."')";
$sql .="AND reviewcat IN('".$reviewcat."')";
}
$resultpag = $conn->prepare($checksql);
$resultpag->execute();
$total_rows = $resultpag->fetchColumn();
$total_pages = ceil($total_rows / $no_of_records_per_page);
$sql .="ORDER BY reviewsub DESC LIMIT $offset, $no_of_records_per_page ";
$result = $conn->prepare($sql);
$result->execute();
$output='';
if (count($result) > 0) {
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$my_date = $row['reviewsub'];
$date = DATE("d/m/Y",strtotime($my_date));
$output .= '
<div class="col-sm-6 col-lg-3 py-2">
<div class="card mb-4">
<img class="card-img-top card-images" src="Images/Reviews/'.$row['reviewimage1'].'" alt="'.$row['reviewtitle'].'" >
<div class="card-body">
<h5 class="card-title">'.$row['reviewtitle'].'</h5>
<p class="card-text">'.$row['reviewsynop'].'</p>
<a href="Reviews/review-content.php?id='.$row['id'].'&reviewtitle='.$row['reviewtitle'].'" class="btn btn-primary my-4" >Read More</a>
<div class="card-footer" style="padding: 1%;">
<small class="text-muted">Submitted: '.$date.'</small>
</div>
</div>
</div>
</div>
';
} //While Loop End
}else{
$output = "<h3>No Reviews Found!</h3>";
}
echo $output;
}
?>
You can do it a couple of ways. One is to add the filter to the GET URI parameters of each page link at the end of your filter function, and add code that marks the filters as selected/checked if the parameters exist in the URI before running the POST request. The other is to change the code so that pagination is done with the same POST request instead of actually navigating to a new URL.

Changing class for subsequent rows

I have this piece of code regarding a carousel. I need the first card to have the "active" class but not the following ones or the carousel wont slide. How can i do that?
Here's my code...
$sql = "select wine.wine_name, wine.id, wine.wine_img,
region.region_name, winetype.winetype_name from wine, region, winetype
where wine.region_id = region.id and wine.winetype_id = winetype.id
and sponsored = 1;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<div class="carousel-item col-md-4 active">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src='.$row["wine_img"].' alt="Card image cap">
<div class="card-body">
<h4 class="card-title">'.$row["wine_name"].'</h4>
<p class="card-text">'.$row["winetype_name"].' - '.$row["region_name"].'</p>
</div>
</div>
</div>';
}
} else {
echo "ERRO!";
}
you can use a counter to set the active class.
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$count_results = 0;
while($row = $result->fetch_assoc()) {
echo '<div class="carousel-item col-md-4 ' . ($count_results == 0 ? 'active' : '') . '">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src='.$row["wine_img"].' alt="Card image cap">
<div class="card-body">
<h4 class="card-title">'.$row["wine_name"].'</h4>
<p class="card-text">'.$row["winetype_name"].' - '.$row["region_name"].'</p>
</div>
</div>
</div>';
$count_results++;
}
} else {
echo "ERRO!";
}
You could just have a field with the extra class part set to active and then reset it at the end of the loop...
// output data of each row
$class = ' active';
while($row = $result->fetch_assoc()) {
echo '<div class="carousel-item col-md-4' .$class. '">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src='.$row["wine_img"].' alt="Card image cap">
<div class="card-body">
<h4 class="card-title">'.$row["wine_name"].'</h4>
<p class="card-text">'.$row["winetype_name"].' - '.$row["region_name"].'</p>
</div>
</div>
</div>';
$class = '';
}
This should do the trick:
while($row = $result->fetch_assoc()) {
$active = isset($active) ? '' : 'active';
echo '<div class="carousel-item col-md-4 ' . $active . '">
...
}
The variable $active will only be unset (undefined or null) on the first iteration. Then, on the other iterations, the variable will be defined and not null (since it's an empty string) so then we simply set it as an empty string again.
I would make a counter and simply check if you need to add the class. Of course, since you only need it once, you can use a boolean.
<?php
$sql = "select wine.wine_name, wine.id, wine.wine_img,
region.region_name, winetype.winetype_name from wine, region, winetype
where wine.region_id = region.id and wine.winetype_id = winetype.id
and sponsored = 1;";
$result = $conn->query($sql);
?>
<?php if ($result->num_rows > 0): ?>
<?php $first = true; ?>
<?php while ($row = $result->fetch_assoc()): ?>
<div class="carousel-item col-md-4 <?= $first ? 'active' : '' ?>">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src=<?= $row["wine_img"] ?> alt="Card image cap">
<div class="card-body">
<h4 class="card-title"><?= $row["wine_name"] ?></h4>
<p class="card-text"><?= $row["winetype_name"] ?> - <?= $row["region_name"] ?></p>
</div>
</div>
</div>
<?php $first = false; ?>
<? endwhile; ?>
<?php else: ?>
<span>Error!</span>
<?php endif; ?>
I've also taken the liberty of taking all of your HTML out of the single echo. This is much cleaner :)

How to change <div> property of certain content panel in a php webpage?

<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="row text-center">
<?php
if($_GET[id] == 0)
{
$querysb = mysql_query("SELECT * FROM services WHERE parentid !=0 order by parentid, cid ");
}
else
{
$querysb = mysql_query("SELECT * FROM services WHERE parentid='".$_GET[id]."'");
}
while($rowsb = mysql_fetch_assoc($querysb))
{
if($val == '6' || $val =='10'){
$classname = 'whitebg';
} else {
$classname = 'bg-blue co-white';
}
?>
<div class="col-md-4 mrgnBtm15">
<div class="<?php echo $classname;?> padding30" style="min-height: 250px">
<h3 class="service-heading">
<?php echo $rowsb['cname'];?>
</h3>
<h4>
RS <?php echo $rowsb['price'];?><br>
</h4>
<div class="mrgnTop15 clearfix"></div>
<a class="btn bg-orange co-white" href="<?php echo MYWEBSITE;?>servicedetail/<?php echo to_prety_url($rowsb['cname']).'-'.$rowsb['cid'];?>.html">
<font style="size:14px; color:#000; font-weight:bolder;font-family: "Open Sans";">Register</font></a>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
I am working on a dynamic website. here is the code of a particular page. In this page there is a div section with class="col md-4". If the number of content panel in that division appears to 5 or 7 in that case I want only the last panel (i.e 5th and 7th) to be in full column (col-12). What should I do?
Since you are using col-md-4, 3 divs will be shown each row. So I think what you are looking for is a way to make the last div full width if its going to be alone in its row. In that case, you will need to make it col-md-12 on 4th (not 5th) and 7th and 10th records and so on. This is exactly what the following code does,
I think what you want to do is show the
<?php
if($_GET[id] == 0)
{
$querysb=mysql_query("SELECT * FROM services WHERE parentid !=0 order by parentid, cid ");
}
else
{
$querysb=mysql_query("SELECT * FROM services WHERE parentid='".$_GET[id]."'");
}
$number_of_records = mysql_num_rows($querysb);
$counter = 1;
while($rowsb=mysql_fetch_assoc($querysb))
{
if($val == '6' || $val =='10'){
$classname= 'whitebg';
}else{
$classname= 'bg-blue co-white';
}
//if($number_of_records %3 ==1 && $counter == $number_of_records)
if(($number_of_records == 5 || $number_of_records == 7) && $counter == $number_of_records)
$col_class = "col-md-12";
else
$col_class = "col-md-4";
?>
<div class="<?php echo $col_class;?> mrgnBtm15">
<div class="<?php echo $classname;?> padding30" style="min-height: 250px">
<h3 class="service-heading">
<?php echo $rowsb['cname'];?>
</h3>
<h4>
RS <?php echo $rowsb['price'];?><br>
</h4>
<div class="mrgnTop15 clearfix"></div>
<a class="btn bg-orange co-white" href="<?php echo MYWEBSITE;?>servicedetail/<?php echo to_prety_url($rowsb['cname']).'-'.$rowsb['cid'];?>.html">
<font style="size:14px; color:#000; font-weight:bolder;font-family: "Open Sans";">Register</font></a>
</div>
</div>
<?php
$counter++;
} ?>
</div>
</div>
</div>

Show result of query with count

I'm making a query with a count statement and I wanna show the result
something like this
I have a file with name functions.php where I make the query in a (obviously) function.
those are my functions:
function conection($bd_config){
try {
$conection = new PDO('mysql:host=localhost;dbname='.$bd_config['database'], $bd_config['user'], $bd_config['pass']);
return $conection;
} catch (PDOException $e) {
return false;
}
}
function checksession(){
if (!isset($_SESSION['user'])) {
header('Location: ' . ROUTE. '/login.php');
}
}
#$us = $_SESSION['USER'][0];
function notification_count($conexion, $us){
$count = $conection->prepare('SELECT * FROM postulate WHERE du = $us AND seen = 0');
$count->execute();
$total = $count->rowCount();
$total = $count->fetchColumn();
return $total;
}
I have another file with name lateral.php where I "call" the function and the view of latera.view.php
lateral.php:
<?php session_start();
require 'extras/config.php';
include 'functions.php';
checksession();
$conection = conection($bd_config);
$quantity = notification_count($conection, $us);
require "views/lateral.php";
lateral.view.php:
<div class="block-section text-center ">
<img src="images/<?php echo($_SESSION['user'][14]) ?>" class="img-rounded" alt="">
<div class="white-space-20"></div>
<h4><?php echo($_SESSION['user'][3]) ?></h4>
<div class="white-space-20"></div>
<ul class="list-unstyled">
<li> Messages </li>
<li> Notifications<?php here is where I want the total of the query ?></li>
<li> Change password</li>
</ul>
<div class="white-space-20"></div>
</div>
</div>
<div class="col-md-9 col-sm-9">
<!-- Block side right -->
in the file of functions I need to make my query like this:
function notifications_count($conection, $us){
$count= $conection->prepare("SELECT COUNT(*) FROM postul WHERE du = $us AND seen = 0");
$count->execute();
$number_notu = $count->fetchAll();
return $number_notu;
}
then in the file of notifications.php where onlye I print code and make a require of the view(HTML) I need to put something like this:
<?php session_start();
require 'extra/config.php';
require 'functions.php';
$conection = conection($bd_config);
if (!$conection) {
header('Location: error.php');
}
$postul= get_people($notu_config['post_notu'], $conection, $us);
if (!$postul) {
$vacio = "<li>The aren't any notifications</li>";
}
$total_notu = notu_count($conection, $us);
require "views/notifications.php";
and in the view of notifications where I print only HTML I need to make a foreach:
<div class="block-section text-center ">
<img src="images/<?php echo($_SESSION['user'][14]) ?>" class="img-rounded" alt="">
<div class="white-space-20"></div>
<h4><?php echo($_SESSION['user'][3]) ?></h4>
<div class="white-space-20"></div>
<ul class="list-unstyled">
<li> Messages</li>
<?php foreach ($total_notu as $notu) : ?>
<li> Notificaciones <span class="badge "><?php echo "$notu[0]"; ?></span></li>
<?php endforeach; ?>
<li> Change password</li>
</ul>
<div class="white-space-20"></div>
</div>
</div>
<div class="col-md-9 col-sm-9">
<!-- Block side right -->
I hope I can help someone ho are on the same troble like me :)

Limiting number of items to display per page

I have files like (for an instance ) being shown as shown in
http://lawcommission.gov.np/site/NepalLawCommissionFinal/NepalLawCommissionFinal/document.php?cat=112&sub_cat=10008
The document.php has following html to display the files:
<section class="three-fourth">
<div class="sort-by">
<h3>Sort by</h3>
<ul class="sort">
<li>Date ascendingdescending</li>
<li>Name ascendingdescending</li>
<li>Category ascendingdescending</li>
</ul>
<ul class="view-type">
<li class="grid-view">grid view</li>
<li class="list-view">list view</li>
</ul>
</div>
<div class="deals clearfix">
<!--deal-->
<?php
$id = $_GET['cat'];
$base_obj = new Base();
$lst = $base_obj->list_doc($id);
$lst->execute();
foreach ($lst as $rec)
{
$sub_head = $rec['Head_Id'];
$get_sub = $base_obj->list_by_id('Head_Id', $sub_head, 'tbl_heading');
$get_sub->execute();
foreach ($get_sub as $h)
{
$heading = $h['Head_Name'];
}
?>
<article class="one-fourth">
<figure><span style="float:left"><img src="images/uploads/london1.jpg" alt="" style="width:100px" /></span><span style="float:left"></span></figure>
<div class="details">
<h1><?=$rec['Doc_Name'];?></h1>
<span class="address"><?=$heading;?></span>
<span class="price">Added On <em><?=substr($rec['Uploaded_Date_Time'],0,10);?></em> </span>
Download
</div>
</article>
<!--//deal-->
<?php
} ?>
<!--bottom navigation-->
<div class="bottom-nav">
<!--back up button-->
Back up
<!--//back up button-->
<!--pager-->
<div class="pager">
<span>First page</span>
<span><</span>
<span class="current">1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
<span>></span>
<span>Last page</span>
</div>
<!--//pager-->
</div>
<!--//bottom navigation-->
</div>
</section>
<!--//three-fourth content-->
</div>
<!--//main content-->
</div>
<div class="wrap clearfix">
<!--info boxes--><!--//info boxes-->
</div>
Can you guys please suggest a way to show only limited no. of pdf files in one page?
here is how you need to change your function:
public function list_doc($cat, $page=0) {
$itemsPerPage = 20;
$cat = intval($cat, 10);
$start = intval($page, 10) * $itemsPerPage;
if ($start < 0) $start = 0;
$qry = $this->dbobj->db1->prepare("SELECT SQL_CALC_FOUND_ROWS *
FROM tbl_documents, tbl_cat_head_doc
WHERE tbl_cat_head_doc.Cat_Id = " . $cat ." AND
tbl_cat_head_doc.Doc_Id = tbl_documents.Doc_Id AND
tbl_documents.Status = 'a'
limit " . $start . "," . $itemsPerPage);
return $qry;
}
and in main page call function like this:
$lst = $base_obj->list_doc($id, $_GET['page']);
to prevent next question: read about SQL_CALC_FOUND_ROWS and FOUND_ROWS()

Categories