Currently I am trying to load file by a data array in codeigniter. But the is file not picking it up.
I can not echo my logo to the view page
What is the best way to get it like below so I can get images as an array.
public function index() {
if (is_file(dirname(FCPATH) . '/image/data/logo.png')) {
$data['logo'] = dirname(FCPATH) . '/image/' . 'data/logo.png';
} else {
$data['logo'] = '';
}
$this->load->view('theme/default/template/common/header', $data);
}
Tried all ready
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Header extends MX_Controller {
public function index() {
//$this->load->library('document');
if (is_file(FCPATH) . 'image\data\logo.png') {
$data['logo'] = FCPATH . 'image\data\logo.png';
} else {
$data['logo'] = '';
}
$this->load->view('theme/default/template/common/header', $data);
}
}
View Header File
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo $title;?></title>
<link rel="stylesheet" href="catalog/views/theme/default/stylesheet/stylesheet.css">
<link rel="stylesheet" href="catalog/views/theme/default/stylesheet/bootstrap.css">
<link href="//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
<div class="thumbnail">
<img src="<?php echo $logo;?>" class="img-responsive">
</div>
</div>
</div>
</div>
try to change FCPATH to base_url() in your header controller file... it works!!
if (is_file(base_url()) . 'image/data/logo.png') {
$data['logo'] = base_url() . 'image/data/logo.png';
} else {
$data['logo'] = '';
}
public function index()
{
if (is_file(FCPATH) . 'image\data\logo.png')
{
$data['logo'] = FCPATH . 'image\data\logo.png';
}
else
{
$data['logo'] = '';
}
$this->load->view('theme/default/template/common/header', $data);
}
put this.. in your header file put this "<img src="<?php echo $logo; ?>";
Related
i am kinda new to PHP and not sure if this has been answered before i cant really find anything. what im asking for help with is with a google sign in if the email address is not in the database i would like it to redirect the user back to the login page, i have attached the code for the login below if anyone can help it will be highly appreciated thank you.
//session_start();
//index.php
include('classDev.php');
//Include Configuration File
include('config.php');
$login_button = '';
if(isset($_GET["code"]))
{
$token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]);
if(!isset($token['error']))
{
$google_client->setAccessToken($token['access_token']);
$_SESSION['access_token'] = $token['access_token'];
$google_service = new Google_Service_Oauth2($google_client);
$gData = $google_service->userinfo->get();
if(!empty($gData['given_name']))
{
$_SESSION['user_first_name'] = $gData['given_name'];
}
if(!empty($gData['family_name']))
{
$_SESSION['user_last_name'] = $gData['family_name'];
}
//if (empty(($data->select_where("user",$where_condition)))) {
// header("location: index.php?status=failure");
//}
if(!empty($gData['email']))
{
$where_condition = array("email" => $gData['email']);
if (($data->select_where("user",$where_condition))) {
$_SESSION['user_email_address'] = $gData['email'];
}
}
if (empty($_SESSION['email'])) {
//echo "please enter an email address that is valid.";
//header('location: index.php?status=failure');
}
if(!empty($gData['gender']))
{
$_SESSION['user_gender'] = $gData['gender'];
}
if(!empty($gData['picture']))
{
$_SESSION['user_image'] = $gData['picture'];
}
}
}
if(!isset($_SESSION['access_token']))
{
$login_button = 'Login With Google';
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Login using Google Account</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<br />
<h2 align="center">PHP Login using Google Account</h2>
<br />
<div class="panel panel-default">
<?php
if($login_button == '')
{
echo '<div class="panel-heading">Welcome User</div><div class="panel-body">';
echo '<img src="'.$_SESSION["user_image"].'" class="img-responsive img-circle img-thumbnail" />';
echo '<h3><b>Name :</b> '.$_SESSION['user_first_name'].' '.$_SESSION['user_last_name'].'</h3>';
echo '<h3><b>Email :</b> '.$_SESSION['user_email_address'].'</h3>';
echo '<h3><a href="logout.php">Logout</h3></div>';
//header('location: home.php');
include('home.php');
}
else
{
echo '<div align="center">'.$login_button . '</div>';
}
?>
</div>
</div>
</body>
</html>```
I am working on building a site, but right now it has several images that I don't have actual images for yet. As this site has thousands of images or places where images should be, I don't want to have to manually change each of them and then change them again when I find the correct image. Is there a way to create a function that will look for the missing images and replace them with a specified image until the correct image is found?
Update: Since I am still a bit confused as to where to even place this function, I am going to add the code for one of the pages that I need this for then maybe someone can help me figure out how to place it.
Here is the code for one of the pages:
<?php
require_once('dbconnection.php');
mysqli_select_db($conn, $dbname);
$query_master = "SELECT DISTINCT * FROM `master_list` INNER JOIN types_join ON master_list.join_id=types_join.join_id WHERE `type_id` = 171 ORDER BY `item_id`";
$master = mysqli_query($conn, $query_master) or die(mysqli_error());
$row_master = mysqli_fetch_assoc($master);
$totalrows_master = mysqli_num_rows($master);
?>
<!doctype html>
<html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flower Trees</title>
<link href="/css/styles.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" type="image/png" href="img/favicon.png">
</head>
<body>
<div class="wrapper">
<a id="top"></a>
<?php require_once('header.php'); ?>
<?php require_once('nav.php'); ?>
<div class="category"><h2>Flower Trees</h2></div>
<div class="display">
<?php do { ?>
<ul>
<li><a href="details.php?recordID=<?php echo $row_master['master_id']; ?>"><img class="thumb" src="img/<?php echo $row_master['img']; ?>"/>
<br />
<span class="name"><?php echo $row_master['name']; ?></span></a></li>
</ul>
<?php } while ($row_master = mysqli_fetch_assoc($master)); ?>
<!-- end .display --></div>
<?php
mysqli_free_result($master);
?>
<?php require_once('footer.php'); ?>
<!-- end .wrapper --></div>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
</body>
</html>
Since this is as simple as a foreach loop, and not tons of images scattered across your webpage, you can use something like:
$image = file_exists('img/' . $row_master['img']) ? 'img/' . $row_master['img'] : 'placeholder.png';
Full code:
<?php
require_once('dbconnection.php');
mysqli_select_db($conn, $dbname);
$query_master = "SELECT DISTINCT * FROM `master_list` INNER JOIN types_join ON master_list.join_id=types_join.join_id WHERE `type_id` = 171 ORDER BY `item_id`";
$master = mysqli_query($conn, $query_master) or die(mysqli_error());
$row_master = mysqli_fetch_assoc($master);
$totalrows_master = mysqli_num_rows($master);
?>
<!doctype html>
<html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flower Trees</title>
<link href="/css/styles.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" type="image/png" href="img/favicon.png">
</head>
<body>
<div class="wrapper">
<a id="top"></a>
<?php require_once('header.php'); ?>
<?php require_once('nav.php'); ?>
<div class="category"><h2>Flower Trees</h2></div>
<div class="display">
<?php do {
$image = file_exists('img/' . $row_master['img']) ? 'img/' . $row_master['img'] : 'placeholder.png';
?>
<ul>
<li><a href="details.php?recordID=<?php echo $row_master['master_id']; ?>"><img class="thumb" src="<?php echo $image; ?>"/>
<br />
<span class="name"><?php echo $row_master['name']; ?></span></a></li>
</ul>
<?php } while ($row_master = mysqli_fetch_assoc($master)); ?>
<!-- end .display --></div>
<?php
mysqli_free_result($master);
?>
<?php require_once('footer.php'); ?>
<!-- end .wrapper --></div>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
</body>
</html>
I don't want to have to manually change each of them and then change them again when I find the correct image. Is there a way to create a function that will look for the missing images and replace them with a specified image until the correct image is found?
Such a function might be written as:
function im($imgName) {
$pathToImgs = "images/";
if (file_exists( $pathToImgs . $imgName )) {
echo $pathToImgs . $imgName;
}
else {
echo $pathToImgs . "placeholder.jpg";
}
}
Then in your html:
<img src="<?php im("product1.jpg"); ?>">
<img src="<?php im("product2.jpg"); ?>">
<img src="<?php im("product3.jpg"); ?>">
As a start.
***Edit 1:
Given your code where it says:
<img class="thumb" src="img/<?php echo $row_master['img']; ?>"/>
You might modify it with a conditional that inserts the placeholder image in the event that the target image simply doesn't exist, yet.
<img class="thumb" src="<?php
if (file_exists("img/" . $row_master['img'])) {
echo "img/" . $row_master['img'];
}
else {
echo 'img/placeholder.jpg';
}
?>">
You could reuse this functionality by turning the conditional into a php function, so described as a starter above.
Using jQuery, you can accomplish something easy enough using a global $('img') handler when errors occur. Simply swap them out with your placeholder image afterwards.
$('img').on('error', function() {
const oldSrc = encodeURIComponent($(this).attr('src'));
$(this).attr('src', `https://via.placeholder.com/300/000000/FFFFFF/?text=${oldSrc}`);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="imagethatdoesntexist.jpg"><br />
<img src="anotherimage.jpg">
Here I use placeholder.com (A convenient site for placeholder images such as this), and inject the old image source into the query string, which the site will render in the image itself.
I can access my dashboard without login, and when I submit it in localhost or on one of my other hosting it is working good. When I am trying new host, my login page doesn't work. I don't find any error also.
Any suggestion please?
here my login page code
<?php include '../classes/Adminlogin.php'; ?>
<?php
$al = new Adminlogin();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$adminUser = $_POST['adminUser'];
$adminPass = md5($_POST['adminPass']);
$loginChk = $al->adminLogin($adminUser,$adminPass);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Meta, title, CSS, favicons, etc. -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Admin Login</title>
<!-- Bootstrap -->
<link href="vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Font Awesome -->
<link href="vendors/font-awesome/css/font-awesome.min.css" rel="stylesheet">
<!-- NProgress -->
<link href="vendors/nprogress/nprogress.css" rel="stylesheet">
<!-- Animate.css -->
<link href="../vendors/animate.css/animate.min.css" rel="stylesheet">
<!-- Custom Theme Style -->
<link href="build/css/custom.min.css" rel="stylesheet">
</head>
<body class="login">
<div>
<a class="hiddenanchor" id="signup"></a>
<a class="hiddenanchor" id="signin"></a>
<div class="login_wrapper">
<div class="animate form login_form">
<section class="login_content">
<form action="" method="post">
<h1>Help Educare Login</h1>
<!--Show Message-->
<span style="color: red; font-size: 18px;">
<?php
if (isset($loginChk)) {
echo $loginChk;
}
?>
</span>
<!--Show Message End-->
<div>
<input type="text" class="form-control" placeholder="Username" name="adminUser" />
</div>
<div>
<input type="password" class="form-control" placeholder="Password" name="adminPass" />
</div>
<div>
<input class="btn btn-default submit" type="submit" value="Login" />
</div>
<div class="clearfix"></div>
<div class="separator">
<!-- <p class="change_link">New to site?
Create Account
</p>
<div class="clearfix"></div>
<br /> -->
<div>
<h1><i class="fa fa-paw"></i> Help Educare</h1>
<p>©2018 All Rights Reserved Help Educare.</p>
</div>
</div>
</form>
</section>
</div>
</div>
</div>
</body>
</html>
Here is my admin login class code
<?php
$filepath = realpath(dirname(__FILE__));
include ($filepath.'/../lib/Session.php');
Session::checkLogin();
include_once ($filepath.'/../lib/Database.php');
include_once ($filepath.'/../helpers/Format.php');
?>
<?php
/**
* Adminlogin Class
*/
class Adminlogin {
private $db;
private $fm;
public function __construct(){
$this->db = new Database();
$this->fm = new Format();
}
public function adminLogin($adminUser,$adminPass){
$adminUser = $this->fm->validation($adminUser);
$adminPass = $this->fm->validation($adminPass);
$adminUser = mysqli_real_escape_string($this->db->link, $adminUser);
$adminPass = mysqli_real_escape_string($this->db->link, $adminPass);
if (empty($adminUser) || empty($adminPass)) {
$loginmsg = "Username and Password must not be empty!!";
return $loginmsg;
} else{
$query = "SELECT * FROM tbl_admin WHERE adminUser = '$adminUser' AND adminPass = '$adminPass'";
$result = $this->db->select($query);
if ($result != false) {
$value = $result->fetch_assoc();
Session::set("adminlogin", true);
Session::set("adminId", $value['adminId']);
Session::set("adminUser", $value['adminUser']);
Session::set("adminName", $value['adminName']);
Session::set("role", $value['role']);
header("Location:dashboard.php");
} else{
$loginmsg = "Username and Password not match !!";
return $loginmsg;
}
}
}
}
here my session class code
<?php
/**
* Session Class
**/
class Session{
public static function init(){
if (version_compare(phpversion(), '5.4.0', '<')) {
if (session_id() == '') {
session_start();
}
} else {
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
}
}
public static function set($key, $val){
$_SESSION[$key] = $val;
}
public static function get($key){
if (isset($_SESSION[$key])) {
return $_SESSION[$key];
} else {
return false;
}
}
public static function checkSession(){
self::init();
if (self::get("adminlogin") == false) {
self::destroy();
header("Location:login.php");
}
}
public static function checkLogin(){
self::init();
if (self::get("adminlogin")== true) {
header("Location:dashboard.php");
}
}
public static function destroy(){
session_destroy();
header("Location:login.php");
}
}
?>
here is my site url
In the example above, we are using “dynamically added data”, which you will see below.
controller
public function index()
{
$data['main_menus'] = '';
if(count($this->menu_model->findActive()) > 0){
$data['main_menus'] = $this->link_class->bootstrap_menu($this->menu_model->findActive());
}
$data['header'] = $this->load->view('common/header',$data);
$data['posts'] = $this->home_model->find_active(3);
$data['content'] = $this->load->view('common/content',$data);
$this->load->view('layout',$data);
}
view
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="DBlog : Material Style Personal Blog Theme from Wowbootstrap">
<meta name="author" content="wowbootstrap.com">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,700|Merriweather:400,400italic,700italic" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Roboto:400,100,100italic,500,400italic,700,700italic,900,900italic,300,300italic,500italic" rel="stylesheet" type="text/css">
<title><?php echo $page_title?> - CI Blog</title>
</head>
<body>
<div id="wrapper">
<?php echo $header;?>
<section class="main">
<div class="container">
<div class="row">
<div class="col-md-8">
<?php //echo $content;?>
</div>
<div class="col-md-4">
<?php //echo $right_sidebar;?>
</div>
</div>
</div>
</section>
<?php //echo $footer;?>
</div>
<script>
$.material.init();
</script>
<?php if(!empty($home_page)):?>
<script type="text/javascript">
$(window).scroll(function(){
if ($(this).scrollTop() > 1){
$('.top-navbar').removeClass('navbar-transparent');
}else{
$('.top-navbar').addClass('navbar-transparent');
}
});
</script>
<?php endif;?>
<script type="text/javascript">
$(document).ready(function () {
$('[data-toggle="tooltip"]').tooltip();
});
</script>
</body>
</html>
I am using codeigniter to develop php web site. Now I have the question.
In controller,$this->load->view('index', $data);
I want $data can also share in header.php that included in index.php.
How to do???
If i do templating on codeigniter I do like this
In Layout.php
<!--Header -->
<?php $this->load->view('path/to/header'); ?>
<!--Content -->
<?php $this->load->view($content); ?>
<!--Footer-->
<?php $this->load->view('path/to/footer'); ?>
In controller
$data['content'] = 'path/to/content/page'; # or any dynamic data with $this->model...
$this->load->view('template',$data);
In Your Controller : to load view file like this,
public function index()
{
$data['main_menus'] = '';
if(count($this->menu_model->findActive()) > 0){
$data['main_menus'] = $this->link_class->bootstrap_menu($this->menu_model->findActive());
}
$data['posts'] = $this->home_model->find_active(3);
// To load view files
$this->load->view('common/header',$data); // header view file
$this->load->view('common/content',$data); // body content view file
$this->load->view('layout',$data); // footer view file
}
I have a little problem, I can't get $view in included file. Please, help me, I don't know what I have to do. Tamplates are changing as shoud, because else condition is working good. var_dump($view); results:
Notice: Undefined variable: view in C:\xampp\htdocs\Accessories\adminpanel\tpl\main.php on line 26
NULL
index.php:
<?php
$view = empty($_GET['view']) ? 'indexPage' : $_GET['view'];
switch ($view)
{
}
if($view != "indexPage")
{
include "/tpl/main.php";
}
else
{
include "/tpl/indexPage.php";
}
?>
main.php:
<?php session_start(); ?>
<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Admin Panel</title>
<link rel="stylesheet" href="../css/style.css"/>
</head>
<body>
<div id="wrapper">
<nav>
<ul id="category">
<span>Hello, <?=$_SESSION['username'];?></span>
<li>Add Item | </li>
<li>Exit</li>
</ul>
</nav>
<article>
<?php
if(isset($view))
{
include($_SERVER['DOCUMENT_ROOT'] . '/Accessories/adminpanel/tpl/pages/'. $view . '.php');
}
else
{
include($_SERVER['DOCUMENT_ROOT'] . '/Accessories/adminpanel/tpl/pages/defaultPanel.php');
}
// var_dump($view);
?>
</article>
</div>
</body>
</html>
UPDATE:
I used same thing, when wrote another website It Worked:
index.php:
<?php
include 'catalog.php';
$view = empty($_GET['view']) ? 'generalPage' : $_GET['view'];
$cat_name = empty($_GET['cat_name']) ? '' : $_GET['cat_name'];
switch ($view)
{
case('generalPage'):
$rand_goods = get_random_good();
break;
case('categoryPage'):
if ($ids) $products = get_products($ids);
else $products = null;
break;
case('productPage'):
$product = $get_one_product;
break;
}
switch ($cat_name)
{
case('about'):
$about = get_about();
break;
}
include($_SERVER['DOCUMENT_ROOT'] . '/Accessories/tpl/main.php');
?>
main.php:
<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Accessories</title>
<link rel="stylesheet" href="css/style.css"/>
</head>
<body>
<div id="wrapper">
<header></header>
<div id="breadCrumbs">
<?= $breadcrumbs; ?>
</div>
<nav>
<ul class="category">
<li>General</li>
<?php echo $categories_menu; ?>
</nav>
<article>
<?php
if ($cat_name == '')
include($_SERVER['DOCUMENT_ROOT'] . '/Accessories/tpl/pages/' . $view . '.php');
else
{
include($_SERVER['DOCUMENT_ROOT'] . '/Accessories/tpl/pages/' . $cat_name . 'Page.php');
}
?>
</article>
<div class="clear"></div>
<footer>
<p id="copy">© 2014</p>
</footer>
</div>
<script type="text/javascript" src="js/jquery-2.1.0.min.js"></script>
<script type="text/javascript" src="js/jquery.accordion.js"></script>
<script type="text/javascript" src="js/jquery.cookie.js"></script>
<script>
$(document).ready(function () {
$(".category").dcAccordion();
});
</script>
</body>
</html>
I think the problem is the scope of your variable $view.
Try this:
index.php
<?php
$view = empty($_GET['view']) ? 'indexPage' : $_GET['view'];
if($view != "indexPage")
{
include "/tpl/main.php";
}
else
{
include "/tpl/indexPage.php";
}
?>
main.php
<?php session_start(); ?>
<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Admin Panel</title>
<link rel="stylesheet" href="../css/style.css"/>
</head>
<body>
<div id="wrapper">
<nav>
<ul id="category">
<span>Hello, <?=$_SESSION['username'];?></span>
<li>Add Item | </li>
<li>Exit</li>
</ul>
</nav>
<article>
<?php
$view = $_GET['view'];
if(isset($view)){
include($_SERVER['DOCUMENT_ROOT'] . '/Accessories/adminpanel/tpl/pages/'. $view . '.php');
}else{
include($_SERVER['DOCUMENT_ROOT'] . '/Accessories/adminpanel/tpl/pages/defaultPanel.php');
}
echo var_dump($view);
?>
</article>
</div>
</body>
</html>