Echo PHP in HTML - php

I'm trying to show my PHP in a div in the HTML section of my web page,
here is the PHP
$result = mysqli_query($connect,"SELECT * FROM adopter");
if(mysqli_num_rows($result)>0){
while($row=mysqli_fetch_assoc($result)){
echo 'AdopterID: '. $row['AdopterID'] . '<br>';
echo 'Name: '. $row['Name'] . '<br>';
echo 'Address: '. $row['Address'] . '<br>';
echo 'TelephoneNumber: '. $row['TelephoneNumber'] . '<br> <br>';
I would like this to be displayed in my div in the HTML since it is always displaying this information at the top literally above my page instead of the blank space I want in my website design.
<div class="title">
</div>
<title> PHP UPDATE DATA </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
</div>
</div>
I'm trying to put it into this section of the DIV in HTML

There is a problem about the understanding of the timing between the actions.
PHP is a server side language, so it will be processed ALWAYS before the client side language (likehtml and css)
so, if you cast echo is normal that PHP will put it on the top of the page (because is processed first, so printed first)
The way to do that is to assign the output to a variable and echo that variable is the point you need.
So instead of
echo 'AdopterID: '. $row['AdopterID'] . '<br>';
echo 'Name: '. $row['Name'] . '<br>';
echo 'Address: '. $row['Address'] . '<br>';
echo 'TelephoneNumber: '. $row['TelephoneNumber'] . '<br> <br>';
You should
$myvar = "";
while (youcode) {
$myvar .= 'AdopterID: '. $row['AdopterID'] . '<br>';
$myvar .= 'Name: '. $row['Name'] . '<br>';
$myvar .= 'Address: '. $row['Address'] . '<br>';
$myvar .= 'TelephoneNumber: '. $row['TelephoneNumber'] . '<br><br>';
}
than in some point you will have your HTML:
<div>
<?php echo $myvar; ?>
</div>
i hope i was clear enough

This is what you want. You need to place your output within the <body> for it to display as intended.
<!doctype html>
<html>
<head>
<title> PHP UPDATE DATA </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="title">
<?php
$result = mysqli_query($connect, "SELECT * FROM adopter");
if ( mysqli_num_rows($result) > 0 ) {
while ( $row = mysqli_fetch_assoc($result) ) {
echo 'AdopterID: '. $row['AdopterID'] . '<br>';
echo 'Name: '. $row['Name'] . '<br>';
echo 'Address: '. $row['Address'] . '<br>';
echo 'TelephoneNumber: '. $row['TelephoneNumber'] . '<br> <br>';
}
}
?>
</div>
</body>
</html>

Related

Including CSS file to mpdf

I am trying to implement a CSS file to my mPDF file. Here is my mPDF file:
$pdf_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>' . get_bloginfo() . '</title>';
$cssFile = __DIR__.DIRECTORY_SEPARATOR.'styles.css';
// echo $cssFile;
if (file_exists($cssFile)) {
$pdf_output .= '<style type="text/css">'.file_get_contents($cssFile).'</style>';
}
$pdf_output .= '</head>
<body xml:lang="en">
<pagebreak>
<div id="header"><div id="headerimg">
<h1>' . get_bloginfo('name') . '</h1>
<div class="description">' . get_bloginfo('description') . '</div>
</div>
</div>
<div id="content" class="widecolumn"><div id="test">Lorem ipsum dolorem sit amet</div>';
if(have_posts()) :
while (have_posts()) : the_post();
$pdf_output .= '<div class="post">
<h2>' . $pageTitle . '</h2>';
$pdf_output .= '<div class="entry">POST: ' . wpautop($post->post_content, true) . '</div>';
$pdf_output .= '</div> <!-- post -->';
endwhile;
else :
$pdf_output .= '<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn\'t here.</p>';
endif;
$pdf_output .= '</div> <!--content-->';
$pdf_output .= '
</body>
</html>';
Everything the styles.css says is completely ignored as if the file wasn't even loaded. Interestingly enough, if I append
echo '<pre><div id="output">'.$pdf_output.'</div></pre>';
exit;
to the very end I get an HTML page and everything works just fine. Where exactly is my error here? I guess it's some kind of option in the mPDF settings.
OP found out the solution from the comments, so I'll add it here just for the sake of closure:
It seems mPDF doesn't support CSS in style tags (see here: https://mpdf.github.io/css-stylesheets/introduction.html). In this case, you could replace this:
$cssFile = __DIR__.DIRECTORY_SEPARATOR.'styles.css';
if (file_exists($cssFile)) {
$pdf_output .= '<style type="text/css">'.file_get_contents($cssFile).'</style>';
}
By this:
$cssFile = __DIR__.DIRECTORY_SEPARATOR.'styles.css';
if (file_exists($cssFile)) {
$pdf_output .= '<link rel="stylesheet" href='.$cssFile.'></link>';
}
Or else use the alternative approach that's calling mPDF's WriteHTML() twice, once for the stylesheet and another for the body, as on the example on the documentation above.

PHP Custom blog system, Duplicates message for every user

so I'm making a blog system on https://aindrigo.com/blog.php?bid=1 and for some reason the post duplicates for every user that exists.. any help?
<?php
$con = mysqli_connect("localhost","root","don't try hacking my db","data");
$bid = $_GET["bid"];
$queryb = $con->query("SELECT bid,title,content FROM blogposts WHERE bid=$bid");
$queryu = $con->query("SELECT id,username,password,avatarurl FROM accounts");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="aindev's Website">
<link rel="stylesheet" href="style.css?v=2222">
<link href="https://fonts.googleapis.com/css?family=Inter&display=swap" rel="stylesheet">
<title>Adam Indrigo</title>
</head>
<body>
<div class="start">
<h1>aindev</h1>
</div>
<div class="info">
<div class="inner">
<h1>aindev's Blog</h1>
<?php
if(!$bid){
echo "<h1>Please enter a Blog ID (bid) in the url</h1>";
}
while($row = mysqli_fetch_assoc($queryb)){
while($row2 = mysqli_fetch_assoc($queryu)){
echo "<img src='" . $row2["avatarurl"] . "?v=22222' style='max-width: 60px; max-height: 60px; border-radius: 6px;'>";
echo "<h2>" . $row["title"] . " by " . $row2["username"] . "</h2>";
echo "<p>" . $row["content"] . "</p>";
}
}
?>
</div>
</div>
</body>
</html>
I don't know the problem. I tried adding it so that it only shows it for the 1 user that created it. Even adding a cid tag, but that didn't work..
So apparently I made a mistake when I added the CID tag. I added the cid to the user query and not the blog query.

How to print next row of fetched mysql data whenever button is clicked?

Have no idea on how to update or fetch the next row of data when the button is clicked.
I had successfully connected to the database and retrieved the data and tabulated them in the form of a result table.
<!DOCTYPE html>
<html>
<link rel="stylesheet" type="text/css" href="css/html.css">
<head>
<title>Welcome to my quiz! \(* 3 *)/</title>
</head>
<body>
<h1>Welcome to my quiz</h1>
<link rel="stylesheet" type="text/css" href="css/background.css">
<?php
$conn = new mysqli("jdbc:mysql://localhost:3306/quizapp?useSSL=false&serverTimezone=UTC", "myuser", "1234");
$result=mysqli_query($conn,"SELECT id, question, optionA, optionB, optionC, optionD FROM question");
$count=0;
if($row=mysqli_fetch_array($result))
{
echo $row['id']. ". " . $row['question'];
echo "<br>";
echo "<br>";
echo "A. ".$row['optionA'];
echo "<br>";
echo "<br>";
echo "B. ".$row['optionB'];
echo "<br>";
echo "<br>";
echo "C. ".$row['optionC'];
echo "<br>";
echo "<br>";
echo "D. ".$row['optionD'];
echo "<br>";
echo "<br>";
}
?>
<form> <input type="button" value="Next" onclick="alert('You clicked the button!')"> </form>
</body>
</html>
In what way I can code it so that I can proceed to the next row of data ( next question with option A, B, C, D ) when the button is clicked?

Display only the current user's details using PHP and MySQL

In this part of the code, both the score table, and the profile page display everything I want, however it is displaying EVERYBODIES details like
First Name
Surname
Email
Category
Username
However, I want it so that when the user is logged in they can only see THEIR OWN details
This is score.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
require("db_connect.php");
session_start();
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../../../favicon.ico">
<?php
$con=mysqli_connect("localhost","username","Password","Database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Score where 'Username' LIKE _['Username']");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Username</th>
<th>Score</th>
<th>Gamedate</th>
<th>QuizTitle</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Username'] . "</td>";
echo "<td>" . $row['Score'] . "</td>";
echo "<td>" . $row['Gamedate'] . "</td>";
echo "<td>" . $row['QuizTitle'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
this is profile.php
<?php
include_once 'db_connect.php';
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../../../favicon.ico">
<title>Profile page </title>
<?php
$con=mysqli_connect("localhost","Username","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT `FirstName`,`Surname`,`Email`,`Username`,`Date_Creation`FROM Users ");
while($row = mysqli_fetch_array($result))
{
echo "<br />Your <b><i>Profile</i></b> is as follows:<br />";
echo "<b>First name:</b> ". $row['FirstName'];
echo "<br /><b>Last name:</b> ".$row['Surname'];
echo "<br /><b>Email:</b> ".$row['Email'];
echo "<br /><b>Year:</b> ".$row['Username'];
echo "<br /><b>Date created :</b> ".$row['Date_Creation'];
}
mysqli_close($con);
?>
</main>
</html>
These are the errors I recieve when I try to run the page. Before I changed the select query so it picked the current user that is logged in, but it displayed everyone's information
Single quotes in SQL are used to refer to a specific string like 'Bob'. If you're trying to check the value of the column, don't put the column name in quotes as 'Username'. Instead, put the column name without quotes: where Username like
About the errors. You can use the error handling to show what's the problem on your query. Second, you can use the ID to get the data information of the logged in user. You can save the ID of the user via session, when user logged in saved the ID then use it to your where clause.
in your score.php file, on this line:
$result = mysqli_query($con,"SELECT * FROM Score where 'Username' LIKE _['Username']");
Look at your WHERE statement, seems to be wrong.
And in your profile.php, on this line:
$result = mysqli_query($con,"SELECT `FirstName`,`Surname`,`Email`,`Username`,`Date_Creation`FROM Users ");
You dont pass any WHERE statement. So, the problems are in your SQL, specifically on your wheres.

How to wrap html when running foreach loop

i have a loop like this below
foreach( $b as $entry) {
$title2 = "<!doctype html>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<head>
<link rel='stylesheet' href='../../css/bootstrap.min.css'>
</head>
<body>";
$title2 .= "<div class='data'><div id=".$id."><span style='font-family: Web'>".$entry->pubDate." </span><a href='../../fetch.php?url=".$id."' title='$entry->title' >" .$title. "</a><br/><div class='content'>".$description."</div></div></div>";
$title3 = "<!doctype html>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;'>
<head>
<link rel='stylesheet' href='../../../css/bootstrap.min.css'>
</head>
<body><div class='container'>
<div class='row'>
<div class='col-lg-12' style='margin-top:20px;'>";
$title3 .= "<div class='list-group'><span style='font-family: Malithi Web'>".$entry->pubDate." </span>'<div><h4 class='list-group-item-heading'>'" .$title. "</h4></div><br/></div></div></div></div></div></div></div>";
if (!file_exists('File')) {
mkdir('File', 0777, true);
}
if (!file_exists('./File/'.date("Y-m-d"))) {
mkdir('./File/'.date("Y-m-d"), 0777, true);
}
if (!file_exists('./File/titles/'.date("Y-m-d"))) {
mkdir('./File/titles/'.date("Y-m-d"), 0777, true);
}
$File = './File/'.date("Y-m-d").'/'."File.html";
file_put_contents($File, $title2, FILE_APPEND | LOCK_EX);
$FileT = './File2/titles/'.date("Y-m-d").'/'."File2.html";
file_put_contents($FileT, $title3, FILE_APPEND | LOCK_EX);
}//foreach end
When i save the file what i want is to save it with html head and styles..but now it is saving for each time when the loop runs..i want it for just run once.
Thank you!
Do you want to have two dayly log files with the same data but differently formatted?
Then move your header , footer and filenaming logic outside of your loop.
<?php
foreach ($b as $entry) {
$body2 .= "<div class='data'><div id=" . $id . "><span style='font-family: Web'>" . $entry->pubDate . " </span><a href='../../fetch.php?url=" . $id . "' title='$entry->title' >" . $title . "</a><br/><div class='content'>" . $description . "</div></div></div>";
$body3 .= "<div class='list-group'><span style='font-family: Malithi Web'>" . $entry->pubDate . " </span>'<div><h4 class='list-group-item-heading'>'" . $title . "</h4></div><br/></div></div></div></div></div></div></div>";
}
if (!file_exists('File')) {
mkdir('File', 0777, true);
}
if (!file_exists('./File/' . date("Y-m-d"))) {
mkdir('./File/' . date("Y-m-d"), 0777, true);
}
if (!file_exists('./File/titles/' . date("Y-m-d"))) {
mkdir('./File/titles/' . date("Y-m-d"), 0777, true);
}
$File = './File/' . date("Y-m-d") . '/' . "File.html";
$title2 = "<!doctype html>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<head>
<link rel='stylesheet' href='../../css/bootstrap.min.css'>
</head>
<body>";
$footer2 = "</body></html>";
file_put_contents($File, $title2 . $body2 . $footer2, FILE_APPEND | LOCK_EX);
$title3 = "<!doctype html>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;'>
<head>
<link rel='stylesheet' href='../../../css/bootstrap.min.css'>
</head>
<body><div class='container'>
<div class='row'>
<div class='col-lg-12' style='margin-top:20px;'>";
$footer3 = "</div></div></div></body></html>";
$FileT = './File2/titles/' . date("Y-m-d") . '/' . "File2.html";
file_put_contents($FileT, $title3 . $body3 . $footer3, FILE_APPEND | LOCK_EX);

Categories