HTML table not populating from mysql db - php

I am trying to get a simple HTML table of the data in a table of my mysql database, but it is just showing up as a jumbled mess of the text and code in a paragraph on the page. Any ideas what may be causing this? Below is the code I am running:
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
Title
</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="./swipe_da_code_files/bootstrap.min.css" rel="stylesheet">
<link rel="icon" type="image/ico" href="http://lndapp.wpi.edu/img/favicon.ico">
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
</style>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="http://url:Port">AdminPortal</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Create User</li>
<li>Database</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container">
<h1>Database Landing Page</h1>
</div>
<?php
$server = mysql_connect("server", "user", "password");
$db = mysql_select_db("database", $server);
$query = mysql_query("SELECT * FROM Table");
?>
<table>
<tr>
<td>username</td>
<td>password</td>
<td>physicianID</td>
</tr>
<?php
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row[username]."</td>";
echo "<td>".$row[password]."</td>";
echo "<td>".$row[physicianID]."</td>";
echo "</tr>";
}
?>
</table>
</body></html>
I have tried a variety of different ways to display the data, this was the most simple form I could think of to just display a few rows in table form, however it is not running. I have double checked my connection details I entered, as well as the query for the database, and am certain that is not what's causing it.

Replace
echo "<td>".$row[username]."</td>";
echo "<td>".$row[password]."</td>";
echo "<td>".$row[physicianID]."</td>";
By
echo "<td>".$row['username']."</td>";
echo "<td>".$row['password']."</td>";
echo "<td>".$row['physicianID']."</td>";
You are missing quotes!

Please use mysqli or PDO since mysl method is now deprecated.
<?php
$server=mysqli_connect("server", "user", "password" , "my_db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($server, "SELECT * FROM Table")
?>
then
<?php
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['password']."</td>";
echo "<td>".$row['physicianID']."</td>";
echo "</tr>";
}
?>
Akintunde007 is correct in his/her answer that you are parsing a constant.

Related

Load data on bootstrap-table

