Results 1 to 5 of 5

Thread: PHP Loader and function question...

  1. #1
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Question PHP Loader and function question...

    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 Code:
    <?php include("page2.php"); ?>
    Page 2 *Grouping of If...Else Statements*
    PHP Code:
    <?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...

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    <?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:
    Code:
    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    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...

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You might want to try an array of question/answer objects:
    Code:
    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    k
    thanks man.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •