Formatting echo output in a HTML table - php

I have an object in PHP with 5 attributes using the following code:
<?php
class Person
{
private $gender, $race, $height, $weight, $eyes_color;
public function start ($gender,$race,$height, $weight, $eyes_color)
{
$this->gender=$gender;
$this->race=$race;
$this->height=$height;
$this->weight=$weight;
$this->eyes_color=$eyes_color;
}
public function show_attributes()
{
return sprintf("%s, %s, %s, %s, %s", $this->gender, $this->race, $this->height, $this->weight,$this->eyes_color);
}
}
$person=new person();
?>
I'm calling this class using the following HTML code
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Class Person</title>
</head>
<body>
<?php
require_once("Person.php");
$person->start("Male","Latin","1.83 cm","85 kg","Brown");
echo $person->show_attributes();
?>
</body>
</html>
Now, that will print something like
Male, Latin, 1.83 cm, 85 kg, Brown
But I want to print something like
--------------------------------------
|Male | Latin | 1.83 cm | 85 kg | Brown|
--------------------------------------
Using a HTML table.
I have try a couple of things, but I can't make it happend.
Is there a way to force
echo $person->show_attributes();
to only show one attribute so I can call it from inside a HTML cell table?
Thanks.

Try this
<?php
class Person
{
private $gender, $race, $height, $weight, $eyes_color;
public function start ($gender,$race,$height, $weight, $eyes_color)
{
$this->gender=$gender;
$this->race=$race;
$this->height=$height;
$this->weight=$weight;
$this->eyes_color=$eyes_color;
}
public function show_attributes()
{
return sprintf("<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>", $this->gender, $this->race, $this->height, $this->weight,$this->eyes_color);
}
}
$person=new person();
?>
HTML code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Class Person</title>
</head>
<body>
<?php
require_once("Person.php");
$person->start("Male","Latin","1.83 cm","85 kg","Brown");
echo "<table>":
echo "<tr>";
echo $person->show_attributes();
echo "</tr>";
echo "</table>";
?>
</body>
</html>

I don't know how in-depth you're looking to go, but you can load the data into a data modeler/table system like http://backgridjs.com.
I realize that may be completely over the top for what you're looking for, but it's robust and (mostly) easy to learn.

Related

display database result array in Codeigniter Controller and Also Pass in View

How Can We Fetch specific details from result array in controller and also pass result array to view. below i have written the code that i am using to get data from database in codeigniter model and then including it in controller and also able to pass it to view and echo results there, but i want to get some specific column results ( Metatitle, Metadesc, Metakeywrd ) in controller, so i can set meta_title, meta_description, meta_keywords values in controller only and pass it to view head dynamically,
This is My Controller
<?php
class India extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function memberview()
{
$data['meta_title'] = '';
$data['meta_description'] = '';
$data['meta_keywords'] = '';
$teamid = $this->uri->segment(6);
$data['view'] = 'region/india/team-member-view.php';
$this->load->model('region/India_model');
$data['team'] = $this->India_model->tmview($teamid);
$data['teamlist'] = $this->India_model->teamlist();
$this->load->view('region/layout', $data);
}
}
?>
This is My Model
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class India_model extends CI_Model{
public function tmview($teamid){
$this->db->select('*');
$this->db->from('ojiteam');
$this->db->where("id",$teamid);
$query = $this->db->get();
return $query->result_array();
}
}
?>
Here in View i am fetching data like this, this is working properly but head parts meta tags is getting set at controller, there are multiple static pages for that meta tags getting set in controller file and for some dynamic pages meta tags have been saved into database column.
<?php
foreach($team as $value){
};
?>
<!doctype html>
<html lang="en">
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="content-language" content="en"/>
<title><?php echo $meta_title; ?></title>
<meta name="description" content="<?php echo $meta_description; ?>" />
<meta name="keywords" content="<?php echo $meta_keywords; ?>" />
i'm not sure if i understood what you want but if i did i guess this is what you want ?
$team = $this->India_model->tmview($teamid);
$data['meta_title'] = $team['meta_title'];
$data['meta_description'] = $team['meta_description'];
$data['meta_keywords'] = $team['meta_keywords'];

