I am working on a search page where I am getting the input from the user and then using PHP, I am connecting to a database, executing a query and trying to display the results on the page. I need to display the result in a div tag where I need various search results to be displayed.
<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test1"; // Database name
$tbl_name="postjob"; // Table name
// Create connection
$conn = new mysqli($host, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$dbconn = mysqli_select_db($conn, $db_name) or die(mysql_error());
echo $dbconn;
echo "Connected to Database<br />";
$output = '';
$keywords=$_POST['keywords'];
$location=$_POST['location'];
$keywords = mysqli_real_escape_string($conn, $keywords);
$location = mysqli_real_escape_string($conn, $location);
$sql="SELECT * FROM $tbl_name WHERE job_title LIKE '%$keywords%' OR job_type
LIKE '%$keywords%' OR job_category LIKE '%$keywords%' OR job_tags LIKE
'%$keywords%' OR description LIKE '%$keywords%' OR company_name LIKE
'%$keywords%' OR location LIKE'%$location%'";
$result=mysqli_query($conn, $sql);
$count=mysqli_num_rows($result);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sign Up</title>
</head>
<body>
<section class="content_area">
<div class="banner">
<div class ="result">
<table>
<tr>
<?php
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>"."<h3>".$jobsearch['job_title']."</h3>"."
<br>".$jobsearch['company_name']."<br>".$jobsearch['description']."</td>";
echo "</tr>";
}
?>
</tr>
</table>
</div>
</div>
</section>
</body>
</html>
I am calling the above PHP file using post action from HTML form page which has Keywords and location textbox. I am fetching job_title, company_name and description from Database and displaying it. Here I am using a table to display. But I wanted use a div where I need to do some styling. I tried to use below code replacing table with div, but it is not displaying any result.
<?php
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<div>"."<h3>".$jobsearch['job_title']."</h3>"."
<br>".$jobsearch['company_name']."<br>".$jobsearch['description'].
</div>";
}
?>
I am expecting result like below inside a div
job_title
company_name
description
<button>Apply</button>
I tried the below code, but it is not displaying any result.
<?php
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<div><h3>".$jobsearch['job_title']."</h3></div>
<div>".$jobsearch['company_name']."</div>
<div>".$jobsearch['description']. "</div>";
}
?>
I need to customize this div to do some styling. Could someone help me how to achieve this?
Hope it will work.
<ul>
<?php
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<li>"."<h3>".$jobsearch['job_title']."</h3>"."</li>"
echo "<li>"."<h3>".$jobsearch['company_name']."</h3>"."</li>"
echo "<li>"."<h3>".$jobsearch['description']."</h3>"."</li>"
echo "<button>Apply</button>";
}
?>
</ul>
your code should throw an error as " is missing in your echo statement
try this
echo "<div><h3>".$jobsearch['job_title']."</h3></div>
<div>".$jobsearch['company_name']."</div>
<div>".$jobsearch['description']. "</div>";
attached is the output, this output is by replacing your variables with some defined values
Try this.
<div class ="result">
<table>
<?php
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>"."<h3>".$jobsearch['job_title']."</h3></td>";
echo "</tr>";
echo "<tr>";
echo "<td>"."<h3>".$jobsearch['company_name']."</h3></td>";
echo "</tr>";
echo "<tr>";
echo "<td>"."<h3>".$jobsearch['description']."</h3></td>";
echo "</tr>";
echo "<br />";
}
?>
</table>
</div>
I want to create one page, where users add their informations..
I allready have that page created, but my real problem its down code..
I have some kind of problem, with that part of code:
<?php
//Connect to DB
$db = mysql_connect("localhost","USER","PASS") or die("Database Error");
mysql_select_db("DB",$db);
//Get ID from request
$idstire = isset($_GET['idstire']) ? (int)$_GET['idstire'] : 0;
//Check id is valid
if($idstire > 0)
{
//Query the DB
$resource = mysql_query("SELECT * FROM stiri2 WHERE idstire = " . $idstire);
if($resource === false)
{
die("Eroare la conectarea cu baza de date");
}
if(mysql_num_rows($resource) == 0)
{
die("Se pare ca stirea nu mai exista, sau a fost stearsa. <a href='http://www.wanted-web.ro'>ACASA</a>");
}
$user = mysql_fetch_assoc($resource);
echo "
<div class='main-article-content'>
<h2 class='article-title'>asd</h2>
<div class='article-photo'>
<img src='" . $user['poza'] . "' class='setborder' alt='' />
</div>
<div class='article-controls'>
<div class='date'>
<div class='calendar-date'>" . $user['data'] . "</div>
</div>
<div class='right-side'>
<div class='colored'>
<a href='' class='icon-link'><span class='icon-text'></span>Printeaza articol</a>
<a href='#' class='icon-link'><span class='icon-text'></span>Trimite prietenilor</a>
</div>
<div>
<a href='#' class='icon-link'><span class='icon-text'></span>de Cristian Cosmin D.</a>
<a href='#' class='icon-link'><span class='icon-text'></span>39 comentarii</a>
</div>
</div>
<div class='clear-float'></div>
</div>
<div class='shortcode-content'>
<p>" . $user['nume'] . " , " . $user['prenume'] . " , " . $user['varsta'] . " , " . $user['localitatea'] . "</p>
</div>
</div>
";
}
$query = "UPDATE stiri2 SET accesari = accesari + 1 WHERE idstire=\"" . $idstire . "\"";
$result = mysql_query($query) OR die(mysql_error());
?>
It's show me error from here:
if(mysql_num_rows($resource) == 0)
{
die("Se pare ca stirea nu mai exista, sau a fost stearsa. <a href='http://www.wanted-web.ro'>ACASA</a>");
}
I really dont understand why!?
Can someone explain me?
Thank you!
mysql_query should have the second parameter as the connection which is in your case $db
$resource = mysql_query("SELECT * FROM stiri2 WHERE idstire = " . $idstire,$db);
if this also not works then use mysql_error to know the exact error
$row=mysql_num_rows($resource);
if($row)
{
}
else
{
mysql_error();
}
this will show you if there is problwm in mysql_num_rows
Well the problem is, that your SQL Statement does not have any results.
Please check if your database contains some rows for this idstire.
If $_GET['idstire'] is not set, you are setting $idstire to 0. Is there any entry in the table for idstire=0?
You can print your php query with $idstire replaced with its value. Take that query and execute in phpmyadmin to see the error. Also verify that row exists for the value of $idstire being used in the query.
I have a table of books which has info such as bname, bauthor, bdetails...etc
I want to provide a php script to search any thing from the table ...i tried a few scripts but it didnt work ..
Also i want to provide the option to provide option of selecting category ...
if the category is not selected then also the script should run...
Can anyone help me in this please ...
..
I m trying this :
$data = mysql_query("SELECT * FROM info WHERE upper($field) LIKE'%$find%'");
$result = mysql_query($data);
if (!$result) die ("DAtabase acces faild bc : ". mysql_error());
echo " <ul class=listbk>";
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo " <li class=bkd><a href=details.php?bid=".$row['pid']. "> <img src=images/".$row['bname']. ".jpg />
<ul class=showcasetitle> <span> ". $row['bname'] . " </span> </ul>
<p align=center style=text-align:center; font-size:10px;> By <span> ". $row['bauthor'] . "</span></p>
</a> <p align=center style=text-align:center; color: #F00;><span class=WebRupee style=color: #F00;>₹</span> ". $row['price'] . " only !!!</span></p>
</li> " ;
}
echo " </ul> ";
<?php
$text = 'John';
$query = "SELECT * FROM `books` WHERE MATCH(`bname`,`bauthor`,`bdetails`) AGAINST ('".$text."')";
?>
Ok, I have a gallery that's being populated by a database but for some reason it's not pulling entries from beyond May 31st 2011. I've scoured the code and can't find any date limitations so I'm at a loss as to why it's not pulling in any recent entries.
I've also looked through the database table and don't see any irregularities between pre May31st and more current entries.
<?php
//GALLERY PAGE
$user="USER";
$password = "PASSWORD";
$database = "GALLERY";
$hostname_portfolio ="localhost:3306";
//gets the page number from the URL
if($_GET["pageNum"]==''){
$listedNum=0;
//gets the page limit from the URL
$limit=5;
}
else{
$listedNum=$_GET["pageNum"];
//gets the page limit from the URL
$limit=$_GET["limit"];
}
//creates the list of projects and puts them into an array
$project= array();
$con = mysql_connect($hostname_portfolio,$user,$password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$dbcon = mysql_select_db("GALLERY", $con);
if (!$dbcon)
{
die('Could not connect: ' . mysql_error());
}
if ($dbcon)
{
}
mysql_select_db($database, $con);
$result = mysql_query("SELECT * FROM Persons ORDER BY date_uploaded DESC");
echo"<div class='clear' style='clear:both;'></div>";
$j=0;
do{
if($row['approved']=="true"){
$project[$j] =
"<div class='project-list'>
<div class='user-project'>
<div class='container'>
<div class='before-box' >
<p class='picture_state'>Before</p>
<a href='http://THEURL.com/". $row['image_path']."'>
<img src= '". $row['image_path']. "' width='400px' height='300px'/></a>
<br />
</div>
<div class='after-box'>
<p class='picture_state'>After</p>
<a href='". $row['picture_state']."'>
<img src= '". $row['picture_state']. "' width='400px' height='300px' /></a>
<br />
</div>
<div class='sidebox'>
<div class='inner-sidebox'>
<p class='date-project'>
Submitted by " . $row['FirstName'] . " " . $row['LastName']. " on " .$row['date_uploaded']. "</p>
<p> " .$row['decription']. "</p>";
if($row['ATTR1'] || $row['ATTR2']){
$project[$j] .= "<p>Used ";
if ($row['ATTR1']){
$project[$j] .= "PRODUCT2™";
if ($row['color1']){
$project[$j] .= " in " . $row['color1'];
}
if ($row['ATTR2']){
$project[$j] .= " and ";
}
}
if($row['ATTR2']){
$project[$j] .= "PRODUCT<sup>®</sup>";
if ($row['color2']){
$project[$j] .= " in " . $row['color2'];
}
}
$project[$j] .= "</p>";
}
$project[$j] .= "
</div>
</div>
</div>
<div class='clear' style='clear:both;'>
</div>
</div>
<div class='clear' style='clear:both;'>
</div>
</div>
<div class='clear' style='clear:both;'>
</div>";
$j++;
}
}
while($row = mysql_fetch_array($result));
$max=sizeof($project);
for($i=$listedNum;$i<$limit;$i++){
echo $project[$i];
}
$max=sizeof($project) - 1;
echo "<div class='bottom' style='width:170px;margin:0px auto;'>";
if($listedNum > 0){
$prevPageNum=$listedNum - 5;
$lastPage= $limit - 5;
echo "<a href='http://THEURL.com/gallery.php? pageNum=".$prevPageNum."&limit=".$lastPage."'>< Last Page </a>";
}
else{
$prevPageNum=$listedNum;
$lastPage= $limit;
}
echo" ";
if($limit <= $max){
$newPageNum=$listedNum + 5;
$nextPage= $limit + 5;
echo "<a href='http://THEURL.com/gallery.php?pageNum=".$newPageNum."&limit=".$nextPage."'> Next Page ></a>";
}
else{
$newPageNum=$listedNum;
$nextPage= $limit;
}
echo "</div>";
mysql_close($con);
?>
Im going to guess that its one of the following:
Youre server is only allowing you to pull 'x' amount of rows within each query (and that 'x' is dont at may 31st')
or theres something different with the files after may 31st. compared to the ones earlier
There's nothing in the code. It might be the way your database is configured where it only lets a certain number of results be returned and that happens to coincide with the date.
Check data in table. May be, after may 31st it was not "approved" ("approved" field was not set to something that casts to boolean TRUE)
Judging from your code - either date_uploaded is null in the records after May 31 or something else is wrong in your DB. Approved not equaling true on the offending records, for example.
Failing that, maybe it has something to do with this limitation. Try passing in a number higher than 5 and see if you get more records showing.
//gets the page number from the URL
if($_GET["pageNum"]==''){
$listedNum=0;
//gets the page limit from the URL
$limit=5;
}
else{
$listedNum=$_GET["pageNum"];
//gets the page limit from the URL
$limit=$_GET["limit"];
}
I wish to set the title of my webpage to Ultan.me - Whatever the post title. I want it to display the post title. The posts are submitted to a MySQL database and the title row is called "title". Any help is appreciated with this small question.
Update:
Here is the page itself now but it doesn't display the title. Should I open the php document and connect to my database somewhere different to it's current locations?
The Code (The only necessary piece is the beginning):
<html>
<head>
<meta name="keywords" content="Mac user Ultan Casey TheCompuGeeks UltanKC">
<title>Ultan.me - <?echo $title;?></title>
<link rel="stylesheet" href="css/styles.css" type="text/css" />
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript"
src="js/jquery.labelify.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(":text").labelify();
});
</script>
<style>
a {text-decoration:none}
</style>
</head>
<body>
<div id="main">
<!-- Menu Start -->
<div id="menu">
<ul>
<li>home</li>
<li>about me</li>
<li>archives</li>
<li>contact</li>
<li>gallery</li>
</ul>
</div>
<!-- Menu End -->
<img src="images/banner.png" />
<div id="content">
<div id="posts">
<?php
mysql_connect ('localhost', 'root', 'root') ;
mysql_select_db ('ultankc');
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die("Invalid ID specified.");
}
$id = (int)$_GET['id'];
$sql = "SELECT * FROM php_blog WHERE id='$id' LIMIT 1";
$result = mysql_query($sql) or print ("Can't select entry from table php_blog.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$date = date("l F d Y", $row['timestamp']);
$title = stripslashes($row['title']);
$entry = stripslashes($row['entry']);
$get_categories = mysql_query("SELECT * FROM php_blog_categories WHERE `category_id` = $row[category]");
$category = mysql_fetch_array($get_categories);
?>
<p><?php echo "<p id='post-title'><strong>" . $title . "</strong></p>"; ?><br /><br />
<div id="entry"><?php echo $entry; ?>
</div><br /><br />
<p id="date">Posted in <?php echo $category['category_name']; ?> on <?php echo $date; ?></p>
</p>
<h2 id="share-title">Share This Post</h2>
<div id="social-share">
<li id="link-right"><a href="http://twitter.com/home?status=
I just read <?php echo $title; ?> at http://ultan.me/post.php?id=<?php echo $id; ?>"><center>Twitter</center></a></li>
<li id="link-left"><center>Digg</center></li>
<br>
<li id="link-right"><center>Facebook</center></li>
<li id="link-left"><a href="http://www.google.com/buzz/post?url=http://ultan.me/post.php?id=<?php echo $id; ?>
"><center>Google Buzz</center></a></li>
<div class="clr"></div>
</div>
<h2 id="comments-title">Comments</h2>
<div id="comment-list">
<?php
}
$commenttimestamp = strtotime("now");
$sql = "SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp";
$result = mysql_query ($sql) or print ("Can't select comments from table php_blog_comments.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$timestamp = date("l F d Y", $row['timestamp']);
printf("<div class='comment-ind'><p id='comments'><a id='username' href=\"%s\">%s</a> %s</p>", stripslashes($row['url']), stripslashes($row['name']), $timestamp);
print("<p class='comments'>" . stripslashes($row['comment']) . "</p><div class='clr'><br></div></div>");
}
?>
<div class="clr"></div>
<form id="commentform" method="post" action="process.php">
<p><input type="hidden" name="entry" id="entry" value="<?php echo $id; ?>" />
<input type="hidden" name="timestamp" id="timestamp" value="<?php echo $commenttimestamp; ?>">
<input type="text" name="name" id="name" title="Name (required)" /><br />
<input type="text" name="email" id="email" title="Mail (will not be published) (required)" /><br />
<input type="text" name="url" id="url" title="Website" value="http://" /><br />
<br />
<textarea title="Your Comment Goes Here" name="comment" id="comment"></textarea></p>
<p><input type="submit" name="submit_comment" id="submit_comment" value="Add Comment" /></p>
</form>
</div>
<div id="pages">
<?php
$total_results = mysql_fetch_array(mysql_query("SELECT COUNT(*) AS num FROM php_blog"));
$total_pages = ceil($total_results['num'] / $blog_postnumber);
if ($page > 1) {
$prev = ($page - 1);
echo "<< Newer ";
}
for($i = 1; $i <= $total_pages; $i++) {
if ($page == $i) {
echo "$i ";
}
else {
echo "$i ";
}
}
if ($page < $total_pages) {
$next = ($page + 1);
echo "Older >>";
}
?>
</div>
</div>
</div>
<!-- Sidebar Start -->
<div class="sidebar">
<!-- Item 1 -->
<div id="side-item">
<h2>
<a href="http://www.dailybooth.com/UltanCasey">
<img src="images/db-icon.jpg">Dailybooth
</a></h2>
<div id="side-item-content">
<center>
<img src="http://dailybooth.com/UltanCasey/latest/medium.jpg" />
</center>
</div>
</div>
<!-- Item 2 -->
<div id="side-item">
<h2><img src="images/connect.jpg" />Connect</h2>
</div>
<div id="side-item-content">
<div class="tweet-title"><p>Latest Tweet:</p></div>
<div id="tweet">
<?php
function getTwitterStatus($userid){
$url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=1";
function auto_link_twitter ($text)
{
// properly formatted URLs
$urls = "/(((http[s]?:\/\/)|(www\.))?(([a-z][-a-z0-9]+\.)?[a-z][-a-z0-9]+\.[a-z]+(\.[a-z]{2,2})?)\/?[a-z0-9._\/~#&=;%+?-]+[a-z0-9\/#=?]{1,1})/is";
$text = preg_replace($urls, " <a href='$1'>$1</a>", $text);
// URLs without protocols
$text = preg_replace("/href=\"www/", "href=\"http://www", $text);
// Twitter usernames
$twitter = "/#([A-Za-z0-9_]+)/is";
$text = preg_replace ($twitter, " <a href='http://twitter.com/$1'>#$1</a>", $text);
// Twitter hashtags
$hashtag = "/#([A-Aa-z0-9_-]+)/is";
$text = preg_replace ($hashtag, " <a href='http://hashtags.org/$1'>#$1</a>", $text);
return $text;
}
$xml = simplexml_load_file($url) or die("could not connect");
foreach($xml->status as $status){
$text = $status->text;
}
echo auto_link_twitter ($text);
}
getTwitterStatus("UltanKC");
?>
</div>
<br>
<ul>
<li id="social">YouTube</li>
<li id="social">Twitter</li>
<li id="social">LastFM</li>
<li id="social">Email</li>
</ul>
</div>
<!-- Item 2 End-->
<div id="side-item">
<h2><img src="images/archive.jpg" />Archives</h2>
</div>
<div id="archive-side">
<?php
mysql_connect ('localhost', 'root', 'root') ;
mysql_select_db ('ultankc');
$result = mysql_query("SELECT FROM_UNIXTIME(timestamp, '%Y') AS get_year, COUNT(*) AS entries FROM php_blog GROUP BY get_year");
while ($row = mysql_fetch_array($result)) {
$get_year = $row['get_year'];
$entries = $row['entries'];
echo "<li id='tag'>Entries from " . $get_year . " (" . $entries . ")<br /></li>";
}
$result1 = mysql_query("SELECT * FROM php_blog_categories ORDER BY category_name ASC");
while($row = mysql_fetch_array($result1)) {
$result2 = mysql_query("SELECT COUNT(`id`) AS entries FROM php_blog WHERE category = $row[category_id]");
$num_entries = mysql_fetch_array($result2);
echo '<li id="tag">' . $row['category_name'] . ' (' . $num_entries['entries'] . ')</li>';
}
?>
</div>
</div>
<div class="clr" />
</div>
<!-- Sidebar End -->
<div id="footer">
<p> © Ultan Casey 2010</p>
<p style="margin-top: -18px; float:right">Home | About Me | Email Me</p>
</div>
</div>
</div>
</body>
</html>
?>
Here's the method I use (for similar things, not just title):
<?
ob_start (); // Buffer output
?>
<!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="en">
<head>
<title><!--TITLE--></title>
</head>
<body>
<?
$pageTitle = 'Title of Page'; // Call this in your pages' files to define the page title
?>
</body>
</html>
<?
$pageContents = ob_get_contents (); // Get all the page's HTML into a string
ob_end_clean (); // Wipe the buffer
// Replace <!--TITLE--> with $pageTitle variable contents, and print the HTML
echo str_replace ('<!--TITLE-->', $pageTitle, $pageContents);
?>
PHP usually works be executing any bits of code and printing all output directly to the browser. If you say "echo 'Some text here.';", that string will get sent the browser and is emptied from memory.
What output buffering does is say "Print all output to a buffer. Hold onto it. Don't send ANYTHING to the browser until I tell you to."
So what this does is it buffers all your pages' HTML into the buffer, then at the very end, after the tag, it uses ob_get_contents () to get the contents of the buffer (which is usually all your page's HTML source code which would have been sent the browser already) and puts that into a string.
ob_end_clean () empties the buffer and frees some memory. We don't need the source code anymore because we just stored it in $pageContents.
Then, lastly, I do a simple find & replace on your page's source code ($pageContents) for any instances of '' and replace them to whatever the $pageTitle variable was set to. Of course, it will then replace <title><!--TITLE--></title> with Your Page's Title. After that, I echo the $pageContents, just like the browser would have.
It effectively holds onto output so you can manipulate it before sending it to the browser.
Hopefully my comments are clear enough.
Look up ob_start () in the php manual ( http://php.net/ob_start ) if you want to know exactly how that works (and you should) :)
You parse the field from the database as usual.
Then let's say you put it in a variable called $title, you just
<html>
<head>
<title>Ultan.me - <?php echo htmlspecialchars($title);?></title>
</head>
EDIT:
I see your problem. You have to set $title BEFORE using it. That is, you should query the database before <title>...
header.php has the title tag set to <title>%TITLE%</title>; the "%" are important since hardly anyone types %TITLE% so u can use that for str_replace() later. then, you use output buffer like so
<?php
ob_start();
include("header.php");
$buffer=ob_get_contents();
ob_end_clean();
$buffer=str_replace("%TITLE%","NEW TITLE",$buffer);
echo $buffer;
?>
For more reference, click PHP - how to change title of the page AFTER including header.php?
What about using something like:
<?php
$page_title = "Your page tile";
include("navigation.php"); // if required
echo("<title>$page_title</title>");
?>
Move the data retrieval at the top of the script, and after that use:
<title>Ultan.me - <?php echo htmlspecialchars($title, ENT_QUOTES, 'UTF-8'); ?></title>
You need to set the value of $title before echoing it.
Also, you should really sanitize any data before using it in queries as this is a security risk
create a new page php and add this code:
<?php
function ch_title($title){
$output = ob_get_contents();
if ( ob_get_length() > 0) { ob_end_clean(); }
$patterns = array("/<title>(.*?)<\/title>/");
$replacements = array("<title>$title</title>");
$output = preg_replace($patterns, $replacements,$output);
echo $output;
}
?>
in <head> add code: <?php require 'page.php' ?> and on each page you call the function ch_title('my title');
The problem is that $title is being referenced on line 5 before it's being assigned on line 58. Rearranging your code isn't easy, because the data is both retrieved and output at the same time. Just to test, how does something like this work?
Because you're only retrieving one row, you don't need to use a while loop, but I left it with hopes that it'll make it easier for you to relate to your current code. All I've done is removed the actual output from your data retrieval, and added variables for category and category name which are then referred to as usual later on. Also, I haven't tested this. :)
It'll be tricky to rearrange your code to make this work, but I'll try :)
So, put this at the top of your code:
<?php require_once('mysql.php'); ?>
The top of the file should look like:
<?php require_once('mysql.php'); ?>
<html>
<head>
<meta name="keywords" content="Mac user Ultan Casey TheCompuGeeks UltanKC">
<title>Ultan.me - <?php echo htmlspecialchars($title); ?> </title>
Then, create a file called mysql.php in the same directory that the file which contains the code you quoted is in.
Put this is mysql.php:
<?php
mysql_connect ('localhost', 'root', 'root');
mysql_select_db ('ultankc');
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die("Invalid ID specified.");
}
$id = (int)$_GET['id'];
$sql = "SELECT * FROM php_blog WHERE id='$id' LIMIT 1";
$result = mysql_query($sql) or print ("Can't select entry from table php_blog.<br />" . $sql . "<br />" . mysql_error());
$res = mysql_fetch_assoc($result);
$date = date("l F d Y", $res['timestamp']);
$title = $res['title'];
$entry = $res['entry'];
$get_categories = mysql_query("SELECT * FROM php_blog_categories WHERE `category_id` = $res['category']");
$category = mysql_fetch_array($get_categories);
?>
Well, hope that helped :)
I know this is an old post but having read this I think this solution is much simpler (though technically it solves the problem with Javascript not PHP).
<html>
<head>
<title>Ultan.me - Unset</title>
<script type="text/javascript">
function setTitle( text ) {
document.title = text;
}
</script>
<!-- other head info -->
</head>
<?php
// Make the call to the DB to get the title text. See OP post for example
$title_text = "Ultan.me - DB Title";
// Use body onload to set the title of the page
print "<body onload=\"setTitle( '$title_text' )\" >";
// Rest of your code here
print "<p>Either use php to print stuff</p>";
?>
<p>or just drop in and out of php</p>
<?php
// close the html page
print "</body></html>";
?>
Simply add $title variable before require function
<?php
$title = "Your title goes here";
require("header.php");
?>
header.php
<title><?php echo $title; ?></title>
<?php echo APP_TITLE?> - <?php echo $page_title;?>
this should work fine for you
if you want to current script filename as your title tag
include the function in your project
function setTitle($requestUri)
{
$explodeRequestUri = explode("/", $requestUri);
$currentFileName = end($explodeRequestUri);
$withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $currentFileName);
$explodeCurrentFileName = explode("-", $withoutExt);
foreach ($explodeCurrentFileName as $curFileValue)
{
$fileArrayName[] = ucfirst($curFileValue);
}
echo implode(" ", $fileArrayName);
}
and in your html include the function script
and replace your title tag with this
<title>Your Project Name -
<?php setTitle($_SERVER['REQUEST_URI']); ?>
</title>
it works on php7 and above but i dont have any idea about php 5.*
Hope it helps