Here is the index file where i initiate Application class. When i run it i get Fatal error:Cannot redeclare class Application in C:\xampp\htdocs\blog_with_pdo\project\classes\application.php on line 4
<?php
if(!class_exists('Application')) {
require_once'./classes/application.php';
$obj_app=new Application();
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="asset/front_end/css/bootstrap.min.css" />
<script src="asset/front_end/js/jquery.min.js"></script>
<script src="asset/front_end/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<?php
include 'includes/header.php';
?>
<div class="container-fluid">
<div class="row">
<div class="container">
<?php
include_once 'includes/left_sidebar.php';
?>
<div class="col-md-8"></div>
<?php
include 'includes/right_sidebar.php';
?>
</div>
</div>
</div>
<?php
include 'includes/footer.php';
?>
</div>
</body>
</html>
Here is the Application class
<?php
require_once 'db_connect.php';
class Application {
public $pdo;
public function __construct() {
$obj_db = new Db_connect();
$this->pdo = $obj_db->pdo;
}
public function select_all_published_category() {
try {
$query = "SELECT * FROM tbl_category WHERE publication_status='1' ";
$result = $this->pdo->query($query);
return $result;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}
Here is left_sidebar.php where i want to use Application class object.
<?php
$result=$obj_app->select_all_published_category();
?>
<div class="col-md-2" style="border-right: 1px solid #ffcc99; margin: 0; padding: 0;">
<nav class="nav-sidebar">
<ul class="nav tabs">
<li class="sidebar-brand">
<span class="fa fa-home solo">বিষয়ভিত্তিক</span>
</li>
<?php
while ($row=$result->fetch(PDO::FETCH_ASSOC)) { ?>
<li><?php $row['category_name']; ?></li>
<?php } ?>
</ul>
</nav>
</div>
Related
Article title is showing multiple times in foreach loop
Here is the articles.php
<?php
include('functions.php');
$article = new Article;
$articles = $article->fetch_all();
?>
<html>
<head>
<title>Sample Blog</title>
<link rel = "stylesheet" type = "text/css" href = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<div class="col-sm-6">
<h1 class="text-center">Articles</h1> </div>
<ol>
<?php
foreach($articles as $article){?>
<li>
<a href="article.php?id=<?php echo $articles["article_id"];?>">
<?php echo $articles["article_title"];?>
</a>
</li>
<?php
}
?>
</ol>
</div>
</body>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</html>
And here is the functions.php code
<?php
require ('config.php');
if (!$connection) {
echo "Failed to connect to Server!".mysqli_connect_error();
}
class Article{
public function fetch_all(){
$connection = mysqli_connect(host,username,password,database) or die("Unable to connect to the host");
$select = "SELECT * FROM articles";
$result = mysqli_query($connection,$select) or die(mysqli_error($connection));
return (mysqli_fetch_assoc($result));
}
}
?>
Here is the screenshot of the output
The problem is within your loop. You are calling $articles["article_id"] and $articles["article_title"].
You want $article["article_id"] and $article["article_title"].
foreach($articles as $article){?>
<li>
<a href="article.php?id=<?php echo $article["article_id"];?>">
<?php echo $article["article_title"];?>
</a>
</li>
<?php
}
I am working on an e-commerece website.I am using sessions as part of my cart system. On the main categories/shop page, the site should load all the items in a category unless it is already in their cart. If the cart is not even set/is empty, it automatically should load all items.
The problem is that, even when I add an item to the cart, it still displays it. I figured out, for some reason, it is not recognizing $_SESSION['cart'] as being set. This is particularly odd because the header, which makes use of a custom cart class that uses $_SESSION['cart'], works as it should (it displays the correct number of items.
What might the issue be?
shop.php
<?php
include("header.php");
$catID = $_GET['cat'];
$db = new pdoAccess("localhost","root","");
$items = $db->getItemsByCategory($catID, "stock");
$category = $db->getCategoryName($catID, "categories");
?>
<div id='mainContent'>
<div class='row'>
<h2 class="categoryHeader"> <?php echo $category;?></h2>
</div>
<?php
foreach($items as $item) {
if(!isset($_SESSSION['cart'])) {
echo "<h2>Cart Not Set!</h2><div class='col-sm-4 well'><div class='row'><img class='img-responsive' src='img/items/$item->image'></div><div class='row itemTitle'><a href='item.php?id=$item->id'>$item->name</a></div><div class='row price'>Price: $item->price</div><div class='row condition'>Condition: $item->condition</div> </div>";
} else {
if(array_search($item->id, $_SESSSION['cart']) == false) { // the item is not in the cart
echo "<h2>" . $_SESSSION['cart'] . "</h2>";
echo "<div class='col-sm-4 well'><div class='row'><img class='img-responsive' src='img/items/$item->image'></div><div class='row itemTitle'><a href='item.php?id=$item->id'>$item->name</a></div><div class='row price'>Price: $item->price</div><div class='row condition'>Condition: $item->condition</div> </div>";
}
}
}
?>
</div>
<?php
include("footer.php");
?>
addCart.php
<?php
include("header.php");
$itemID = $_GET['itemID'];
$catID = $_GET['catID'];
if(isset($_SESSION['cart'])) {
array_push($_SESSION['cart'], $itemID);
} else {
$_SESSION['cart'] = array($itemID);
}
?>
Continue Shopping?
Checkout
item.php
<?php
include("header.php");
$itemID = $_GET['id'];
$db = new pdoAccess("localhost","root","");
$item = $db->getItemDetails($itemID, "stock");
$category = $db->getCategoryName($item->category, "categories");
?>
<div id='mainContent'>
<div class='row'>
<h2 class="itemHeader"> <?php echo $item->name; ?> </h2>
</div>
<div class="row">
<div class='col-md-6'>
<img class='img-responsive' src="img/items/<?php echo $item->image;?>">
</div>
<div class='well col-md-6'><ul><li>Platform: <?php echo $category; ?> </li>
<li>Condition: <?php echo $item->condition; ?></li>
<li>Price: <?php echo $item->price; ?></li>
<//ul>
</div>
</div>
<div class="row">
<div class="col-md-8">
<p><?php echo $item->description; ?></p>
</div>
<div class="col-md-4">
<form action="addCart.php" method='get'>
<input type='hidden' name='itemID' value="<?php echo $item->id;?>" />
<input type='hidden' name='catID' value="<?php echo $category;?>" />
<input type='submit' class="addToCart" value="Add to Cart">
</form>
</div>
</div>
<?php
include("footer.php");
?>
header.php
<?php
session_start();
include("pdo.php");
$cart = new cart();
$itemCount = $cart->getItemCount();
?>
<!doctype html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="scripts/main.js" /></script>
<script src="http://code.jquery.com/jquery-1.12.3.min.js" integrity="sha256-aaODHAgvwQW1bFOGXMeX+pC4PZIPsvn2h1sArYOhgXQ=" crossorigin="anonymous"></script>
<script src="scripts/bootstrap.js" /></script>
<link href="styles/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="styles/base.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
<!-- Begin Header -->
<header>
<nav class='navbar navbar-inverse'>
<div class='navbar-header'>
<button class='navbar-toggle' data-toggle='collapse' data-target='headerNav'>
<span class='glyphicon glyphicon-plus-sign' />
</button>
<a class='navbar-brand' href='index.php'><img class='img-responsive' src='logo.png' /></a>
</div>
<div class='collapse navbar-collapse' id='headerNav'>
<ul class='nav navbar-nav'>
<li class='dropdown'>
<a class='dropdown-toggle' data-toggle="dropdown" href="#">Console<span class='glyphicon glyphicon-plus'></span></a>
<ul class='dropdown-menu'>
<li>Playstation</li>
<li>Wii</li>
<li>Gamecube</li>
<li>N64</li>
<li>Other</li>
</ul>
</li>
<li class='dropdown'>
<a class='dropdown-toggle' data-toggle="dropdown" href="#">Portable<span class='glyphicon glyphicon-plus'></span></a>
<ul class='dropdown-menu'>
<li>Nintendo DS</li>
<li>GB/GBC/GBA</li>
<li>Other</li>
</ul>
</li>
<li>Videos</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>Cart (<?php if($itemCount >= 1) {echo $itemCount;} else {echo "0";} ?> )</li>
<li>Contact</li>
</ul>
</div>
</nav>
</header>
...and the cart class:
class cart {
public $items = array();
function __construct() {
$pdo = new pdoAccess("localhost","root","");
$counter = 0;
if (isset($_SESSION['cart'])) { foreach($_SESSION['cart'] as $item) {
$items[$counter] = $pdo->getItemDetails($item, "items");
$counter++;
}
}
}
public function emptyCart() {
session_unset();
session_destroy();
}
public function removeItem($itemID) {
$index = array_search($itemID, $_SESSION['cart']);
unset($_SESSION['cart'][$index]);
}
public function getItemCount() {
if(isset($_SESSION['cart'])) {
return count($_SESSION['cart']);
} else {
return 0;
}
}
public function getSubtotal() {
$total = 0;
foreach($items as $item) {
$total += $item->price;
}
return $total;
}
public function getDiscount() {
$sub = $this->getSubtotal();
if($this->getItemCount >= 2) {
$discount = $sub * .1;
} else {
$discount = 0;
}
return $discount;
}
public function getTotal() {
$sub = $this->getSubtotal;
if($this->getItemCount >= 2) {
$sub +- $this->getDiscount;
}
$total = $sub + SHIPPING;
return $total;
}
}
I would appreciate any help. Thanks in advance!
I have a problem with my code in php.
Error: Undefined Variable 'id'.
<?php
include_once '../includes/connection.php';
include_once '../includes/functions.php';
session_start();
$posts = $pdo->query("SELECT * FROM posty ORDER BY post_id DESC");
$j=0;
?>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="../sources/css/style.css">
<script type="text/javascript" src="../sources/scripts/time.js"></script>
<script type="text/javascript" src="../sources/scripts/scroll.js"></script>
</head>
<body onload="timeLine(); setInterval('timeLine()', 1000 )" >
<header>
<div class = "menu">
<ul>
<li> Blog </li>
<li> Archives </li>
<li> Contact </li>
<li id="addPost"> Add Post </li>
<li id="editPost"> Edit Post </li>
<li id="deletePost"> Delete Post </li>
<li id="showBlogContent"> Hide Blog </li>
<li id="menuRightLogin"> Logout </li>
<li id="menuRightDate"><?php echo date('jS F Y').' '; ?><span id="clock"> </span> </li>
</ul>
</div>
</header>
<div id="showBlog" style="display: block;">
<div class="container">
<div class="postsContainer">
<?php foreach($posts as $post) {
if ($j <= 5) { ?>
<div class="postBox">
<div class="postTitle">
<br />
<?php echo $post['post_title']; ?>
</div>
<div class="postContent">
<br />
<?php if(($wordCount = str_word_count($post['post_content']) <=50)) {
echo $post['post_content'];?>
Edit Post
<br />
<?php } else {
echo excerpts($post['post_content'], 50).'... <br/> <br/>
Edit Post
<br /> <br />';
} ?>
</div>
<div class="postDate">
<?php echo '<b id="author">Author:</b> '.$post['post_author'].' <b id="posted">posted:</b> '.date('jS F Y', $post['add_date']); ?>
</div>
</div>
<?php $j++; } }?>
</div>
</div>
<footer>
<small> © Copyright 2015, n3stis </small>
</footer>
</div>
</body>
</html>
So from here I'm sending a 'id' to edit form:
<?php
include_once '../includes/connection.php';
include_once '../includes/functions.php';
session_start();
$id = $_GET['id'];
if (isset ($id)) {
if (isset($_SESSION['logged_in'])) {
$query = $pdo->prepare("SELECT * FROM posty WHERE post_id='" . $id . "' LIMIT 1");
$query->execute();
$post = $query->fetch();
if (isset($_POST['post_title'], $_POST['post_content'], $_POST['post_author'])) {
$postTitle = $_POST['post_title'];
$postAuthor = $_POST['post_author'];
$postContent = nl2br($_POST['post_content']);
$postTime = time();
if (empty($post_title) or empty($post_content) or empty($post_author)) {
$error = 'All fields required.';
} else {
$sql = ("UPDATE `posty` SET `post_title` = :title, `post_author` = :author, `post_content` = :content, `edit_date` = :editDate WHERE `post_id` = :postID ");
$query = $pdo->prepare($sql);
$query->bindValue(':title', $postTitle);
$query->bindValue(':author', $postAuthor);
$query->bindValue(':content', $postContent);
$query->bindValue(':editDate', $postTime);
$query->bindValue(':postID', $id);
$query->execute();
header('Location: adminPanel.php');
}
}
?>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="../sources/css/style.css">
<script type="text/javascript" src="../sources/scripts/time.js"></script>
<script type="text/javascript" src="../sources/scripts/scroll.js"></script>
</head>
<body onload="timeLine(); setInterval('timeLine()', 1000 )">
<header>
<div class="menu">
<ul>
<li> Blog </li>
<li> Archives </li>
<li> Contact </li>
<li id="#addPost"> Add Post </li>
<li id="#editPost"> Edit Post </li>
<li id="#deletePost"> Delete Post </li>
<li id="showBlogContent"> Hide Blog </li>
<li id="menuRightLogin"> Logout </li>
<li id="menuRightDate"><?php echo date('jS F Y') . ' '; ?><span id="clock"> </span></li>
</ul>
</div>
</header>
<div id="showBlog" style="display: block;">
<div class="container">
<div class="postsContainer">
<?php if (isset($error)) { ?>
<p id="error"><?php echo $error ?> </p>
<?php } ?>
<form action="editPostPanel.php" method="post">
<input type="text" name="post_title" value="<?php echo $post['post_title'];?>"/>
<input type="text" name="post_author" value="<?php echo $post['post_author'];?>"/>
<br/>
<br/>
<textarea name="post_content" rows="15" cols="90"><?php echo $post['post_content'];?></textarea>
<br/>
<br/>
<input type="submit" value="Wyślij post"/>
</form>
</div>
</div>
<footer>
<small> © Copyright 2015, n3stis </small>
</footer>
</div>
</body>
</html>
<?php
} else {
echo 'Error';
} }
else {
echo 'Error';
}
When I send a form I get this:
Notice: Undefined index: id on line 7
I will be very greatfull for yours help :)
change
$id = $_GET['id'];
if (isset ($id))
to
if (isset ($_GET['id'])) {
$id = $_GET['id'];
Your form doesn't have the id parameter in it. You need to change
<form action="editPostPanel.php" method="post">
to:
<form action="editPostPanel.php?id=<?php echo $id; ?>" method="post">
This is in addition to correcting the isset check that the other answers noted:
if (isset($_GET['id'])) {
$id = $_GET['id'];
While I don't have your line numbers, I'd guess that line 7 is $id = $_GET['id'];. This indicates that you don't have a GET (query) param called id set. To fix that Notice, you probably want to swap the two lines as follows:
if (isset($_GET['id'])){
$id = $_GET['id']
But all that will do is start echoing 'Error' back to you. You'll also want to figure out why your line editPostPanel.php?id='.$post['post_id'] is not apparently setting the id as desired.
I have two navigation one is in the header and the other is in the same page, now what I want is to do is be able to pull all the data from the table product a specific product from the sub category when I click the sub category. what I have now, is when I click on the menu that is on the header page that I have included in every page I get the product listed but the menu that is on product page produce an error that the foreach is empty. when I use my other function to pull all the sub category it works fine but I want to pull only the sub category that belong to that particular main category. the code below will make more sense.
header Page:
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');?>
<?php require_once("initi.php");?>
<html>
<head>
<title></title>
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/main.css" />
<link rel="stylesheet" href="css/footer.css" />
<script type="text/javascript" src="/ghaniya.com/javascript/jqu.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".logbtn").click(function(){
$("#login2").hide("show");
$("#login1").slideToggle("slow");
$(this).toggleClass("active");
});
$(".logbtn2").click(function(){
$("#login1").hide("show");
$("#login2").slideToggle("slow");
$(this).toggleClass("active");
});
});
</script>
<style type="text/css">
div#login1{
background-color:#efebee;
margin:0 auto;
width:100%;
display:none;
height:100px;
}
div#login2{
background-color:#f5dce4;
margin:0 auto;
width:100%;
display:none;
height:100px;
}
.size{width:960px; }
.flotl{float:left; font-size:11px; text-decoration:none; height:30px; margin-left:20px;}
div#centerl{ width:960px; margin:0 auto; }
</style>
</head>
<body>
<div id="head">
<div class="size center">
<img src="img/logosmall.png">
<ul id="mainLink">
<li><a class="logbtn">Women</a></li><li><a class="logbtn2" href="#">man</a></li><li>Beauty</li><li>Gifts</li><li>Perfumes</li>
</ul>
<div id="loginBox">
<h3>REGISTER | SIGN IN</h3>
</div>
<div id="search">
<input type="submit"/><input type="text" value="Search"/>
</div>
<div id="bag"></div>
</div>
</div>
<div id="login1" >
<div id="centerl">
<ul class="flotl" >
<?php
$cata = Catagory::find_all();
foreach($cata as $catag){?>
<li><?php echo $catag->name; ?></li>
<?php }?>
</ul>
</div>
</div>
Product Page:
<?php require_once("includes/head.php"); ?>
<?php
$cata_id = "";
$subcat ="";
if(isset($_GET['catid']))
{
$cata_id = $_GET['catid'];
$product = Product::find_by_cata_id($cata_id);
}
elseif(isset($_GET['subcat']))
{
$subcat =$_GET['subcat'];
$subcat = SubCata::find_by_cata_id(1);
print_r($subcat);
}
?>
<div class="cBoth"></div>
<div id="contentEveryWhere" class="center">
<div id="cata">
<div id="leftNoticeBoard"></div>
<ul id="catagories">
<h1>CLOTHING</h1>
<?php foreach($subcat as $sub){ ?>
<li><?php echo $sub->name; ?></li>
<?php } ?>
</ul>
</div>
<div id="products">
<div id="catatitle">
<ul>
<li>Home ></li><li>Women ></li><li>Coat<li>
</ul>
<h1>Coat</h1>
<p class="floatl">Sort items by Price: High | Low View as: <img src="#" alt="icon png"/> |<img src="#" alt="icon png"/></p>
</div>
<ul id="product">
<?php $product = Product::find_by_cata_id($cata_id);?>
<?php foreach($product as $pro){?>
<?php $image = Image::find_by_product_id( $pro->product_id);?>
<li>
<?php if(isset($image->filename)){ ?>
<img src="/ghaniya.com/img/products/<?php echo $image->filename;?>"/>
<?php } ?>
<h2><?php echo $pro->name;?></h2>
<h3> <?php echo $pro->product_desc;?></h3>
<h4><?php echo $pro->price;?></h4>
</li>
<?php } ?>
</ul><!--end of product list-->
</div> <!--end of products div-->
</div><!--end of contentEveryWhere div-->
<div class="cBoth"></div>
<?php require_once("includes/footer.php"); ?>
Warning: Invalid argument supplied for foreach() in /Users/mughery/Website/ghaniya.com/product.php on line 25
The problem is quite simple:
if(isset($_GET['catid']))
{
$cata_id = $_GET['catid'];
$product = Product::find_by_cata_id($cata_id);
}
elseif(isset($_GET['subcat']))
{
$subcat =$_GET['subcat'];
$subcat = SubCata::find_by_cata_id(1);
}
That means you're only setting $subcat if, and only if $_GET['catid'] isn't set and $_GET['subcat'] is. In all other cases $subcat is an empty string, not an array. Just add a line like:
$subcat = (is_array($subcat) ? $subcat : SubCata::find_by_cata_id(1));
//or:
$subcat = (is_array($subcat) ? $subcat : array());
And you'll be good to go
Also, this:
$subcat =$_GET['subcat'];
$subcat = SubCata::find_by_cata_id(1);
Doesn't really make sense to me, I think you need something like:
elseif(isset($_GET['subcat']))
{
$subcat =SubCata::find_by_cata_id($_GET['subcat']);
}
$subcat = (is_array($subcat) ? $subcat : SubCata::find_by_cata_id(1));
If not, regardless of the subcat parameters actual value, your fetching all data for id 1...
When using a foreach, the first argument has to be a array.
You could change the code to:
<?php
if( is_array( $subcat ) )
foreach($subcat as $sub){
?>
4 line:
<?php
$subcat = array();
?>
I have the following navigation bar script:
<?php session_start();
require('includepath.inc.php');
require($include_path.'loginsysfunc.inc.php');
$current_page = $_SERVER['REQUEST_URI'];
?>
<div class="navbar">
<img class="navlogo" src="logo.png">
<div class="navbutton">Home</div>
<div class="navbutton">About</div>
<div class="navbutton">Donate</div>
<?php
if (loggedIn()){
?>
<div class="navusername"><?php echo $_SESSION['username']; ?></div>
<div class="navtoolsettings">Settings</div>
<div class="navtoollogout">Log out
<?php
} elseif ($current_page == '/login.php') {
?>
<div class="navregister">Register</div>
<?php
} else {
?>
<div class="navusername">Log in</div>
<?php
}
?>
</div>
For some reason, a strange "?>" is being displayed. I am super confused, so please help.
Here is includepath.inc.php (the only I reason it's there is because I am on a shared host, and I don't want to type '/home/bigdumbhash/public_html/include' everytime. But, here it is:
<?php
$include_path = '/home/a6595899/public_html/include/';
?>
Here is loginsysfunc.inc.php. These are functions that go with my login system to save time:
<?php
function valUser() {
session_regenerate_id();
$_SESSION['valid'] = true;
$_SESSION['username'] = $userid;
echo '<meta http-equiv="refresh" content="0;URL=\'index.php\'">';
}
function loggedIn()
{
if($_SESSION['valid'] == true) {
return true;
} else {
return false;
}
}
function createSalt() {
$string = $string = md5(uniqid(rand(), true));
return substr($string, 0, 3);
}
function logout()
{
$_SESSION = array();
session_destroy();
echo '<meta http-equiv="refresh" content="0;URL=\'index.php\'">';
}
?>
Here is the actual HTML of the page:
<!DOCTYPE html>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<title>
Log in
</title>
</head>
<body>
<div class="navbar">
<img class="navlogo" src="logo.png">
<div class="navbutton">Home</div>
<div class="navbutton">About</div>
<div class="navbutton">Donate</div>
<div class="navregister">Register</div>
</div> ?>
<div class="loginbox">
<h1>Log in</h1>
<form action="logingo.php" method="POST">
<input class="userpass" type="text" name="username" value="Username" onFocus="this.value='';">
<br>
<input class="userpass" type="password" name="password" value="Password" onFocus="this.value='';">
<br>
<input class="loginbutton" type="submit" value="Log in!">
</form>
</div>
</body>
</html>
<?php session_start();
require('includepath.inc.php');
require($include_path.'loginsysfunc.inc.php');
$current_page = $_SERVER['REQUEST_URI'];
?>
<div class="navbar">
<img class="navlogo" src="logo.png">
<div class="navbutton">Home</div>
<div class="navbutton">About</div>
<div class="navbutton">Donate</div>
<?php
if(loggedIn()){
?>
<div class="navusername"><?php echo $_SESSION['username']; ?></div>
<div class="navtoolsettings">Settings</div>
<div class="navtoollogout">Log out
<?
}else if($current_page == '/login.php'){
?>
<div class="navregister">Register</div>
<?
}else{
?>
<div class="navusername">Log in</div>
<?php
}
?>
</div>
According to the HTML output you posted, the problem seems to be somewhere in the file that displays <div class="loginbox"> (before that).
Have a look to the contents of that file.