PHP display $variable in HTML before it is defined - php

I have a webpage called search.php
I want it to have a title/tab tag that looks like this:
<head>
<title><?php echo $number_count; ?> items found - Mysearch </title>
<head>
But the variable $number_count is 0 until later php scripts are called to query the database and display the items one at a time. At the end of the HTML I can easily display <p> <?php echo $number_count; echo "items found" ?> </p> and it works with the correct count.

It must be defined first.
Best (and only in this case) approach to do it is to do calculations first, then generate website output.
EDIT:
You can do it like that:
<?php
$count = 125;
?><!DOCTYPE html>
....
<title>Title count: <?php echo $count ?></title>
....

Related

Include 'count.html.php'

This book 'php and mysql novice to ninja' said that to include the files count.html and count.php, I must type the following into the bottom of the count.php file:
include 'count.html.php';
However, this does not seem to work unless it is just count.html without the .php.
Is this book accurate or not? I cannot find anything online about this way of including.
I've seen two separate lines of including tags such as:
include 'a.html';
include 'b.php';
Sorry for the shit formatting. I've never posted to this site before.
count.php:
<?php
$output = '';
for ($count = 1; $count <= 10; $count++) {
$output .= $count . ' ';
}
include 'count.html.php';
?>
count.html:
<!DOCTYPE html>
<html lang="eng">
<head>
<meta charset="utf-8">
<title>counting to ten</title>
</head>
<body>
<p>
<?php echo $output; ?>
</p>
</body>
</html>
include looks for a file name exactly matching what you put in the quotes, it will not include two different files at once as you are describing.
Your count.html file contains PHP code in it, so it shouldn't have the .html extension, it would most commonly use .php or since it looks mostly like an html template file, you could use .phtml. The extensions .php and .phtml do the exact same thing, but generally by convention if you have a file exclusively with php code, you would name it .php, and it it has a lot of html, you might want to name it .phtml. Either is fine, but not the plain .html.
Assuming you decided to name it count.phtml, then your two files would then look like:
count.php
<?php
$output = '';
for ($count = 1; $count <= 10; $count++) {
$output .= $count . ' ';
}
include 'count.phtml';
?>
count.phtml
<!DOCTYPE html>
<html lang="eng">
<head>
<meta charset="utf-8">
<title>counting to ten</title>
</head>
<body>
<p>
<?php echo $output; ?>
</p>
</body>
</html>
The author was showing his personnel method, which is confusing. Basically using the .php at the end of .html.php as a reminder to himself that it was related to the .php document. It made the html code appear as php, and the author was not the clearest.

Why does echo display variables as their names, and also the code following ";?

