I am totally new to web development. I am doing a personal project for learning purpose.
I have a php page, which query my database and return the data.
My query is :
SELECT * FROM myTable ORDER BY id;
That works fine from a SQL GUI tool.
When I load my page, the data is not ordered by the ID. Each time I refresh the page, the order is randomly changed.
My HTML header section
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script>
<script>window.jQuery || document.write('<script src="/js/jquery-1.12.4.min.js">\x3C/script>')</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
My loadData.php page
try {
$connection = new PDO($dsn, $username, $password, $options);
$sqlData = "SELECT * FROM cc_data_tbl where cc_uuid = '" . $_GET['load_uuid'];
$statement = $connection->query($sqlData);
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
if ($statement->rowCount() == 0){
echo "No Data";
} else { ?>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header clearfix">
<?php
echo "<table class='table table-bordered table-striped' id=\"transactionList\">";
echo "<thead>";
echo "<tr>";
echo "<th>Transaction Date</th>";
echo "<th>Listing Date</th>";
echo "<th>Description</th>";
echo "<th>Amount</th>";
echo "<th>Category</th>";
echo "<th>Category Type</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach($results as $row){
echo "<tr>";
echo "<td>" . $row['cc_transac_date'] . "</td>";
echo "<td>" . $row['cc_listing_date'] . "</td>";
echo "<td>" . $row['cc_description'] . "</td>";
echo "<td>" . $row['cc_amount'] . "</td>";
echo "<td>" . $row['cc_category'] . "</td>";
echo "<td>" . $row["cc_category_type"] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
</div>
</div>
</div>
</div>
</div>
<?php }
} catch (PDOException $error) {
echo $error -> getMessage();
}
I was expecting to have my data sorted by ID, which is not the case.
This is a rather really basic SQL query... so I assume there's something that I really do not understand with HTML / PHP / Bootstrap as all these things are new to me.
if your problem still continue , please check your table class("table table-bordered table-striped") . Sorting variables in table class . I will give you example . I hope it helps for you.
$('#simple-table').DataTable({
"order": [[ 0, "desc" ]],
"autoWidth": true,
"language": {
"url": "//cdn.datatables.net/plug-ins/1.10.16/i18n/Turkish.json"
}
});
Related
I am trying to build a forum for my school project. Now i want to get the data out of my database so i can print it and people can see and react to it. Now i have the problem that it only print the commenters post and not the creators post. I will ad some picture and the code so you guys can undestand. (PS. i have a form on the previous page where you can search)
Database
Website
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./style.css">
<title><?php echo $_GET["search"];?></title>
</head>
<body>
<?php
//navbar
include "./include/nav.php";
//database connection
include "./include/conn.php";
$search = $_GET["search"];
if($search == ""){
// if empty return to index
echo "
<script>alert('thread does not exist')</script>
<script>window.location = './index.php'</script>
";
} else {
//check if data exist
$sql = "SELECT thread, post, username, status FROM post WHERE thread = ? ";
$query = $conn->prepare($sql);
$query->execute(array($search));
$data = $query->fetch();
if($data){
// print data if exist
echo "<div class='padding'>";
foreach ($query as $test) {
echo "<tr>";
echo "<td>" . $test["thread"] . "</td>";
echo "<td>" . $test["post"] . "</td>";
echo "<td>" . $test["username"] . "</td>";
echo "<td>" . $test["status"] . "</td>";
echo "</tr>" . "</br>";
}
echo "<button onclick='comment()'>Comment</button>";
echo "</div>";
echo "<div id='test'>";
} else {
// if data does not exist return to index
echo "
<script>alert('thread does not exist')</script>
<script>window.location = './index.php'</script>
";
}
}
?>
<script>
// if comment btn is pressed save thread name and send to comment.php
function comment() {
document.getElementById("test").innerHTML =
"<?php
session_start();
$_SESSION['thread'] = $data['thread'];
?>";
window.location.assign("./comment.php")
}
</script>
</body>
</html>
I never used the data that i got out of the database. I should have printed it like this:
if($data){
// print data if exist
echo "<div class='padding'>";
foreach ($data as $test) {
echo "<tr>";
echo "<td>" . $test["thread"] . "</td>";
echo "<td>" . $test["post"] . "</td>";
echo "<td>" . $test["username"] . "</td>";
echo "<td>" . $test["status"] . "</td>";
echo "</tr>" . "</br>";
}
echo "<button onclick='comment()'>Comment</button>";
echo "</div>";
echo "<div id='test'>";
} else {
// if data does not exist return to index
echo "
<script>alert('thread does not exist')</script>
<script>window.location = './index.php'</script>
";
}
}
I need help, I cannot figure out, I cannot find why I am having errors and I am not able to achieve something freaking simple.
Long story short, I have a website to manage projects, so when I run the search function it throws a table with some records from the database, there is a button called "see details" which is assigned to a project id with database i.e. 21, 1, 48 etc, the problem is that when I click "see details" it throws everything from the table proposals instead of 1 project, no matter which button I click on, if its id 1, 21, 48, it prints everything.
details page
details.php:
<?php
include '../includes/config.php';
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM proposals_table WHERE id LIKE '_%'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered'>";
echo "<tr>";
echo "<th>Organisation</th>";
echo "<th>Project</th>";
echo "<th>Proposal Date</th>";
echo "<th>Date Received</th>";
echo "<th>Notes</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['company'] . "</td>";
echo "<td>" . $row['project'] . "</td>";
echo "<td>" . $row['proposal_date'] . "</td>";
echo "<td>" . $row['date_received'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
?>
search/result page
proposals.php
<?php
include '../includes/config.php';
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM proposals_table";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered'>";
echo "<tr>";
echo "<th>Organisation</th>";
echo "<th>Project</th>";
echo "<th>Proposal Date</th>";
echo "<th>Date Received</th>";
echo "<th>Options</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['company'] . "</td>";
echo "<td>" . $row['project'] . "</td>";
echo "<td>" . $row['proposal_date'] . "</td>";
echo "<td>" . $row['date_received'] . "</td>";
echo "<td> <a class='btn btn-primary' href='details.php?id={$row['id']}'>See details</a></td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
?>
If you want to show only the selected element on your details page then you need to fetch only that selected item from the database.
First of all you should separate HTML from PHP. The best would be to have them in separate files. In PHP you prepare the data to be displayed and then in HTML you fill in the blanks with PHP values.
To select a value from MySQL using a given ID you must use prepared statements with parameter binding. So if you create your link in this way:
echo "<td> <a class='btn btn-primary' href='details.php?id=".urlencode($row['id'])."'>See details</a></td>";
You can receive this ID in your details page using $_GET['id']. You can bind that value to your WHERE clause in SQL.
<?php
include '../includes/config.php';
// Attempt select query execution
$stmt = $link->prepare("SELECT * FROM proposals_table WHERE id=?");
$stmt->bind_param('s', $_GET['id']);
$stmt->execute();
$proposals = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
if($proposals) {
?>
<table class='table table-bordered'>
<tr>
<th>Organisation</th>
<th>Project</th>
<th>Proposal Date</th>
<th>Date Received</th>
<th>Notes</th>
</tr>
<?php foreach($proposals as $row): ?>
<tr>
<td><?=$row['company'] ?></td>
<td><?=$row['project'] ?></td>
<td><?=$row['proposal_date'] ?></td>
<td><?=$row['date_received'] ?></td>
<td><?=$row['notes'] ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
} else {
echo 'No records matching your query were found.';
}
And of course your config.php page should look like this:
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = new mysqli('localhost', 'user', 'pass', 'db');
$link->set_charset('utf8mb4'); // always set the charset
<?php
try{
include("dbconnectie.php");
$query = $db->prepare("SELECT * FROM shop WHERE id_u = :id");
$query->bindParam("id", $_SESSION['id_u']);
$query->execute();
$result = $query->fetchALL(PDO::FETCH_ASSOC);
echo "<table>";
foreach($result as &$data) {
echo "<tr>";
echo "<td>" . $data["brand"] . "</td>";
echo "<td>" . $data["model"] . "</td>";
echo "<td>" . $data["cond"] . "</td>";
echo "<td>" . $data["price"] . "</td>";
echo "</tr>";
}
echo "</table>";
} catch(PDOException $e) {
die("Error!: " . $e->getMessage());
}
?>
<html>
<body>
<div class="poster">
<img src="<?php echo $data['img_url']; ?>" width='400' height='300' ></img>
</div>
</body>
</html>
So in a different file, $_SESSION['id_u'] was defined as id_u which is in the 'account' table in my database. Now in the 'shop' table I have every sell placement written down with the corresponding user id: "id_u"
Now what I'm trying to do it Select all the sell placements that are put under that user id, but it's not working. Now for some reason it just shows a big border with nothing but a broken image icon. Not even the corresponding text.
Do a session_start(); at the top.
I am trying to create a CRUD dashboard, using a MySql backend and a bootstrap front-end with PHP and PDO to communicate with the database. I am a noob to web development, but not to coding.
The goal is to create a web app to log my patient consults. Thus, my table structure is a single "main" table and two children tables with relationships to the "main" table, named "consults" and "procedures".
I am trying to make a dashboard, where I display my "main" table, and then add two children tables below it. (Later on I will style it better, but I am trying to get this working).
The following is the best MWE I could think of (would love it if someone had a simpler solution). The first "logbook patients" table works well, and displays rows of patients well. Its the second table that is the problem, and in particular:
$sql = "SELECT * FROM proc";
if($result = $pdo->query($sql)){
if($result->rowCount() > 0){
This is the area I keep getting an error. The error is:
Fatal error: Uncaught Error: Call to a member function query() on null in /home/paincl5/public_html/logbook/logbook.php:110 Stack trace: #0 {main} thrown in /home/paincl5/public_html/logbook/logbook.php on line 110
The code at line 110 is
unset($pdo);
My full code is:
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header clearfix">
<h2 class="pull-left">Logbook Patients</h2>
<a href="create.php" class="btn btn-success pull-right" >Add New Patient</a>
</div>
<?php
// Include config file
require_once 'config.php';
// Attempt select query execution
$sql = "SELECT * FROM main";
if($result = $pdo->query($sql)){
if($result->rowCount() > 0){
echo "<div style='height:300px;overflow-y:scroll;;'>";
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>Surname</th>";
echo "<th>first_name</th>";
echo "<th>DOB</th>";
echo "<th>Hospital</th>";
echo "<th>MRN</th>";
echo "<th>Action</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = $result->fetch()){
echo "<tr>";
echo "<td>" . $row['Surname'] . "</td>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['DOB'] . "</td>";
echo "<td>" . $row['Hospital'] . "</td>";
echo "<td>" . $row['MRN'] . "</td>";
echo "<td>";
echo "<a href='read.php?id=". $row['id'] ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
echo "<a href='update.php?id=". $row['id'] ."' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
echo "<a href='delete.php?id=". $row['id'] ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "</div>";
// Free result set
unset($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
unset($pdo);
?>
</div>
</div>
</div>
</div>
// Procedure Table
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header clearfix">
<h2 class="pull-left">Procedures</h2>
<a href="create_proc.php" class="btn btn-success pull-right" >Add New Procedure</a>
</div>
<?php
// Include config file
require_once 'config.php';
// Attempt select query execution
$sql = "SELECT * FROM proc";
if($result = $pdo->query($sql)){
if($result->rowCount() > 0){
echo "<div style='height:300px;overflow-y:scroll;;'>";
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>Procedure Type</th>";
echo "<th>Procedure Name</th>";
echo "<th>Notes</th>";
echo "<th>Action</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = $result->fetch()){
echo "<tr>";
echo "<td>" . $row['procedure_type'] . "</td>";
echo "<td>" . $row['procedure_name'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>";
echo "<a href='update.php?id=". $row['id1'] ."' title='Update Record' data-toggle='modal' data-target='#myModal' ><span class='glyphicon glyphicon-pencil'></span></a>";
echo "<a href='delete.php?id=". $row['id1'] ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "</div>";
// Free result set
unset($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
unset($pdo); //Here occurs the error (line 110)
?>
</div>
</div>
</div>
</div>
You are destroying your $pdo variable with unset($pdo) so any subsequent calls are trying to run methods against a null object.
Try removing references to that unset.
I'm assuming that the $pdo object is coming from config.php. Since you're using require_once, it will only include the config file the first time the require_once is called. That's why the $pdo is destroyed and not recreated.
I have a table that shows me data from a call flow.
I need the Data from this table to be manipulated per row in such a way that all the values of my table, which are being looked up from my DB, (which are now in code) will be translated into a text value. Let me show U and explain:
My Table:
<?php
include_once "Connect2Martijn1.php";
?>
<link rel="stylesheet" href="CSSMartijn1.css">
</link>
<head>
<meta charset="iso-8859-1">
<meta name="description"content="VoizXL ">
<meta name="keywords"content="VoizXL ">
<meta name="author"content="Kenn Lo-A-Tjong">
</meta>
<title>Call Flow</title>
</head>
<fieldset>
<article class="rondehoeken">
<header>
<div class="streep1"></div>
<div class="streep2"></div>
<div class="streep3"></div>
<div class="streep4"></div>
<div class="streep5"></div>
<h1 id="artikel-titel" >Call Flow</h1>
</header>
<div id="artikel-container">
<table class="table 1">
<thead>
<title>Call Flow</title>
<meta charset = "UTF-8" />
<style type = "text/css">
table, td, th {
border: 1px solid black;
}
</style>
</thead>
<tbody>
<?php
$con=mysqli_connect("localhost","root","","voizxl_wachtrij");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Callflow");
echo "<table border='0'>
<tr>
<th>Nummer</th>
<th>Naam</th>
<th>Status</th>
<th>Time</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . $row['statusAnswered'] . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Example of (how I want to be) Translating the Data:
<?php
if ($row['statusAnswered'] ="NULL")
{
echo "Not Answered!";
}
else
{
echo "Answered!";
}
?>
What I want to Achieve is for eg. that the value in this table from $row['statusAnswered'] will be displayed in text as "Answered or Not Answered" if the Value of this row in the DB is NULL or Not...
How do I do this?
Right now I can only achieve to have 1 echo under the table saying Answered :S
No Idea how to put it per $row.
while($row = mysqli_fetch_array($result))
{
if (!isset($row['statusAnswered']))
{
$answered = "Not Answered!";
}
else
{
$answered = "Answered!";
}
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . $answered . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
if ($row['statusAnswered'] =="NULL"||$row['statusAnswered'] =="Null" || $row['statusAnswered'] =="null" || $row['statusAnswered'] =="")
{
echo "<td>Not Answered!</td>";
}
else
{
echo "<td>Answered!</td>";
}
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<table border='0'>
<tr>
<th>Nummer</th>
<th>Naam</th>
<th>Status</th>
<th>Time</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . ($row['statusAnswered']=='NULL'?'Not Answered!':'Answered!') . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
You could use;
$checkstatus = if($row['statusAnswered'] = NULL) { echo "Not Answered!"; } else {echo "Answered!";};
then call that by replacing;
echo "<td>" . $row['statusAnswered'] . "</td>";
with;
echo "<td>{$checkstatus}</td>"