Redirecting to another page. How do I display a countdown? - codeigniter

I'm just wondering if the following is possible:
I don't know anything about JQuery and I don't know how to make my own javascripts yet.
When a user logs into my website, they'll get a page that says "login completed".
Under that sentence, I would like to get a sentence that counts down the seconds until they are redirected. Is there a simple way to do this?
I already have a countdown function:
When the function is first called, the view 'view_login_success' is loaded and the $data[] array is passed to it. $data['sec'] holds the seconds remaining that should be printed on the screen.
When the do-while loop is over, the user should be redirected to the homepage.
What happends is, the 'view_login_success'-view isn't loaded at all and after 5 seconds, the user is redirected to the homepage.
function timerIn() {
$now = time();
$this->load->view('view_login_success', $this->data);
do {
if (time() - $now != 0) {
$this->data['sec'] = $this->data['sec'] - 1;
$this->load->view('view_login_success', $this->data);
$now = time();
}
} while ($this->data['sec'] != 1);
$this->data['sec'] = 5;
redirect('user/start');
}
Here is the view:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login geslaagd</title>
<?php $this->load->view('templates/header2'); ?>
</head>
<body>
<h2 style="text-align: center;"><?php echo strtoupper(substr($this->session->userdata('gebruikersnaam'),0,1));
echo substr($this->session->userdata('gebruikersnaam'),1); ?>, U bent ingelogd!</h2>
<p>U word doorverwezen: <?php echo $sec; ?> seconden...</p>
</body>
<?php $this->load->view('templates/footer'); ?>
If there's a way to do this in JQuery of Javascript, then please explain to me what I should do to get it to work.
Thanks in advance! :)

That's not how php works.
If you want the page to redirect after 5 secs you have to do so in the JS code.
You redirect the user without sending the view to the client.
Right now you are only waiting 5 seconds to redirect. So:
Show the view
The JS will have the timer and then redirects.
The javascript code would be something like this:
setTimeout(function(){
window.location.href = 'theURLyouwant';
},5000)
OR you could use the ol' meta tag
<meta http-equiv="refresh" content="5; URL=theURLyouwant">

Use code-igniter to solve this problem. Use this reference class output. Output class will be loaded automatically. You don't need to load this. Use this function to set header.
$this->output->set_header('refresh:time-to-redirect; url='.site_url("your-url"));
For more detail about this class.

Related

Delay and redirect

Is there anyway to display set flash data message first and then redirect to new page after a 5 secs delay in controller.
echo "Message";
sleep(5);
redirect();
Thanks
When you are in your controller method to set the flashdata, send to time ins econds :
controller.php
public function do_something(){
//do somthing
$this->session->set_flashdata('message_id', 'Message');//message rendered
$this->session->set_flashdata('seconds_redirect', 5);//time to be redirected (in seconds)
$this->session->set_flashdata('url_redirect', base_url('controller/method'));//url to be redirected
redirect('folder/temp_view', 'refresh');
}
in your view/folder/temp_view.php
<html>
<head>
<meta http-equiv="refresh" content="<?= $this->session->flashdata('seconds_redurect');?>;url=<?= $this->session->set_flashdata('url_redirect')?>" />
</head>
<body>
<?= $this->session->flashdata('message_id');?>
</body>
</html>
with this you pass the message variables, time in which you want to be redirected to another page and the url to where it is redirected
Since you are loading the page, you need to use javascript to redirect.
Put this code in your html view :
<script>setTimeout(function(){window.location.href='newpage.html'},5000);</script>
I was stuck at the same place:
1.Type this for static url in view file:
<?php
header("Refresh: 5; url=\"http://www.google.com\"");
echo "You will be redirected to google in 5 seconds...";
?>
2.Type this for base_url in view file :
<?php
header('Refresh:5; url= '. base_url().'/create_event');
echo "You will be redirected in 5 seconds...";
?>
//DON'T Forget to put semi-colon at end.

Site URL CodeIgniter Not Working

I have Controller on CI like this
class Testing extends CI_Controller {
//put your code here
public function xx() {
$this->load->helper('url');
$this->load->view('testing');
}
public function linkURL() {
$this->load->helper('url');
$data['test'] = "testing123";
$this->load->view('xxx_view', $data);
}
}
I'm running on function linkURL and call view xxx_view, the code on view like this
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$segment = array('Testing', 'xx');
?>
Link
</body>
view call a href and using helper site_url to call Controller Testing and function xx. But the link is not working. I am already capture on firebug and link looks like weird. The Link on href contain *http://::1*. How to solved that link
You can print_r($_SERVER) in your controller and check it. or You can use
$config['base_url'] = 'http://localhost:8081/your-project/'
In my opinion, seem to you sent data test to view xxx_view but not using this variable. try echo $test and I see in url using xxx not xx
You need to autoload URL helper in your config/autoload.php.This time you are loading URl helper in function. That will not work for your view.

Header not appearing & Footer not correct