These are my problems:
Note: the single queotes (') are not actually displayed
echo $variable; returns nothing
echo "$variable" returns '$variable'
returns 'stuff"; code ?'
this problem appears both in a portable version of Chrome and in Firefox
here is my code for reference:
<!DOCTYPE HTML>
<html lang = "it" >
<head>
<meta charset="utf-8">
<meta name = "keywords" content = "catcher, gioco, top, 10">
<meta name="author" content="Luca Ballarati">
<?php require 'connessione'; ?>
<title> TOP 10 </title>
</head>
<body style = "background-color: white;">
<h1>PROVA:</h1>
<?php
apri_conn();
mysqli_select_db($con,"catcher");
$sql="SELECT Username, Record FROM utenti ORDER BY Record LIMIT 10";
$result = mysqli_query($con,$sql);
chiudi_conn();
echo '<b><center><h2>TOP 10 GIOCATORI e RECORD</h2></center></b><br><br>.';
?>
<?php
$i=0;
while ($i < 11) {
$i++;
$NomeUtente = mysql_result($result,$i,"NomeUtente");
$Record = mysql_result($result,$i,"Record");
$str = "AAA"
echo "<p>AAA</p>";
}
?>
</body>
I think I'm using the correct syntax, from the comparisons I made with other posts on this site and others. Am I wrong, or is there something else at play?
Apart from you numerous errors contained within this code, you shouldn't use echo on a HTML document, you can simply close the PHP tag. For example
if( 1 == 1 ) { //True every time
?>
<p>AAA</p>
<?php } ?>
is almost the same as you would write :
if( 1 == 1 ) { //True every time
echo "<p>AAA</p>";
}
Also if you want to mix HTML with PHP code, I would advise you to use endif and endwhile for your loops.

Querying database without using echo, producing results for design

there's something that's giving me a bit of a headache at the moment. I am querying a database retrieving some data after clicking a link from a previous page.
Easy enough I get to play about with some code in the 'echo'. Problem is in the ‘echo’ as I need to put in php includes and general html/design code for design purposes. Is there a way or a method where I can write the code but then call variables again later and have the ability to place other php code such as includes? Basically something which is going to let me have to have the code but then allow me to concentrate on the design aspect of my page.
Any help much appreciated.
<?php
if(!empty($_GET['book_url']))
{
$sql = "
SELECT articles.id, articles.order_ref, articles.art_title, articles.art_book, articles.art_url, book.id AS bookid, book.book_name AS book_name, book.book_url AS book_url
FROM articles
LEFT JOIN book ON articles.art_book = book.id
WHERE book_name = \"" .$_GET['book_url'] . "\"
ORDER BY id ASC
";
// then do the query, etc....
}
$results = $db->query($sql);
if($results->num_rows) {
While($row = $results->fetch_object()) {
echo "
////////Show Stuff
";
?>
You can separate the code and the HTML by putting the code in <?php ?>. Then, in your HTML you can have short code snippets that print the work you did in the code sections. Like this (there might be a syntax error or two, I don't have Apache to test):
<?php
$name = $_GET['name'];
function stuff() {
// this would normally be long and complicated
return "stuff";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>title</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
</head>
<body>
<!-- try to keep these short so you can focus on design-->
<h1>The name is: <?php echo $name; ?></h1>
<h1>The stuff is: <?php echo stuff(); ?></h1>
</body>
</html>
Alternatively, you can use a framework like Laravel which will provide a templating system.

How to remove warning in echoing a string constant in PHP

I declared a string constant named LABTITLE. When I tried to echo it in the title tag and run the code, the title of the webpage shows a warning. Is there something wrong with my code?
Our professor specifically instructed to print the Title in this way.
The Title tag:
<title>
<?php
echo constant("LABTITLE");
?>
</title>
The PHP code:
<?php
define("LABTITLE", "Laboratory Activity No. 2");
?>
Then whenever I run, this shows in the Title tab:
Warning : constant(): Couldn't find constant LABTITLE in C:\xampp\htdocs\Lab2\index.php on line 12
Define Your constant first then you can get value. looks like you are using constant value before you define.
<?php
define("LABTITLE", "Laboratory Activity No. 2");
?>
<title>
<?php
echo constant("LABTITLE");
?>
</title>
Instead you could directly use
<title>
<?php
echo LABTITLE;
?>
</title>
given following is included.
<?php
define("LABTITLE", "Laboratory Activity No. 2");
?>

View PHP file with includes included

Is there a tool that will show me what my .php file looks like AFTER the "preprocessor" goes through it and includes all the "include" and "require" files? In other words, if I have a file called "index.php":
<?php
#My root index page
include 'vars.php';
include 'header.php';
include 'body.php';
?>
If the files are:
vars.php:
<?php
SITENAME = "MySite";
$where = "here";
include 'ThatOne.php';
?>
header.php:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<title>My page</title>
</head>
body.php:
<?php
print "<body>\n";
print "<H1>Hello World</H1>\n";
print "</body>\n";
?>
ThatOne.php
<?php
$ThatOne = "This One";
?>
I would like to be able to see that this page results in a working page that looks like:
<?php
#My root index page
SITENAME = "MySite";
$where = "here";
$ThatOne = "This One";
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<title>My page</title>
</head>
<?php
print "<body>\n";
print "<H1>Hello World</H1>\n";
print "</body>\n";
?>
This is obviously a contrived example, but I am working with a site with includes that are nested about 4 deep, and I would like to make sure that what PHP is working with is really what I want it to be working with.
I am fairly new to PHP, but I asked this question of a colleague who is also working with PHP and his reaction was "That would be really usedful, but I have never seen anything that will show that."
If you wish, you may create your own tool. Just use get_included_files and file_get_contents, as follows:
<?php
#My root index page
include 'vars.php';
include 'header.php';
include 'body.php';
$included_files = get_included_files();
foreach ($included_files as $filename) {
echo file_get_contents( $filename );
}
?>
According to the manual, get_included_files() will get any required or required_once files, too (see here). The only limitation is that you'll only be able to see text or HTML output, so vars.php wouldn't have visible content. If you change a pure php file by giving it an extension of .phps, then you can see the PHP source code which can appear highlighted.

Categories