[eluser]oldblueday[/eluser]
Hello all,
I have a style question. I'm writing a testing script which has a lot of text describing a disaster scenario interspersed with questions about the scenario. The questions are generated by php/CI.
Is there a best practice on how to approach this? Right now I have a controller with a lot of HTML in it. Alternatively, I can have a view with some PHP in it.
Thanks,
- Rahul
Code: // okay this part gets messy, not sure how to do it
// I got a lot of HTML but I need to mix some PHP in there, too
// should I put it in the controller or the view?
$html_quiz .= '
<div class="case1">
<h2>Lunch at Blue Rice Ski Resort</h2>';
$html_quiz .= image_asset('scenario/blue_rice_trail.png','',array('class' =>'float_center'));
$html_quiz .= ' <p>The Blue Rice trail is crowded by skiers when suddenly, from the north side of Snow Rice, a big thunder is heard and a white cloud rises...</p>
<p>SLAB AVALANCHE!</p>
<p>Not far from the Blue Rice trail, around 50 skiers are having a lunch in a shelter beside the ski run.</p>';
$html_quiz .= image_asset('scenario/avalanche_1.png','',array('class' =>'float_left'));
$html_quiz .= image_asset('scenario/avalanche_2.png','',array('class' =>'float_right'));
$html_quiz .= ' <div class="clearer"></div>
<p>Loose snow avalanche!!!!</p>
<p>The avalanche roared down crushing one of the shelter\'s walls and covering the shelter with up to 3 meters of snow. Many of the people made it out of the shelter on their own, but others had to be assisted.</p>
</div>';
$this->_print_scenario_question($scenario_questions[0], $html_quiz, $ans_key);
$this->_print_scenario_question($scenario_questions[1], $html_quiz, $ans_key);
$this->_print_scenario_question($scenario_questions[2], $html_quiz, $ans_key);
[eluser]mddd[/eluser]
Stuff that you output should be in a view. Don't you agree this code is more legible than what you pasted above?
Code: <div class="case1">
<h2>Lunch at Blue Rice Ski Resort</h2>
<?=image_asset('scenario/blue_rice_trail.png','',array('class' =>'float_center'))?>
<p>The Blue Rice trail is crowded by skiers when suddenly, from the north side of Snow Rice, a big thunder is heard and a white cloud rises...</p>
<p>SLAB AVALANCHE!</p>
<p>Not far from the Blue Rice trail, around 50 skiers are having a lunch in a shelter beside the ski run.</p>
<?=image_asset('scenario/avalanche_1.png','',array('class' =>'float_left')?>
<?=image_asset('scenario/avalanche_2.png','',array('class' =>'float_right'))?>
<div class="clearer"></div>
<p>Loose snow avalanche!!!!</p>
<p>The avalanche roared down crushing one of the shelter's walls and covering the shelter with up to 3 meters of snow. Many of the people made it out of the shelter on their own, but others had to be assisted.</p>
</div>
<?php
$questions->_print_scenario_question($scenario_questions[0], $html_quiz, $ans_key);
$questions->_print_scenario_question($scenario_questions[1], $html_quiz, $ans_key);
$questions->_print_scenario_question($scenario_questions[2], $html_quiz, $ans_key);
?>
[eluser]oldblueday[/eluser]
Absolutely. Thanks, mddd. However I am having a problem, which I'm hoping has a simple solution.
Code: <p>Loose snow avalanche!!!!</p>
<p>The avalanche roared down crushing one of the shelter walls and covering the shelter with up to 3 meters of snow. Many of the people made it out of the shelter on their own, but others had to be assisted.</p>
</div>
<?php
// if I call a bunch of functions via a procedure, it doesn't work
echo print_scenario_question($scenario_questions[0], $ans_key);
// however if I call the functions outright, it works well
// $q = $this->quizmaker->generate_question($scenario_questions[0]);
// echo $q['HTML_question'];
// $ans_key[] = $q['answer_key'];
?>
<?php echo form_close(); ?>
</div>
<?php
/* --------------------------------------------------------------------------------
* PRINT SCENARIO QUESTION
*
* just shorthand to generate the HTML string for the question and update
* the answer key
*
* --------------------------------------------------------------------------------
*/
function print_scenario_question($question, &$ak)
{
$q = $this->quizmaker->generate_question($question);
$html .= "<li>" . $q['HTML_question'] . "</li>";
$ak[] = $q['answer_key'];
return $html;
}
I've narrowed it down to the calling of the $this->quizmaker->generate_question($question) call. Why would it work well inline but not when called from a procedure?
I noticed that you had translated my
Code: <?php
$this->questions->_print_scenario_question($scenario_questions[0], $html_quiz, $ans_key);
$this->questions->_print_scenario_question($scenario_questions[1], $html_quiz, $ans_key);
$this->questions->_print_scenario_question($scenario_questions[2], $html_quiz, $ans_key);
?>
...into...
Code: <?php
$questions->_print_scenario_question($scenario_questions[0], $html_quiz, $ans_key);
$questions->_print_scenario_question($scenario_questions[1], $html_quiz, $ans_key);
$questions->_print_scenario_question($scenario_questions[2], $html_quiz, $ans_key);
?>
Does the $this object not need to be there? I tried removing it with no luck, though.
Thanks-Rahul
[eluser]oldblueday[/eluser]
I solved the problem by doing two things:
1. Moving that procedure into a helper function (I'm not sure this made a difference, but it at least got it out of the view
2. Realized that $this does not exist in my helper function & possibly my in-line function, so I had to get an instance of the codeigniter superobject first... then it worked.
Just putting this up here in case someone has a similar problem:
Code: function print_scenario_question($question, &$ak)
{
$CI =& get_instance();
$CI->load->library('quizmaker');
$q = $CI->quizmaker->generate_question($question);
$HTML_question = "<li>" . $q['HTML_question'] . "</li>";
$ak[] = $q['answer_key'];
return $HTML_question;
}
|