when looking in codeigniter user manual or some other open source CI base project, i realize that form are store/generate/create in views, here the example:
application/view/main_view.php
<?php echo form_open("/user/create");?>
<?php echo form_input("input1",$value); ?>
.
.
.
<?php echo form_close(); ?>
then load it in controller.
But in my project, i create long string that contain form + table code, when load->view, passing the hold string into that view, here are part of the code:
application/controller/user.php
$this->table->add_row(array("data"=>form_input("name",$value)));
$data["content"]=form_open("/user/create");
$data["content"].=$this->table->generate();
$data["content"].=form_close();
$this->load->view("main_view",$data);
which style are better? and why?
(PS: sorry for the broken English)
i using table to ensure the form layout, here come with the situation:
form writing in view with html table tag.
load html library in view(or related controller), but my view is flooding with many php code
if u work in a team better you use a pure html in view file and never do defining part of view in controller. but if you want to use CI form helper better you type like this :
<?php
$form = form_open("/user/create");
$form .= form_input("input1",$value) . '<br>' ;
$form .= form_input("input2",$value) . '<br>' ;
$form .= form_input("input3",$value) . '</br>' ;
$form .= form_close();
echo $form;
?>
it just a simple example to make shorter of code.
Related
I am using woody snippets to insert php code to make an API call to return information in a table. How do I get the table to display using the default css from my wordpress theme?
I am new to PHP, but not to wordpress. And I know just enough CSS to get myself in trouble. So, I need ELI5 instructions.
My programmer provided the basic PHP code to call the API and return the data in a basic table. But, it looks horrid and there's no spacing or styling.
$tableStart = "<table>";
$tableEnd = "</table>";
$trStart = "<tr>";
$trEnd = "</tr>";
$tdStart = "<td >";
$tdEnd = "</td>";
$fname = 'alabama_energy_data.json';
$data = json_decode(file_get_contents($fname));
echo $tableStart;
foreach($data as $bill_data)
{
echo $trStart;
echo $tdStart . $bill_data->bill_id . $tdEnd . $tdStart . $bill_data->title . $tdEnd;
echo $trEnd;
}
echo $tableEnd;
This returns a basic table with the data I need, but it's not styled. How do I get it to use the default CSS from our wordpress site that it's displaying in so that the table renders in a format that looks decent?
If your table isn't styled like it should be, then you're probably missing and id or class attribute at your table. You could check on what current attributes your "wordpress" tables have, and just add them to your table.
Example:
<table class="wp-class-table">
# OR
<table id="wp-id-table">
And one other thing, I personally don't really like the way you "echo" your data. If you want, you can read this answer. It shows a nicer way to do it in my opinion.
i had save data in mysql data base and i had create template html tags in one column. now i want to display template data in php, but its not working.
MYSQL DATABASE IMAGE CLICK HEAR TO SHOW DATABASE
Controller
class Project extends CI_Controller{
function template(){
$query = $this-db->query("select * from templete where id='11' ");
$result = $query->result_array();
$data['result'] = $result[0];
$this->load->view('template_view',$data);
}
}
View
<html>
<head></head>
<body>
<?php
extract($result);
echo $template_data;
?>
</body>
</html>
Output :
{$ticker}
{$address1}
{$address2}
{$city}
{$phone}
{$website}
I want output
HSPG.OL
Sparebankgården
Bjørkelangen
http://www.hsbank.no
template data dyanamic
its gone be
<p>{$ticker}</p><p>{$address1}</p><p>{$address2}</p><p>{$city}</p><p>{$phone}</p><p>{$website}</p>
or
<p>{$ticker}</p><p>{$address1}</p><p>{$address2}</p><p>{$city}</p>
any one help, i am post this question 2nd time with full coding.
Although Codeigniter as a framework doesn't stop you or restrict you to stay in MVC boundaries but it is really appreciated and a good practice that one should do so.
BEST PRACTICE
Create a model and put your query in a function in that model. Call
that function in your controller, get your data in an array/object and
then send it to the view that's how MVC is supposed to be.
In your case, you are using php extract() function to get data out of the $result array which it has done perfectly but what you are printing is not the correct index.
Read this for php extract info
so instead of printing $template_data use the following ;
<html>
<head></head>
<body>
<?php
extract($result);
echo $ticker.'<br>';
echo $address1.'<br>';
echo $city.'<br>'; // and so on
?>
</body>
</html>
Lot of things wrong with this code. You shouldn't be getting template data from the database, that should be in the view. You should probably delete the template_data column from your table.
class Project extends CI_Controller{
function template(){
$query = $this-db->query("select * from templete where id='11' ");
$data['result'] = $query->result_array();
$this->load->view('template_view',$data);
}
}
View:
<html>
<head></head>
<body>
<?php echo $result['ticker']; ?>
<?php echo $result['address1'] . ' ' . $result['address2']; ?>
<?php echo $result['city']; ?>
<?php echo $result['phone']; ?>
<?php echo $result['website']; ?>
?>
</body>
</html>
I am trying to create a body class name based on a specific page in php. What I need to do is first define a variable and then check if the variable exists, and then if it exists display that one, otherwise if it is something else, then do something else, or if nothing then do not show any.
<body<?php if (defined('PAGE_KEY') && var == "homepage") echo " class=\"homepage\"";
elseif (defined('PAGE_KEY') && var == "page3") echo " class=\"page3\"";
elseif (defined('PAGE_KEY') && var == "page12") echo " class=\"page12\""; ?>>
This code will be located in my head.php.
My thought was to first check if variable was defined and if so then check what the variable was defined as and then based on that variable it will display a respective class.
The goal is for example on the page12 for the body tag to look like this:
<body class="page12">
But for example for the body tag on page55 (which I don't want to show a class for) to look like this:
<body>
In doing so I am able to now define css specifically for a page within the header where the body tag happens to be located.
Problem is first I don't know how to define the variable in the page, and second I don't know exactly how to write the php code above properly.
Attempt, for example on page12 I would have this code:
<?php PAGE_KEY = "page12" ?>
This code would be for example located in page12.php.
Also keep in mind, that the variable will come AFTER the body tag.
I also thought of trying to see what the page URL is but I think that's just making things too complicated.
Based on #Jordi's suggestion, how about this:
<body class="<?php echo PAGE_KEY ?>">
on head.php.
And then on page12.php, this:
<?php PAGE_KEY = "page12" ?>
and for example on page5.php this:
<?php PAGE_KEY = "page5" ?>
so that on those respective pages the body tag shows this:
on page5.php:
<body class="page5">
and on page12.php the body tag will show this:
<body class="page12">
Is this right?
#Jose made this suggestion, is this correct?
for example, on page12.php, to define the variable as 'page12', this:
<?php define("PAGE_KEY", "page12"); ?>
Is that what you were suggesting to do?
Ok! This problem is solved. I just needed to add in the code in the individual pages before the head.php include so I could define it. Thanks for your help! :)
You can use and define a constant like this :
<?php
define( "PAGE_KEY","homepage" );
?>
.
.
.
<body<?php echo " class=\"" . constant( "PAGE_KEY" ) . "\""; ?>>
For another page :
<?php
define( "PAGE_KEY","page5" );
?>
.
.
.
<body<?php echo " class=\"" . constant( "PAGE_KEY" ) . "\""; ?>>
You only change the constant, the rest is the same for every page.
Edit #1:
<body
<?php
define( "PAGE_KEY","homepage" );
echo " class=\"" . constant( "PAGE_KEY" ) . "\"";
?>
>
Edit #2:
<?php
define( "PAGE_KEY","homepage" );
?>
.
.
.
<?php
include( "head.php" >
?>
Now, head.php is something like this :
echo "<body class=\"" . constant( "PAGE_KEY" ) . "\">";
Start with something like this.
<?php
//each page and its class. Many pages can share the same class. If a page doesn't
//have a class, don't include it.
$classes = [
'homepage'=>'home_class',
'page1'=>'base_class',
'page2'=>'home_class',
'page3'=>'special_class'
];
//Adjust this from one page to the next
$this_page = 'homepage';
//get the class corresponding to current page, or '' if no class
$this_class = isset($classes[$this_page])? $classes[$this_page] : '';
?>
//insert the class for this page.
<body class="<?=$this_class ?>">
You can improve on it by moving the $classes array to another file (eg a config file) and including it in all your pages. This way you don't have to rewrite the array on every page (a bad idea because difficult to make a change, easy to make a mistake)
I'm new to zend and i'm struggling with some items.
I tried using Helpers, but then realized it doesn't do what I wanted it to do...
either i'm using $this->placeHolder... wrong or it doesn't do what I want it to do
I want to do the following:
I want to create a custom class that basically has 2 methods addScriptContent, and getScriptContent...
addScriptContent adds what ever is passed into it to a string that is global for the page.
getScriptContent will just output whatever data that has been added using addScriptContent
i.e.
this->addScriptContent('var x="foo";')
....some html
this->addScriptContent('var y="feee";')
....some html
echo this->getScriptContent() //writes out var x="foo"; var y="fee";
The placeholder helper does exactly what you are after:
<?php $this->placeholder('scriptContent')->set('var x="foo";') ?>
....some html
<?php $this->placeholder('scriptContent')->append('var y="feee";') ?>
....some html
<?php echo $this->placeholder('scriptContent')?>
the key methods on the helper are set, append and prepend; which overwrite, add to, and add to the start of the content respectively.
If you really want to write your own helper for this, this wouldn't be too difficult. Take a look at the headTitle or headScript helpers for an example, but would be something along the lines of:
class My_View_Helper_ScriptContent extends Zend_View_Helper_Placeholder_Container_Standalone
{
public function scriptContent($script)
{
$this->append($script);
}
}
usage would then be:
<?php $this->scriptContent('var x="foo";') ?
....some html
<?php $this->scriptContent('var y="feeee";') ?>
....some html
<?php echo $this->scriptContent() ?>
I use codeigniter. I have footer_view.php, header_view.php and other view php files for pages like aboutus, contactus, etc... for example for about us page, I create model for db actions, than I create a controller to get db variables and send variable to about us view as:
$this->load->view('aboutus/',$data);
Everything is fine until this point, but when I need to get data from db at footer, how will I use in the CodeIgniter Way?
If I create a footer_model, I cannot make view('footer/') because it is actually a part if page, not a page itself :/
You can use $this->load->vars($data);
This will make all data available to all views, footer_view.php, header_view.php and any other views.
$data['your_info'] = $this->user_model->get_info();
$data['your_latest_info'] = $this->user_model->get_latest_info();
$data['your_settings_info'] = $this->user_model->get_settings_info();
$this->load->vars($data);
$this->load->view('your_view');
You view can and will access the data like so:
Your "primary" view
<?php
// your_view.php
// this can access data
$this->load->view('header_view');
<?php foreach ($your_info as $r):?>
<?php echo $r->first_name;?>
<?php echo $r->last_name;?>
<?php endforeach;?>
$this->load->view('footer_view');
?>
Your header_view
<?php
// header_view.php
<?php echo $your_latest_info->site_name;?>
?>
Your footer_view
<?php
// footer_view.php
<?php foreach ($your_setting_info as $setting):?>
<?php echo $setting->pages;?>
<?php endforeach;?>
?>
No need for any template library...
I wrote a blog post about this http://joshhighland.com/blog/2008/11/09/how-i-do-layouts-and-views-in-codeigniter/
basically, you want to load all the data that all your views are going to need in the controller. I use an array of data elements, then pass that to the layout view. My blog post explains more about this, and shows more detail. Here is a sample of my controller.
$this->load->model('search');
$lastest_albums = $this->search->last_n_albumsAdded(10);
$popular_songs = $this->search->popular_n_songs(10);
$featuredAlbums = $this->search->getFeaturedAlbums();
$body_data['featured'] = $featuredAlbums;
$body_data['newest'] = $lastest_albums;
$body_data['popular'] = $popular_songs;
$layout_data['content_body'] = $this->load->view(’home/homePage’, $body_data, true);
$this->load->view('layouts/main', $layout_data);