show php variable in html heading [duplicate] - php

This question already has answers here:
php variable in html no other way than: <?php echo $var; ?>
(7 answers)
Closed 5 years ago.
i want to print a php variable in the html h1 tag. Important thing to know is that html is defined within the php as follow:
<?php
$test=" <html>
<h1><?php echo (rand(10,1000000)); ?></h1>
</html> ";
echo $test;
?>

You can write either 1:
<?php
$a="hi";
?>
<html>
<h1><?php echo $a;?> </h1>
</html>
or 2:
<?php
$a="hi";
echo "<html><h1>".$a."</h1></html>";
?>

use the following
<?php
$a="hi";
$prints =" <html>
<h1> $a</h1>
</html>";
echo"$prints";
?>

You can directly 'show' php variables by 'echoing' them in the html. For example consider this code.
<?php
include_once "./php/db.php";
$result=$conn->query("SELECT * FROM medtable");
while($row=mysqli_fetch_array($result)){
echo '<form id="form-'.$row["Product"].'"type="POST">';
echo '<table class="table table-hover table-condensed" id="table-'.$row["Product"].'">';
echo '<tr>';
echo'<td style="width:50%" name="name-'.$row["Product"].'">'.$row["Product"].'</td>';
echo '<td style="width:20%"> <input type="number" name="amount" id="num-'.$row["Product"].'" min="0" value="'.$row['Amount'].'"</input></td>';
echo '<td style="width:10%">'.$row["Price"]. '</td>';
echo '<td id="subtotal-'.$row["Product"].'"style="width:10%" value="'.$row["Subtotal"].'">'.$row["Subtotal"].'</td><input type="hidden" name="hidden-'.$row['Product'].'"id="hidden-'.$row["Product"].'"value="'.$row["Subtotal"].'"</input>';
echo '<td style="width:10%"><input type="button"id="'.$row["Product"].'"
name="'.$row["Product"].'"class="btn btn-primary" value="Submit">
</input></td>';
echo '<td><input type="hidden" name="product" value="'.$row["Product"].'"></input></td>';
echo "</tr>";
echo "</table></form>";
}
?>
As you can see a query is made from a table and html tags are then echod into the html file. Hope this helps :)

Related

Need Assistance with following ```HTML``` Error?

I am getting the following validation error, it claims I have "misplaced non-space characters inside the table" I have tried everything and for the life of me cannot fix this error. The error is the following
Here is the PHP/HTML being used for
<?php
$title = "Alter Records";
include('includes/head.php');
include('includes/nav.php');
ini_set('display_errors', 0);
require_once('config.php');
require_once('db_class.php');
$connection = new dbController(HOST,USER,PASS,DB);
$sql = "select id,name, image,location from location";
$results = $connection->getAllRecords($sql);
//var_dump($results);
?>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Location</th>
<th>Image</th>
<th colspan="2">Make Changes</th>
</tr>;
<?php
foreach ($results as $row){
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['location']}</td>";
echo "<td><img class='thumb' src='{$row['image']}' alt='{$row['name']}'> </td>";
echo "<td><a href=''>Update</a></td>";
echo "<td><a href='delete_record.php?id={$row['id']}'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
include('includes/footer.php')
?>
Here is the code inside validator.w3.org
↩
<!DOCTYPE html>↩
<html lang="en">↩
<head>↩
<meta charset="utf-8">↩
<title>Alter Records</title>↩
<link href="https://fonts.googleapis.com/css2?family=Quicksand&display=swap" rel="stylesheet">↩
<link href="https://fonts.googleapis.com/css2?family=Bellota+Text:ital,wght#1,300;1,400&display=swap" rel="stylesheet">↩
<link rel="stylesheet" href="css/style.css">↩
</head>↩
<body>↩
<header><h1> TOURISM VICTORIA</h1></header>↩
<nav>↩
<ul class="flex-nav">↩
<li>Home</li>↩
<li>Add</li>↩
<li>Alter</li>↩
<li>↩
<form class="search-form" action="search.php" method="get">↩
<input type="text" name="search">↩
<button type="submit">Search</button>↩
</form>↩
</li>↩
</ul>↩
</nav>↩
<table>↩
<tr>↩
<th>Id</th>↩
<th>Name</th>↩
<th>Location</th>↩
<th>Image</th>↩
<th colspan="2">Make Changes</th>↩
</tr>;↩
<tr><td>1</td><td>Melbourne ShowGrounds</td><td>Royal Melbourne Showgrounds. Ascot Vale, Victoria, Austra
Any assistance with this would be greatly appreciated. If I am still unclear, please ask for further clarification.
just seeing your last include will also result in an error, you will need to end with ;

How to use an HTML button to trigger a function?

