Webpage Title user name - php

I want to put the name of the loggedin user as the title of the page. But I've no idea how to do it. I've tried several ways but each one show a parse error. here is my php code for logging in the user and then showing his details.
<?php
//if the login session does not exist therefore meaning the user is not logged in
if(strcmp($_SESSION['uid'],"") == 0){
//display and error message
echo "<center>You need to be logged in to user this feature!</center>";
}else{
//otherwise continue the page
//this is out update script which should be used in each page to update the users online time
$time = date('U')+50;
$update = mysql_query("UPDATE `employer` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");
$display_query = mysql_query("SELECT * FROM employer WHERE `id` = '".$_SESSION['uid']."'");
echo "<table id='pageTable'><tbody><th>Your Details</th>";
echo "<tbody>";
while($row = mysql_fetch_array($display_query)){
echo "<tr><td>Name: </td><td>".$row['name']."</td><tr>";
$titlename = $row['name'];
echo "<tr><td>E-Mail ID: </td><td>".$row['email']."</td><tr>";
echo "<tr><td>Contact No.: </td><td>".$row['contact']."</td><tr>";
echo "<tr><td>Company: </td><td>".$row['company']."</td><tr>";
echo "<tr><td>Designation: </td><td>".$row['designation']."</td><tr>";
}
echo "</tbody>";
echo "</table>";
echo "<table><tr><td>";
echo '<div class="button">Logout</td></tr></table>';
//make sure you close the check if they are online
}
?>

You need to get the data you want before you output the <head> section of the page, then include a <title> element in it.
<title><?php echo htmlspecialchars($myTitle); ?></title>

Make sure you properly markup your page: doctype, html-head-body etc. You can do that and while the body is still 'open', state your php code by simply starting with <?php followed by your script.
Then, the relevant part of your loginname-as-title code:
<head>
<title><?php echo $loginName ?></title><!-- thanks to Berry Langerak for noting 'echo' was missing -->
</head>
<body>
<?php
where $loginName is of course your var for the login ID you want to show.

use below code to display loginname as title
echo '<script language="javascript">';
echo 'document.title = \''.$row['name'].'\'';
echo '</script>'

Related

I Want to show the alert of fetched row from database table on body load event

I want to compare current system date with my database table date as shown. If both matched then corresponding member Name will be shown in alert on body load.
My Code
<?php
$now=date("Y/m/d");
$sql = "select MemName from payment where $now ='$RemindDate' ";
$result = mysql_query($sql) or die(mysql_error());
while($rowval2 = mysql_fetch_array($result)) {
$MemName=$rowval2['MemName']);
}
alert("$rowval2['MemName']");
?>
First fetch the date from database table then compare with your system date using date()Ref.1 Ref.2
if the date match then use member id to fetch member name and display it using alert().
<?php
$now=date("Y/m/d");
$sql = "select MemName from payment where date_column_name = '".$now."' ";
$result = mysql_query($sql) or die(mysql_error());
while($rowval2 = mysql_fetch_array($result))
{
$MemName=$rowval2['MemName'];
}
echo '<script language="javascript">';
echo 'alert ('<?php echo $MemName; ?>')';
echo '</script>';
?>
Now the other option is, When you use JavaScript tag in echo function, It can be misleading because of "quotation" signs in php echo function. I suggest you to close tag if you are beginner. Also try to echo each query and its result so you will get idea that it actually working or not.
<?php
$now=date("Y/m/d");
$sql = "select MemName from payment where date_column_name = '".$now."' ";
$result = mysql_query($sql) or die(mysql_error());
while($rowval2 = mysql_fetch_array($result))
{
$MemName=$rowval2['MemName'];
}
?>
<script type="text/javascript">
alert("<?php echo $MemName; ?>");
</script>
<?php
?>

Calling from a MySQL/PHP array multiple times in a page

