I am trying to call an external function within a class method property. It actually does gets called but at the end of the page and whatever is inside the method property, remains separate.
Since I am a self taught student, it is recent that I have started learning PHP classes so I am not really sure if this can be done or not.
Please guide me how this can be done correctly or if not, then what could be the workaround?
The class I have written is as follows: It will take the input from user while creating of instance and render a modal box based on the input and options selected.
class modalBox{
private $modal_id, $modal_anim, $header_title, $modal_content,$footer;
private $headerOpt,$titleOpt,$footerOpt,$closeBtn;
public function setID($id){
$this->modal_id = $id;
}
public function getID(){
$modal_id = $this->modal_id;
return $modal_id;
}
public function setTitle($title){
$this->header_title = $title;
}
public function getTitle(){
$title = $this->header_title;
return $title;
}
public function setBodyContent($content){
$this->modal_content = $content;
}
public function getBodyContent(){
$modalContent = $this->modal_content;
return $modalContent;
}
public function setFooterContent($footer){
$this->footer = $footer;
}
public function getFooterContent(){
$footerContent = $this->footer;
return $footerContent;
}
public function initiateModal($modal_anim, $headerOpt, $titleOpt, $closeX, $footerOpt, $footerCloseBtn){ ?>
<div class="modal <?php if($modal_anim != 'false'){echo $modal_anim;} ?>" id="<?php echo $this->getID(); ?>" style="z-index: 2;">
<div class='modal-dialog'>
<div class='modal-content'>
<?php
// display if header option is set to true
if ($headerOpt){
?>
<div class="modal-header">
<h4><?php echo $this->getTitle(); ?></h4>
<?php
// display if close button (X) is set to true
if($closeX){
?> <button type="button" class="close" data-dismiss="modal">×</button> <?php } ?>
</div>
<?php } ?>
<div class="modal-body"><?php echo $this->getBodyContent(); ?></div>
<?php if($footerOpt){ ?>
<div class="modal-footer"><?php echo $this->getFooterContent(); ?>
<?php if($footerCloseBtn){ ?>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
<?php } ?>
</div>
<?php } ?>
</div>
</div>
</div>
<?php
}
}
?>
The function I am trying to call within property is as follows; This function is not inside a class. This is present independently in functions.php which I have included in index file.
function getDocNameList() {
global $db;
$getDoc = $db->prepare("SELECT id,doc_name from doctor");
$getDoc->execute();
while($docName = $getDoc->fetch(PDO::FETCH_ASSOC)){
// print the returned rows in options list of <select>
echo "<option value='".$docName['id']."'>".$docName['doc_name']."</option>";
}
}
The initiation of class instance is as follows, Please note where I am calling the function
// create class instance
$rangeModal = new modalBox;
//set the modal id
$rangeModal->setID ("rangeFields");
//set the modal title in header
$rangeModal->setTitle("Select Date Range");
// set the body content
$rangeModal->setBodyContent("
<form method='post' action='expenditure.php'>
<div role='wrapper' class='input-group mb-3'>
<input id='datepicker1' name='exp_date_from' value='From Date' required/>
</div>
<div role='wrapper' class='input-group mb-3'>
<input id='datepicker2' name='exp_date_to' value='To Date' required/>
</div>
<div role='wrapper' class='input-group mb-3'>
<select>" . getDocNameList() . "</select>
</div>
");
//set the footer content
$rangeModal->setFooterContent("
<input type='submit' class='btn btn-success' name='submitRange' />
</form>
");
/*
* @args ---
* modal animation
* modal header (boolean)
* modal title (boolean)
* modal close X (boolean)
* modal footer (boolean)
* modal footer close button (boolean)
*/
// initiate modal
$rangeModal->initiateModal('fade',true,true,true,true,true);
I expect the output of the function to be displayed as .... within the block but instead it gets rendered at the bottom of the page just before tag.
答案 0 :(得分:3)
You echo
it here, so it will be displayed immediately:
echo "<option value='".$docName['id']."'>".$docName['doc_name']."</option>";
So it is not concatenated here, the function does not return
anything:
<select>" . getDocNameList() . "</select>
Build it and return
it instead:
$output = '';
while($docName = $getDoc->fetch(PDO::FETCH_ASSOC)){
$output .= "<option value='".$docName['id']."'>".$docName['doc_name']."</option>";
}
return $output;
Or build an array and join the elements:
while($docName = $getDoc->fetch(PDO::FETCH_ASSOC)){
$output[] = "<option value='".$docName['id']."'>".$docName['doc_name']."</option>";
}
return implode($output);