PHP variables in HTML shows up as 'zero' in page title - php

I have a programming newbie. I am learning from the alphabets. Pardon me of my very basic questions. I have a HTML template inside a PHP gallery script. In the title tag of the HTML two variables are placed. <title>"$gtitle $pcaption"</title> ($gtitle is gallery title and $pcaption is photo caption) My problem is $pcaption shows up as zero in the title tag of the page on the gallery page.It shows up in the title like
"example gallery-0"
( I am thinking it is because $pcaption is empty on gallery page and when a picture is clicked the title tag shows up with the caption ). My question is, is there anyway to avoid the zero showing up?
I am working on a localhost and is using PHP 5.5.12.
My HTML part is below ( Sorry if this is full of mistakes )..
// Final Output
echo <<<__HTML_END
<html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<head>
<base href="/viewgallery.php/">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<!-- <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type"/> -->
<title><? php echo $gtitle . " " . $pcaption;?></title>
<link rel="stylesheet" type="text/css" href="/styles.css">
<meta name="author" content="by .">
<meta name="Copyright" content="All rights reserved by .">
<meta name="keywords" content="$p_keywords,Photography by .">
<meta name="title" content="$gallery_listing">
<meta name="description" content="$gallery_description,$pcaption.">
<link rel="image_src" href="/photos/$cname/$pcaption/">
<link rel="icon" type="image/ico" href="/favicon.ico">
</head>
<body>
<div class="wrapper">
<h1 id="header1"><span></span></h1>
<div class="main">
<ul class="nav">
<li>HOME |</li>
<li>MY GALLERIES |</li>
<li>SEARCH |</li>
<li>ABOUT ME</li>
</ul>
$result_final
</div>
</div>
<div class="footer">
<div class="footinner">
<div id="seeker">
<form action="/searchmyway.php" method="get"><input type="text" name="q" id="search">
<input type="submit" id="find" value="Search">
<input type="hidden" name="form_id:search" value="1">
</form></div></div>
<p class="footnote"> </p>
</div>
</body>
</html>
__HTML_END;
?>
Thanks for any help...
EDIT - I have to admit this is not my own gallery script. I am trying to learn from this code. After looking at this for a long time. I see that there is an " echo " just above the HTML code. Looks like the final HTML is echoed out with the $results_final and I am trying to make changes after the echo command, may be that's where I am doing it wrong?

You can perform a test -
if(0 == $pcaption) {
echo '<title>' . $gtitle . '</title>';
} else {
echo '<title>' . $gtitle . ' - ' . $pcaption . '</title>';
}

$galleryTitle = $gtitle;
if(!empty($pcaption) && $pcaption > 0) { $galleryTitle .= " " . $pcaption; }
Put the 2 lines above right above the 'echo' line of your code and replace your title tag inside of that echo with the following.
<title>$galleryTitle</title>
That statement that is being echoed out is called a heredoc statement.
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

You could use an if/then/else statement to accomplish this, something like:
echo "<title>".$gtitle;
if($pcaption != 0){echo " - ".$pcaption;}
echo "</title>";
You already know that you want to output your open and closing title tags and your $gtitle variable so you only need to write an if statement that checks the value of $pcaption and if it's not 0 then echo it. You might also want to add a check to see if $pcaption is empty (http://php.net/manual/en/function.empty.php) as well, so your new if statement would be:
if($pcaption != 0 && !empty($pcaption)){...}
This way you don't end up out putting something like <title>$gtitle - </title> and having something that looks weird.

Related

