random unwanted '1' appearing in html - php

I'm getting random 1's throughout my html page in chrome.
Can't seem to find this issue on forums or SO.
This is an image of how the page is being rendered:
My php file looks like this:
<!DOCTYPE html>
<html lang="en">
<?= include('head.php') ?>
<body>
<div id="wrapper">
<?= include('nav.php') ?>
<div id="page-wrapper">
I have no clue what's causing this, can anybody push me in the right direction?

From my comments:
Simple: it's the ='s - <?= is short hand for "echo" so remove those.
and change your <?= to <?php since short tags are not enabled on your system.
The 1's are boolean which tell your includes did work/that the file(s) exist.
If it would have failed, then those would have appeared as 0's.

Related

How to add meta tag's values for seo using PHP?

I am using one header file for every page which will show the HTML head(which includes meta tags and other CSS links)
Here I have used everything as dynamic as if I like in the case of canonical tags I have used $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_HOST'] and all other links to CSS are also accessible even if it can be any file from any directory.
Now I want to create a one-time title meta tag & description tags like this will be my main file looks like
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo $page_title; ?></title>
<meta name="description" content="<?php echo $page_content; ?>" />
//Other meta tags & linking to css & js files
</head>
<body>
// Content ........
</body>
</html>
Now here is the twist is that many people will say that just use
$page_title = "Here is the title of the specific page";
$page_content = "Here goes the long-form description describing the page of specific page";
Yes, this would have worked out if the pages were different and had the same place to put that all before including the header file.
But my index page looks like this let me explain it too. (Using Bulma as a framework)
<?php
require 'include/db_connect.php';
require 'include/header.php';
?>
<form name="submitform" method="POST">
<div class="columns is-multiline" id="wrapper">
<div class="column is-6-desktop is-12-tablet" id="main_content">
<div class="box">
<?php
if($country)
{
?>
<?php require 'inc/country.php'; ?>
<?php
}
else if($country && $state)
{
?>
<?php require 'inc/country_state.php' ?>
<?php
}
else if($country && $state && $district)
{
?>
<?php require 'inc/country_state_district.php'; ?>
<?php
}
else
{
?>
<?php require 'inc/other_than.php'; ?>
<?php
}
?>
</div>
</div>
</div>
</form>
<?php require 'footer.php'; ?>
</body>
</html>
The main point here is that I am using the dropdown button which gets auto-submitted using js that's not relevant here but from the top, I have just explained what is the structure of my code.
Now as you can see the if-else structure which includes other files that create dynamic pages but their code starts only from the body and not from the head directly so I am not able to add those title tags and descriptions.
Now how to add title & description tags uniquely to each of these pages.
Any solutions, please... Thanks in Advance
Like already written in the comments. You should define the vars ahead of require 'include/header.php'; or use an MVC structure.
If you still want to stay with the existing code ob_get_contents() might help you.
Use ob_start(); at the beginning to tell PHP to not print any output and instead write the output to a buffer.
You can then at a later point use ob_get_contents(); to fetch the output buffer and print it.
You have to search in the output buffer for an identifier like %page_title% then and replace it with the actual value before sending the output.
This may look like following:
echo str_replace(%page_title%, $page_title, ob_get_contents());
Still I'd rather suggest restructuring your code, as the solution with output buffer is slower, uses more memory and is poor to debug.

PHP - Load header from another file

I've run into problem whereby I'm not able to include a header file into a PHP file
In the code below, I've removed the original content as it's quite big & anyway what the code does is irrelevant to including the header file
MAIN.PHP
<?php
php content
?>
<html>
<head>
</head>
<body >
<div>
header content
</div>
<br/>
<div>
</div>
</body>
</html>
I would like to have the following code in the header.php file & include it on all pages.
<div>
header content
</div>
When I do so with the main.php file looking as below, the page draws blank.
<?php
php content
?>
<html>
<head>
</head>
<body >
<div>
<? php include('header.php') ?>
</div>
<br/>
<div>
</div>
</body>
</html>
I've tried debugging this & following a few solutions on stackoverflow, but evidently have failed to achieve what I'm after.
Could I please request a second pair of eyes to help me spot the problem?
Well, first of all, I don't think you copied the code correctly to here.
As my debugging eyes can see, you got 2 issues:
Line 1: < is missing at the start. Change it to <?php. (Maybe
copy paste error?)
<?php include('header.php'); is missing closing tag. Change it to:
<?php include('header.php'); ?>
If you get a blank page, you probably have some errors. Try finding the "error.log" file in you working directory or add the following code at the start of the page:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Implement "pagename.html#id_of_section" similar functionality in php

