My content controller in codeIgniter:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class AutoLoadDiv extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('ngoding/AutoLoad');
}
public function getData() {
$this->load->library('table');
$err = file_get_contents("application\logs\log.php");
$filestr = preg_replace(array('/(^|\R)ERROR\s*-\s*/', '/(^|\R)(.*?)\s*-->\s*/'), array('$1', '$1$2 '), $err);
$arraySpacing = nl2br($filestr);
$arr = explode("\n", $arraySpacing);
for ($i = count($arr)-1; $i >= 0; $i--) {
echo "<html><body><table><tr><td>$arr[$i];</td></tr></table></body>/html>";
}
}
}
I have a problem make table in controller , I want to print a table like this:
I have view :
https://codeshare.io/GqyWmk
To print table like in the image you need to change your code like this.
Assuming you have only 4 column in the file:
$output = "<html><body><table>"; // Keep this outside for loop to print only once.
var $j = 0;
for ($i = count($arr)-1; $i >= 0; $i--) {
if ($j % 4 == 0) {
$output.="<tr>";
}
$output.="<td>$arr[$i]</td>"; // Adding new row in your output varible.
if ($j % 4 == 0) {
$output.="</tr>";
}
++$j;
}
$output.="</table></body>/html>"; // This should also be outside because you want to close table, body and html only once.
echo $output; // Printing your final data.
You can modify this code as per your requirement;
Related
I have the following:
public function go(){
for($x=0;$x<=31;$x++) {
$this->get_answerrules($x);
$DoneTime+=$DoneTime;
}
echo $gmdate("i:s", $DoneTime);;
}
and
public function get_answerrules($x){
...
...
...
if($response = $this->request($data)){
$obj = json_decode($response,true);
foreach($obj as $file) {
$Time += $file['batch_dura'];
}
$DoneTime = $Time;
return $DoneTime;
}else{}
}
How do I get the value from the 31 for loop cycles and add them together?
Right now my results are coming up blank.
You are not using the results of your method call:
public function go(){
for($x=0;$x<=31;$x++) {
$this->get_answerrules($x);
$DoneTime+=$DoneTime;
}
// The below won't work as `$gmdate` is not in the scope of the method.
echo $gmdate("i:s", $DoneTime);;
}
Should probably be something like:
public function go() {
// Initialize the variable
$DoneTime = 0;
for($x = 0; $x <= 31; $x++) {
$DoneTime += $this->get_answerrules($x);
}
echo $gmdate("i:s", $DoneTime);
}
I'm trying to delete an user out of a file.
This is how I delete the user from my array:
if (isset($_GET['delete'])) {
$id = key($_GET['delete']);
for ($i = 0; $i < count($liste); $i++) {
if ("$i" == "$id") {
array_splice($liste, $id, 1);
} else {
//do something
}
}
saveDataToFile('passwd.txt', '$liste');
}
Then I use an other function to write it in a file. But I always get the error
implode(): Invalid arguments passed
This is the function.
function saveDataToFile($fileName, $liste){
$file=fopen($fileName,"w");
for ($i = 0; $i < count($liste); $i++) {
$zArray=$liste[$i];
$zeile=implode("|", $zArray);
if(strlen($zeile) > 0){
$zeile=$zeile."\r\n";
fwrite($file, $zeile);
}
}
fclose($file);
}
update
I also found out that it does delete everytime I refresh.
I think I need a break when it deleted the one I want. But the break doesn't work.
You pass $liste as a string not array, here
saveDataToFile('passwd.txt', '$liste');
May this work with you:
saveDataToFile('passwd.txt', $liste);
I am trying to create a dynamic page links created based on the number of rows in a mysql table. I would like to display 10 results per page and wish to have the php script create links to additional pages.
So I was thinking of using the num_rows and dividing it by 10 however if I have 53 rows the return would be 5.3 where as I would need 6 pages and not 5. I am thinking of using the round function and looping it through a for I statement until $pages > $rows_rounded. And every 10 rows add a link to pages($i) Is this the best method to acheive this or there an alternative simpler route to take?
pagenator class I made. getCurrentPages() returns all the pages you should be displaying in an array. so if you are on page one, and you want to display a total of 9 pages, you would get an array 1-9. if you were on page 10 however, your would get back an array 6-14. if there are 20 total pages and you are on page 20, you would get back an array 11-20.
<?php
class Lev_Pagenator {
private $recordsPerPage;
private $currentPage;
private $numberOfTotalRecords;
private $lastPage = null;
public function __construct($current_page, $number_of_total_records, $records_per_page = 25) {
$this->currentPage = $current_page;
$this->numberOfTotalRecords = $number_of_total_records;
$this->recordsPerPage = $records_per_page;
}
public function getCurrentStartIndex() {
return ($this->currentPage - 1) * $this->recordsPerPage;
}
public function getCurrentPages($number_of_pages_to_display = 9) {
$start_page = $this->currentPage - floor($number_of_pages_to_display / 2);
if ($start_page < 1) $start_page = 1;
$last_page = $this->getLastPage();
$pages = array($start_page);
for ($i = 1; $i < $number_of_pages_to_display; $i++) {
$temp_page = $start_page + $i;
if ($temp_page <= $last_page) {
$pages[] = $temp_page;
} else {
break;
}
}
return $pages;
}
public function getPreviousPage() {
if ($this->currentPage === 1) return false;
return $this->currentPage - 1;
}
public function getNextPage() {
if ($this->currentPage === $this->getLastPage) return false;
return $this->currentPage + 1;
}
public function getLastPage() {
if ($this->lastPage === null) $this->lastPage = ceil($this->numberOfTotalRecords / $this->recordsPerPage);
return $this->lastPage;
}
}
?>
EDIT (USAGE):
<?php
$pagenator = new Lev_Pagenator($current_page, $number_of_total_records, $records_per_page);
$pages_array = $pagenator->getCurrentPages($number_of_pages_to_display);
?>
The idea of a for loop sounds like a good one, you would use something like:
$rows_rounded = ceil(mysql_num_rows($result) / 10);
for($x = 1; $x <= $rows_rounded; $x++){
echo 'Page '.$x.'';
}
But you need to consider detecting the current page, so if, for example, the current page was 3, it might be a good idea to test for that in your for loop and if echoing the 3rd link maybe add some extra class to enable you to style it.
is there a way we can get variable from another function for the current function in controller and user set function to use in corresponding view file?
Sorry guys I meant passing view file variable to another view file...Below is the code...
function get_random_color()
{
$c="";
for ($i = 0; $i<6; $i++)
{
$c .= dechex(rand(0,15));
}
return "#$c";
}
$i = 0;//debug($trips);
foreach ($trips as $trip) {
$colour = get_random_color();
$numItems = count($trip['trip']['coords']);
if($numItems > 3){
$x = 0;
echo 'var flightPlanCoordinates'.$i.' = [';
foreach($trip['trip']['coords'] as $coords) {
if($x+1 == $numItems) {
echo 'new google.maps.LatLng('.$coords['latitude'].','.$coords['longitude'].') ';
}
else {
echo 'new google.maps.LatLng('.$coords['latitude'].','.$coords['longitude'].'), ';
}
$x++;
}
/*
for($x = 0; $x<sizeof($trip['trip']['coords']); $x++) {
echo 'new google.maps.LatLng('.$trip['trip']['coords']['latitude'].','.$trip['trip']['coords']['longitude'].'), ';
}*/
echo ']; ';
echo 'var flightPath'.$i.' = new google.maps.Polyline({
path: flightPlanCoordinates'.$i.',
strokeColor: "'.$colour.'",
strokeOpacity: 1.0,
strokeWeight: 4
});';
echo 'flightPath'.$i.'.setMap(map);';
$i++;
}
}
You can do a requestAction, although it is considered the slower way to get at that data. If you give more details I might be able to give you a better answer.
$this->requestAction('/posts/list');
more on requestActions here: http://book.cakephp.org/view/991/requestAction
Use Configure class if you need a global variable in this situation,request action should be a bad choice.
the problem with the class is that if i want to display 30 records at a time and i have 60 total records the pagination divs that shows the page numbers only shows page #1 and not page #2. i have tried to figure out how to fix it but i have given up. any help would greatly be apreciated.
this is how i call attributes to the class.
$paginate = new Pagination;
$paginate->pageName = "index.php"; //sets the page to use
$paginate->perPage = 10; //show num of records per page
$paginate->adjacents = 3; //current page adjacent to
$paginate->sql = "select * from tbl_products"; //the main query
$query = $db->query($paginate->getData());
while($row = mysql_fetch_object($query)) {
print $row->pName."<br/>";
}
$paginate->showPagination(); //shows the pagination div
here is the class.
<?php
include_once("class.db.php");
class Pagination
{
var $param;
var $perPage;
var $adjacents;
var $start;
var $sql;
var $pageName;
function __construct()
{
$this->db = new MysqlDB;
$this->db->connect();
}
function setParam()
{
if(isset($_GET['page']) && is_numeric($_GET['page']) && ($_GET['page'] > 0))
{
$this->param = $_GET['page'];
}
else
{
$this->param = 1;
}
}
function setIndex()
{
$this->setParam();
return $this->start = ($this->param * $this->perPage) - $this->perPage;
}
function showPagination($param1=null,$param2=null,$param3=null)
{
$qRows = $this->db->query($this->sql);
$numRows = $this->db->num_rows($qRows);
$numOfPages = ceil($numRows / $this->perPage);
$param = $this->param;
$pageName = $this->pageName;
$string = "";
//set pagination parameters.
if($param1 != null)
{
if(isset($_GET[$param1]))
{
$param1Value = $_GET[$param1];
$string .= "&".$param1."=".$param1Value;
}
}
if($param2 != null)
{
if(isset($_GET[$param2]))
{
$param2Value = $_GET[$param2];
$string .= "&".$param2."=".$param2Value;
}
}
if($param3 != null)
{
if(isset($_GET[$param3]))
{
$param3Value = $_GET[$param3];
$string .= "&".$param3."=".$param3Value;
}
}
print "<div class='pagination'>";
print "<a href='$this->pageName?page=1$string' class='previous-off'> First </a>";
// ----------------------------------------------------------------------------
// PRINT ALL PAGES TO THE LEFT //
if(($param - $this->adjacents) > 1)
{
print "<span>...</span>";
$lowerLimit = $param - $this->adjacents;
//print all on left side.
for($i = $lowerLimit; $i< $param; $i++)
{
print "<a href='$pageName?page=$param = $i$string'> $i </a>";
}
}
else
{
//print all numbers between current page and first page.
for($i = 1; $i < $param; $i++)
{
print "<a href='$pageName?page=$i$string'> $i </a>";
}
}
// ----------------------------------------------------------------------------
//print current page
if(($param != 0) && ($param != $numOfPages))
{
print "<span class='current'>$param</span>";
}
// ----------------------------------------------------------------------------
//PRINT ALL PAGES TO THE RIGHT
if(($param + $this->adjacents) < $numOfPages)
{
$upperLimit = $param + $this->adjacents;
for($i=($param + 1); $i<=$upperLimit; $i++)
{
print "<a href='$pageName?page=$i$string'> $i </a>";
}
print "<span>...</span>";
}
else
{
//print all page numbers if out of padded range
for($i = ($param + 1); $i<$numOfPages; $i++ )
{
print "<a href='$pageName?page=$i$string'> $i </a>";
}
}
// ----------------------------------------------------------------------------
$lastPage = $numOfPages - 1;
print "<a class='next' href='$pageName?page=$lastPage$string'> Last </li>";
print "</div>";
}
function getData()
{
$query = $this->sql;
$this->start = $this->setIndex();
return "$query LIMIT $this->start, $this->perPage";
}
function errors()
{
$query = $this->sql;
print "$query LIMIT $this->start, $this->perPage"."<br/>";
print "this->start ".$this->start."<br/>";
print "this->param ".$this->param."<br/>";
print "this->perPage ".$this->perPage."<br/>";
print "this->setindex() ".$this->setIndex()."<br/>";
}
}
?>
Personally I tend to stray from SELECT * FROM tbl type queries as if you have a large table, it can be slow. Therefore, I run two queries: one to get the total number of records, and one to get the data set I need based on a paging parameter in the URL, e.g. example.com/news.php?page=2.
From there, I can instantiate my own paging class, which is pretty simple:
<?php
class Paging {
private $total;
private $batch;
private $page;
private $prefix;
public function __construct($total=0, $batch=10, $page=1, $url="") {
$this->total = intval($total);
$this->batch = intval($batch);
$this->page = intval($page);
$this->url = $url;
}
public function total_pages() {
return ceil($this->total/$this->batch);
}
public function render() {
$html = "";
// only display paging if we have more than the batch value
if ($this->total > $this->batch) {
$html.= '<p>Go to page:';
for ($i=1; $i <= $this->total_pages()) {
if ($i==$this->page) {
$html.= ' <span class="current">'.$i.'</span>';
}
else {
$html.= ' '.$i.'';
}
}
$html.= '</p>';
}
return $html;
}
}
Then to use:
<?php
// get total number of records
$sql = "SELECT COUNT(*) AS total FROM tbl";
$res = $db->query($sql);
$row = $res->fetch();
$total = $row['total'];
$batch = 20;
$page = intval($_GET['page']);
// instantiate paging class
require_once('paging.class.php');
$paging = new Paging($total, $batch, $page, "mypage.php?page=%s");
// do normal page stuff here...
// render the pagination links
echo $paging->render();
Feel free to use the above code and modify to your needs