I'm probably asking a very simple question here - I know the basics of calling an array but I think I'm probably not doing it in the most efficient way... I'm calling some data into an array at the start of my page and then I want to be able to use this data-set multiple times throughout the page without wrapping everything in PHP if possible.
At present I'm doing it like this -
A variable ('video') is passed to my page through the URL which I get like so:
<?php
$video = $_GET['video'];
?>
My <title> tag is pulled from the selected database (also titled 'video')
<?php
$title = mysql_query("SELECT * FROM video WHERE ID = '{$video}'") or die(mysql_error());
mysql_real_escape_string($video);
while($head = mysql_fetch_array( $title )) {
echo "{$head['title']} - BY XXXXX</title>";
echo "<meta property=\"og:title\" content=\"{$head['title']} - BY XXXX\"/>";
}
?>
I then want to use the {$video} data later on the same page, but defining a slightly different variable like so:
<?php
$data = mysql_query("SELECT * FROM video WHERE ID = '{$video}' ORDER BY added DESC") or die(mysql_error());
mysql_real_escape_string($video);
while($info = mysql_fetch_array( $data )) if ($info['ytembed'] == 'yes') {
echo "{$info['embedcode']}";
echo "<div class=\"videobox1\">";
echo "<div class='video-title'>{$info['title']}</div>";
echo "<div class='video-subtitle'>{$info['subtitle']}</div>";
echo "<div class='video-credits'>{$info['cast']}</div>";
echo "<div class='back'>«back</div></div>";
} else {
echo "no embed code";
}
?>
So at the moment every time I want to pull from that data I'm calling the whole array again - it would be amazing if instead of doing this I could just print/echo selected items
Is there a way to make my code more efficient and do this?
I'm also looking to Validate the ID and if it doesn't exist within the video DB send the user to a 404 page - but perhaps that's a separate question.
Hello this is refined code
Replace first 1 with this.
$video = $_GET['video'];
$video = mysql_real_escape_string($video);
$videodata = mysql_query("SELECT * FROM video WHERE ID = '{$video}' LIMIT 1") or die(mysql_error());
// execute the query and check if video id exist or not
if(mysql_num_rows($videodata) == 0){
// 404 redirect code.
}
Replace Second with
$videodataArray = array(); // created array for storing video data
while ($head = mysql_fetch_array($videodata))
{
$videodataArray = $head ; // store the value in video data array for to use in fulll page
echo "{$videodataArray['title']} - BY XXXXX</title>";
echo "<meta property=\"og:title\" content=\"{$videodataArray['title']} - BY XXXX\"/>";
}
Replace last one with
echo "{$videodataArray['embedcode']}";
echo "<div class=\"videobox1\">";
echo "<div class='video-title'>{$videodataArray['title']}</div>";
echo "<div class='video-subtitle'>{$videodataArray['subtitle']}</div>";
echo "<div class='video-credits'>{$videodataArray['cast']}</div>";
echo "<div class='back'>«back</div></div>";

Refresh page php

how would i go about refreshing a page after i have submitted a form and done some php stuff with it. Heres my form and the php so far.
<form class="removeform"action='peteadd.php'method='post' enctype='multipart/form-
data' name='image_remove_form' >
<?php
include '../inc/connect.php';
$q = "SELECT * FROM gallerythumbs WHERE gallery = 1";
if($r = mysql_query($q)){
while($row=mysql_fetch_array($r)){
echo "<div class='thumb'>",
"<input type='checkbox' name='remove[{$row['id']}]'>",
"<label for='Remove'><span class='text'>Remove</span></label>",
"<br />",
"<img class='thumbnail' src='{$row['filename']}'
alt='{$row['description']}' />",
"</div>";
}
}
else{
echo mysql_error();
}
?>
<input type='submit' name='submit' value='Remove' />
</form>
</div>
<?php
include '../inc/connect.php';
//if delete was checked, delete entries from both tables
if(isset($_POST['remove'])){
$chk = (array) $_POST['remove'];
$p = implode(',',array_keys($chk));
$t = mysql_query("SELECT * FROM galleryimages WHERE id IN ($p)");
$r = mysql_query("SELECT * FROM gallerythumbs WHERE id IN ($p)");
$url=mysql_fetch_array($t);
$image=$url['filename'];
$url2=mysql_fetch_array($r);
$image2=$url2['filename'];
if ($t){
unlink($image);
unlink($image2);
$q = mysql_query("DELETE FROM galleryimages WHERE id IN ($p)");
$s = mysql_query("DELETE FROM gallerythumbs WHERE id IN ($p)");
}
else{
echo "<span class='text'>
There has been a problem, go back and try again.
<br />
<a href='peteadd.php'>Back</a>
</span>";
}
}
else{
echo "<span class='title'>
There are no images in the gallery
<br />
<a href='peteadd.php'>Add Images</a>
</span>";
}
?>
This for display some thumbnails that are saved in mysql with a remove checkbox above them. When I check them then submit the form the are deleted from the direcotries and the mysql tables ok, but how can I refresh the page so the deletion is obvious?
Thanks for looking
what you are describing sounds like you are displaying your page and within it you run some additional code - like deletion - so when you post your form you end up with images being pull from database and then removed
you should run your logic first and only then display page - that way you will be first deleting your records and then when it came to get data from database it will get right data (without records already deleted)
any other soultion will be nothing but hacky way to bypass problem that souldn't exist in the first place :)
You cannot directly refresh a page with PHP, but you can echo out a refresh tag like this
echo '<meta http-equiv="refresh" content="0">'
, or you can do it with javascript, like
location.reload(true);