Calling function in PHP

I am trying to call a function called displaySentence() and have it output the value of the 'sentence' typed into the form on candycontest.php. The function will eventually have more features, but for now I am just trying to echo out the value of that sentence to ensure the function works. When I run the script, the pages displays until it gets to my function, at which point it is blank.
candycontest.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Pete's Treats Candy Contest</title>
</head>
<body>
<form action="checkticket.php" method="post">
<label for="ticketNum">Enter your ticket number:</label>
<input type="number" name="ticketNum" style="width:100px"><br/>
<label for="sentence">Enter the magic sentence:</label>
<input type="text" name="sentence" style="width:600px"><br/>
<input type="submit" value="Am I a Winner?">
</form>
</body>
</html>
checkticket.php
<?php
$userTicket = $_POST['ticketNum'];
class MagicSentence {
public $sentence;
public function __construct($sentence) {
$this->setSentence($sentence);
}
public function getSentence() { return $this->sentence; }
public function setSentence($sentence) {
$this->sentence = $sentence;
}
} // End class MagicSentence
class Ticket extends MagicSentence {
public $ticketNum;
public function displaySentence() {
$userSentence = $_POST['sentence'];
echo $userSentence;
}
}
$magicSentence = new MagicSentence("The cow jumped over the moon.");
?>
<html>
<head>
<meta charset="utf-8">
<title>Pete's Treats Candy Contest</title>
</head>
<body>
<?php
echo 'Your ticket number is: ' . $userTicket . "<br>";
echo 'The magic sentence is: ' . $magicSentence->getSentence() . "<br>";
displaySentence();
?>
</body>
</html>
Change $magicSentence = new MagicSentence("The cow jumped over the moon."); to $magicSentence = new Ticket("The cow jumped over the moon.");
You need to do this because the displaySentence() method exists under the Ticket class (which extends off the MagicSentence class).
Also, change displaySentence(); to $magicSentence->displaySentence(); in order to call your method. You cannot call a method as you would a regular function.
Do that and you should be golden.
Create an object of class Ticket, $ticketObj and use $ticketObj->displaySentence(); in place of displaySentence();
displaySentence(); is a method of the Ticket class, of which you never instantiated an object, so it doesn't exist in any context yet.
$magicSentence = new MagicSentence("The cow jumped over the moon.");
needs to be:
$magicSentence = new Ticket("The cow jumped over the moon.");
and
displaySentence(); needs to be: $magicSentence->displaySentence();

Extract code from PHP function and insert in earlier function

Is it possible to call a function in PHP that contains code meant for the head of a document that can be inserted into the head.
eg.
<head>
<title>PHP Test</title>
<?php extra_data();?>
</head>
<body>
<?php new_function();?>
</body>
So when new_function() is called it outputs some code but also contains so extra info for the head such as a stylesheet that can be added to the extra_data() function in the head at the top.
Since its all being executed on the server anyway is this possible?
You can define extra_data() like so:
function extra_data(){
$new_function_data = new_function();
switch($new_function_data['extra']){
case 1:
echo '<link href="extra1.css" rel="stylesheet" type="text/css">';
break;
case 2:
echo '<link href="extra2.css" rel="stylesheet" type="text/css">';
break;
}
}
You can define new_function() like so:
function new_function(){
//Add logic to determine what data should be returned
$data['normal'] = 'Normal data to output';
$data['extra'] = 'Extra data for extra_data()';
return $data;
}
and change your php to
<?php $new_function_data = new_function(); echo $new_function_data['normal']; ?>

Passing variables between php pages

