Here's what I'm trying to do. I'm trying to echo blog posts stored in my database. Simple enough, but I want them to be redirected to view_post.php to show the full post, when they click on the little preview. Here's my code:
<?php
session_start();
require_once('required/db.php');
$_SESSION['admin'] = false;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>WillWam - Blog</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="assets/css/style.css" type="text/css">
<link rel="ico" href="assets/favicon.ico">
</head>
<body>
<nav><h2 class="title">The Blog</h2></nav>
<?php
$sql="SELECT id,title,author,body FROM posts ORDER BY id DESC";
if ($result=mysqli_query($con,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
printf('<div class="row"><div class="row-inner"><p><strong>%s</strong> | %s |</p></div></div>', $row[0],$row[1],$row[2]);
}
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
<div class="top-margin wrapper"><div class="container"><p>Administrator? Click here.</p></div></div>
</body>
</html>
How would I go about making the preview row a link dynamically (such as view_post.php?id=1)? What would I put in view_post.php?
Assumming on $row the id is contained you can create the links like this
printf('<a href="view_post.php?id='.$row['id'].'"> <----just put the id there
And on view_port use the value
<div class="row"><div class="row-inner">
<p><strong>%s</strong> | %s |</p></div></div></a>', $row[0],$row[1],$row[2]);
Related
This is the index page, from which the user is redirected to the payment page where the payment is made. Once the payment is made, I want that the user is then redirected to the success page, which eventually redirects to the details page of the "specific" item for which the payment has been made. $customer['id'] is an integer value determines the unique id of the item, so if we can redirect the user to details.php?id=$customer['id'] then the work is done. However, I can't seem to manage to find a way to do so.
Here's the part of the index in which I am trying to modify $customer['id'] so that it can be easily passed to the details page through the payment page:
<?php
$newID = ($customer['id']);
?>
<div class="card-action right-align">
<a class="brand-text" href="payment.php">more info</a>
</div>`
here's the success page code which is supposed to redirect to :
<?php
include 'index.php';
if(!empty($_GET['tid'] && !empty($_GET['product']))) {
$GET = filter_var_array($_GET, FILTER_SANITIZE_STRING);
$tid = $GET['tid'];
$product = $GET['product'];
} else {
header('Location: payment.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Thank You</title>
</head>
<body>
<div class="container mt-4">
<h2>Thank you for purchasing <?php echo $product; ?></h2>
<hr>
<p>Your transaction ID is <?php echo $tid; ?></p>
<p>Check your email for more info</p>
<?php header('Refresh: 2; URL=details.php?id=$customer['id']');?>
</div>
</body>
</html>
amongst the above code, this is the most important part(must be modified somehow to make it work I guess):
<?php header('Refresh: 2; URL=details.php?id=$customer['id']');?>
can somebody help me out please?
You have problem with quotes in there. they cancel each other out in the wrong place. Call the variable outside single quote marks, cause they cannot process php variables.
<?php header('Refresh: 2; URL=details.php?id='.$customer['id']);?>
First I had this:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="css/style.css" />
<title>PHP file</title>
</head>
<body>
<h1>
<?php
echo "Hi again...";
?>
</h1>
</body>
</html>
I accessed the file through localhost/learningphp/myfirstfile.php and it rendered properly, showing me a h1 element with the text "Hi again...".
Then I changed to this:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="css/style.css" />
<title>PHP file</title>
</head>
<body>
<p>
<?php
echo "Hi again...";
$myName = "Sahand";
echo $myName;
?>
</p>
</body>
</html>
Notice the change of <h1> tags to <p> tags, and the addition of myName. Still, when I go to localhost/learningphp/myfirstfile.php myName (Sahand) is not added to the page, and "Hi again..." is still shown in "h1 styling", like when I viewed the first version of the php file. Why is this and what can I do about it?
You will have two issues in this case may be
You are saving the file somewhere else or you didn't save the
updated content.
Your Browser history try a hard refresh by using Ctrl+F5 (for windows) Keys
together
I'm getting weird results when trying to kind of make a "templating engine". Basically, I want to be able to use PHP variables that contain data from an SQL database.
What happens is that everything works properly with the PHP side, what does not is the page that needs to display this information (index.php).
I'm working on a way to get the website's name from the sql database, so I have something like that on my index:
<?php
include ('php/data/sitename.php');
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $sitename; ?> - Home</title>
<!--Import Google Icon Font-->
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="css/materialize.min.css" media="screen,projection"/>
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<div class="container">
<!-- HEADER: Navbar -->
<?php $navbar; ?>
<!-- MAIN: Index Page contents -->
<?php $page_index ?>
<!-- FOOTER: Footer -->
<?php $footer; ?>
<?php $sitename; ?>
</div>
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="js/materialize.min.js"></script>
</body>
</html>
This variable comes from a file (that has been included) called sitename.php, with the following code:
<?php
include ('../db.php');
$sql = "SELECT id, sitename FROM GeneralData";
$getname = mysqli_query($conn, $sql);
if ($getname->num_rows > 0) {
while($row = $getname->fetch_assoc()) {
$sitename = $row['sitename'];
echo $sitename;
}
}
?>
Yes, I used echo $sitename;, I know it wont echo the actual data, but I did it to test some things, and here are the results:
Including the file sitename.php to index.php will do nothing, it would be like if it did not exist. However, if I write "echo "123";" on it, it will echo 123 on index. What does not work is what I need.
If I go to sitename.php directly, it will simply output the correct SQL value I requested because I told it to echo (as I stated before). But, it wont work in index, it will simply not work.
Also, I'll leave my project structure here. It might help.
What can I do?
Thanks in advance!
try set GLOBAL for sitename
GLOBAL $sitename;
or
GLOBALS['sitename'];
$sitename = ...
EDIT
try use
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/yourpath/yourfile.php";
include_once($path);
I have content stored in a mysql database. When I am trying to retrieve the content using PHP and display it using html and CSS, some of it displays these special characters ������.
I have declared the encoding type in my HTML, used to render the PHP content as follows.
I have also added this to the head off the html document
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="utf-8">
<!-- [portable options] -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0;" />
<meta name="apple-mobile-web-app-capable" content="yes" />
What mistake am I making that the retrieved content shows the special characters?
This is the document that contains the PHP script that retrieves the data from mysql
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="utf-8">
<!-- [loading stylesheets] -->
<link type="text/css" rel="stylesheet" href="css/style.css" />
<!-- [loading stylesheets] -->
<link type="text/css" rel="stylesheet" href="css/style.css" />
<link type="text/css" rel="stylesheet" href="css/flexslider.css" />
<!-- [loading scripts] -->
<script type="text/javascript" src="js/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<div class="blog-post">
<div class="wrapper">
<!--
<iframe frameborder="0" scrolling="yes" width="100%" height="500px"
src="debug/includes/news_index.php" name="" id="">
</iframe>
-->
<?php
// connection string constants
define('DSN', 'mysql:dbname=mydb;host=localhost');
define('USER', 'myadmin');
define('PASSWORD', 'mypwd2015');
// pdo instance creation
$pdo = new PDO(DSN, USER, PASSWORD);
// query preparation
$stmt = $pdo->query("
SELECT title, introtext, id, created, created_by, catid
FROM mytbl_items
");
// fetching results
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
// if this returns 0 then it means no records are present
echo count($result) . "\n";
// the loop should print valid table
foreach ($result as $index => $row) {
if ($index == 0) echo '<div>';
echo <<<HTM
<span class="post-date">{$row['created']}</span>
<h2 class="blog-post-title">{$row['title']}</h2>
<p>
{$row['introtext']}
</p>
<p>
<a href='read_serverside.php?id={$row['id']}'><input type="button" value="Read More" /></a>
</p>
<div class="blog-meta">
<img src="img/avatar.png" alt="Avatar" />
<h4 class="blog-meta-author">{$row['created_by']}</h4>
<span>Category: {$row['catid']}</span>
</div>
HTM;
if ($index == (count($result)-1)) echo '</div>';
}
?>
</div> <!-- .blog-meta -->
</div> <!-- .blog-post #2 -->
</body>
</html>
What else should I do to avoid the special chars from showing up?
Try setting the utf8 charset at the PDO Connection like this:
$pdo = new PDO(DSN, USER, PASSWORD,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
This works for me.
Try this one before the start of HTML.
I hope it will also work for you.
<?php header('Content-Type: text/html; charset=iso-8859-15'); ?>
<!DOCTYPE html>
<html lang="en-US">
<head>
There are characters in your database that does not exist in Unicode UTF 8. The data are not recognized by this format so thew are displayed as weird characters. I suggest you change the <meta charset="utf-8"> into #charset 'iso-8859-15' or to delete the line. If this does not work you have to go to the data that are causing the error and determine the type of encoding that is used.
If you have main or front controller you should put
header('Content-Type: text/html; charset=utf-8');
somewhere there.
If you haven't put that call at the very beginning of your file before you start output html:
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html>
<head>
......
I would like to give a title to each of my pages, but all my pages are linked to my index.php:
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Test</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php include("top_bar.php");?>
<?php include("header.php");?>
<?php include("container.php");?>
<?php include("footer.php");?>
</body>
</html>
Here is how my site is: http://prntscr.com/47nn7h
All my pages have the title I put for index.php, but how to add a title to a specific page (example, when I go to the page members.php)?
members.php:
<?php include "index.php";?>
Thanks.
Replace the existing <title> tag with this.
<title><?php echo $pagetitle; ?> </title>
in your <head> block.
Make sure that $pagetitle actually contains the desired title before you emit the tag. It's not clear from your question where these titles are coming from - you'll probably need some PHP right at the top of the page to set all this up.
.
You could use this method and add the
$pageTitle = 'Title of Page';
to your content page (i.e. member page)