This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I'm working on a simple project and I'm using PHP Object Oriented. Basically I have a class that contains information about how to get data from MySQL database and show them on a page. This class is called admin.php and goes like this:
<?php
class Admin{
private $db,$username,$password,$id,$group,$Datetime,$msg;
public function __construct()
{
$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function getAdmin($name)
{
if(!empty($name))
{
$adm = $this->db->prepare("select * from admins where username=?");
$adm->bindParam(1,$name);
$adm->execute();
while($row = $adm->fetch())
{
$this->id = $row['id'];
$this->username = $row['username'];
$this->password = $row['password'];
$this->group = $row['group'];
$this->Datetime = $row['date_joined'];
$this->msg = $row['welcome_message'];
}
}
}
public function getID()
{
return $this->id;
}
public function getUsername()
{
return $this->username;
}
public function getPassword()
{
return $this->password;
}
public function getGroup()
{
return $this->group;
}
public function gtDate()
{
return $this->Datetime;
}
public function welcomeMessage()
{
return $this->msg;
}
}
?>
Then on another page which is called dashboard.php ,I have included this class file and coded this:
<?php if ($dataSet->welcomeMessage()== 0){
echo "
<li class='dropdown messages-menu'>
<!-- Menu toggle button -->
<a href='#' class='dropdown-toggle' data-toggle='dropdown'>
<i class='fa fa-envelope-o'></i>
<span class='label label-success'>1</span>
</a>
<ul class='dropdown-menu'>
<li class='header'>You have one new message</li>
<li>
<!-- inner menu: contains the messages -->
<ul class='menu'>
<li><!-- start message -->
<a href='message.php?msg=".$dataSet->msg();."'>
<div class='pull-left'>
<!-- User Image -->
<img src='dist/img/user2-160x160.jpg' class='img-circle' alt='User Image'>
</div>
<!-- Message title and timestamp -->
<h4>
Support Team
<small><i class='fa fa-clock-o'></i>Just now</small>
</h4>
<!-- The message -->
<p>Welcome to your admin panel</p>
</a>
</li>
<!-- end message -->
</ul>
<!-- /.menu -->
</li>";
}
?>
But whenever I run it ,I get this error message:
Parse error: syntax error, unexpected '.' on line 15 in dasboard.php
Here's the line 15 of the dasboard file:
<a href='message.php?msg=".$dataSet->msg();."'>
Pretty sure that this must be related to the wrong concatenating and combining a class method within a html tag while I'm echoing out a statement.
So what's the correct way to do this ?
"$dataSet->msg" is not a function but a property, and you need to call it like this.
<a href='message.php?msg=".$dataSet->msg."'> // but wont work as it is private
or use the method call as in the below line
<a href='message.php?msg=".$dataSet->welcomeMessage()."'>
Ok, let supoose that $dataSet is defined, and it has all required methods. Then you just need to omit the ;:
echo "some text " . $dataSet->msg() . " more text";
Related
This question already has answers here:
PHP sessions that have already been started [duplicate]
(11 answers)
Closed 5 years ago.
I am trying to developing a ecommerce website. But My problem is in product purchase confirmation page.It show this type of message: (Notice: A session had already been started - ignoring session_start() in C:\xampp\htdocs\online shop\dataAccessLayer\dalSession.php on line 6) I need to some suggestion how can I solve this issue.
Here is my code.
This is my dalSession.php page:
<?php
class Session
{
public static function Start(){
session_start(); //line 6
}
public static function Set($key , $value){
$_SESSION[$key] = $value;
}
public static function Get($key){
if (isset($_SESSION[$key])) {
return $_SESSION[$key];
}
else{
return false;
}
}
public static function Stop(){
session_destroy();
header("Location:user/login.php");
}
public static function StopA(){
session_destroy();
header("Location:../user/login.php");
}
public static function StopB(){
session_destroy();
header("Location:login.php");
}
public static function Check()
{
self::Start();
//echo $this->user_id;;
if (self::Get("Mlogin")==false)
{
self::Stop();
header("Location:login.php");
}
}
public static function CheckA(){
self::Start();
if (self::Get("Alogin")==false) {
self::StopA();
header("Location:../user/login.php");
}
}
public static function CheckAll()
{
if (self::Get("Mlogin")==false && self::Get("Alogin")==false)
{
return false;
}
else
{
return true;
}
}
public static function CheckUserLogin()
{self::Start();
if (self::Get("Mlogin")==false)
{
return false;
}
else
{
return true;
}
}
public static function Auto(){
self::Start();
if(self::Get("Mlogin")==false){
echo "<a style=\"color:white;\" href='user/login.php'>Login</a>";
} else {
echo "<a style=\"color:red;\" href='?action=logout'>Logout</a>";
if(isset($_GET['action']) && ($_GET['action']== "logout")){
self::Stop();
}
}
}
public static function AutoA(){
self::Start();
if(self::Get("Mlogin")==false){
echo "<a style=\"color:white;\" href='login.php'>Login</a>";
} else {
echo "<a style=\"color:red;\" href='?action=logout'>Logout</a>";
if(isset($_GET['action']) && ($_GET['action']== "logout")){
self::StopB();
}
}
}
}
?>
This is My confirm.php code:
<?php
require_once("/../dataAccessLayer/dalSession.php");
require_once("/../dataAccessLayer/dalLogin.php");
session::check();
?>
<?php
if(isset($_GET['action']) && ($_GET['action']== "logout")){
Session::Stop();
}
?>
<!doctype html>
<html lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Success Notification Boxes</title>
<link rel="stylesheet" type="text/css" media="all" href="style.css">
<script type="text/javascript" src="https://ajax.googleapis.com /ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<div id="w">
<div id="content">
<!-- Icons source http://dribbble.com/shots/913555-Flat-Web-Elements -->
<div class="notify successbox">
<h1>Success!</h1>
<span class="alerticon"><img src="" alt="checkmark" /></span>
<p>Thanks For Your Purchase.</p>
<p>Your Purchase Will Deliver Within 72 Hours.</p>
<p>Any Question? Please Call: <b>+8801721557233</b></p>
<p></p>
</div>
<h1><a class="navbar-brand" href="../index.php">Home</a></h1>
</div><!-- #end #content -->
</div><!-- #end #w -->
</body>
</html>
You can modify your function Start. Replace:
public static function Start(){
session_start();
}
By:
public static function Start(){
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
}
It will fix your problem (the notice will not be displayed anymore). The idea is: if your session is already started, don't start it again.
Then if it does not work, remove ALL the space in your confirm.php file before the first <?php (it seems you have two spaces before the first <?php but I'm not sure it's because you copy/pasted it).
Remove spaces before
That characters are starting a session. and may be its better to use some template engine to generate html? Smarty, twig or simple php templates.
I am working on a project where I need to pass a variable from the View to a Controller's method where I'll be using that variable's value. I have tried the following.
View
...
$user = 3;
...
<ul class="nav navbar-nav navbar-right">
<li>
<a href="<?php echo base_url() ?>index.php/studentDashboardController/index?user=$user">
My Dashboard
</a>
</li>
...
studentDashboardController (Method 1)
public function index()
{
...
if ( isset($_GET['user']) ) {
$user = $_GET['user'];
echo '<script type="text/javascript">alert("User taken from GET: ' . $user . '")</script>';
}
...
Output for Method 1
studentDashboardController (Method 2)
public function index()
{
...
if($this->input->get())
{
$user = $this->input->get('user');
echo '<script type="text/javascript">alert("Uid taken from Method 2 ' . $user . '")</script>';
}
...
Output for Method 2
Any suggestions on how to get the value of this passed variable will be highly appreciated.
You were missing the PHP notation while printing the $user variable. Update the below line in the View
<a href="<?php echo base_url() ?>index.php/studentDashboardController/index?user=<?php echo $user; ?>">
I want to get some advice of using PHP OOP.
For example I'm createing some PHP/MySQL code using OOP and I need to output the code, how to do it?
I might have been bit confusing, I will show you all the code and you tell me if I do it right:
index.php
<?php
require_once CLASSES."Pages.Class.php";
$obj = new Pages;
if (isset($_GET['page'])) {
$obj->setPage($_GET['page']);
echo $obj->getPage();
} else {
echo $obj->getFrontPage();
}
?>
Pages.Class.php - one function from it
public function getDatabasePage() {
global $conn;
$sql = "SELECT * FROM pages WHERE page_seo_title = '$this->pageId' LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="well">
<div class="page-head">
<?php echo $row["page_title"]; ?>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="well">
<div class="page-content">
<?php echo $row["page_content"]; ?>
</div>
</div>
</div>
<?php
}
} else {
echo "0 results";
}
}
So basiclly the question is: Where do I need to put HTML code?
In the index.php file or in the pages.class.php file?
How to do this right? Maybe I can get some links with documentation about this from you :) ..
Thanks.
btw - code is an example, not fully done. Just as example - I know it's not right as it is :D
In your Pages class, create a separate method that outputs HTML and does only that; the DB-related logic should be in another method or (preferably) in a totally separate class.
class Pages {
public function __construct(){
//Object initialization logic comes here
}
public function getOutputHTML($resultOfQuery = ''){
return "<div> Your html </div>";
}
}
Other class file only for DB access , just pass parameter and get result as Database result.
class DB {
public function insertMethod($table,$fields,$where){
//Your logic
}
public function updateMethod($table,$fields,$where){
//Your logic
}
public function deleteMethod($table,$fields,$where){
//Your logic
}
public function selectMethod($table,$fields,$where){
//Your logic
}
}
Instantiate DB class like $dbObj = new DB();
Then you can access DB methods like $resultQuery = $dbObj.selectMethod('users');
So you can pass the $resultQuery result to other method like : $objPages = new Pages();$objPages.getoutputHTML($resultOfQuery); . It separates the Database and HTML logic/functionality.
I recommand you to use OOP with MVC so there will be templating system like Smarty,Blade etc.
I`m trying to find a way to show the image and title of the last 3 clicked image links.
Here is the construction of the links:
<a href="?link=test1">
<img src="http://www.mijncambodja.nl/?link=cambodja-phnom-penh" id="img-phnom-penh" title="phnom penh">
<div class="disp-div">phnom penh</div>
</a>
<a href="?link=test2">
<img src="http://www.mijncambodja.nl/?link=cambodja-sihanoukville" id="img-sihanoukville" title="sihanoukville">
<div class="disp-div">sihanoukville</div>
</a>
<a href="?link=test3">
<img src="http://www.mijncambodja.nl/?link=cambodja-siem-reap" id="img-siem-reap" title="siem reap">
<div class="disp-div">siem reap</div>
</a>
<a href="?link=test4">
<img src="http://www.mijncambodja.nl/?link=cambodja-kampot" id="img-kampot" title="kampot">
<div class="disp-div">kampot</div>
</a>
<a href="?link=test5">
<img src="http://www.mijncambodja.nl/?link=cambodja-kep" id="img-kep" title="kep">
<div class="disp-div">kep</div>
</a>
So i would like to see the last 3 clicked images and titles in a session stored. Everytime a new image link is clicked, the oldest of the 3 links is destroyed.
Can someone please help me out here? If possible in jsfiddle
Thanks.
Every click should pass through PHP script.
PHP script should implement queue.
So for example (not tested):
class Image {
protected $name;
protected $title;
public function __construct($name, $title) {
$this->name = $name;
$this->title = $title;
}
public function getName() {
return $this->name;
}
public function getTitle() {
return $this->title;
}
}
class ImageQueue {
protected static $queue = array();
public static function addImage($title, $name) {
$image = new Image($name, $title);
array_shift(self::$queue);
array_push($image);
}
public static function toArray() {
$array = array();
foreach(self::$queue as $image) {
$array[] = array(
'name' => $image->getName(),
'title' => $image->getTitle()
);
}
return $array;
}
public static function saveToSession() {
$_SESSION['mage_queue'] = self::toArray();
}
}
Place this code at the top of your page before <!DOCTYPE html>.
$click_id=filter_input(INPUT_GET, 'link', FILTER_SANITIZE_STRING); // Get the image ID sent by the AJAX.
if(!isset($_SESSION['clicks'])) // If the SESSION is not created already, create it.
{
$_SESSION['clicks']=array();
}
array_unshift($_SESSION['clicks'], $click_id); // Prepend the last click ID to the SESSION array.
if(sizeof($_SESSION['clicks'])>3)
{
array_pop($_SESSION['clicks']); // If the array size is greater than 3, delete the last index, that is the oldest value.
}
So you can get the last three clicks by
$_SESSION['clicks'][0], // Most recent click
$_SESSION['clicks'][1], // Second recent click
$_SESSION['clicks'][2], // Oldest click
This is very simple to understand
Image Class
<?php
class Image extends Zend_Db_Table_Abstract {
protected $_name = 'images';
public function getList() {
return $this->fetchAll();
}
}?>
My PHP Code
<?php
require 'config.php';
$imgTable = new Image(); // Create Object
$imgList = $imgTable->getList(); // fetch Data
$template = new Template('portfolio'); // Initialize Template and tell which template to pick
$template->imgList = $imgList; // set template variable
$template->render(); // Generate Template output
?>
I can access template variable inside template using $this
Below code is from inside the template
$xback = 0;
foreach ($this->imgList as $images) {
echo 'imageArray[' . $xback . '] = "' . $images['sef'] . '";';
$xback++;
}
?>
.......
<?php
foreach ($this->imgList as $images) {
?>
<div class="portfolio_item">
<img src="<?php echo PATH_WEB . $images['image_thumb'] ?>" height="146" width="209" />
<div class="title"><?php echo $images['image_title'] ?></div>
<div class="right link">
View Details
</div>
</div>
<?php
}
?>
Above code is working fine, but below some few lines, I have to iterate over the same data again dont output any thing. If I comment the first one, 2nd starts working.
First one is to create the JS array and is in head section,
Second part is in HTML to display images
I hope its a pointer issue, I may have to set the loop current item to start, but I am not understanding it right now .... reset($this->imgList) didnt worked
please help
I think it has something to do with the fetchAll call, try this:
<?php
class Image extends Zend_Db_Table_Abstract {
protected $_name = 'images';
protected $images;
public function getList() {
// Employ lazy loading pattern to load images if they aren't set yet
if (!isset($this->$images)) {
$this->images = $this->fetchAll();
}
return $this->images;
}
}?>