Dynamic url or pages in php

Well it's been my very first initiative to build a dynamic page in php. As i'm a newbie in php, i don't know much about php programming. i've made a database named "dynamic" and it's table name "answer" after that i've inserted four fields namely 'id', 'A1','A2', 'A3'.
I inserted the value in id=1 which are A1=1,A2 and A3-0,
In id=2, i have inserted A1=0, A2=1, A3=0
In id-3, i have inserted A1 and A2=0 A3=1
So now what i wanted is whenever i will click on the link of id=1 then it will display the content of id=1 and so on...
What i've done so far are:-
$conn= mysql_connect("localhost","root", "");
$db= mysql_select_db("dynamic", $conn);
$id=$_GET['id'];
$sql= "select * from answer order by id";
$query= mysql_query($sql);
while($row=mysql_fetch_array($query, MYSQL_ASSOC))
{
echo "<a href='dynamic.php?lc_URL=".$row['id']."'>Click Here</a>";
if($row['A1']==1)
{
echo "A1 is 1";
}
else if($row['A2']==1)
{
echo "A2 is 1";
}
else if($row['A3']==1)
{
echo "A3 is 1";
}
else {
echo "Wrong query";
}
}
?>
When i've executed this codes then it is showing me the exact id and it is going to the exact id but the values has not been changing..
I want whenever i will click on the id then it will display the exact value like if i click on id=2 then it will echo out "A2 is 1" nothing else....
Can anyone please help me out?
I also have noticed about
$id=$_GET['id'];
what is it and how to use it. Can anyone explain me out..
Thanks alot in advance:)
It may be best to start here to get a good understanding of php, before diving so deep. But to answer the specific questions you asked here...
The php $_GET variable is defined pretty well here:
In PHP, the predefined $_GET variable is used to collect values in a
form with method="get".
What this means is that any parameters passed via the query string (on a GET request) in the URL will be accessible through the $_GET variable in php. For example, a request for dynamic.php?id=1 would allow you to access the id by $_GET['id'].
From this we can derive a simple solution. In the following solution we use the same php page to show the list of items from the answer table in your database or single row if the id parameter is passed as part of the url.
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
$mysqli = new mysqli("localhost", "user", "password", "dynamic");
$query = 'SELECT * FROM answer';
if ($_GET['id']) {
$query .= ' WHERE id = '.$_GET['id'];
} else {
$query .= ' ORDER BY id';
}
$res = $mysqli->query($query);
if ($res->num_rows == 0) {
echo '<p>No Results</p>';
} else if ($res->num_rows == 1) {
// Display Answer
$row = $res->fetch_assoc();
echo '<h3>Answer for '.$row['id'].'</h3>';
echo '<ul>';
echo '<li>A1 = '.$row['A1'].'</li>';
echo '<li>A2 = '.$row['A2'].'</li>';
echo '<li>A3 = '.$row['A3'].'</li>';
echo '</ul>';
} else {
// Display List
echo '<ul>';
while ($row = $res->fetch_assoc()) {
echo '<li>Answers for '.$row['id'].'</li>';
}
echo '</ul>';
}
?>
</body>
</html>
OK, this might not be exactly what you are looking for, but it should help you gain a little better understanding of how things work. If we add a little javascript to our page then we can show/hide the answers without using the GET parameters and the extra page request.
<!DOCTYPE HTML>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
<?php
$mysqli = new mysqli("localhost", "user", "password", "dynamic");
$query = 'SELECT * FROM answer ORDER BY id';
$res = $mysqli->query($query);
if ($res->num_rows == 0) {
echo '<p>No Results</p>';
} else {
// Display List
echo '<ul>';
while ($row = $res->fetch_assoc()) {
echo '<li>Answers for '.$row['id'].'';
echo '<ul id="answers_'.$row['id'].'" style="display:none;">';
echo '<li>A1 = '.$row['A1'].'</li>';
echo '<li>A2 = '.$row['A2'].'</li>';
echo '<li>A3 = '.$row['A3'].'</li>';
echo '</ul>';
echo '</li>';
}
echo '</ul>';
}
?>
<script>
function toggleAnswers(answer) {
$('#answers_' + answer).toggle();
}
</script>
</body>
</html>
There are many more solutions, each more complicated that what I've presented here. For example we could set up an ajax request to load the answers into the list page only when an item is clicked. My advice is to go through some beginner tutorials on php and look at some of the popular PHP frameworks: Zend, CodeIgniter, CakePHP, etc. Depending on what you overall goal is, one of these might really help you get there faster.
Be warned that the code provided here is only an example of how to accomplish what you were asking. It definitely does not follow all (if any) best practices.

