Show result of query with count - php

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 :)

Related

Page Goes Blank With Function On Php

I have the following function.php:
function getPublishedPosts() {
// use global $conn object in function
global $conn;
$sql = "SELECT * FROM posts WHERE published=true";
$result = mysqli_query($conn, $sql);
// fetch all posts as an associative array called $posts
$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
$final_posts = array();
foreach ($posts as $post) {
$post['topic'] = getPostTopic($post['id']);
array_push($final_posts, $post);
}
return $final_posts;
}
and I am calling it to the other page with this:
<!-- THIS LOOP -->
<?php $posts = **getPublishedPosts();** ?>
<div class="content">
<h2 class="content-title">Recent Articles</h2>
<hr>
<!-- more content still to come here ... -->
<!-- Add this ... -->
<?php $posts = getPublishedPosts(); ?>
<?php foreach ($posts as $post): ?>
<div class="post" style="margin-left: 0px;">
<img src="<?php echo BASE_URL . '/static/images/' . $post['image']; ?>" class="post_image" alt="">
<!-- Added this if statement... -->
<?php if (isset($post['topic']['name'])): ?>
<a
href="<?php echo BASE_URL . 'filtered_posts.php?topic=' . $post['topic']['id'] ?>"
class="btn category">
<?php echo $post['topic']['name'] ?>
</a>
<?php endif ?>
<a href="single_post.php?post-slug=<?php echo $post['slug']; ?>">
<div class="post_info">
<h3><?php echo $post['title'] ?></h3>
<div class="info">
<span><?php echo date("F j, Y ", strtotime($post["created_at"])); ?></span>
<span class="read_more">Read more...</span>
</div>
</div>
</a>
</div>
<?php endforeach ?>
</div>
However, when it is on the server, the page goes blank.
I finally solved it, it was just a server extension and version problem regarding the incompatibility of my function.

How do I output two values from two different tables to a href?

I want to output something like this:
viewlogs.php?id=1&EmployeeNo=15000000600
However, I'm not sure what condition I should be using. I have two separate database to call, one is on MySQL which is easily taken to the localhost, and MSSQL which I'm not very familiar with.
I'm trying a logic where I use && within a while loop.
My code works something like this:
<?php
session_start();
if (!isset($_SESSION['username']))
{
header('location: login.php');
die();
}
?>
<?php
try {
include ("config.php");
include ("sqlsrv.php");
}catch(Exception $e){
die("ERROR:".$e->getMessage());
}
if(isset($_POST['usn']) && $_POST['usn']!=""){
$res = $conn->prepare("SELECT EmployeeNo, FirstName, MiddleName, LastName, DateHired, ResignationDate FROM TA3.dbo.Employees");
$res->execute();
$req = $db->prepare("SELECT * FROM students WHERE usn LIKE :usn");
$req->execute(array(
'usn'=>'%'.$_POST['usn'].'%'));
if ($req->rowCount()==0 && $_SESSION['type']=="Super_Admin") { ?>
<span class="notfound">Student not found? <a class="addstud" href="create.php">add student?</a></span>
<?php
}
elseif ($req->rowCount()==0 && $_SESSION['type']=="Admin") { ?>
<span class="henhen">Student not found</span>
<?php
}
else{
while ($data=$req->fetch() && $outcome=$res->fetch()){
?>
<div class="infohen">Info </div>
<div class="uy">
<div class="name"><?php echo $data['fname']." ".$data['mname']." ".$data['lname']; ?></div>
<div class="email"><?php echo $data['email']; ?></div>
<div class="usn"><?php echo $data['usn']; ?></div>
<div class="schedule"><?php echo $data['schedule']; ?></div>
<div class="strand"><?php echo $data['strand']; ?></div></div>
<?php if ($_SESSION['type']=="Super_Admin")
{ ?>
<div class="gridme">
<div class="action-list">
<div class="action">Action</div>
<div class="edit"> Edit</div>
<div class="logs">Logs</div>
<?php } else { ?>
<div class="gridme">
<div class="action-list">
<div class="action">Action</div>
<div class="logs">Logs</div>
<?php } ?>
</div>
</div>
</div>
</div>
<?php
}
}
}else { ?>
<span class="message">Enter USN!</span>
<?php
}
?>
<!-- <div class="enable"> <a onclick="return confirm('Are you sure you want to Enable student?')" href="enable.php?id=<?= $data['id']?>"> Enable </div>
<div class="disable"> <a onclick="return confirm('Are you sure you want to disable student?')" href="disable.php?id=<?= $data['id']?>">Disable</a>
</div> -->
<!-- </div>
<div class="status-list">
<div class="status">Status</div>
<div class="active">Active</div>
</div> -->
If I use while ($data=$req->fetch() && $outcome=$res->fetch()), the details called from $data are all gone.
I'm having trouble with logic since I'm not very good at it.
If anyone might need it, I found a solution.
I just needed to use open and close parenthesis.
while (($data=$req->fetch()) && ($outcome=$res->fetch()))

PHP echo display outside the div

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>

Display nav list of content is available with php

I've got this PHP code:
<div class="connect_wrap <?php echo $this->class; ?> block" <?php echo $this->cssID; ?>>
<?php if(!$this->empty): ?>
<?php foreach($this->entries as $entry): ?>
<div class="entry block <?php echo $entry->class; ?>">
<div class="tab">
<ul class="tabs">
<li class="tab-link current" data-tab="Kunden">Kunden</li>
<li class="tab-link" data-tab="Loesungen">Lösungen</li>
</ul>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM kunden WHERE partner=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Kunden" class="tab-content current">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/kunden-detail/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM solutions WHERE solution_provider=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Loesungen" class="tab-content">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/synaptic-commerce-solution/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<?php endif;?>
In that code, I've got two DB queries. My problem is, if there is no data for both queries, the div with the class "connect_wrap" should not be displayed.
How can I do that?
Thanks in advance.
do you want to check if the query has data in it and its not empty ? if so try num_rows it will return the number of rows that the query found/affected for example to check if th query returned one or more rows
if($result->num_rows >= 1) {
//do stuf
} else {
// no result found
}
Move the query execution up or preferably: not within the html but in a different file/class. Then add a check before the content_wrap div: if($kundenResult->num_rows >= 1 || $solutionsResult->num_rows >= 1). If you just keep the individual checks like you already have, it will only show the content_wrap div if either or both of the queries return rows of data.

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