Is it possible to display HTML from within a PHP class? - php

Is it possible to write a form and javascript validations using class and functions. Her is my code sample.
class files{
function displayhtml(){?>
select name="df" onchange=analert(this.value);>
for($i=0;$i<10;$i++){?>
<option value="<?php echo "$i"; ?>"><?php echo "$i"; ?></option><?php
}?></select><?php
}
}
echo files::displayhtml();
?>
On change event i need to alert the value selected in dropdown, how will i write javascript in this same class. ???

Yes, I think you're looking for this:
<?php
class file {
public static function displayHTML() {
$result = "<form><select onchange='alert(this.value)'>";
for($i=0;$i<10;$i++) {
$result .= "<option value='".$i."'>".$i."</option>";
}
$result .= "</select></form>";
return $result;
}
}
echo file::displayHTML();
?>

in event onchange must : onchange="alert(this.value)"

Related

Disable select option when other specifig value is selected

I have a question by these given information:
private function loadIntervallOptions($intervallid = 1, $intervallfactor=1){
echo "<select name='intervallfactor'>";
for($intervallnumber=1;$intervallnumber<10;$intervallnumber++){
$checked = "";
if($intervallnumber== $intervallfactor){
$checked = "selected";
}
echo "<option $checked>$intervallnumber</option>";
}
echo "</select>";
echo "<select name='intervallid'>";
for($i=0;$i<count($this->todo_intervall);$i++){
echo "<option value='{$this->todo_intervall[$i]->getintervallid()}' ";
if($this->todo_intervall[$i]->getIntervallid() == $intervallid){
echo "selected";
}
echo ">{$this->todo_intervall[$i]->getIntervall()}</option>";
}
echo "</select>";
}
What I want to do:
If intervallid=1 I want to hide the select 'intervallfactor'. Is this possible?
Thank you in advance!
EDIT:
I'm trying this but it won't work
$('.intervallid').change(function(e) {
if ($('.intervallid').val()==1){
$('.intervallfactor').attr('disabled','disabled');
}
else{
$('.intervallfactor').removeAttr('disabled');
}
});
$('.intervallid').trigger('change');
You are trying to reference the html element using a class name .intervallid which doesnt exist.
either you should use $('select[name="intervalid"]) or add an id to the select from php and, use that to reference the element.
PHP
echo "<select name='intervallid' id='intervall_id'>";
JS
$('#intervall_id').attr('disabled', 'true')

How to get date month year using loop function

I Wanted to make a function from where i can get date , month , year
but i m unable to write functions for that also i don't know how to call those function in Drop down menus
for example
function get_date(){
for($i=1;$i<31;$i++){?>
<?php return '<option value="$i"> $i </option>';
}
now if i called it in select menu
<select >
echo get_date();
</select>
it returns echo $i kindly guide me where i went wrong
Try This
<?php
function get_date()
{
$var="";
for ($i = 1; $i < 31; $i++) {
$num_padded=sprintf("%02d", $i);
$var .='<option value="'.$i.'"> '.$num_padded.' </option>';
}
return $var;
}
?>
<select >
<?php echo get_date(); ?>
</select>
This one for month
<?php
function get_month()
{
$var="";
for ($m=1; $m<=12; $m++) {
$var .= '<option value="'.$m.'">'.date('F', mktime(0,0,0,$m, 1, date('Y'))).' </option>';
}
return $var;
}
?>
<select >
<?php echo get_month(); ?>
</select>
This one for Year
<?php
function get_year($start,$end){
$var="";
while($start <= $end){
$var .="<option value=".$start.">".$start."</option>";
$start++;
}
return $var;
}
?>
<select >
<?php echo get_year(1988,2000); ?>
</select>
Using variables in strings only works when using double quotes ("). Try this:
function get_date(){
$dates = "";
for($i=1;$i<31;$i++){
$dates .= "<option value='$i'> $i </option>";
}
return $dates;
}
Also, there is no need to use php tags in functions.
To call the function you need to put the echo between php tags
<select>
<?php echo get_date(); ?>
</select>
Create a string with all options and then return
function get_date(){
$options = "";
for($i=1;$i<31;$i++){
$options .= "<option value='$i'>$i</option>";
}
return $options;
}
And then print like this
<select name="nameofdropdown" id="idofdropdown">
<?php echo get_date(); ?>
</select>
Check your errors
If you return if immediate return and stop iterations.
Php variables inside singel quotes will not evaluates like '$i' it must be "$i" or "'$i'"within single qotes
Use php tag <?php ?> to print function returned value.
try this
function get_date(){
$date = "";
for($i=1;$i<31;$i++){
$date .= '<option value="'.$i.'>'.$i.'</option>';
}
return $date;
}
Try
function get_date(){
for($i=1;$i<31;$i++){?>
<?php return '<option value="'.$i.'"> '.$i.' </option>';
}

How to transfer values from controller to view codeigniter into dropdownlist

if (is_array($jsonhome)) {
foreach ($jsonhome as $query) {
foreach ($query['results']['place'] as $places) {
if (is_array($places['country'])) {
echo "Country:\n";
echo "Content: " . $places['country']['content'] . "\n\n";
}
if (is_array($places['admin1'])) {
echo "State:\n";
echo "Content: " . $places['admin1']['content'] . "\n\n";
}
if (is_array($places['admin2'])) {
echo "District/City:\n";
echo "Content: " . $places['admin2']['content'] . "\n\n";
}
}
}
}
$location_data['user_current_country'] = $places['country']['content'];
$this->load->view('header_register', $location_data);
$this->load->view('body_complete_register', $location_data);
$this->load->view('footer_register');
I want to transfer the country to input type select in the view codeigniter:
Say i get two countries via the above queries say India and USA i want to pass it to the view
Do i do it via ajax?
Please help i am new to codeigniter
So in your controller you want to build an array of options for your <select>. Something like
//Build array of country options
$aData['countryOptions'] = array();
foreach ($jsonhome as $query) {
foreach ($query['results']['place'] as $places) {
$aData['countryOptions'][] = $places['country']['content'];
}
}
$this->load->view('body_complete_register', $aData);
Then in your view you can use the form_dropdown function providing you have the form helper loaded
$this->load->helper('form');
echo form_dropdown('countries', $countryOptions);
Of if you don't want to use the helper you could do
<select name="countries" id="countries">
<?php foreach($countryOptions as $key => $countryName) { ?>
<option value="<?php echo $key ?>"><?php echo $countryName ?></option>
<?php } ?>
</select>
Please note that you shouldn't be echoing anything out in the controller. The controller is just for passing data between models, libraries and views

(EDITED QUES) PHP - Call function within another function

I have referred to similar questions like this and have done exactly the same way as it should have been but no success. Hence I would appreciate if some one can help me with this
I have a file called view.php which calls a function from a class based on a switch case condition. This function displays the output on the page with few links. When clicked on the link it calls another function which sits in my first function. But obviously when I click my link, nothing happens. That's my code.
view.php
require_once(..'/functions.php');
$functions = new myfile_functions();
<form method="post" action="view.php"><div>
<p><select name="potentialareas" id="potentialareas">
<option style="font-weight:bold;" value="-1">Select an area to view </option>
<?php
foreach($accessareas as $accessarea){
echo "<option value='".$accessarea->id."'>".$accessarea->name. " - " . $accessarea->reporttype. "</option>";
}
?>
</select>
</p>
<p><input name="view" id="view" type="submit" value="View" title="view" /><br /></p>
</div></form>
<div style="border-top:1px dotted #ccc;"></div>
<?php
if(isset($_POST['view']))
{
$hierarchyid = $_POST['potentialareas'];
$reporttype = $DB->get_record_sql("some query");
switch($reporttype->reporttype)
{
case "D Report":
$functions->getDepartmentReport($hierarchyid);
break;
case "S Report":
$functions->getSectionReport($hierarchyid);
break;
case "A Report":
$functions->getAreaReport($hierarchyid);
break;
}
}
functions.php
class myfile_functions(){
function getDepartmentReport($departmentid)
{
global $USER, $CFG, $DB;
$dname = $DB->get_record_sql("some query");
$output = "<div id='actualreport'><p><b>" . $dname->name. " Department Report</b></p>";
$output .= "<p>Activities started: </p>";
$output .= "<p>Activities Completed: </p></div>";
$output .= "<div id='separator' style='border-top:1px dotted #ccc;'></div>";
$output .= "<div id='listofsections'><p><b><i>Select the following for a more detailed report.</i></b></p>";
$snames = $DB->get_records_sql('some query');
foreach($snames as $sname)
{$output .= "<p>" .$sname->name. " <a href='view.php?section=" .$sname->id. "' name='section'><i>view report</i></a></p>";
}
$output .= "</div>";
if(isset($_GET['section']))
{
$this->getSectionReport($_GET['section']);
}
echo $output;
}
function getSectionReport($sectionid)
{
global $USER, $CFG, $DB;
$sname = $DB->get_record_sql("some query");
$output = "<div id='actualreport'><p><b>" . $sname->name. " Report</b></p>";
$output .= "<p>Num Users: </p>";
$output .= "<p>Activities Completed: </p></div>";
$output .= "<div id='separator' style='border-top:1px dotted #ccc;'></div>";
$output .= "<div id='listofareas'><p><b><i>Select the following for a more detailed report.</i></b></p>";
$anames = $DB->get_records_sql('some query');
foreach($anames as $aname)
{$output .= "<p>" .$aname->name. " <a href='view.php?area=" .$aname->id. "' name='view' id='view'><i>view report</i></a></p>";
}
$output .= "</div>";
if(isset($_GET['area']))
{
$areaid = $_GET['area'];
$this->getAreaReport($areaid);
}
echo $output;
}
Similarly another function calling the above function, n so on.
function getAreaReport($id)
{ .. same content but calling another function...}
So when ever I click my view report link, I get the id appended id in my querystring, something like
http://mydomain.com/view.php?section=5
Ideally the contents of getSectionReport() should get printed but its not. Please point out what is it that I am doing wrong.
Thanks in advance.
What's your main method for displaying everything? At the moment you have three functions, all of which link to eachother in various ways, but it doesn't look as if you're instantiating anything first. The PHP is being fed a parameter in your URL, sure, but if a class isn't instantiated and a method declared, how does it know what you want it to do?
For myfile.php, maybe you should do something like:
class MyFile
{
public function other_function()
{
// Various stuff
return 'stuff';
}
public function other_other_function()
{
// Various other stuff
return 'other stuff';
}
public function page_view($file_id)
{
$var = $this->other_function($file_id);
return $this->other_other_function($var);
}
}
$class = new MyFile;
echo $class->page_view($_GET['id']);
If the two functions are part of a class (as your comment above hints), then the call should be written as
$this->getUserReport($id);
If you're accessing a sibling function inside the same class, use:
class ClassName
{
public function sibling_function()
{
return 'Hey bro!';
}
public function my_function()
{
return $this->sibling_function();
}
}
If not, use the standard:
public function my_function()
{
return sibling_function();
}
If you're still having trouble, make sure your class is properly instantiated. There's no point calling another function if you're not even instantiating the class first:
$obj = new ClassName;
echo $obj->my_function();

Codeigniter validation

I have many goals to be printed on the screen.
but it shows error when i use it like this
echo $this->validation->rshort_goal.$i;
What is the right way to use this?
if($sgoal !='')
{
$scount = count($sgoal);
$i =1;
foreach($sgoal as $row)
{
<textarea name="rshort_goal<?php print $i;?>" id="short_goal" class="short_go">
<?php if($this->validation->rshort_goal.$i)
{
echo $this->validation->rshort_goal.$i;
}
elseif($this->validation->rshort_goal.$i._error !='')
{ echo ''; }
else
{echo $$row->goal_description; }
?>
</textarea>
<?php
$i++;
}
}
echo #$this->validation->{'rshort_goal'.$i};
Perhaps you want to call a function like this?
call_user_func($this->validation, 'rshort_goal' . $i);

Categories