Session with included header - php

I am having trouble getting this if statement to work using the included php file. I know that you have to test if the $_SESSION['name'] is set but for some reason its not letting me use this function in php to test if I set the session for the user.
Here is my 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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/mystyles.css"/>
<link href='http://fonts.googleapis.com/css?family=Comfortaa:700' rel='stylesheet' type='text/css'>
<title></title>
</head>
<body>
<div id="wrapper">
<?php include 'includes/header.php';?>
<?php include 'includes/footer.php';?>
</div>
</body>
</html>
Here is my updated header.php
<?php
echo "<header>
<div class='social'>
<ul>
<li><a href='#'><img src='images/Social Media/Facebook.png'/></a></li>
<li><a href='#'><img src='images/Social Media/Twitter-Bird.png'/></a></li>
<li><a href='#'><img src='images/Social Media/Google-Plus.png'/></a></li>
<li><a href='#'><img src='images/Social Media/Linkedin.png'/></a></li>
</ul>
</div>
<div class='users'>";
if(isset($_SESSION['user']){
echo "
<ul>
<li><a href='profile.php'>Profile</a></li>
<li><a href='logout.php'>Logout</a></li>
</ul>";
} else{
echo "
<ul>
<li><a href='signin.php'>Sign-In</a></li>
<li><a href='register.php'>Register</a></li>
</ul>
</div>";
}
echo "</div>
<h1><a href='index.php'>CollegeConnection</a></h1>
</header>";
?>
Now I am getting this error
Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\CollegeConnection\includes\header.php on line 13

This never will work.
Better way:
$content = "<div class='users'>";
if(isset($_SESSION['user']){
$content .= "<ul>";
$content .= "<li><a href='profile.php'>Profile</a></li>";
$content .= "<li><a href='logout.php'>Logout</a></li>";
$content .= "</ul>";
} else {
$content .= "<ul>";
$content .= "<li><a href='signin.php'>Sign-In</a></li>";
$content .= "<li><a href='register.php'>Register</a></li>";
$content .= "</ul>";
}
$content .= "</div>";
echo $content;

What you want to do is probably :
if(isset($_SESSION['user']){
echo "
<ul>
<li><a href='profile.php'>Profile</a></li>
<li><a href='logout.php'>Logout</a></li>
</ul>";
} else{
echo "
<ul>
<li><a href='signin.php'>Sign-In</a></li>
<li><a href='register.php'>Register</a></li>
</ul>
</div>";
}

Related

Why are the tabs squeezed together and does not change colour?

I am trying to create a dynamic tab with PHP and MySQL using the Bootstrap Framework.
I am wondering why the dynamic tab is squeezed together. In addition, when I click the tab, it does not change color to show that I clicked that tab.
Actual Result:
The tabs 'Now Showing' and 'Coming Soon' are squeezed together. The selected tab does not change color even when it is clicked.
Expected Result:
The tabs 'Now Showing' and 'Coming Soon' should not be squeezed together. When either of the tab is clicked, that particular tab should change color.
How do I solve this problem such that the tabs will not be squeezed together AND the tabs will change color when it is clicked (to differentiate it from the tabs that are not clicked ---> as shown in the Expected Result)
Here are my codes:
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'movie';
$conn = new mysqli($servername, $username, $password, $dbname);
$tab_query = "SELECT * FROM category ORDER BY Category_ID";
$tab_result = mysqli_query($conn, $tab_query);
$tab_menu = '';
$count = 0;
while ($row = mysqli_fetch_array($tab_result)) {
if ($count == 0) {
$tab_menu .= '<li class="active">' . $row["Category_Name"] . '</li>';
} else {
$tab_menu .= '<li>' . $row["Category_Name"] . '</li>';
}
$count++;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Testing Movie Website</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="HandheldFriendly" content="true">
<!--Bootstrap CSS-->
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity=
"sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!--Custom CSS-->
<link rel="stylesheet" href="css/main.css">
<!--jQuery-->
<script defer
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<!--Bootstrap JS-->
<script defer
src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.bundle.min.js"
integrity="sha384-6khuMg9gaYr5AxOqhkVIODVIvm9ynTT5J4V1cfthmT+emCG6yVmEZsRHdxlotUnm"
crossorigin="anonymous"></script>
<script defer src="js/main.js"></script>
</head>
<body>
<?php include "nav.inc.php"; ?>
<div class="container">
<br>
<ul class="nav nav-tabs">
<?php echo $tab_menu; ?>
</ul>
</div>
</body>
</html>
I followed this video: https://www.youtube.com/watch?v=qkmqncXTiLU
The structure and classes for .tabs is:
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
</ul>
Looks like you missed some classes.
The active class needs to go to <a> tag and the <li> tag needs a class nav-item. This will make it work. So it should essentially be:
<?php
while ($row = mysqli_fetch_array($tab_result)) {
if ($count == 0) {
$tab_menu .= '<li class="nav-item"><a class="nav-link active" href="#' . $row["Category_ID"] . '" data-toggle="tab">' . $row["Category_Name"] . '</a></li>';
} else {
$tab_menu .= '<li class="nav-item"><a class="nav-link" href="#' . $row["Category_ID"] . '" data-toggle="tab">' . $row["Category_Name"] . '</a></li>';
}
$count++;
}

Including CSS file to mpdf

I am trying to implement a CSS file to my mPDF file. Here is my mPDF file:
$pdf_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>' . get_bloginfo() . '</title>';
$cssFile = __DIR__.DIRECTORY_SEPARATOR.'styles.css';
// echo $cssFile;
if (file_exists($cssFile)) {
$pdf_output .= '<style type="text/css">'.file_get_contents($cssFile).'</style>';
}
$pdf_output .= '</head>
<body xml:lang="en">
<pagebreak>
<div id="header"><div id="headerimg">
<h1>' . get_bloginfo('name') . '</h1>
<div class="description">' . get_bloginfo('description') . '</div>
</div>
</div>
<div id="content" class="widecolumn"><div id="test">Lorem ipsum dolorem sit amet</div>';
if(have_posts()) :
while (have_posts()) : the_post();
$pdf_output .= '<div class="post">
<h2>' . $pageTitle . '</h2>';
$pdf_output .= '<div class="entry">POST: ' . wpautop($post->post_content, true) . '</div>';
$pdf_output .= '</div> <!-- post -->';
endwhile;
else :
$pdf_output .= '<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn\'t here.</p>';
endif;
$pdf_output .= '</div> <!--content-->';
$pdf_output .= '
</body>
</html>';
Everything the styles.css says is completely ignored as if the file wasn't even loaded. Interestingly enough, if I append
echo '<pre><div id="output">'.$pdf_output.'</div></pre>';
exit;
to the very end I get an HTML page and everything works just fine. Where exactly is my error here? I guess it's some kind of option in the mPDF settings.
OP found out the solution from the comments, so I'll add it here just for the sake of closure:
It seems mPDF doesn't support CSS in style tags (see here: https://mpdf.github.io/css-stylesheets/introduction.html). In this case, you could replace this:
$cssFile = __DIR__.DIRECTORY_SEPARATOR.'styles.css';
if (file_exists($cssFile)) {
$pdf_output .= '<style type="text/css">'.file_get_contents($cssFile).'</style>';
}
By this:
$cssFile = __DIR__.DIRECTORY_SEPARATOR.'styles.css';
if (file_exists($cssFile)) {
$pdf_output .= '<link rel="stylesheet" href='.$cssFile.'></link>';
}
Or else use the alternative approach that's calling mPDF's WriteHTML() twice, once for the stylesheet and another for the body, as on the example on the documentation above.

how to use css link in php code?

hi i'm doing my web programming h/w and got a problem
this is code :
<!DOCTYPE html>
<html>
<head>
<title>Music Viewer</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="http://www.cs.washington.edu/education/courses/cse190m/09sp/labs/3-music/viewer.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="header">
<h1>190M Music Playlist Viewer</h1>
<h2>Search Through Your Playlists and Music</h2>
</div>
<ul>
<?php
$songs = glob("songs/*.mp3");
foreach ($songs as $songfile) {
$text = file_get_contents($songfile);
file_put_contents($songfile, strrev($text));
basename($songfile)
?>
<li><a href <?= "$songfile" ?>> <?= basename($songfile) ?></a> </li>
<?php
}
?>
<?php
$txt = glob("songs/*.txt");
foreach ($txt as $textfile) {
$textt = file_get_contents($textfile);
file_put_contents($textfile, strrev($textt));
basename($textfile)
?>
<li><a href <?= "$textfile" ?>> <?= basename($textfile) ?></a> </li>
<?php
}
?>
</ul>
</body>
</html>
this is about showing the song lists in the folder using glob
and i wanna apply same css style from html code which is
<link href="http://www.cs.washington.edu/education/courses/cse190m/09sp/labs/3-music/viewer.css" type="text/css" rel="stylesheet" />
how to do this ???

MySQL and PHP display error

Hello i m new to MySQL and PHP under training please tell me where i m wrong
My problem occurs in ad01 and ad03 they are echo Advertise Here but ad02 echo properly
<?php require_once('Connections/localhost.php'); ?>
<?php
mysql_select_db($database_localhost, $localhost);
$query_advtDisplay = "SELECT * FROM advt";
$advtDisplay = mysql_query($query_advtDisplay, $localhost) or die(mysql_error());
$row_advtDisplay = mysql_fetch_assoc($advtDisplay);
$totalRows_advtDisplay = mysql_num_rows($advtDisplay);
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Download Links</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="wrapper">
<div class="ad01">
<?php if ($row_advtDisplay['advt-no']=='ad01')
{
echo $row_advtDisplay['advt-content'];
}
else{ echo "Advertise Here";}
?>
</div>
<div class="middlebox">
<div class="ad02">
<?php if ($row_advtDisplay['advt-no']=='ad02')
{
echo $row_advtDisplay['advt-content'];
}
else{ echo "Advertise Here";}
?>
</div>
<div class="linkbox">
<p>Download Links</p>
<ul>
<li>Link 01</li>
<li>Link 02</li>
<li>Link 03</li>
<li>Link 04</li>
<li>Link 05</li>
</ul>
<ul>
<li>Link 06</li>
<li>Link 07</li>
<li>Link 08</li>
<li>Link 09</li>
<li>Link 10</li>
</ul>
<ul>
<li>Link 11</li>
<li>Link 12</li>
<li>Link 13</li>
<li>Link 14</li>
<li>Link 15</li>
</ul>
</div>
<div class="passwordbox">
<p>RAR Password</p>
</div>
</div>
<div class="ad03"><?php if ($row_advtDisplay['advt-no']=='ad03')
{
echo $row_advtDisplay['advt-content'];
}
else{ echo "Advertise Here";}
?></div>
<div class="clear"></div>
</div>
</body>
</html>
<?php
mysql_free_result($advtDisplay);
?>
only ad02 shows properly not ad01 and ad03 No Problem with Table only Coding Problem sorry for bad english
mysql_fetch_assoc will only return one row ever... so you need to loop through to see all your data.
$just_one_row = mysql_fetch_assoc($advtDisplay);
// how to loop through all the rows
while ($row = mysql_fetch_assoc($advtDisplay)) {
echo $row["advt-no"];
echo $row["advt-content"];
}
// maybe do something like...
$mydata = array();
while ($row = mysql_fetch_assoc($advtDisplay)) {
$mydata[ $row['advt-no'] ] = $row['advt-content'];
}
<!-- and then -->
<div class="ad01">
<?php
if ( isset($mydata['ad01']) && !empty($mydata['ad01']) ) {
echo $mydata['ad01'];
} else { echo "Advertise Here"; } ?>
</div>
and I have to say it... you should really be using PDO or mysqli.

dropdown bootstrap menu is not getting populated on webpage

I have a header file where I am trying to include a bootstrap dropdown which will have the user id and two more options in the dropdown. In the header file I have created a new div for dropdown and and linked it a php file where I have the logic for populating the user id information for dropdown.
header.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">
<head>
<!--<style>{height:150px; width:1300px;}</style>-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="Public">
<title><?php echo $page_title?></title>
<script src="js/bootstrap.js"</script>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.min.js"></script>
<script src="script_dbafactory.js?<?php echo rand(0,511);?>"></script>
<link href="css/bootstrap.css" rel="stylesheet">
<!--<link href="css/style_header.css" rel="stylesheet">-->
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-4 column">
<img alt="logo" src="./images/source.png">
</div>
<div class="col-md-8 column dbf">
<h2>DBA Factory</h2>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="tabbable" id="tabs-775712">
<ul class="nav nav-tabs">
<li>
Home
</li>
<li>
Submit a project
</li>
<li>
All Projects
</li>
<li>
Innovative Ideas
</li>
<li>
Matchmaker
</li>
<li>
Meet the Team
</li>
</ul>
</div>
<div class="dropdown">
<?php include "userlogin.php"; ?>
</div>
</div>
</div>
</div>
</body>
<script>
$('.dropdown-toggle').dropdown();
$('#userlogin').dropdown('toggle');
</script>
userlogin.php
if ($loginid!="")
{
echo "<a id=\"userlogin\" role=\"button\" data-toggle=\"dropdown\" href=\"#\">" .$loginid. "<span class=\"caret\"></span></a>";
//echo "<li role=\"presentation\"><a role=\"menuitem\" tabindex=\"-1\" href=\"#\">" .$loginid. "</a></li>";
echo "<ul class=\"dropdown-menu\" role=\"menu\" aria-labelledby=\"userlogin\">";
echo "<li role=\"presentation\"><a role=\"menuitem\" tabindex=\"-1\" href=\"#\">Settings</a></li>";
echo "<li role=\"presentation\" class=\"divider\"></li>";
echo "<li role=\"presentation\"><a role=\"menuitem\" tabindex=\"-1\" href=\"http://profilelink\">My Profile</a></li>";
echo "<li role=\"presentation\"><a role=\"menuitem\" tabindex=\"-1\" href=\"https://logoutpage\"> Sign Out </a></li>";
echo "</ul>";
}
else
{
echo "<a id=\"userlogin\" role=\"button\" data-toggle=\"dropdown\" href=\"#\"> User <span class=\"caret\"></span></a>";
echo "<ul class=\"dropdown-menu\" role=\"menu\" aria-labelledby=\"userlogin\">";
echo "<li role=\"presentation\"><a role=\"menuitem\" tabindex=\"-1\" href=\"https://reloginpage\"> Sign In </a></li>";
echo "</ul>";
}
I am able to get the userid on the main login page. The dropdown works fine for me on jsfiddle but not on the concerned page. When I debug with Firebug. I see the tag not highlighted but the tag i.e. userid highlighted. Can this be a problem?
Can someone please let me know what is the problem here? How do i proceed solving this and debugging?
Make sure to include <?php include "userlogin.php"; ?> in the <body> of your document. You can't output divs and lists in your <head>.
It's also good practice to include all the JS libraries e.t.c right at the END of your body (directly before the </body> tag). Like that the rendering of the site doesn't get blocked.
Edit:
#rond Just in case you haven't figured it out yet, I created a gist (not tested) how this should look -> https://gist.github.com/sidneywidmer/8839857

Categories