I am currently working on a php project in which i needed to implement a page redirection.In HTML the page redirection
<a href=abc.html#section>Pagename<a/>
works proper to get me to any section of the page. But in php i tried to implement the same with tag and include method which gives me error.the code i tried is
if(?tag==tagname)
include('pagename.php#section');
How can i redirect visitor to some other page on some section which is in middle of the page?? implementing abc.html#section in php. Please give your guidance. Thanx in advance.
The structure is
<html>
<body>
<header>
<Navigation menu>
<span>Home</span>
</end Navigation menu>
<php>
if(?tag==home)
include(home.php);
</end php>
<footer>
</end body>
</end html>
This works fine but it will always open new page from the top. I needed to take users to home.php#section. Please help me out.
use $_GET to access query-paramters.
e.g.:
index.php?foo=bar&some=thing
$_GET['foo']; //contains bar
$_GET['some']; // contains thing
<html>
<body>
<header>
<Navigation menu>
<span>Home</span>
</end Navigation menu>
<?php
if(isset($_GET) && $_GET['tag'] && $_GET['tag'] == 'home') {
include(home.php);
}
?>
<footer>
</end body>
</end html>

PHP include not working properly in XAMPP and WAMP changing source code causing stray tag issues

I have an interesting issue with webservers, hope you could help me with it.
When I include php extansion files, though they appear in the source code but causing problems eg. stray tags. The interesting part is without include and include the source code seems exactly the same, however browsers and online validator give errors (and literally i dont see any difference between them).
Have anyone met with this problem before?
UPDATED with the source code!
Anyway it has to be a configuration problem since its working fine in the school.
<!DOCTYPE HTML>
<html>
<?php include 'head.php'; ?>
<body>
<header>
<h1 class="logo">Logo</h1>
<nav>
<ul>
<li>Index</li>
<li>Downloads</li>
<li>Forum</li>
<li>Contact</li>
</ul>
</nav>
<div class="clear"></div>
</header>
<div id="container">
<aside>
<div class="widget">
<h2>Widgets</h2>
<div class="inner">
Widgets
</div>
</div>
</aside>
<h1>Main</h1>
<p>Content</p>
</div>
<footer>
</footer>
</body>
</html>
content of included file:
<head>
<title>Weboldal neve!</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/screen.css"/>
</head>
Here are the warnings the validator gives(these errors and warning are not showing up when i copy paste the included part only 2 warnings which is not that important, because the browser not complaining about them):
Unable to Determine Parse Mode!
No DOCTYPE found! Checking with default HTML 4.01 Transitional Document Type.
No Character encoding declared at document level
Info Using Direct Input mode: UTF-8 character encoding assumed
Here are the errors the validator gives:
Line 1, Column 1: character "!" not allowed in prolog
Line 2, Column 1: no document type declaration; implying ""
Line 5, Column 16: there is no attribute "CHARSET"
Line 5, Column 23: required attribute "CONTENT" not specified
Line 6, Column 46: NET-enabling start-tag requires SHORTTAG YES
Line 7, Column 7: end tag for element "HEAD" which is not open
i could go on there are 4 more but because of i assume its a config problem i wont go on now with them, ask if something is not clear, and needed additional informations
The solution: Encode in UTF8 without BOM...

Fixing HTML spacing issues from nested PHP

This is something that has always bothered me about PHP and I have never found an answer. Hopefully this example will provide enough information to understand my question, as I don't know how to explain it thoroughly in words.
Here is some PHP inside of HTML, in a .php file. This type of things occurs pretty often:
<html>
<head>
<!-- whatever -->
</head>
<body>
<section id="content">
<ul class="errors">
<?php
foreach ($errors as $error)
{
?>
<li><?= $error ?></li>
<?php
}
?>
I have disregarded the end tags of the HTML and used <?= shortcuts to avoid any unnecessary text.
Now, here is what expect in the HTML output (i.e., right-click -> view source):
<html>
<head>
<!-- whatever -->
</head>
<body>
<section id="content">
<ul class="errors">
<li>Example Error</li>
However, here is the actual result:
<html>
<head>
<!-- whatever -->
</head>
<body>
<section id="content">
<ul class="errors">
<li>Example Error</li>
Why? Because the foreach loop from PHP is indented 3 tabs in, so the <li> it is generating gets an ADDITIONAL 3 tabs. The solution I have been using? Here:
<html>
<head>
<!-- whatever -->
</head>
<body>
<section id="content">
<ul class="errors">
<?php
foreach ($errors as $error)
{
?>
<li><?= $error ?></li>
<?php
}
?>
Sure, this works - but man is it ugly! It seems like I have two options: make my HTML output ugly and my code readable OR make my HTML readable and my code ugly. Why can't I have both? Is there no other way?
The best thing to do, I guess, is make the code readable and the output ugly. Who checks the source code anyway? I know this is probably the most popular reaction. Ugly HTML source code just bothers me, and I wish there was a way around it without sacrificing PHP code readability.
php code doesn't generate any indents on the page. What's generating the indent is the indent in the
<li> line</li>
What you can do is: use echo to output it
<?php
foreach ($errors as $error)
{
echo "<li>$error</li>";
}
?>
Or you could do:
<?php
foreach ($errors as $error)
{?>
<li><?=$error?></li> /(indent it in the begining as much as you want in the markup)
<?}
?>
Although, imo, you needn't worry about how the markup looks. Its not important. Indents have no effect on the output
One solution would be to use the output buffer functions in PHP to buffer all HTML output, and then post-process it to indent everything properly.
See this other question on the topic:
How to properly indent PHP/HTML mixed code?

Categories