Need help styling PHP in HTML - php

This is my PHP coding from which I am grabbing data from my DB with a select where statement and outputting it. I am trying to change the text properties such as font, colour and size on the print statement can anybody assis with how to do this?
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT fname, sname FROM tbl_stylist WHERE fname = 'liza'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
print " - Name: " . $row["fname"]. " " . $row["sname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>

You can style lists using HTML <ul> and <li> elements.
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Stylists</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT fname, sname FROM tbl_stylist WHERE fname = 'liza'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<ul>";
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<li>Name: " . $row["fname"]. " " . $row["sname"]. "</li>";
}
echo "</ul>";
} else {
echo "0 results";
}
mysqli_close($conn);
?></body>
</html>
And in css/style.css put something like this:
li {
color: red;
font-weight: bold;
font-size: 12pt;
}
You can also put the style inline in the head section:
<style type="text/css">
li {
color: red;
font-weight: bold;
font-size: 12pt;
}
</style>

You can output your markdown like this:
print "<div class = 'sampleclass'> - Name: " . $row["fname"]. " " .
Then style it by including the below in an attached css file
.sampleclass {
font-size: 15px;
}

This is only about HTML and CSS, you could output directly your strings in HTML tags with echo. The script needs to be encapsulated into the HTML's body and a pre-defined CSS classes for styling:
<!doctype html>
<html>
<head>
....
<style type="text/css">
.redBold {
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<?php
....
echo '<span class="redBold">This text is red and bold</span>';
?>
</body>
</html>
Of course you can also use inline styling:
<span style="color: red; font-weight: bold;">Also this works</span>

You can always use CSS file and define the values.
For example:
body {
font-family: arial;
font-size: 15px;
}
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
php txt
</body>
</html>
Also, you can use <span> tags and style them.
For example print this span using 'print' of php:
<span style="font-family: arial; font-size: 15px;">text</span>

Related

drawing highchart organization chart from MySQL data with PHP

I want to dynamically create an organization chart from MySQL data
As this link I have two files data.php and index.php
in the data.php I have read MySQL data and in index.php I have accessed the data from data.php file
following in data.php file
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "highcharts";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM org_chart";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$json = [];
while($row = $result->fetch_assoc()) {
//echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["amount"]. "<br>";
$json[]=[ (string)$row["parent"] , (string)$row["child"]];
}
echo json_encode($json);
} else {
echo "0 results";
}
$conn->close();
?>
following is above php file result
[["meraj","azim"],["meraj","masoma"],["meraj","jamal"],["mraj","zainab"],["azim","ajmal"],
["azim","omid"]]
and following is my index file which is to show the org chart, but it is not showing
any help please
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/sankey.js"></script>
<script src="https://code.highcharts.com/modules/organization.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<style>
.highcharts-figure, .highcharts-data-table table {
min-width: 360px;
max-width: 800px;
margin: 1em auto;
}
.highcharts-data-table table {
font-family: Verdana, sans-serif;
border-collapse: collapse;
border: 1px solid #EBEBEB;
margin: 10px auto;
text-align: center;
width: 100%;
max-width: 500px;
}
</style>
</head>
<body>
<figure class="highcharts-figure">
<div id="container"></div>
</figure>
<script type="text/javascript">
$(document).ready(function () {
var options = {
chart: {
height: 600,
inverted: true
},
title: {
text: 'Highcharts Org Chart'
},
accessibility: {
point: {
descriptionFormatter: function (point) {
var nodeName = point.toNode.name,
nodeId = point.toNode.id,
nodeDesc = nodeName === nodeId ? nodeName : nodeName + ', ' + nodeId,
parentDesc = point.fromNode.id;
return point.index + '. ' + nodeDesc + ', reports to ' + parentDesc + '.';
}
}
},
series: [{
type: 'organization',
data: [],
}],
};
$.getJSON("data.php" , function (data) {
options.series[0].data = data;
var chart = new Highcharts.chart('container', options);
});
});
</script>
</body>
</html>

Can someone explain why my MySQL table data is not being displayed?