I'm new to the Kohana Framework. I have a problem - How can I pass the variable $title from Layout.php to Head.php?
In controller:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Admin_Quanly extends Controller_Template {
public $template='admin/layout';
function _showWithTemplate($subview,$title)
{
$admin_path = 'admin/';
$this->template->head = View::Factory(''.$admin_path.'head');
$this->template->subview = View::Factory(''.$admin_path.''.$subview.'');
$this->template->title = $title;
}
public function action_index()
{
$this->_showWithTemplate('subview/home','Trang quản trị hệ thống');
}
}
In view Layout.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php echo $head?>
</head>
<body>
</body>
</html>
In view Head.php:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?=$title?></title>
<base href="<?=URL::base()?>">
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="javascript/jquery.min.js"></script>
<script type="text/javascript" src="javascript/ddaccordion.js"></script>
You could do something like this:
$admin_path = 'admin/';
$this->template->head = View::Factory(''.$admin_path.'head');
$this->template->head->title = $title;
$this->template->subview = View::Factory(''.$admin_path.''.$subview.'');
$this->template->title = $title;
Note the $this->template->head->title = $title; you need to pass it along manually to the head view.
You can use set() or bind(). See example:
$view = View::factory('user/roadtrip')
->set('places', array('Rome', 'Paris', 'London', 'New York', 'Tokyo'));
->bind('user', $this->user);
Ref: http://kohanaframework.org/3.3/guide/kohana/mvc/views
What you are looking for is set_global
http://docs.kohanaphp.com/core/view#set_global
It will allow you to set a variable for all your views to be able to use. You won't be passing it per say but it will still do what you want.
Example fix
function _showWithTemplate($subview,$title)
{
$admin_path = 'admin/';
$this->template->head = View::Factory(''.$admin_path.'head');
$this->template->subview = View::Factory(''.$admin_path.''.$subview.'');
$this->template->set_global('title', $title);
}

sometime SetCookie() not working

Hi I created two file to switch my forum (Language Chinese and English)
enForum.php
<?php
function foo() {
global $_COOKIES;
setcookie('ForumLangCookie', 'en', time()+3600, '/', '.mysite.com');
echo 'running<br>';
$_COOKIES['ForumLangCookie'] = 'en';
bar();
} // foo()
function bar() {
global $_COOKIES;
if (empty($_COOKIES['ForumLangCookie'])) {
die('cookie_name is empty');
}
echo 'Language =' . $_COOKIES['ForumLangCookie'];
echo "<br>";
} // bar()
foo();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>forum EN Version</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
please be patient ...
<script LANGUAGE='javascript'>
location.href='http://www.mysite.com/forum/index.php';
</script>
</body>
</html>
cnForum.php
<?php
function foo() {
global $_COOKIES;
setcookie('ForumLangCookie', 'cn', time()+3600, '/', '.mysite.com');
echo 'running<br>';
$_COOKIES['ForumLangCookie'] = 'cn';
bar();
} // foo()
function bar() {
global $_COOKIES;
if (empty($_COOKIES['ForumLangCookie'])) {
die('cookie_name is empty');
}
echo 'Language =' . $_COOKIES['ForumLangCookie'];
echo "<br>";
} // bar()
foo();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>forum CN Version</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
please be patient ...
<script LANGUAGE='javascript'>
location.href='http://www.mysite.com/forum/index.php';
</script>
</body>
</html>
There are some files including include template('logon');,include template('regist'); etc, I write some code to get the Cookie value and control the flow to load different template files.
$lang = $_COOKIE["ForumLangCookie"];
// for Debug
// echo '$lang is '.$lang;
// echo '<br/>';
if ($lang == "cn"){
include template('logon');
}
else if ($lang == "en"){
include en_template('logon');
}
But sometime the SetCookie() not working. Do I need add Sleep(someSeconds); for my code?
Cookies can be accessed with $_COOKIE,not $_COOKIES.
EDIT:Sorry for misunderstanding. I suggest you to change the variable $_COOKIES as another common one so people can understand your question correctly.
PHP array name is $_COOKIE, not $_COOKIES

Categories