I'm following the instructions in the CodeIgniter Tutorial on the Static Page but the Header does not appear although the tab is labelled "CodeIgniter Tutorial" and the content of the Footer appears on the same line as the content of the Page.
This is the content of my Pages.php - application/controllers/Pages.php
<?php
class Pages extends CI_Controller
{
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
This is the content of my header.php - application/views/templates/header.php
<html>
<head>
<title>CodeIgniter Tutorial</title>
</head>
<body>
<h1><?php echo $title ?></h1>
This is the content of my footer.php - application/views/templates/footer.php
<em>© 2014</em>
</body>
</html>
This is the content of my routes.php - application/config/routes.php
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
This is one URL http://localhost/MyProject/index.php/pages/view
And this is the result;
Home
Hello World! © 2014
And this is the other URL http://localhost/MyProject/index.php/pages/view/about
And this is the result;
About
Hello World! © 2014
I've checked every page numerous times, tried re-booting, and tried another browser, and tried CodeIgniter Forum, all to no avail.
Can somebody explain where I'm going wrong?
Edited
In the header.php file I've changed
<h1><?php echo $title ?></h1>
to
<h3><?php echo $title ?></h3>
which makes the word "Home" decreases in size. The only file I can find the word "home" is Pages.php
public function view($page = 'home')
If I change that word to "CodeIgniter Tutorial" I get a 404 error.
The IE tab is labelled "CodeIgniter Tutorial"
I'm unsure if my expectations are correct, but this is the result that I'm expecting (but with "© 2014" at the bottom of the page);
CodeIgniter Tutorial
Hello World!
© 2014
It seems like the files views/pages/about.php and views/pages/home.php are empty. Try writing something in them, for example:
views/pages/about.php
<i>This is the About page</i>
If the file exist, but is empty, you should get the result you're seeing, because this line will not render anything:
$this->load->view('pages/'.$page, $data);
If the files don't exist, you should get some 404 output - so that's a bit weird. It could happen if the file application/errors/error_404.php is empty.
I tested your project on my localhost and your pages controller is fine.
It just seems to be you need to either use p tags or div etc and then
use css style sheets etc.
But instead of
<em>© 2014</em>
</body>
</html>
Try
<p>© 2014</p>
</body>
</html>
Proof
I created a file at application/views/pages/CodeIgniter Tutorial.php
and changed
public function view($page = 'home')
to
public function view($page = 'CodeIgniter Tutorial')
in Pages.php
and changed
<em>© 2014</em>
to
<p><br><br><em>© 2014</em></br></br></p>
in footer.php
I can achieve close to my expectations and now realise my expectations were far too much for a simple piece of coding.

Codeigniter - Query inside foreach

So i'm using latest Codeigniter and my problem is that i have this situation where i should use query inside foreach in controller. I have view that shows all production orders in one list, and i want all user made comments and notes underneath them from other table. Quite general situation in coding, but how i should do this with Codeigniter, cause you should keep your SQL clauses in Models.
Before i just fork raw SQL inside controller, i wanted to ask what is more gentle and proper way to achieve this. Thanks!
Controller
<?php
public function show()
{
$this->load->database();
$this->load->model('report_model');
$this->load->helper('url');
$data['productionOrders'] = $this->report_model->getAllProductionOrders();
$this->load->view('all_reports', $data);
}
?>
Model
<?php
function getAllProductionOrders()
{
$this->db->select(*);
$this->db->from('dbo.QualityControl_ProductionOrders');
$query = $this->db->get();
return $query->result();
}
?>
View
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<?php
foreach ($productionOrdersas $row)
{
?>
<div>
ProdNo: <?php echo $row->Prodno; ?>
Descriptions:
Hours:
etc etc etc etc
</div>
[I want here comments and all user made notes]
<?php
}
?>
</body>
</html>
You should make an effort to avoid spinning over a result set with additional queries like that..imagine if you had 500 production orders. That's 501 queries to make a simple report
Instead, use a join & parse your results either by only printing out the header info once or using Underscore.php to make a nested result, as demonstrated here:
http://codebyjeff.com/blog/2012/08/no-more-machine-gunning-use-underscore-php
Not sure what you asking but try this -
In controller ,
$data['productionOrders'] = $this->report_model->getAllProductionOrders();
foreach($data['productionOrders']['id'] as $id)
{
$data['comment'][$id] = $this->report_model->getCommantsByOrderId($id);
}

Iframe or Ajax ?

at the moment I've got a simple bit of code to display messages on a site.
the page includes an iframe that self refreshes every 90 seconds, and the iframe displays the contents of a mysql table limited to the most recent (30) posts.
I've been reading up on ajax and it still confuses me, i've never got it working properly, and would like to know, for a small task like this, is it really worth it ?
<head>
<META HTTP-EQUIV="refresh" CONTENT="90; URL=">
</head>
<body>
<?
$newsarray = array();
$sql = "SELECT * FROM `news` ORDER BY `date` DESC LIMIT 30";
$result = mysql_query($sql);
if(mysql_error()) { print mysql_error(); } else {
while($stuff = mysql_fetch_assoc($result))
{ array_push($newsarray, $stuff); }
foreach($newsarray as $newsstory) { ?>
<div class="newsstory">
<h2><? echo $newsstory['headline']; ?></h2>
<div><? echo $newsstory['story']; ?></div>
<label>By <? echo $newsstory['user']; ?> on <? echo $newsstory['date']; ?></label>
</div>
<? } ?>
</body>
It would be as simple as adding the jQuery library to your exsiting page and adding the following code:
<div id="result"></div>
Manually Fetch Page
<script language="javascript">
function updatestuff() {
$.ajax({
url: 'mypage.php', // The source
cache: false, // Make sure results are not cached
success: function (data) {
$('#result').html(data); // Update data once retrieved
}
});
}
setInterval("updatestuff()",90000); // In Milliseconds
</script>
This would run the function updatestuff() every 90 seconds which would update the <div> with content fetched from mypage.php.
If you want something that automatically updates every 30seconds then definitely Ajax is the way to go.
Some say (myself include) that you'd be insane not to use jQuery to do this.

Resources