I'm new to PHP and MySQL. I have an HTML table and form that submits the entered data to MySQL, but doesn't display the MySQL data in my HTML table. Here is an image for reference: https://i.imgur.com/OEDd6Px.png. I want the submitted data to display upon submission if possible but am unable to find a solution. Thanks in advance.
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "test";
$conn = mysqli_connect($host, $user, $pass, $db);
if (!$conn) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
if(isset($_POST["asin"]))
{
$asin = $_POST["asin"];
$category = $_POST["category"];
$submit = "INSERT INTO `user_input`(`id`, `asin`, `category`, `date`) VALUES (NULL, '$asin', '$category', CURRENT_DATE())";
$sql = mysqli_query($conn, $submit);
if (!$sql) {
die ('SQL Error: ' . mysqli_error($conn));
}
$display = "SELECT * FROM user_input";
$result = mysqli_query($conn, $display);
if (!$result) {
die ('SQL Error: ' . mysqli_error($conn));
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>testEnv</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
<style>
form {
padding-bottom: 10px;
padding-top: 10px;
}
table, thead, tbody, th, td {
padding: 4px;
border-collapse: collapse;
border: 1px solid black;
}
form {
font-size: 13px;
}
th, td {
width: auto;
text-align: center;
font-size: 13px;
}
</style>
</head>
<body>
<div class="container-fluid">
<form id="form" method="post">
<div>
<label id="asinLabel" for="asin">ASIN:</label>
<input id="asinInput" type="text" name="asin"></input>
<label id="categoryLabel" for="category">Category:</label>
<input id="categoryInput" type="text" name="category"></input>
<input id="submit" type="submit" value="Submit"></input>
</div>
</form>
</div>
<div class="container-fluid">
<table class="container-fluid">
<thead>
<tr>
<th>ID</th>
<th>ASIN</th>
<th>Category</th>
<th>Date</th>
<th>6:00 AM</th>
<th>8:00 AM</th>
<th>10:00 AM</th>
<th>12:00 PM</th>
</tr>
</thead>
<tbody id="tableBody">
<?php
while($row = mysqli_fetch_array($result));
{
echo '<tr>
<td>'.$row['id'].'</td>
<td>'.$row['asin'].'</td>
<td>'.$row['category'].'</td>
<td>'. date('m d, Y', strtotime($row['date'])) .'</td>
<td>'.$row['6am'].'</td>
<td>'.$row['8am'].'</td>
<td>'.$row['10am'].'</td>
<td>'.$row['12pm'].'</td>
</tr>';
}
mysqli_close($conn);
?>
</tbody>
</table>
</div>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script rel="script" type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script rel="script" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.js"></script>
<script rel="script" type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.js"></script>
</body>
</html>
Try to write
$display = "SELECT * FROM user_input";
$result = mysqli_query($conn, $display);
if (!$result) {
die ('SQL Error: ' . mysqli_error($conn));
}
block out side of the condition.

MySQL after insert check the first 14 characters

I am trying something to check the barcodes from the parts, because of the risk of mixing.The barcodes is made up 24 characters, the first 14 characters is the part number as the last 10 characters is the counter, is it a solution to check the first 14 characters?
Here is an example of barcode: 308739420D52AD1702600131
Mixing parts is because next barcode that is from another part:
308739420B0LAD1702600131
Therefore, my question is: Can I check when entering the first 14 characters in the database?
I can say so show me the script that I use today for introducing barcodes in the database:
<html>
<head>
<title>insert data in database using mysqli</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="main">
<h1>Barcode checker</h1>
<div id="login"><hr/>
<form action="" method="post">
<label>Barcode :</label>
<input type="text" name="TAK0010barcode" id="name" required="required" placeholder="Please enter barcode"/><br /><br />
<input type="submit" value=" Submit " name="submit"/><br />
</form>
</div>
</div>
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "barcode";
$password = "barcode";
$dbname = "barcode";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO tak0010 (TAK0010barcode)
VALUES ('".$_POST["TAK0010barcode"]."')";
if ($conn->query($sql) === TRUE) {
echo "<span style='color: green; font-size: 40px; font-weight: bold'>SUCCES!!</span>";
} else {
echo "<span style='color: red; font-size: 40px; font-weight: bold'>EROARE!!</span>";
}
$conn->close();
}
?>
</body>
</html>
Thank you.
You could use PHP function substr():
$control = '308739420D52AD';
$test = substr($_POST["TAK0010barcode"], 0, strlen($control));
if($control == $test){
$sql = "INSERT INTO tak0010 (TAK0010barcode)
VALUES ('".$_POST["TAK0010barcode"]."')";
// etc...
Documentation: http://php.net/manual/en/function.substr.php
UPDATE: Extended example, replace all within your php code with this:
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "barcode";
$password = "barcode";
$dbname = "barcode";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$control = '308739420D52AD'; // YOUR BAR CODE IDENTIFIER
$test = substr($_POST["TAK0010barcode"], 0, strlen($control)); // BEGINNING OF THE POSTED BAR CODE.. SET TO SAME LENGTH AS BAR CODE IDENTIFIER
if($control == $test){ // CHECK IF STRINGS ARE ALIKE
$sql = "INSERT INTO tak0010 (TAK0010barcode)
VALUES ('".$_POST["TAK0010barcode"]."')";
if ($conn->query($sql) === TRUE) {
echo "<span style='color: green; font-size: 40px; font-weight: bold'>SUCCES!!</span>";
} else {
echo "<span style='color: red; font-size: 40px; font-weight: bold'>EROARE!!</span>";
}
$conn->close();
} else { // STRINGS DO NOT MATCH!
echo "<span style='color: red; font-size: 40px; font-weight: bold'>The barcode don't match the part number. Please use a barcode starting with ".$control."</span>";
}
}
?>

Unable to Retrieve data from the postgress database using Php?

I have created a Php file, which fetches the data from the Postgres Database, but when I run the file it doesn't displays the contents (records) which are present in the database table.
I ran this using both Apache server and Python SimpleHTTPServer also.
I restarted the Postgres server too.
Below is the code of that file:
index.php
<!DOCTYPE html>
<html>
<head>
<title>
webpage
</title>
</head>
<body>
<h1>
INFORMATION OF DATABASE
</h1>
<?php
$host = "localhost";
$user = "myappuser";
$pass = "password";
$db = "myapp";
echo "\n test";
$con = pg_connect("host=$host dbname=$db user=$user password=$pass")
or die ("Could not connect to server\n");
$query = "SELECT * FROM app1_snippet";
$rs = pg_query($con, $query) or die("Cannot execute query: $query\n");
while ($row = pg_fetch_assoc($rs)) {
echo $row['id'] . " " . $row['name'] . " " . $row['phone_no']. " " . $row['status'];
echo "\n";
}
pg_close($con);
?>
</body>
</html>
Ya figured out how to do.
below is the Code.
<!DOCTYPE html>
<html>
<head>WELCOME</head>
<body style="background-color:#E4E5E7">
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
table#t01 {
width: 100%;
background-color: #f1f1c1;
}
</style>
</head>
<body>
<?php
$url= 'http://yoururltypehere.com';
$options = array(
'http' => array(
'header' => array(
'name: '.$_GET['name'],
),
'method' => 'GET',
),
);
$context = stream_context_create($options);
$output = file_get_contents($url, false,$context);
$arr = json_decode($output,true);
?>
<table style="width:100%">
<tr>
<th>ID</th>
<th>NAME</th>
<th>PHONE_NO</th>
<th>STATUS</th>
</tr>
<br>
<?php
for($x=0;$x<count($arr);$x++)
{
?>
<tr>
<td><?php echo $arr[$x]['id']; ?>
<td><?php echo $arr[$x]['name']; ?>
<td><?php echo $arr[$x]['ph_no']; ?>
<td><?php echo $arr[$x]['stats']; ?>
</tr>
<?php
}
?>
<br>
</table>
</body>
</html>

