MySQL and PHP display error - php

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.

Related

Change navigation bar if user is logged in

I want the "Login/Sign up" button in the navigation bar to change to "My Account" after the user logs in. I have a problem as my index.html page does not display the navigation bar.
index.html page:
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Index Page</title>
</head>
<body>
<?php include("navigation.php"); ?>
</body>
</html>
navigation.php
<?php
include("check.php");
?>
<?php
if ($loginst == 1){ ?>
<div id="nav">
<ul >
<li class="navbar-left">Page 2</li>
<li class="navbar-left">Page 3</li>
<li class="navbar-right">My Account</li>
<li class="navbar-right"><a href="logout.php">Sign Out</a</li>
</ul>
</div>
<?php } else { ?>
<div id="nav">
<ul >
<li class="navbar-left">Page 2</li>
<li class="navbar-left">Page 3</li>
<li class="navbar-right"><a href="login.php">Login</a</li>
</ul>
</div>
<?php } ?>
check.php
<?php
include('connection.php');
session_start();
$loginst = 0;
if ($_SESSION['username']){
$user_check = $_SESSION['username'];
$ses_sql = mysqli_query($db,"SELECT username FROM users WHERE username='$user_check' ");
$row=mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
$login_user=$row['username'];
if(!empty($login_user))
{
$loginst = 1;
}
}
?>
If I go to index.html then nav bar is not displayed. What am I doing wrong? Thanks.
Because your file format is html and does not support php tags :
<?php include("navigation.php"); ?>
rename index.html to index.php for support php include.

Rendering another layout inside index.php

I am following this Link to understand the basic framework of PHP Web app but I am not able to understand how can I switch back and forth to the different layouts (Articles, Portfolio) through navigation bar?
All different layout must be like index.php.
Image you have articles.php and portfolio.php. There content would be like:
Articles.php:
<?php
// load up your config file
require_once("/path/to/resources/config.php");
require_once(TEMPLATES_PATH . "/header.php");
?>
<div id="container">
<div id="content">
<!-- Your article content here!!! -->
</div>
<?php
require_once(TEMPLATES_PATH . "/rightPanel.php");
?>
</div>
<?php
require_once(TEMPLATES_PATH . "/footer.php");
?>
and portfolio.php:
<?php
// load up your config file
require_once("/path/to/resources/config.php");
require_once(TEMPLATES_PATH . "/header.php");
?>
<div id="container">
<div id="content">
<!-- Your portfolio content here!!! -->
</div>
<?php
require_once(TEMPLATES_PATH . "/rightPanel.php");
?>
</div>
<?php
require_once(TEMPLATES_PATH . "/footer.php");
?>
But everytime you add a new page, you must update your header.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Simple Site</title>
</head>
<body>
<div id="header">
<h1>Simple Site</h1>
<ul class="nav global">
<li>Home</li>
<li>Articles</li>
<li>Portfolio</li>
<li>Any other page</li>
</ul>
</div>
This is what actually I wanted to achieve
<nav>
<ul>
<li>Home</li>
<li>Projects</li>
<li>Calculator</li>
<li>Blog</li>
<li>About</li>
</ul>
</nav>
I am sending the page variable to the index and then it is rendering the layout accordingly through this way.
if (isset($_GET["page"])){
$page=$_GET["page"];
}
else {
$page = "home";
}
renderLayoutWithContentFile($page .".php", $variables);

Session with included header

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>";
}

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

Scraping website in php with javascript code

I need to scrape this site,
here is link
makemytrip.
Can any of you guide me or give some hint how to scrape it in php?
Follow the methods:
the page to scarpe
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <title>This is the Title</title>
<meta name="description" content="Meta Description" />
<meta name="keywords" content="Meta Keywords" />
</head>
<body>
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
<li>Link 5</li>
</ul>
</div>
</body>
scraping method
<?php
$file_string = file_get_contents('page_to_scrape');
preg_match('/<title>(.*)<\/title>/i', $file_string, $title);
$title_out = $title[1];
preg_match('/<meta name="keywords" content="(.*)" \/> /i', $file_string, $keywords);
$keywords_out = $keywords[1];
preg_match('/<meta name="description" content="(.*)" \/> /i', $file_string, $description);
$description_out = $description[1];
preg_match_all('/<li><a href="(.*)">(.*)<\/a><\/li>/i', $file_string, $links);
?>
<p><strong>Title:</strong> <?php echo $title_out; ?></p>
<p><strong>Keywords:</strong> <?php echo $keywords_out; ?></p>
<p><strong>Description:</strong> <?php echo $description_out; ?></p>
<p><strong>Links:</strong> <em>(Name - Link)</em><br />
<?php
echo '<ol>';
for($i = 0; $i < count($links[1]); $i++) {
echo '<li>' . $links[2][$i] . ' - ' . $links[1][$i] . '</li>';
}
echo '</ol>';
?>
</p>

Categories