Trouble with pulling data from a dropdown list and checkboxes to be displayed on a table in another page [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 2 years ago.
When I remove the name="prod_info" from the option tag in the first document, I this error: Warning: Undefined array key "prod_info" in C:\xampp\htdocs\ordersummary.php on line 51, which I know is because there is not anything that is defined as prod_info. However, when I add the name="prod_info" line back in to the option tag in the first document, I get this error: Parse error: syntax error, unexpected identifier "prod_info", expecting "," or ";" in C:\xampp\htdocs\products.php on line 64. I've tried using separate echo statements to solve the problem of expecting the , or ; symbols, but I get the same error. I've also tried placing the name="prod_info line in the option tag outside of the php section, but I still just get the Warning: Undefined array key "prod_info" in C:\xampp\htdocs\ordersummary.php on line 51 error. I'm confused as to how to get the second page to recognize where to pull the data from without using the POST technique. Thank you in advance for any help you are able to provide! The code for the two pages mentioned is listed below:
//The page the info is to be pulled from:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link href="novusreset.css" rel="stylesheet" />
<link href="novusstyles1.css" rel="stylesheet" />
<link href="novusstyles2.css" rel="stylesheet" />
<link href="novusflex.css" rel="stylesheet" />
<link href="novusnavicon.css" rel="stylesheet" />
<link href="novustables.css" rel="stylesheet" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<header>
<img src="novuslogo.png" alt="Novus LLC." />
<nav class="horizontal">
<a id="navicon" href="#"><img src="novusnavicon.png" alt="" /></a>
<ul id="navigation">
<li>Home</li>
<li>Products
<ul id="productlist">
<li>PC Parts</li>
<li>Peripherals</li>
<li>Networking</li>
<li>Drinks</li>
<li>Apparel</li>
</ul>
<li>About</li>
<li>Support</li>
<li>Account</li>
<li>Cart</li>
</ul>
</nav>
</header>
<section id="main">
<article id="overview">
<form action="ordersummary.php" method="post">
<label for="products"><h1>Choose a product</h1></label>
<select name="products" id="products">
<option name="orderinfo">
<?php
include('connection.php');
$query = "SELECT * FROM products";
$result = mysqli_query( $conn, $query );
if(mysqli_num_rows($result) > 0) {
while( $row = mysqli_fetch_assoc($result) ){
echo "<option name="prod_info">" . $row["prod_name"] . " $" . $row["prod_price"] . "</option>";
}
} else{
echo "Ahhh yes, the big empty.";
}
mysqli_close( $conn );
?>
</br>
</br>
</option>
</select>
<label class="container">Gift Wrapping
<input type="checkbox">
<span class="checkmark"></span>
</label>
</br>
<label class="container">Gift Tagging
<input type="checkbox">
<span class="checkmark"></span>
</label>
</br>
</br>
<input type="submit" value="Place Order">
</form>
</article>
</section>
<footer>
©2021-Novus LLC. • 201 Main St. • Galena Il
</footer>
</body>
</html>
//The page that displays the info pulled from the above page in table form for the customer.
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link href="novusreset.css" rel="stylesheet" />
<link href="novusstyles1.css" rel="stylesheet" />
<link href="novusstyles2.css" rel="stylesheet" />
<link href="novusflex.css" rel="stylesheet" />
<link href="novusnavicon.css" rel="stylesheet" />
<link href="novustables.css" rel="stylesheet" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<header>
<img src="novuslogo.png" alt="Novus LLC." />
<nav class="horizontal">
<a id="navicon" href="#"><img src="novusnavicon.png" alt="" /></a>
<ul id="navigation">
<li>Home</li>
<li>Products
<ul id="productlist">
<li>PC Parts</li>
<li>Peripherals</li>
<li>Networking</li>
<li>Drinks</li>
<li>Apparel</li>
</ul>
<li>About</li>
<li>Support</li>
<li>Account</li>
<li>Cart</li>
</ul>
</nav>
</header>
<section id="main">
<article id="overview">
<h1>Order Summary</h1></br>
<table id="customers">
<?php
$prod_info = $_POST['prod_info'];
?>
<tr>
<th>Order Details:</th>
</tr>
<tr>
<td><?php echo prod_info;?></br></td>
</tr>
</table>
</article>
</section>
<footer>
©2021-Novus LLC. • 201 Main St. • Galena Il
</footer>
</body>
</html>
Let's take a look at this error: "Parse error: syntax error, unexpected identifier "prod_info", expecting "," or ";" in". The offending line is:
echo "<option name="prod_info">" . $row["prod_name"] . " $" . $row["prod_price"] . "</option>";
The issue here is that you are using double quotes nested inside another set of double quotes. Let's just look at the part before the first period [.].
The first thing happening here is that a string is defined and there is no syntax error:
"<option>"
Next, an attribute is added to the html <option> tag:
"<option name="prod_info">"
There is a set of double quotes inside another set of double quotes which introduces a syntax error. I would suggest using single quotes for the outside and double quotes for the inside. The following code should fix your error:
echo '<option name="prod_info">' . $row["prod_name"] . " $" . $row["prod_price"] . "</option>";
Fixing this issue should also fix your undefined index issue however, I should add that it is always best to check if an array key exists before calling it. Instead of:
$prod_info = $_POST['prod_info'];
You might want to do something like:
$prod_info = isset($_POST['prod_info']) ? $_POST['prod_info'] : '';
or, if you are using php 7.4+:
$prod_info = $_POST['prod_info'] ?? '';

Dynamically generated "submit" buttons in PHP

First time posting here (so please be gentle, as I am a relative PHP newbie).
I am building an intranet for our company and one of the things I need to do is to create a form that lists all outstanding sales orders (pulled from our accounting database) and provides a "submit" button beside each one to create the relevant work order.
Here is the code:
<div class="report_column">
<div class="report_header">
<div class="report_column_title" style="width:150px;margin-left:5px">ship date</div>
<div class="report_column_title" style="width:200px">customer</div>
<div class="report_column_title" style="width:140px;text-align:right">item</div>
<div class="report_column_title" style="width:120px;text-align:right">quantity</div>
</div>
<?php
// Open connection
include 'includes/dbconnect.php';
// Perform query
$result = mysqli_query($con,"SELECT * FROM tSalOrdr ORDER BY dtShipDate ASC");
// Retrieve results
while($row = mysqli_fetch_array($result)) {
$order = $row['lId'];
if ($row['bCleared'] == 0) {
$shipdate = substr($row['dtShipDate'], 0,10);
$customer = $row['sName'];
$po = $row['sComment'];
echo '<div class="report_item" style="width:750px";>';
echo '<form class="form" action="index.php?page=form&item=create_work_order" method="POST">';
echo '<div class="report_item_date" style="width:120px">'.$shipdate.'</div>';
echo '<div class="report_item_name" style="width:530px">'.$customer;
echo '<input type="hidden" name="po" value="'.$po.'" />';
echo '<input type="submit" class="submit" style="height: 25px;width:100px;margin:0px;padding:0px;margin:0px" value="work order"/>';
echo '</div>';
$result2 = mysqli_query($con,"SELECT * FROM tSOLine WHERE lSOId=$order ORDER BY sDesc ASC");
while($row = mysqli_fetch_array($result2)) {
if ($row['dRemaining'] <> 0) {
echo '<div class="report_item_details">';
echo '<div class="report_item_item">'.$row['sDesc'].'</div>';
echo '<div class="report_item_quantity">'.$row['dRemaining'].'</div>';
echo '</div>';
}
}
echo '</form>';
echo '</div>';
}
}
// Close connection
mysqli_close($con);
?>
</div>
What happens when I do this is that the first "submit" button will, for some reason, send me back to "index.php". The other buttons will load the correct page, however, they do not POST the required value.
Is there something I am doing wrong or is this something that needs different methodology than what I am currently using? My research on this seems to indicate that perhaps I should use javascript or an array to deal with this, but, having never dealt with either, I am not sure how to proceed. Any pointers would be appreciated.
Thanks.
#maniteja: The index.php is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<title></title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<link href="css/forms.css" rel="stylesheet" type="text/css" />
<link rel="icon" type="image/ico" href="favicon.ico" />
<script type="text/javascript" src="scripts/jquery-2.1.0.min.js" ></script>
<script type="text/javascript" src="scripts/tabcontent.js" ></script>
</head>
<body>
<!-- BEGIN MAIN CONTENT -->
<div id="wrapper">
<!-- BEGIN HEADER -->
<div id="header">
<img src="images/logo.jpg">
</div>
<!-- END HEADER -->
<!-- BEGIN MAIN MENU -->
<div id="leftcolumn">
<?php include 'includes/menu.php'; ?>
</div>
<!-- END MAIN MENU -->
<!-- BEGIN CONTENT FRAME -->
<div id="rightcolumn">
<div id="content_area">
<?php
if (isset($_GET['page']))
{$page = $_GET['page'];
include('pages/' . $page . '.php');}
else {include('pages/home.php');}
?>
</div>
</div>
<!-- END MAIN CONTENT -->
</body>
</html>
I've made seventeen other forms with it, so I don't think that it is the problem. I'm hoping that this is just a typo or a logic error on my part.
u can use
<button type='submit' class='' name='' onclick=''>Submit</button>
Try to do onclick. See my example below.
input type="button" name="submit" onclick='location.href="index.php?id=$id"'
AS far as I understand, you are making your forms go to index.php:
echo '<form class="form" action="index.php?page=form&item=create_work_order" method="POST">';
Can you explain what is your expected behavior when a button is pushed?

Page reapets it self when using include

So Lately i was working on a website on LocalHost Using XAMPP Application.
So i created header.php with the code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="A short description." />
<meta name="keywords" content="put, keywords, here" />
<title>Website Name</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<div id="wrapper">
<div id="menu">
<a class="item" href="index.php">Home</a>
<a class="item" href="../forum/">Forums</a>
<a class="item" href="live-chat.php">Live Chat</a>
<a class="item" href="Login.php">Log In</a>
<a class="item" href="Register.php">Register Now!</a>
<div id="userbar">
<?php
error_reporting(E_ALL & ~E_NOTICE);
include 'search.php';
if($_SESSION['signed_in'])
{
echo 'Welcome <b>' . htmlentities($_SESSION['user_name']) . '</b>. Not you? <a class="item" href="logout.php">Log out</a>';
}
?>
</div>
</div>
<div id="content">
And a Login.php with this code ( Here is the 4 lines of it ) :
<?php
include 'connect.php';
include 'header.php';
etc..... php code....
Ok so the problem is When i try to open the login.php in web browser i got the code in header.php duplication many many times like it don't end duplicating it self and if i open the source code of login.php i will got unlimited number of the code used in header.php like all the source code is header.php repeatedly.
So I'am asking you guys for help on how to fix this and what is the error ??
NOTE: Sorry if their was a thread duplication but i didn't know on what to search exactly.
If you want anymore information I'am ready.
Thanks all much appreciated
Use
include_once('header.php');
everywhere instead. It will check to see if the file has already been included.
use require_once instead, it will load the file just once, and only if needed.
You have more info here

how to add links to a list of whats trending from the Foursquare API using PHP

Hi so I have created a list of the trending places in a given area using the Foursquare API: see the following link http://createmate.co/foursquare-whatshot/
I am trying to figure out how to make each item link to its respective Foursquare URL. Here is the code so far...
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Foursquare What's Hot</title>
<!-- external CSS link -->
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="container">
<h1>Foursquare What's Hot</h1>
<?php
/* GET THE DATA */
$trending_url = file_get_contents("https://api.foursquare.com/v2/venues/trending?ll=40.7,-74&oauth_token=BQEPEMYIFHHH2C1OGBTJDI4GBYV5HQAPNFLR5ON1JIAI42GN&v=20130220");
$trending_output = json_decode($trending_url);
/* TEST THE DATA */
//echo "<pre>";
//print_r($trending_output);
//echo "</pre>";
/* PRINT RESULTS */
for ($i=0;$i<10;$i++) {
echo "<ul id ='locations'>";
echo "<li> <a href= " . $trending_output->response->venues[$i]->canonicalURL . "> <h3>" . $trending_output->response->venues[$i]->name . "<br>";
echo "<li> <h4>" . $trending_output->response->venues[$i]->location->address . "<br>";
echo "</ul>";
}
?>
</div>
</body>
Any help is much appreciated.
Adam
The property you retrieve should be canonicalUrl, not canonicalURL.
You should also surround your href attribute with quotes and don't forget to close your <a> tag as well.

$_GET loading content before head tag instead of in specified div

The problem is that when the content loads it displays before the tag before i added the <!DOCTYPE>, <html> <head></head> in the index.php file NOW $content loads before the Body tag... when it should beloaded in the "contents" div.
(1 I'm trying to get file_get_contents to load the links ?=about ?=services etc into to body.tpl in the contents div i have specified with #CONTENTS#
(2 The Dir Tree is as follows
htdocs>
classes> file.class.php
contents> where i want #CONTENTS# (file_get_contents) to grab data from
images> content (changing images)
templates> where the templates are hosted
clean>main template (Files are header.tpl, body.tpl, footer.tpl, styles.css, menu_style.css, and the images folder for images relating to the template itself.)
other templates>(to come)
/* file.class.php */
<?php
$file = new file();
class file{
var $path = "templates/clean";
var $ext = "tpl";
function loadfile($filename){
return file_get_contents($this->path . "/" . $filename . "." . $this->ext);
}
function setcontent($content,$newcontent,$vartoreplace='#CONTENT#'){
$val = str_replace($vartoreplace,$newcontent,$content);
return $val;
}
function p($content) {
$v = $content;
$v = str_replace('#CONTENT#','',$v);
print $v;
}
}
if(!isset($_GET['page'])){
ob_start();
// if not, lets load our index page(you can change home.php to whatever you want:
include("main.txt");
// else $_GET['page'] was set so lets do stuff:
} else {
// lets first check if the file exists:
if(file_exists($_GET['page'].'.txt')){
// and lets include that then:
include($_GET['page'].'.txt');
$content = ob_get_contents();
ob_end_clean();
// sorry mate, could not find it:
} else {
echo 'Sorry, could not find <strong>' . $_GET['page'] .'.txt</strong>';
}
}
?>
if some one could trim that down so it JUST is the template required code and file get contents.
/* index.php */
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<meta name="robots" content="index,follow"/>
<meta name="distribution" content="global"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<link href="templates/clean/style.css" rel="stylesheet" type="text/css" media="screen" />
<link rel="stylesheet" href="templates/clean/menu_style.css" type="text/css" />
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
</head>
<body> //everything before this USED to be in header.tpl but i moved it here until fix so that w3c validator would validate my code
<?php
include('classes/file.class.php');
// load the templates
$header = $file->loadfile('header');
$body = $file->loadfile('body');
$footer = $file->loadfile('footer');
// fill body.tpl #CONTENT# slot with $content
$body = $file->setcontent($body, $content);
// cleanup and output the full page
$file->p($header . $body . $footer);
?>
/* header.tpl */
<div id="header">
<div id="logo"><img src="images/logo.png" border="0" alt=""/></div>
<div id="menuo">
<div class="menu">
<ul id="menu">
<li>Home</li>
<li>About Us</li>
<li>Services
<ul>
<li>InStore Repairs</li>
<li>InHome Repairs</li>
<li>Website Design</li>
<li>Comming Soon.</li>
</ul>
</li>
<li>Products
<ul>
<li>Computer Hardware</li>
<li>Monitor's</li>
<li>Laptop + Netbooks</li>
<li>Comming Soon.</li>
</ul>
</li>
<li>Contact</li>
</ul>
</div>
</div>
</div>
<div id="headerf">
</div>
/* body.tpl */
<div id="bodys">
<div id="bodt"></div>
<div id="bodm">
<div id="contents">
#CONTENT#
</div>
<div id="bodb"></div>
</div>
</div>
/* footer.tpl */
<div id="footer">
<div style="position:absolute; top:4px; left:4px;"><img src="images/ff.png" alt="ok"></div> <div style="position:absolute; top:4px; right:5px; color:#FFFFFF;">©2010 Company Name</div>
</div>
</body>
</html>
My Firefox Showing The Problem In Action
Edited answer:
I'd originally assumed that the p() method was an accidental method you were calling, but that appears (according to the update you made to fix the fact that both pages were the same) to be a red herring. The issue is similar maybe though.
When you:
include('classes/file.class.php'); as part of your index.php, one of the things it does is define the methods and then run a few if statements, as part of this change it includes a .txt file. (e.g. include("main.txt");).
My guess is what you are seeing is the contents of this include file being displayed to screen. What's the point of them being included? Can you just remove them? Should you really just be reading the content to a variable instead of including it (and effectively echo'ing/printing it to screen)?
Try removing them and seeing if that solves your issue - but otherwise, check to make sure your template files contain what you think they contain. :)
I'm a little confused, as your index.php code is the same as your file.class.php code (is that a mistake?). But if I had to guess your problem might be in the print line of the $file->p() function:
function p($content) {
$v = $content;
$v = str_replace('#CONTENT#','',$v);
print $v;
}
This function is fine, depending on when you call it. If you haven't output anything to the screen yet, and you call p() and pass it the contents of body.tpl, it could result in the incorrect HTML output you're getting.
get firebug and check the styles applied to the content div and the text. it seems like a position problem.

Categories