No database selected: login php code

<?php
session_start();
$con=mysqli_connect("localhost","xxx","xxxxxx","xxx");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$eadd = $_POST['eadd'];
$pass = $_POST['pass'];
$eadd = htmlspecialchars(stripslashes(strip_tags($eadd)));
$pass = htmlspecialchars(stripslashes(strip_tags($pass)));
if (filter_var($eadd, FILTER_VALIDATE_EMAIL)) {
$sql = mysql_query("SELECT * FROM accounts WHERE Emailadd = '$eadd' AND Password = '$pass'");
if(!$sql){
die('There was an error in query '. mysql_error());
}
$count = mysql_numrows($sql) or die(mysql_error());
if ($count<=0)
{
echo "
<html>
<style>
body{
background-color:#cccccc;
}
#error{
position:relative;
margin:auto;
top:20px;
width:320px;
height:55px;
background-color:#63a8d7;
border:1px solid #2a262a;
}
#errorC{
position:absolute;
top:20px;
left:20px;
font: 14px arial, tahoma;
}
</style>
<body>
<div id=error>
<div id=errorC>
Incorrect Email Address and Password! <a href=index.php>GO BACK</a>
</div>
</div>
</body>
</html>
";
}
else
{
//have them logged in
$_SESSION['account'] = $eadd;
header('location:home.php');
}
mysqli_close($con);
} else {
echo "
<html>
<style>
body{
background-color:#cccccc;
}
#error{
position:relative;
margin:auto;
top:20px;
width:320px;
height:55px;
background-color:#63a8d7;
border:1px solid #2a262a;
}
#errorC{
position:absolute;
top:20px;
left:20px;
font: 14px arial, tahoma;
}
</style>
<body>
<div id=error>
<div id=errorC>
Invalid Email Address! <a href=index.php>GO BACK</a>
</div>
</div>
</body>
</html>
";
}
?>
Why there is an error "No database selected"? My mysqli_connect is correct. I have another register php code, using which I can register some email address using that connection.But here in login php code, I can't login with the user email address.
From the above code its look like you are using mysqli_connect for database connection and mysql_query for query execution. Use mysqli_query instead of mysql_query . Like this
mysqli_query("SELECT * FROM accounts WHERE Emailadd = '$eadd' AND Password = '$pass'");
Actually, you need to select a database after authentication.
We need to follow these steps while making a database connection.
Create a connection
Select the database
Fire the query
Use query result
Close the connection
Sample Code is here
Firstly, You need to add this line in your code after login
$db_select = mysql_select_db("myDatabase", $con);
Secondly, you need to pass $con as second parameter after selecting your database So your query statement becomes like this
$sql = mysql_query("SELECT * FROM accounts WHERE Emailadd = '$eadd' AND Password = '$pass'", $con);

Categories