Trying to fill fields with Javascript, echoed by PHP

Here's my code. Is there some reason this should not work? I'm getting all of the fields from MySQL.
Basically what I want is to send information back from a page, with an id number, and this should be used to select the row number for MySQL.
Here's my code from the first page:
$org = $_POST['organization'];
header('Location: '.$admin.'?org='.$org);
And then my code on main page:
ECHO '<script>';
ECHO 'document.getElementById("orgid").value="'.$org_id.'"';
ECHO 'document.getElementById("orgname").value="'.$org_name.'"';
ECHO 'document.getElementById("add1").value="'.$add_1.'"';
ECHO 'document.getElementById("add2").value="'.$add_2.'"';
ECHO 'document.getElementById("city").value="'.$city.'"';
ECHO 'document.getElementById("state").value="'.$state.'"';
ECHO 'document.getElementById("zip").value="'.$zip.'"';
ECHO 'document.getElementById("url").value="'.$url.'"';
ECHO 'document.getElementById("email").value="'.$email.'"';
ECHO 'document.getElementById("phone").value="'.$phone.'"';
ECHO 'document.getElementById("contact").value="'.$contact.'"';
ECHO 'document.getElementById("hours").value="'.$hours.'"';
ECHO 'document.getElementById("file").value="'.$file.'"';
ECHO 'document.getElementById("notes").value="'.$notes.'"';
ECHO 'document.getElementById("description").value="'.$description.'"';
ECHO '</script>';
And here's the code to communicate with MySQL:
if (isset($_GET["org"]) && ($_GET['org'] !== '')) {
$org = $_GET['org'];
$resorgfull = mysql_query("SELECT org_id, org_name, add_1, add_2, city, state, zip, url, email, phone, contact, hours, file_loc, notes, description FROM organization WHERE org_id=".$org.");
if (!$resorgfull) {
die('Invalid query: ' . mysql_error());
}
One thing definitely wrong is the way you're echoing the scripts, it should be more like this:
echo "<script type='text/javascript'>\n";
echo "document.getElementById('orgid').value='$org_id';\n";
...
Or preferably...
// Close the PHP tag and output straight HTML with embedded PHP values:
?>
<script type="text/javascript">
document.getElementById('orgid').value='<?php echo $org_id; ?>';
...
If this doesn't fix it, you'll have to give us more info regarding what is not working.
I don't see the code where you take the query result and fetch each row.. are you doing that?

Categories