Log in

View Full Version : PHP Loader and function question...



Rockonmetal
09-17-2007, 08:51 PM
Ok I've asked this before but is there such thing as a php loader? I've asked this before but I bring it up again because last time people said why... and i'm not asking why I'm asking HOW!
Why i need it:
I have come up with this script and its about 20mb *all php if...else statements... divided with php includes...* if you don't understand i got an example below of how i've made this 20mb...
Page 1 *Main page...*

<?php include("page2.php"); ?>
Page 2 *Grouping of If...Else Statements*

<?php
$a=1;
while($a<=101)
{
echo "<?php include('answers/". $a .".php'); ?>"
$a++;
}
$b=1;
while($b<=100)
{
echo "<?php include('questions/". $b .".php'); ?>"
$b++;
}
?>

Then those files in the includes contain the if...else statements...
What I need something that will load either all the include on page 1 *main page*...
I then was wondering if that there was something that would perform a function when the form on that page is submitted...

Thanks if you have any questions let me know...

Twey
09-18-2007, 07:35 AM
<?php
$a=1;
while($a<=101)
{
echo "<?php include('answers/". $a .".php'); ?>"
$a++;
}Here, you have an initialisation ($a=1), a condition ($a<=101), and a step ($a++). This is what for loops are for:
for($a = 1; $a <= 101; $a++)
include("answers/$a.php");
I've asked this before but I bring it up again because last time people said why... and i'm not asking why I'm asking HOW!But the only responsible response to a question like this is indeed to ask "why," because you're evidently doing something hideously wrong elsewhere if you have to do this.

Rockonmetal
09-18-2007, 08:07 PM
But is it possible??? I have 20mb of code because I have a questions and answers form on my site, and I have 20mb of questions and answers...

Twey
09-18-2007, 08:33 PM
You might want to try an array of question/answer objects:
class QuizQuestion {
var $question;
var $answers;
var $right_answer;
var $id;

function QuizQuestion($question, $answers, $right_answer = 0) {
static $id = 0;

$this->id = $id++;
$this->question = $question;
$this->answers = $answers;
$this->right_answer = $right_answer;
}

function to_form_elements() {
ob_start();
?>
<h4><?php echo $this->question; ?></h4>
<ol>
<?php foreach($this->answers as $key => $answer) { ?>
<li>
<label>
<input
name="answer<?php echo $this->id; ?>"
type="radio" value="<?php echo $key; ?>">
<?php echo $answer; ?>
</label>
</li>
<?php } ?>
</ol>
<?php
return ob_get_clean();
}
}

$questions = array(
new QuizQuestion('What is the right answer?',
array('A right answer',
'A wrong answer',
'Another wrong answer')),
new QuizQuestion('Ah, but how about this one?',
array('A wrong answer',
'Another wrong answer',
'A right answer',
'Yet another wrong answer'),
2));If I were you, I'd then have arrays like that of perhaps fifteen questions per file, or better still, a database. A database would solve most of your data-management problems here.

Rockonmetal
09-18-2007, 08:36 PM
k
thanks man.