I am populating an HTML table with information from a database and want to trigger it with a button.
Can someone help me with this, and perhaps add some links to relevant website with examples?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div method="GET">
<table>
<thead>
<tr>
<th>ID</th>
<th>Nombre</th>
<th>Usuario</th>
</tr>
<?php
include "php/populate.php";
?>
</thead>
</table>
<input type="button" value="button" id="button">
</div>
</body>
</html>
<?php
$result = mysqli_query($enlace,"SELECT * FROM tb_personas");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['txt_nombre'] . "</td>";
echo "<td>" . $row['txt_usuario'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($enlace);
?>
You need to use jQuery (the easiest way to use ajax)
take a look at this question on stackoverflow how to call a php script on a html button click
additionally you are including your data at table's header, instead they should be included in table's body.
<table>
<thead>
<tr>
<th>ID</th>
<th>Nombre</th>
<th>Usuario</th>
</tr>
</thead>
<tbody>
<?php include "php/populate.php"; ?>
</tbody>
</table>

How to give a href in a PHP while table

I've gotten into trouble while working on a school project, and I'm stuck.
I'm making a website where you can easily search for amusement parks.
I made a while loop with PHP and put information from the DB into the table which I looped. I've put all the information in a TD and I want to create a-href so when I click on one of the first parks it will redirect to an example: example.php?park=1 for the second park example: example.php?park=2.
Here is the while:
<div class="pretpark-container">
<table id="pretpark-table">
<?php while($pretpark = mysqli_fetch_array($result)){
echo "<tr>";
if(file_exists(__DIR__ . '/img/'. $pretpark['ParkImage'])): ?>
<td><img height="125px;" width="200px;"" src="/WikiParksWeb/Wikiparks-Website/img/<?php print($pretpark['ParkImage']); ?>"></td>
<?php else: ?>
<?php endif;
echo "<td>".$pretpark['ParkName']."</td>";
echo "<td>".$pretpark['ParkLocation']."</td>";
echo "<td>".$pretpark['ParkOpeninsTime']."</td>";
echo "<td>".$pretpark['ParkPrices']."</td>";
echo "<td>".$pretpark['ParkShortDescription']."</td>";
echo "</tr>";
?>
<tr class="filler"></tr>
<?php
}
?>
</table>
</div>
Each pretpark has an ID, the DB table of it is called ParkId.
Can someone help me where I have to put the a href. And how I can do it like its like this example: example.php?park= ParkId
I hope everything is understandable what I just said :/
Thanks,
You can do like this:
<div class="pretpark-container">
<table id="pretpark-table">
<?php while($pretpark = mysqli_fetch_array($result)){
echo "<tr>";
if(file_exists(__DIR__ . '/img/'. $pretpark['ParkImage'])): ?>
<td><img height="125px;" width="200px;"" src="/WikiParksWeb/Wikiparks-Website/img/<?php print($pretpark['ParkImage']); ?>"></td>
<?php else: ?>
<?php endif;
echo "<td>".$pretpark['ParkName']."</td>";
echo "<td>".$pretpark['ParkLocation']."</td>";
echo "<td>".$pretpark['ParkOpeninsTime']."</td>";
echo "<td>".$pretpark['ParkPrices']."</td>";
echo "<td>".$pretpark['ParkShortDescription']."</td>";
echo "<td><a href='example.php?park=".$pretpark['Parkid']."'> More Details</a></td>";
echo "</tr>";
?>
<tr class="filler"></tr>
<?php
}
?>
</table>
</div>
<div class="pretpark-container">
<table id="pretpark-table">
<?php while($pretpark = mysqli_fetch_array($result)){
echo "<tr>";
if(file_exists(__DIR__ . '/img/'. $pretpark['ParkImage'])): ?>
<td><img height="125px;" width="200px;"" src="/WikiParksWeb/Wikiparks-Website/img/<?php print($pretpark['ParkImage']); ?>"></td>
<?php else: ?>
<?php endif;
echo "<td>".$pretpark['ParkName']."</td>";
echo "<td>".$pretpark['ParkLocation']."</td>";
echo "<td>".$pretpark['ParkOpeninsTime']."</td>";
echo "<td>".$pretpark['ParkPrices']."</td>";
echo "<td>".$pretpark['ParkShortDescription']."</td>";
**echo "<td><a href='detailpage.php?id=".$pretpark['ParkId']."'></td>";**
echo "</tr>";
?>
<tr class="filler"></tr>
<?php
}
?>
</table>
</div>
I have added a line with bold formatting. You can get clue from this.
instead of the line
echo "<td>".$pretpark['ParkName']."</td>";
it would be something like..
echo "<td>".$pretpark['ParkName']."</td>";
backslashes are needed to escape the quotes so they come out in the HTML and are not interpreted by php

Update query is not working correctly

I am making a code for editing a text area. The query runs correctly without any errors but the textarea is not updated. Can anyone help me with it please?
<html>
<body bgcolor="skyblue">
<form method= "post" enctype="multipart/form-data" action="upd.php">
<table align="center" width="795" border="2" bgcolor="#CCC">
<?php
$i=0;
$sel="select * from video";
$sel_query=mysqli_query($con, $sel);
while($row=mysqli_fetch_array($sel_query)){
$i++;
$var_id=$row['v_id'];
$var=$row['video'];
?>
<tr align="center">
<td align="right">
<br>
<h2>Upadte Video <?php echo $i; ?> Link: </h2>
<button type="submit" name="btn">
<a name="upd" href="upd.php?insert_brand=<?php echo $var_id; ?>&fram=<?php echo $var; ?>">Update</a>
</button>
<td align="left"><br><textarea name="vids" rows="4" cols="50"><?php echo $var; ?>"</textarea></td>
</td>
</tr>
<?php
}
?>
</table>
</form>
upd.php
<?php
include ("includes/db.php");
$check=#$_GET['insert_brand'];
if(isset($_POST['btn'])){
$vids=$_POST['vids'];
$upds="UPDATE `video` SET `video`='$vids' WHERE `v_id`='$check'";
$query=mysqli_query($con, $upds);
if($query){
echo 'The query is executed';
}
else{
echo 'There was an error'. mysqli_error($con);
}
}
?>
Have you tried mysql_real_escape_string() to retrieve textarea data.

hyperlink trouble in php echo'd table

I"m having trouble with my code hopefully someone can help.
I'm trying to call information using "php echo" to display information in table form and it works except for the links which doesn't recognize the $id. If I don't put it in the table form it works fine but it is not aesthetically appealing.
Any suggestions would be greatly appreciated!
<?php
session_start();
if(!isset($_SESSION['name'])){
header("location: ../index.php");
exit();
}
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once("../scripts/connect.php");
// Delete Item Question to Admin, and Delete Product if they choose
if (isset($_GET['deleteid'])) {
echo 'Do you really want to delete messages with ID of ' . $_GET['deleteid'] .'? Yes | No';
exit();
}
if (isset($_GET['yesdelete'])) {
// delete from database
$id_to_delete = $_GET['yesdelete'];
$sql = mysql_query("DELETE FROM `mystore`.`messages` WHERE `messages`.`id` = '$id_to_delete' LIMIT 1") or die (mysql_error());
}
$messages = "";
$sql = mysql_query("SELECT * FROM messages ORDER BY msg_date DESC LIMIT 20");
$count = mysql_num_rows($sql);
if($count > 0){
while($row = mysql_fetch_array($sql)){
echo '<tr>';
echo '<td>'.$row['msg_name'].'</td>';
echo '<td>'.$row['msg_email'].'</td>';
echo '<td>'.$row['msg_subject'].'</td>';
echo '<td>'.$row['msg_date'].'</td>';
echo '<td>Reply</td>';
echo '<td>Delete</td>';
echo '</tr>';
}
}else{
$messages = "<b>There are no messages in the database at this moment</b>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Admin Messages</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="style/forms.css" media="screen">
<link rel="stylesheet" href="style/main.css" media="screen">
</head>
<body>
<div id="main_wrapper">
<?php include_once("templates/tmp_header.php"); ?>
<?php include_once("templates/tmp_nav.php"); ?>
<section id="main_content">
<h2 class="page_title">Messages</h2>
<br/>
<table width="730" cellspacing="0" cellpadding="3" border="1">
<tr>
<td align="center" width="100">From</td>
<td align="center" width="300">Email</td>
<td align="center" width="300">Subject</td>
<td align="center" width="100">Date</td>
<td align="center" width="100">Actions</td
></tr>
<?php echo $messages; ?>
</table>
</section>
<?php include_once("templates/tmp_aside.php"); ?>
<?php include_once("templates/tmp_footer.php"); ?>
</div>
Please change
echo '<td>Delete</td>';
to
echo "<td><a href='admin_messages.php?deleteid=$id'>Delete</a></td>";
when trying to print out a variable the main string has to be wrapped in double quotes.
If you want to interpolate variables in PHP, you need to use double quotes. echo '$id' will literally print $id, whereas echo "$id" will print the value of the variable. However, I would recommend an alternative approach. Don't use PHP where it isn't needed. There's no need to use echo so much.
I would change the contents of your loop to this:
?>
<tr>
<td><?=$row['msg_name']?></td>
<td><?=$row['msg_email']?></td>
<td><?=$row['msg_subject']?></td>
<td><?=$row['msg_date']?></td>
<td>Reply</td>
<td>Delete</td>
</tr>
<?php
The <?=$id?> is shorthand for <?php echo $id?> and is supported by default in PHP versions >=5.4.0. You can also use it in previous versions if you enable short_open_tags.
As stated in the comments, you should really be using mysqli functions, as mysql functions are deprecated.

Categories