I have made a website with bootstrap framework. In this website I load a table with records of the database, I using php for this.
My question is: php is the best way to load a data on the table or there are a better option?
Also when it's load the data in the website, first it show the text without format and after 1 second show with the css styles :(
This is the code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>AddCloud - Edicions</title>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/bootstrap-table/dist/bootstrap-table.css">
</head>
<body>
<?php
session_start();
if(isset($_SESSION['username']) and $_SESSION['username'] <> ''){
?>
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#defaultNavbar1"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
<a class="navbar-brand" href="http://www.addvantage.es"><font color=#8abe44>AddCloud</font></a></div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="defaultNavbar1">
<ul class="nav navbar-nav">
<li class="active">Edicions<span class="sr-only">(current)</span></li>
<li>Productes</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown"><span class="glyphicon glyphicon-user"></span> <?php echo $_SESSION['username'] ?>
<ul class="dropdown-menu" role="menu">
<li><span class="glyphicon glyphicon-wrench"></span> preferències</li>
<li><span class="glyphicon glyphicon-lock"></span> canviar password</li>
<li class="divider"></li>
<li><span class="glyphicon glyphicon-log-out"></span> log out</li>
</ul>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
<div class="container-fluid">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="text-center">Miquel Alimentació</h1>
</div>
</div>
<hr>
</div>
<div class="container">
<div class="row text-center">
<div class="col-md-6 col-md-offset-3"><h3>Edició 12</h3></div>
</div>
<hr>
<div class="row">
<?php
// Conexió a la base de dades
include("connect.php");
$conn = Conectarse("localhost", "5432", "dbname", "dbuser", "dbpass");
//query
$query = "SELECT * FROM produccion.ma_origen ORDER BY id_articulo ASC";
$result = pg_query($conn, $query);
//se despliega el resultado
echo "<table id='table'
data-toggle='table'
data-toolbar='#toolbar'
data-show-refresh='true'
data-show-toggle='true'
data-sort-name='name'
data-sort-order='desc'
data-show-columns='true'
data-pagination='true'
data-search='true'
data-click-to-select='true'>";
echo "<thead class='thead-inverse'>";
echo "<tr>";
echo "<th data id='seleccion' data-switchable='false' data-checkbox='true'></th>";
echo "<th data id='pagina' data-sortable='true'>pagina</th>";
echo "<th data id='codigo' data-sortable='true' data-switchable='false'>codigo</th>";
echo "<th data id='descripcion' data-sortable='true' data-switchable='false'>descripcion</th>";
echo "<th data id='pvp-cat' data-sortable='true'>pvp-cat</th>";
echo "<th data id='pvp-lev' data-sortable='true'>pvp-lev</th>";
echo "<th data id='pvp-and' data-sortable='true'>pvp-and</th>";
echo "<th data id='pvp-cen' data-sortable='true'>pvp-cen</th>";
echo "<th data id='pvp-nor' data-sortable='true'>pvp-nor</th>";
echo "<th data id='pvp-vas' data-sortable='true'>pvp-vas</th>";
echo "<th data id='fecha-mod' data-sortable='true'>fecha-mod</th>";
echo "<th data id='user' data-sortable='true' data-visible='false'>user</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while ($row = pg_fetch_row($result)){
echo "<tr>";
echo "<td></td>";
echo "<td>$row[2]></td>";
echo "<td>$row[3]</td>";
echo "<td>$row[4]</td>";
echo "<td>$row[5]</td>";
echo "<td>$row[6]</td>";
echo "<td>$row[7]></td>";
echo "<td>$row[8]</td>";
echo "<td>$row[9]</td>";
echo "<td>$row[10]</td>";
echo "<td>$row[11]</td>";
echo "<td>$row[12]</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
</div>
</div>
<hr>
<div class="row">
<div class="text-center col-md-6 col-md-offset-3">
<p>Copyright © 2016 · All Rights Reserved · <a href="http://www.addvantage.es/" >http://www.addvantage.es</a></p>
</div>
</div>
<hr>
</div>
<script>
$("td").dblclick(function(){
alert("The paragraph was double-clicked");
});
</script>
<script src="assets/jquery/jquery.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="assets/bootstrap-table/dist/bootstrap-table.js"></script>
<?php
} else{
?><p>La sesión no está activa, por favor ingrese aquí</p>
<?php
}?>
</body>
</html>
Please Could you help me with this issues?
Thanks!
As for your question:
"My question is: php is the best way to load a data on the table or there are a better option?"
A: If you are loading dynamic data that is retrieved from a source (eg: database or a file), then the best way (or the only way) to do this is through a scripting language like PHP as you have mentioned.
Get your data with Ajax, and using .append()* function (jQuery), fill the table on $(document).ready() or any other triggered event.
*
jQuery.each(data, function(index, value) {
$("#table_div").append("<tr><td>" + value + "</td></tr>");
});
You can check the example below.
var mydata = [{"Nome":"",
"Cognome":"",
"DataN":"0000-00-00",
"Provincia":"",
"Comune":"",
"CAP":"",
"Indirizzo":"",
"Fisso":"",
"Mobile":"",
"Note":""},
{"Nome":"Federico",
"Cognome":"Lupieri",
"DataN":"2015-09-16",
"Provincia":"",
"Comune":"",
"CAP":"34170",
"Indirizzo":"Via Ascoli 1",
"Fisso":"00112233445566",
"Mobile":"00112233445566",
"Note":"Vediamo se funziona questo"}];
$(document).ready(function(){
$.ajax({
url: '/echo/json/',
dataType: 'json',
success: function(data) {
//alert('done');
$('#clienti').bootstrapTable({
data: mydata
});
},
error: function(e) {
console.log(e.responseText);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="display table table-bordered" id="clienti">
<thead>
<tr>
<th data-field="Nome">Nome</th>
<th data-field="Cognome">Cognome</th>
<th data-field="DataN">Data Nascita</th>
<th data-field="Provincia">Provincia</th>
<th data-field="Comune">Comune</th>
<th data-field="CAP">CAP</th>
<th data-field="Indirizzo">Indirizzo</th>
<th data-field="Fisso">Fisso</th>
<th data-field="Mobile">Cellulare</th>
<th data-field="Note">Note</th>
</tr>
</thead>
</table>
Get your data with Ajax and then fill your table directly, so any data that will be seen on website will be sorted in the table.

Php print format or print php value to html scope

hello i am new in php and im trying to print some values from my sql table to an article in html i use article because this is the structure i need . as you will see bellow i basically want to take the path and the username from my sql table and print them to my article any suggestions?
<html >
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Corporate 1</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/custom.css" rel="stylesheet">
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Logo and responsive toggle -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">
<span class="glyphicon glyphicon-fire"></span>
Logo
</a>
</div>
<!-- Navbar links -->
<div class="collapse navbar-collapse" id="navbar">
<ul class="nav navbar-nav">
<li class="active">
Home
</li>
<li>
About
</li>
<li>
Products
</li>
<li class="dropdown">
Services <span class="caret"></span>
<ul class="dropdown-menu" aria-labelledby="about-us">
<li>Engage</li>
<li>Pontificate</li>
<li>Synergize</li>
</ul>
</li>
</ul>
<!-- Search -->
<form class="navbar-form navbar-right" role="search">
<div class="form-group">
<input type="text" class="form-control">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<div class="jumbotron feature">
<div class="container">
<h1><span class="glyphicon glyphicon-equalizer"></span><font color="#F0FFFF" style="Impact">Welcome to Aegean Community</font></h1>
<p><font color="#E9967A">Community for hope</font></p>
<p><a class="btn btn-default" href="LogIn.php">Engage Now</a></p>
</div>
</div>
<!-- Content -->
<div class="container">
<!-- Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Superior Collaboration
<small>Visualize Quality</small>
</h1>
<p>Proactively envisioned multimedia based expertise and cross-media growth strategies. Seamlessly visualize quality intellectual capital without superior collaboration and idea-sharing. Holistically pontificate installed base portals after maintainable products.</p>
</div>
</div>
<!-- /.row -->
<!-- Feature Row -->
<div class="row">
<!-- Feature Row -->
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "some";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM information order by Ranking desc LIMIT 3 ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
?>
<article class="col-md-4 article-intro">
<a href="#">
<img class="img-responsive img-rounded" src=" <?php echo $row['imgagePath'] ?>" alt="">
</a>
<h3>
<?php echo $row['username'] ?>
</h3>
</article>
<?php
}
?>
</div>
</div>
</body>
</html>
You need to use echo $row['var'] to tell PHP to insert that value on the page as a string.
Just change:
<?php $row['imgagePath'] ?>
to
<?php echo $row['imgagePath']; ?>
and
<?php $row['username'] ?>
to
<?php echo $row['username']; ?>
That should be about all you need to do
The major error here is not using an outputting the collected data per $row using echo and you're also missing the semicolon ;.
So changing <?php $row['username'] ?> to <?php echo $row['username'] ?> and <?php $row['imgagePath'] ?> to <?php echo $row['imgagePath']; ?> should do the trick, I believe that's major error you're encountering.
Also instead of using while ($row = $result->fetch_assoc()) { you're better off using while ($row = $result->fetch_assoc()): then you can simply replace the closing bracket } with endwhile;. The same goes for if (expr): and endif;.
Altogether it should look something like this:
<!-- Feature Row -->
<div class="row">
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "some";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM information order by Ranking desc LIMIT 3 ";
$result = $conn->query($sql);
if ($result->num_rows > 0):
// output data of each row
while ($row = $result->fetch_assoc()):
?>
</article>
<article class="col-md-4 article-intro">
<a href="#">
<img class="img-responsive img-rounded" src=" <?php echo $row['imgagePath']; ?>" alt="">
</a>
<h3>
<?php echo $row['username']; ?>
</h3>
</article>
<?php endwhile; ?>
</div>
...
<?php endif; ?>
You should use echo before the variable that you want to print to HTML.
src=" <?php $row['imgagePath'] ?>"
should become
src=" <?php echo $row['imgagePath']; ?>"

updating information Mysqli_php

i'm trying to make admin panel for simple website
now i'm trying to update the information
but i don't know why it don't update
on ControlPage
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once("../guest.php");
require_once("../db.php");
global $tf_handle;
$gb = new guest();
$id = 0;
//get id from url
if(isset($_GET['id']))
{
$id = (int)$_GET['id'];
}
$message = $gb->getMessage($id);
if(isset($_POST['submit']))
{
$uid = $_POST['id'];
echo $uid;
$name = $_POST['name'];
echo $name;
$msg = $_POST['message'];
echo $msg;
$update = $gb->Update($id,$name,$msg);
if($update)
{
echo('updated');
}
else
{
echo("not updated");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Guestbook control panel</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/sb-admin.css" rel="stylesheet">
<!-- Morris Charts CSS -->
<link href="css/plugins/morris.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div id="wrapper">
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php">GuestBook Admin</a>
</div>
<!-- Top Menu Items -->
<ul class="nav navbar-right top-nav">
<li class="dropdown">
<i class="fa fa-user"></i> Ambaleh <b class="caret"></b>
<ul class="dropdown-menu">
<li>
<i class="fa fa-fw fa-gear"></i> Settings
</li>
<li class="divider"></li>
<li>
<i class="fa fa-fw fa-power-off"></i> Log Out
</li>
</ul>
</li>
</ul>
<!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav side-nav">
<li class="active">
<i class="fa fa-fw fa-dashboard"></i> Dashboard
</li>
<li>
<i class="fa fa-fw fa-bar-chart-o"></i>Messages
</li>
<li>
<i class="fa fa-fw fa-bar-chart-o"></i>Logout
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</nav>
<div id="page-wrapper">
<div class="container-fluid">
<!-- Page Heading -->
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
Admin Panel
</h1>
<ol class="breadcrumb">
<li>
<i class="fa fa-dashboard"></i> Dashboard
</li>
<li class="active">
<i class="fa fa-table"></i> Messages
</li>
</ol>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<h2>Update Messages</h2>
<form role="form" action="update.php" method = "post">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" value = '<?php echo $message['name'];?>' class="form-control">
</div>
<div class="form-group">
<label>Message</label>
<textarea name="message" class="form-control" rows="3"><?php echo $message['message'];?></textarea>
</div>
<input type="hidden" name="id" value='<?php echo $id;?>' >
<input type="submit" name="submit" value="save!">
</form>
</div>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>
<!-- /#page-wrapper -->
</div>
<!-- /#wrapper -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
<!-- Morris Charts JavaScript -->
<script src="js/plugins/morris/raphael.min.js"></script>
<script src="js/plugins/morris/morris.min.js"></script>
<script src="js/plugins/morris/morris-data.js"></script>
</body>
</html>
i tried to echo the variables to check
but it already echo & takes the information from the POST request so the problem from another thing
update.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('db.php');
class guest
{
//get all the messages
public function getMessage($id)
{
global $tf_handle;
$querycheck = mysqli_query($tf_handle,"SELECT * FROM `messages` WHERE `id` = $id");
if($querycheck)
{
$message = mysqli_fetch_assoc($querycheck);
return $message;
}
else
{
die('Failed');
}
tinyf_db_close() ;
}
//".$name."
public function Update($id,$name,$message)
{
global $tf_handle;
$query = mysqli_query($tf_handle,"UPDATE `guest`.`messages` SET `name` = ".$name.", `message` = ".$message." WHERE `messages`.`id` = ".$id);
if($query)
{
echo "query Works";
return TRUE;
}
else
{
tinyf_db_close() ;
return False;
}
tinyf_db_close() ;
}
}
?>
i think the problem in this line
$query = mysqli_query($tf_handle,"UPDATE `guest`.`messages` SET `name` = ".$name.", `message` = ".$message." WHERE `messages`.`id` = ".$id);
There may well be other issues but this is the first I see.
text fields in any query need to be wrapped in quotes, single quotes is my preference, so this update wont compile.
$query = mysqli_query($tf_handle,
"UPDATE `guest`.`messages`
SET `name` = ".$name.", `message` = ".$message."
WHERE `messages`.`id` = ".$id);
You can also simplify the building of the query if you remember that double quoted string literals will expand variables automatically
SO Change to
$query = mysqli_query($tf_handle,
"UPDATE `guest`.`messages`
SET `name` = '$name', `message` = '$message' WHERE
`messages`.`id` = $id" );
Also after any mysql calls you should really do something with the actual error message rather than just return false.
if($query) {
echo "query Works";
return true;
} else {
$this->LastError = mysqli_error($tf_handle);
tinyf_db_close() ;
return false;
}
Then in the calling code do
if(isset($_POST['submit']))
{
$uid = $_POST['id']; <-- also change this to
$id = $_POST['id']; <-- this so it matches Update() params
echo $uid;
$name = $_POST['name'];
echo $name;
$msg = $_POST['message'];
echo $msg;
$update = $gb->Update($id,$name,$msg);
if($update) {
echo('updated');
} else {
echo $gb->LastError;
}

Progress bar php and mysql

I need to build progress bar next to the names of people showing how many costumers each people have working for him. When you see the code you will understand my problem. I dont know how to build this.
Just before end of body, before script tags i have progress bar and var that show's names from db. I need to show that names and beside them progress bar with percentage of costumers.
this are my tables
<?php
if (isset($_GET['poslovni_korisnici'])) {
$active1 = 'class="active"';
}
else {
$active1 = '';
}
if(isset($_GET['korisnici'])) {
$active2 = 'class="active"';
}
else {
$active2 = '';
}
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = '';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=zadatak1", $username, $password);
/*** echo a message saying we have connected ***/
/**echo 'Connected to database';**/
}
catch(PDOException $e)
{
echo $e->getMessage();
}
echo'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Statistika</title>
<!-- Bootstrap -->
<link href="css/bootstrap.css" rel="stylesheet">
<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css">
<!-- jQuery -->
<script type="text/javascript" charset="utf8" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<!-- DataTables -->
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.js"></script>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesnt work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php">POSLOVNA STATISTIKA</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li ' . $active1 . '>Poslovni korisnici <span class="sr-only">(current)</span></li>
<li ' . $active2 . '>Korisnici</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>';
switch (isset($_GET)) {
case isset($_GET['poslovni_korisnici']):
echo'
<h1>Poslovni korisnici</h1>
<button type="button" class="btn btn-default btn-sm">Unos</button>
</br>
</br>';
echo'
<table id="table_id" class="display">
<thead>
<tr>
<th>Partner id</th>
<th>Partner name</th>
<th>Partner street</th>
<th>Partner zip</th>
<th>Partner city</th>
<th>Partner country</th>
</tr>
</thead>
<tbody>';
$sql = "SELECT * FROM poslovni_partneri";
foreach ($dbh->query($sql) as $row)
{
echo'
<tr>
<td>'.$row['Partner_id'].'</td>
<td>'.$row['Partner_name'].'</td>
<td>'.$row['Partner_street'].'</td>
<td>'.$row['Partner_zip'].'</td>
<td>'.$row['Partner_city'].'</td>
<td>'.$row['Partner_country'].'</td>
</tr>';
}
echo'
</tbody>
</table>';
break;
case (isset($_GET['korisnici'])):
echo'
<h1>Korisnici</h1>
<button type="button" class="btn btn-default btn-sm">Unos</button>
</br>
</br>
<table id="table_id" class="display">
<thead>
<tr>
<th>User id</th>
<th>Partner ID</th>
<th>User name</th>
<th>User department</th>
<th>User email</th>
<th>User phone</th>
<th>User mobile</th>
</tr>
</thead>
<tbody>';
$sql = "SELECT * FROM korisnici
INNER JOIN poslovni_partneri
ON korisnici.Partner_id=poslovni_partneri.Partner_id";
foreach ($dbh->query($sql) as $row)
{
echo'
<tr>
<td>'.$row['User_id'].'</td>
<td>'.$row['Partner_id'].'</td>
<td>'.$row['User_name'].'</td>
<td>'.$row['User_department'].'</td>
<td>'.$row['User_email'].'</td>
<td>'.$row['User_phone'].'</td>
<td>'.$row['User_mobile'].'</td>
</tr>';
}
echo'
</tbody>
</table>';
break;
default:
$sql = "SELECT Partner_name FROM poslovni_partneri";
$del = $dbh->prepare('SELECT * FROM poslovni_partneri');
$del->execute();
$count = $del->rowCount();
$del = $dbh->prepare('SELECT * FROM korisnici');
$del->execute();
$count2 = $del->rowCount();
echo '<div class="panel panel-primary">
<div class="panel-heading">Popis poslovnih partnera</div>
<div class="panel-body">
<button class="btn btn-primary" type="button">
Broj poslovnih korisnika <span class="badge">'.$count.'</span>
</button>
</br>
</br>
<button class="btn btn-primary" type="button">
Broj korisnika <span class="badge">'.$count2.'</span>
</button>
</br>
</br>
<div class="panel panel-primary">
<div class="panel-heading">Popis poslovnih partnera <p class="text-center">Ukupni udio</p></div>
<ul class="list-group">';
foreach ($dbh->query($sql) as $row)
{
echo'
<li class="list-group-item list-group-item-info">
<span class="badge">144</span>
<div class="progress">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width:%">
<span class="sr-only">40% Complete (success)</span>
</div>
</div>
<h4 class="names">'.$row['Partner_name'].'</h4>
</li>';
}
echo'
</ul>
</div>
</div>';
break;
}
echo'
<script>
$(document).ready( function () {
$("#table_id").DataTable();
} );
</script>
<!-- jQuery (necessary for Bootstraps JavaScript plugins) -->
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> -->
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>';
?>
You should first get count of customers of each people. Now on basis of number of customers build your progress bar.
Step 1.
Get count of customers.
//add your logic here
$customer_count=<for_exampe_its_20>
Step 2.
Now on basis of count display your progress bar
<div class="progress">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="<? php echo ($customer_count) ?>" aria-valuemin="0" aria-valuemax="100" style="width: <? php echo ($customer_count) ?>%">
<span class="sr-only"><? php echo ($customer_count) ?>customers(success)</span>
</div>
</div>

Specifying output location of PHP queries

I'm working on a small PHP project as an introduction to the language and am having trouble with formatting database query results. My PHP functions are working correctly, but the results are being displayed above all of the HMTL, like this:
Here's my code, all in a file called index.php. Could somebody point out where I'm going wrong? I want it to display right below the navigation bar (Home, Friend List, etc), How do I specify exactly where I want he PHP to appear in the HTML?
#!/usr/local/bin/php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BumpIt</title>
<!-- Bootstrap -->
<link href="css/bootstrap.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/custom.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- Login PHP -->
<?php
ini_set("display_errors", true);
session_start();
// Store username and password in session variables
if (isset($_POST['signin_user']))
{
$_SESSION['signin_user']=$_POST['signin_user'];
$_SESSION['signin_pass']=$_POST['signin_pass'];
}
// Reset username and pass to that of the session variables
$signin_user = $_SESSION['signin_user'];
$signin_pass = $_SESSION['signin_pass'];
$isSigningIn = $_POST['isSigningIn'];
// Connect to the database, check if error happens
$conn = pg_connect(Redacted*);
$_SESSION['conn'] = $conn;
if (!$conn) {
echo "Connection failed";
exit;
}
/**************************************************************
**************************************************************
***************** FUNCTIONS ***************************
**************************************************************
***************************************************************/
// Load Friend List function
if($_GET['friendList']){friendList();}
function friendList(){
$userID = $_SESSION['userID'];
$query = sprintf("SELECT username FROM (SELECT friend_id FROM friends WHERE user_id = $userID) AS currFriends, users WHERE friend_id = user_id;");
$result = pg_query($_SESSION['conn'], $query);
echo "<table class='table table-striped table-bordered table-hover'>\n";
echo "<caption>Friend List</caption>\n";
while ($line=pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
}
// LOGIC NOT WORKING RIGHT YET (DISPLAYING MULTIPLES OF SAME NAME)
// Find Friends function
if($_POST['search']){searchForFriends();}
function searchForFriends() {
$search = $_POST['search'];
$userID = $_SESSION['userID'];
$query = sprintf("SELECT userName FROM (SELECT friend_id FROM friends WHERE user_id = $userID) AS currFriends, users WHERE friend_id <> users.user_id AND userName ILIKE ( '%%' || "."'".$search."'"." || '%%');");
echo "DEBUG PURPOSES-> SEARCHFORFRIENDS QUERY IS: ".$query;
$result = pg_query($_SESSION['conn'], $query);
echo "<table class='table table-striped table-bordered table-hover'>\n";
echo "<caption>Users</caption>\n";
while ($line=pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
}
?>
<img class="header-logo" src="img/bumpit.png"></img>
<!-- Navigation Bar -->
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li><a id="friendList" href="index.php?friendList=true">Friend List</a></li>
<li>
<a id="logoutBtn" href="index.php?logoutBtn=true">Logout</a>
</li>
<li class="dropdown">
Dropdown <b class="caret"></b>
<ul class="dropdown-menu">
<li>Another action</li>
<li>Something else here</li>
<li class="divider"></li>
<li>Separated link</li>
<li class="divider"></li>
<li>One more separated link</li>
</ul>
</li>
</ul>
<!-- Search bar -->
<form class="navbar-form navbar-right" role="search" action="index.php?search=true" method="post">
<div class="form-group">
<input name="search" type="text" class="form-control" placeholder="Search">
<input name="isSigningIn" type="hidden" value="1">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<!-- Main content -->
<div class="container">
<div class="starter-template">
<!-- WE WANT THE FRIENDS LIST HERE -->
</div>
</div><!-- /.container -->
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
I believe you want to move this:
// Load Friend List function
if($_GET['friendList']){friendList();}
To where you want the friendList printed:
<div class="container">
<div class="starter-template">
<!-- WE WANT THE FRIENDS LIST HERE -->
<?php
// Load Friend List function
if (isset($_GET['friendList'])) { friendList(); }
?>
</div>
</div><!-- /.container -->
You have to add it in <?php ?> and I have also altered if statement so it does not throw a notice if friendList wouldn't be set.

Categories