Grouping certain rows together - php

<?php
error_reporting(E_ALL);
ini_set('display_errors' ,1);
require "connection.php";
$query= "SELECT client_id, array_agg(insurance) AS insurance from vouchers WHERE parsing_date=CURRENT_DATE GROUP BY client_id ";
$result = pg_query($conn,$query);
?>
<!DOCTYPE html>
<html>
<head>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<link href = "http://fonts.googleapis.com/css?family=Roboto:400">
<style>
.responstable {
margin: 1em 0;
width: 100%;
overflow: hidden;
background: #FFF;
color: #024457;
border-radius: 10px;
border: 1px solid #167F92;
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="container-fluid">
<div>
<h1>Clients</h1>
</div>
<table class="responstable" rules='all' style='border-collapse: collapse;'>
<thead>
<tr>
<th>Client id</th>
<th>Insurance</th>
<th>Number of rows</th>
</tr>
</thead>
<?php
while($row = pg_fetch_array($result))
{
?>
<tbody>
<td><?php echo $row['client_id']; ?></td>
<td><?php echo $row['insurance']; ?></td>
<td><?php echo $row['rows'];?></td>
</tr>
<?php }
?> </tbody>
</table>
</div>
</body>
</html>
I have the code above to make my output from:
Client id Insurance
------------ ---------------
123 {"AA","EE","U"}
125 {"AA","UE"}
126 {"CU"}
124 {"UE"}
I want this:
Client id Number of rows Insurance
------------ ----------------------- --------------
123 3 rows AA,EE,U
125 2 rows AA,UE
126 1 rows CU
124 1 rows UE
Im not sure how to fix it, i tried add this line to my query:
concat(count(*), ' rows') AS rows
But it just gave me an error, any ideas on what I could do for the rows to come out and the "{" and '"' goes?

u can use explode function.
try this code
<?php while ( $row = pg_fetch_array ( $result ) ) { ?>
<tr>
<td><?php echo $row['client_id']; ?></td>
<td><?php echo implode(',',json_decode($row['insurance'])); ?></td>
<td><?php
$sRows = '';
if($row['iCnt'] > 0) {
$sRows = $row['iCnt']==1?' row':' rows';
}
echo ''.$row['iCnt'].$sRows;
?></td>
</tr>

JSON Parsing and Query issues are solved
<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );
require "connection.php";
$query = "SELECT client_id,
count(*) AS iCnt,
array_agg(insurance) AS insurance from vouchers
WHERE parsing_date=CURRENT_DATE GROUP BY client_id ";
$result = pg_query ( $conn, $query );
?>
<!DOCTYPE html>
<html>
<head>
<link
href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"
rel="stylesheet">
<link href="http://fonts.googleapis.com/css?family=Roboto:400">
<style>
.responstable {
margin: 1em 0;
width: 100%;
overflow: hidden;
background: #FFF;
color: #024457;
border-radius: 10px;
border: 1px solid #167F92;
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="container-fluid">
<div>
<h1>Clients</h1>
</div>
<table class="responstable" rules='all' style='border-collapse: collapse;'>
<thead>
<tr>
<th>Client id</th>
<th>Insurance</th>
<th>Number of rows</th>
</tr>
</thead>
<tbody>
<?php while ( $row = pg_fetch_array ( $result ) ) { ?>
<tr>
<td><?php echo $row['client_id']; ?></td>
<td><?php echo implode(',',json_decode($row['insurance'])); ?></td>
<td><?php
$sRows = '';
if($row['iCnt'] > 0) {
$sRows = $row['iCnt']==1?' row':' rows';
}
echo ''.$row['iCnt'].$sRows;
?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</body>
</html>

Change you query like this
SELECT client_id, concat(cast(count(*) AS char),' rows')
This works good in mysql. You have to type cast integer to string for concatenation
http://www.mysqltutorial.org/mysql-cast/
For PostgreSQL it's should be like this,
SELECT client_id,
count(*) AS cnt,
''||count(*)||' rows' AS concatString
https://www.postgresql.org/docs/8.3/static/functions-string.html
You can add If and else like this on query
SELECT client_id,
CASE WHEN count(*)>0 THEN
CASE WHEN count(*)==1 THEN ''||count(*)||' row'
ELSE ''||count(*)||' rows' END
ELSE ''
END AS concatenatedString
I don't know about the performance
Postgres nested if in case query

You want to use array_to_string:
SELECT
client_id,
concat(count(*), ' rows'),
array_to_string(array_agg(insurance),',') AS insurance
FROM vouchers
WHERE parsing_date=CURRENT_DATE
GROUP BY client_id

Related

Style is not being applied except for the first row from sql query in html code

I'm retrieving all the rows from sql database through a query, and i'm using a loop to get all the rows
I have a style.css file which i'm specifying a style for the , however, only the first row is receiving the style and the style is not being applied to the rest.
<!DOCTYPE html>
<html>
<link href="styles.css" rel="stylesheet">
<head>
<title>IIACSS search center</title>
</head>
<body>
<form method="post">
<label>Search</label>
<input type="text" name="search">
<input type="submit" name="submit">
</form>
<?php
$con = new PDO("mysql:host=localhost;dbname=data",'root','admin123');
if (isset($_POST["submit"])) {
$str = $_POST["search"];
$sth = $con->prepare("SELECT * FROM `questions` WHERE question LIKE '%$str%'");
$sth->setFetchMode(PDO:: FETCH_OBJ);
$sth -> execute();
?>
<table>
<tr>
<th>Project</th>
<th>Question</th>
<th>Sample</th>
</tr>
<?php
for ($a=0;$row = $sth->fetch();$a++) {
?>
<tr>
<td><?php echo $row->Proj_Name; ?></td>
<td><?php echo $row->Question;?></td>
<td><?php echo $row->sample;?></td>
<br><br>
</tr>
</table>
<?php
}
}
?>
body {
background-color: white;
}
h1 {
color: black;
}
td {
color: black;
font-family: sans-serif;
}
th {
color: blue;
font-family: sans-serif;
}
It is not a CSS problem, it is because you are closing your </table> in the loop, so it is ending your table after the first tr.
Just remove it from the loop:
<?php
$con = new PDO("mysql:host=localhost;dbname=data",'root','admin123');
if (isset($_POST["submit"])) {
$str = $_POST["search"];
$sth = $con->prepare("SELECT * FROM `questions` WHERE question LIKE '%$str%'");
$sth->setFetchMode(PDO:: FETCH_OBJ);
$sth -> execute();
?>
<table>
<tr>
<th>Project</th>
<th>Question</th>
<th>Sample</th>
</tr>
<?php
for ($a=0;$row = $sth->fetch();$a++) {
?>
<tr>
<td><?php echo $row->Proj_Name; ?></td>
<td><?php echo $row->Question;?></td>
<td><?php echo $row->sample;?></td>
<br><br>
</tr>
<?php
} ?>
</table>
<?php }
?>

Spread data in 3 columns

I need your help with this small issue as my developing level is not the best In the room ( faraway :-) )
I would like to spread the results of my SQL request in 3 columns.
After the first 37 entries, I would like the entries 38 to 74 are filling the column #2, and column #3 over 74...
I hope it is clear.
please find below the code of my page:
<title>TITLE</title>
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght#700;800;900&display=swap" rel="stylesheet">
<style>
body {background-color: #000000;}
table {
background-color: #000000;
border-collapse: collapse;
width: 100%;
}
table, .table {
color: #fff;
font-family: "Montserrat", sans-serif;
font-size: 18px;
font-weight:800;
font-weight:Extra-bold;
}
tr:nth-child(even) {
background-color: #60451e;
}
</style>
<div class="container">
<div class="row">
<?php
include_once("inc/db_connect.php");
$sqlQuery = "SELECT name, GROUP_CONCAT(id ORDER BY id SEPARATOR ' | ') as grouped_id FROM developers GROUP BY name";
$resultSet = mysqli_query($conn, $sqlQuery) or die("database error:". mysqli_error($conn));
?>
<table id="editableTable" class="table table-bordered">
<tbody>
<?php while( $developer = mysqli_fetch_assoc($resultSet) ) { ?>
<tr id="<?php echo $developer ['id']; ?>">
<td><?php echo $developer ['name']; ?><br>  <span style="color: #ffc000">BOXES > <?php echo $developer ['grouped_id']; ?></span></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
and I would like to keep a formatting similar to this for each entry:
echo $developer ['name']; ?><br>  <span style="color: #ffc000">BOXES > <?php echo $developer ['grouped_id']; ?></span>
Thank you in advance

Database information not lining up in a table (php/sql)

When using CSS for my DetailsTable the information I am getting from the database is not being displayed correctly in the table. All of that information is getting bunched up together and therefore isn't being laid out as a table.
If anyone could suggest why the information I am trying to get isn't being laid out in a table form that would be much appreciated
</head>
<body>
<div id="DetailsTable" >
<table>
<tr>
<th>Name</th>
<th>TypeOfShoe</th>
<th>Description</th>
<th>Price(£)</th>
<th>Fabric</th>
<th>Colour</th>
<th>Brand</th>
</tr>
</table>
<?php
$shoesID=$_GET['id'];
$stmt = $conn->prepare("SELECT shoes.name, shoes.images, shoes.description, shoes.price, types.typeOfShoe, types.fabric, types.colour, brands.name AS bname FROM shoes INNER JOIN types on types.types_id = shoes.type_id INNER JOIN brands on brands.brands_id = shoes.brands_id
WHERE shoes.id = :id");
$stmt->bindValue(':id',$shoesID);
$stmt->execute();
if ($shoes=$stmt->fetch()){
echo "<td>".$shoes['name']."</td>";
echo "<td>".$shoes['typeOfShoe']."</td>";
echo "<td>".$shoes['description']."</td>";
echo "<td>".$shoes['price']."</td>";
echo "<td>".$shoes['fabric']."</td>";
echo "<td>".$shoes['colour']."</td>";
echo "<td>".$shoes['bname']."</td>";
}
$conn=NULL;
?>
<br> <br/>
<div id="Image" >
<img src="<?php echo $shoes['images']; ?>" height="500px" width="500px" />
</div>
</div>
The CSS code is below.
body {
background-color: lemonchiffon;
}
#DetailsTable {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
#Image{
display: block;
margin: auto;
width: 50%;
height: 10px;
clear: both;
}
You have closed the</table> tag before the php code, that maybe the problem
</head>
<body>
<div id="DetailsTable" >
<table>
<tr>
<th>Name</th>
<th>TypeOfShoe</th>
<th>Description</th>
<th>Price(£)</th>
<th>Fabric</th>
<th>Colour</th>
<th>Brand</th>
</tr>
<?php
$shoesID=$_GET['id'];
$stmt = $conn->prepare("SELECT shoes.name, shoes.images, shoes.description, shoes.price, types.typeOfShoe, types.fabric, types.colour, brands.name AS bname FROM shoes INNER JOIN types on types.types_id = shoes.type_id INNER JOIN brands on brands.brands_id = shoes.brands_id
WHERE shoes.id = :id");
$stmt->bindValue(':id',$shoesID);
$stmt->execute();
if ($shoes=$stmt->fetch()){
echo '<tr>';
echo "<td>".$shoes['name']."</td>";
echo "<td>".$shoes['typeOfShoe']."</td>";
echo "<td>".$shoes['description']."</td>";
echo "<td>".$shoes['price']."</td>";
echo "<td>".$shoes['fabric']."</td>";
echo "<td>".$shoes['colour']."</td>";
echo "<td>".$shoes['bname']."</td>";
echo '</tr>';
}
$conn=NULL;
?>
</html>
<br> <br/>
<div id="Image" >
<img src="<?php echo $shoes['images']; ?>" height="500px" width="500px" />
</div>
</div>

How to get id to another page and make it show value based on id

I have already referred this post How to get row ID in button click? but still can't make it load values and I do not know why.I'm pretty sure the id has been properly captured at profile.php. Below is my code:
hanj.php
style type="text/css">
.buttonize {
text-decoration: none;
border: 1px solid #ccc;
background-color: #efefef;
padding: 10px 15px;
-moz-border-radius: 11px;
-webkit-border-radius: 11px;
border-radius: 11px;
text-shadow: 0 1px 0 #FFFFFF;
}
<?php require_once('Connections/conn.php');?>
</style>
<table border="1" cellpadding="5" cellspacing="2" width="600">
<tr>
<th>Vendor Id</th>
<th>Kategori</th>
<th>Nama Vendor</th>
<th>Alamat</th>
<th>Poskod</th>
<th>Bandar</th>
<th>Negeri</th>
<th>No</th>
<th>Email</th>
</tr>
<?php
session_start();
$_SESSION['profile']= $row ['v_id'];
mysql_select_db ($database_conn,$conn);
$query="SELECT v_id,type,companyName,address,code,city,state,contact,email FROM vendor";
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result))
{
echo "</td><td>";
echo $row['v_id'];
echo "</td><td>";
echo $row['type'];
echo "</td><td>";
echo $row['companyName'];
echo "</td><td>";
echo $row['address'];
echo "</td><td>";
echo $row['code'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
echo $row['state'];
echo "</td><td>";
echo $row['contact'];
echo "</td><td>";
echo $row['email'];
echo "</td><td>";
print '<center>View</center>';
echo "</td></tr>";
}
?>
profile.php
<?php require_once('Connections/conn.php'); ?>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Casado</title>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="http://pingendo.github.io/pingendo-bootstrap/themes/default/bootstrap.css" rel="stylesheet" type="text/css">
<div class="container">
<div class="col-md-12">
<div class="col-md-2"></div>
<div class="col-md-8">
<table class="table table-bordered">
<thead>
<tr>
<th>Pakej</th>
<th>Image</th>
<th>Harga</th>
<th>Vendor Id</th>
</tr>
</thead>
<tbody>
<?php
session_start();
mysql_select_db ($database_conn,$conn);
$vid = $_SESSION['profile'];
$sql = mysql_query("SELECT * from item where v_id = '$vid' ") or die (mysql_error());
while($res = mysql_fetch_array($sql)) {
?>
<tr>
<td><?php echo $res['pakej'] ?></td>
<td><?php echo '<img src="data:image;base64,'.$res['image'].'" class="img-thumbnail">' ?></td>
<td><?php echo $res['harga'] ?></td>
<td><?php echo $res['v_id'] ?></td>
</tr>
<?php }
?>
</tbody>
</table>
</div>
<div class="col-md-2"></div>
</div>
</div>
</body>
</html>
btw there is no syntax error
Instead of $_SESSION['profile'] use $_REQUEST['id'] in your profile.php and it should work.
In profile.php use $_GET to reference the passed in 'id'. i.e
Replace the following line
$vid = $_SESSION['profile'];
with this one.
$vid = $_GET['id'];
Hope this helps.
this question is answered by Bikash Paul above where he suggest to change $_SESSION['profile'] to $_REQUEST['id']
Instead of $vid = $_SESSION['profile']; try $vid = $_GET['profile'];, if not works, then print_r($_GET) or print_r($_REQUEST) & show us the details

Error in mysqli after pagination

It's me again, sorry...
I've been looking for the answer right in this forums, but there are no posts has been solved. I don't know if the questioner has been resolved the problems and not given a solved comment or something like that. All the comments/replies from the questioner is 'not work' or 'get some new errors' ect.
Now my scripts has worked before I put pagination scripts on them, but again errors in 'mysqli'. The errors are:
Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\paging\index.php on line 8
Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\paging\index.php on line 9
My scripts contained two files:
index.php:
<?php
$con = mysqli_connect("localhost", "root", "", "db_book") or die(mysqli_error($con));
$per_page = 3;
//getting number of rows and calculating no of pages
$sql = "SELECT COUNT(*) FROM flipbook";
$result = mysqli_query($sql);
$count = mysqli_fetch_row($result);
$pages = ceil($count[0]/$per_page);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>DIGITAL LIBRARY</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript" src="pagination.js"></script>
<style>
body { margin: 0; padding: 0; font-family:Verdana; font-size:15px }
a
{
text-decoration:none;
color:#B2b2b2;
}
a:hover
{
color:#DF3D82;
text-decoration:underline;
}
#loading {
width: 100%;
position: absolute;
}
#pagination
{
text-align:center;
margin-left:120px;
}
li{
list-style: none;
float: left;
margin-right: 16px;
padding:5px;
border:solid 1px #dddddd;
color:#0063DC;
}
li:hover
{
color:#FF0084;
cursor: pointer;
}
</style>
</head>
<body>
<div align="center">
<div style="margin-top:50px;"></div>
<div id="content" ></div>
<table width="800px">
<tr><Td>
<ul id="pagination">
<?php
//Show page links
for($i=1; $i<=$pages; $i++)
{
echo '<li id="'.$i.'">'.$i.'</li>';
}
?>
</ul>
</Td>
</tr></table>
<div id="loading" ></div>
</div>
</body>
</html>
and data.php:
<?php
$con = mysqli_connect("localhost", "root", "", "db_book");
$sql = mysqli_query($con,"SELECT b.*, title, author_name, url_flipbook, p.publisher_name, ct.cat_name FROM biblioflip AS b
LEFT JOIN mst_publisherflip AS p ON b.publisher_id=p.publisher_id
LEFT JOIN mst_catflip AS ct ON b.cat_id=ct.cat_id
ORDER BY flip_id limit $start,$per_page") or die(mysqli_error($con));
$per_page = 3;
if($_GET)
{
$page=$_GET['page'];
}
//getting table contents
$start = ($page-1)*$per_page;
?>
<table id="tbl">
<th>Title</th>
<th>Author</th>
<th>Publisher</th>
<th>Category</th>
<th>Link</th>
<?php
while($row = mysqli_fetch_array($result))
{
$title = $row['title'];
$author = $row['author_name'];
$publisher = $row['publisher_name'];
$category = $row['cat_name'];
$link = 'FLIPBOOK</td>';
?>
<tr>
<td><?php echo $title; ?></td>
<td><?php echo $author; ?></td>
<td><?php echo $publisher; ?></td>
<td><?php echo $category; ?></td>
<td><?php echo $link; ?></td>
</tr>
<?php
} //End while
mysqli_close($con);
?>
</table>
<style>
#tbl
{
width:800px;
border:1px solid #98bf21;
margin-top:50px;
}
#tbl tr:nth-child(odd) {
background: #EAF2D3
}
#tbl td{
border:1px solid #98bf21
}
#tbl th
{
background: #A7C942;
border:1px solid #98bf21
}
</style>
Thanks again...thank you...
best regards,
The error message is quite descriptive and clear: The first parameter to mysqli_query should be the connection handle.
Instead of $result = mysqli_query($sql);, use $result = mysqli_query($con, $sql);

Categories