I have the following code that change Title on the page:
class.php
<?php
class Title_And_Page
{
public $pagekey;
public $title;
public $page;
private $pages = array(
'start_page' => array("Startsida", "start_page.php"),
'products' => array("Produkter", "products.php"),
'max_ot' => array("Max-OT", "max_ot.php"),
'blog' => array("Blogg", "blog.php"),
'tools' => array("Verktyg", "tools.php"),
'about_us' => array("Om oss", "about_us.php"));
public function __construct($pagekey)
{
$this->pagekey = $pagekey;
}
public function setTitle()
{
if(array_key_exists($this->pagekey, $this->pages))
{
$this->title = $this->pages[$this->pagekey][0]; //Returns the value in title, that it gets when the constructs is run
return $this->title;
}
}
public function includePage()
{
if(array_key_exists($this->pagekey, $this->pages))
{
$this->page = $this->pages[$this->pagekey][1]; //Returns the value in page, that will be included
return $this->page;
}
}
}
?>
Here is my some code from my index.php
if(isset($_GET['page']))
{
$page = $_GET['page'];
}
$page_title = new Title_And_Page($page);
<title><?= $page_title->setTitle(); ?></title>
<li id="info"><a href="?page=products" class='clickme'>Produkter</a></li>
<li id="info">MAX-OT</li>
<li id="info">Blogg</li>
<li id="info">Verktyg</li>
<li id="info">Om oss</li>
This works. However, I have articles in the blog page that contains "Read more"-links. When I click on a "Read more"-link, the URL changes to this: index.php?page=blog&readmore_from_firstpage=1&article_header=Vilken kolhydrat är bäst att äta efter träningen?
How can I change the title of the page, to the value in $_GET['article_header'] as you can see above?
Just extend your GET checks:
if(isset($_GET['article_header']))
{
$page = $_GET['article_header'];
}
elseif(isset($_GET['page']))
{
$page = $_GET['page'];
}
But since you're checking in your class whether that page is whitelisted, you'd need to either add another variable to force avoiding such check or simply just to print out the title if article_header is present.
Here's an example of the latter:
$avoidClass = false;
if(isset($_GET['article_header']))
{
$page = $_GET['article_header'];
$avoidClass = true;
}
elseif(isset($_GET['page']))
{
$page = $_GET['page'];
}
Then in HTML:
<title><?= $avoidClass ? $page : $page_title->setTitle(); ?></title>
Or, probably the simplest way:
if(isset($_GET['article_header']))
{
$page_title = $_GET['article_header'];
}
elseif(isset($_GET['page']))
{
$page_title = new Title_And_Page($_GET['page'])->setTitle();
}
else
{
$page_title = 'Default title here';
}
HTML
<title><?= $page_title ?></title>
Related
I am translating this webpage however I the translation will only work when I add the parameter after .php (e.g. http://localhost/fr/about.php?lang=fr_FR)
How can i make it work like (e.g. http://localhost/fr/about.php/?lang=fr_FR)
<?php
/*
* Template Name: Test */
$url = $_SERVER["REQUEST_URI"];
$locale_lang = "en_EN";
if (substr($url,0,3) == "/fr/") { $locale_lang = "fr_FR"; }
if (substr($url,0,3) == "/en/") { $locale_lang = "en_US"; }
$lang = substr($locale_lang,0,2);
require_once("languages/lib/streams.php");
require_once("languages/lib/gettext.php");
$locale_file = new FileReader("languages/lib/$locale_lang/fr_FR.mo");
$locale_fetch = new gettext_reader($locale_file);
function _loc($text) {
global $locale_fetch;
return $locale_fetch->translate($text);
}
?>
<title><?php echo ("Title"); ?></title>
<h1><?php echo _loc("English Version"); ?></h1>
I am listing the registered employee from the db and showing 10 employee per page. I'm using codeigniter pagination for the purpose.
If I deleted an employee in page 2, after deleting I need to go to page 2 showing rest of the employees in page 2.
View:
<a href="<?php echo $path;?>welcome/delete_employee/employeeid/<?php echo $row->empID;?>/pageNum/<?php echo $currentPage; ?>" onClick="return delAlert()";><img src="<?php echo $path;?>/img/delete.png" height="30px" width="30px" /></a>
Controller:
public function delete_employee()
{
$session_id = $this->session->userdata('logged_in');
if($session_id) {
$array = $this->uri->uri_to_assoc(3);
$count=$array['pageNum']-1;
$i=$count*10;
$this->load->model('welcomemodel','m',true);
$this->m->deleteemployee($array['employeeid']);
$config = array();
$config["base_url"] = base_url() . "welcome/employee";
$config["total_rows"] =$this->m->employee_count();
$config["per_page"] = 10;
$config["uri_segment"] = $array['pageNum'];
$data['showData'] = $this->m->getEmployee($config["per_page"], $i);
$this->pagination->initialize($config);
$data["links"] = $this->pagination->create_links();
$data["currentPage"] =$array['pageNum'];
$this->load->view('header');
$this->load->view('employee',$data);
} else {
$this->load->view('session_expired');
}
}
Model:
public function deleteemployee($employeeid)
{
$this->db->where('empID',$employeeid);
$this->db->delete('employee');
return $this->db->affected_rows();
}
public function getEmployee($limit, $start)
{
$this->db->limit($limit, $start);
$this->db->select()
->from('employee')
->order_by('emp_fname');
$this->db->join('service', 'employee.serviceID = service.serviceID','left');
$query=$this->db->get();
return $query;
}
public function employee_count()
{
return $this->db->count_all("employee");
}
But now the pagination link shows first page's link,even though contents of second page is displayed....
model method
public function employee_count()
{
$result = $this->db->get('employee');
if ($result->num_rows() > 0) {
//employee record available
return true;
}
else
{
// employee record not available
return false;
}
}
View
<?php
$getData = $this->[your model name]->getEmployee($limit, $start);
?>
<?php if ($getData): ?>
//Your Pagination Part Here.....
<?php else: ?>
// else Nothing
<?php endif ?>
I'm new to PHP and I can't understand why is my code not working as intended. I have two functions on a class controller, which one of them is supposed to be called when the user is logged out and one of them when the user is logged in. But what happens is the function is never called and I have no idea why. Only the function when the user is logged out is being called.
//index.php
<!DOCTYPE html>
<?php
require_once 'Control/Controller.php';
session_start();
$_SESSION['logged'] = FALSE;
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="mid">
<div id="left">
<?php
if (User::isLogged())
{
$menu = Controller::initMenuUser();
}
else
{
$menu = Controller::initMenuGuest();
}
if (!empty($menu))
{
foreach ($menu as $item)
{
echo '<li>' . $item['text'] . '';
}
}
</div>
</div>
</body>
</html>
What should be happening is that on the div with id="left", there should be a different set of links and names depending either the user is logged in or not.
//Controller.php
...
public static function initMenuGuest()
{
$menu = array(
array(
'url' => 'index.php?page=Login',
'text' => 'Login'
),
array(
'url' => 'index.php?page=Register',
'text' => 'Register'
)
);
return $menu;
}
public static function initMenuUser()
{
$menu = array(
array(
'url' => 'index.php?page=Profile',
'text' => 'Profile'
),
array(
'url' => 'index.php?page=Cart',
'text' => 'Cart'
),
array(
'url' => 'index.php?logout=true',
'text' => 'Logout'
),
array(
'url' => 'index.php?page=Administration',
'text' => 'Administration'
)
);
return($menu);
}
Here is the method of the User class responsible to changing the $_SESSION['logged'] to TRUE. I'm calling this function on the Controller.php. if (isset($_POST['login'])) then call the function. $_POST['login'] is a form on the index.php
//User.php
public static function login()
{
$login = $_POST['loginl'];
$password = $_POST['passwordl'];
$u = new User($login);
$res = $u ->check();
$udb = $res->fetch_object();
if (password_verify($password, $udb->password))
{
$_SESSION['user'] = $udb->login;
$_SESSION['id'] = $udb->id;
$_SESSION['logged'] = TRUE;
$msg = 'Welcome '.$_SESSION['user'].' '.$_SESSION['logged'];
}
else
{
$msg = "Wrong Login information.";
}
$res->free();
return $msg;
}
public static function isLogged()
{
if ($_SESSION['logged'] == TRUE)
{
$res = TRUE;
}
else
{
$res = FALSE;
}
return $res;
}
You are setting $_SESSION['logged'] = FALSE in the third line of index.php. Regardless of the login status of a User, this line sets the value of logged to false, thereby failing the User::isLoggedIn() condition that follows.
Remove that line.
I am a beginner and wrote a web page based on the tutorial JREAM (MVC).
I've encountered a problem in displaying articles.
How can I send a variable $from (index.php) from the controller to the model?
Of course, I can connect to the database in the index.php but whether that is good?
index.php
$limit = LIMIT; // article per page
$article = $this->Ile[0]['num']; // number of articles
if (isset($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 1;
}
$allpage = round($article/$limit);
$from = $limit * ($page - 1);
// $from send value to model and model return array with article
foreach($this->Article as $key => $value)
{
echo '<div style="float:left;width:660px;"><hr/><br/><p><span class="strong" style="color:#CC0000;">';
echo $value['title'];
echo '</span></p><br/><p>';
echo $value['content'];
echo '</p><div style="height:130px;">';
echo $value['photo'];
echo '</div></div>';
}
echo '<hr/>';
echo '<div style="width: 660px; position: relative; pointer-events: none; text-align: center; top: 14px;">'.$page.' z '.$allpage.'</div>';
if ($page == 1 && $page < $allpage)
{
echo 'STARSZE >>';
}
elseif ($page >= $allpage)
{
echo '<< NOWSZE';
} else {
echo '<< NOWSZE';
echo 'STARSZE >>';
}
?>
news.php
class News extends Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->view->title = 'News';
$this->view->Ile = $this->model->Ile();
$this->view->Article = $this->model->Article();
// I can put some value in ...->Article(0); and it works, but how connect in index.php
$this->view->render('header');
$this->view->render('news/index');
$this->view->render('footer');
}
}
news_model.php
<?php
class News_Model extends Model
{
public function __construct()
{
parent::__construct();
}
public function Ile()
{
return $this->db->select('SELECT COUNT(*) as num FROM `articles`');
}
public function Article($from)
{
return $this->db->select('SELECT * FROM `articles` ORDER BY `newsid` DESC LIMIT '.$from.', '.LIMIT.'');
}
}
Regards,
Thomas
You should do this in your controller not in view :
Controller :
$limit = LIMIT; // article per page
$article = $this->view->Ile[0]['num']; // number of articles
if (isset($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 1;
}
$allpage = round($article/$limit);
$from = $limit * ($page - 1);
$this->view->Article = $this->model->Article($form); // This is how you can pass $form to model
And then you can assign This variable to your view like :
$this->view->page = $page
$this->view->allpage = $allpage
Now in view you can use $this->page instead of $page, same for $allpage
Hope it helps
I have made a template system but the {var} doesnt output the worth.
It just output {var}.
Here is my template class:
<?php
class Template {
public $assignedValues = array();
public $tpl;
function __construct($_path = '')
{
if(!empty($_path))
{
if(file_exists($_path))
{
$this->tpl = file_get_contents($_path);
}
else
{
echo 'Error: No template found. (code 25)';
}
}
}
function assign($_searchString, $_replaceString)
{
if(!empty($_searchString))
{
$this->assignedValues[strtoupper($_searchString)] = $_replaceString;
}
}
function show()
{
if(count($this->assignedValues) > 0)
{
foreach ($this->assignedValues as $key => $value)
{
$this->tpl = str_replace('{'.$key.'}', $value, $this->tpl);
}
}
echo $this->tpl;
}
}
?>
And here is what I execute on the index:
<?php
require_once('inc/classes/class.template.php');
define('PATH', 'tpl');
//new object
$template = new Template(PATH.'/test.tpl.html');
//assign values
$template->assign('title', 'Yupa');
$template->assign('about', 'Hello!');
//show the page
$template->show();
?>
I really need some help, if you can help I'd would be very grateful.
Instead of line:
$this->assignedValues[strtoupper($_searchString)] = $_replaceString;
You should have:
$this->assignedValues[$_searchString] = $_replaceString;
and it will work.
Of course I assume that inside your template file you have content:
{title} {about}
You should change
$this->assignedValues[strtoupper($_searchString)] = $_replaceString;
to this:
$this->assignedValues["{".$_searchString . "}"] = $_replaceString ;
this will only replace your keywords with values.