<?php
            $xml = simplexml_load_file('/exam/'.$file);
            foreach($xml->main as $main)
            {
                if($main['select'] == "selected")
                { ?>
                <div>
                <textarea rows="2"  class="span11"><?php echo $main->question ?></textarea>
                    <div class="row" style="padding-left:3%;margin-bottom:1%;">
                    <ul>
                    <?php 
                    foreach($main->ans as $ans)
                    { ?>
                        <li style="float:left; margin-left:1%;" ><input type="radio"  name="optionsRadios"  class="rdChk"  style="margin-top:-2%">&nbsp;<span><?php echo $ans ?></span></li>
                <?php    }
                    ?>                                                                  
                    </ul>
                    </div >
            </div>
            <?php    }
            }

             ?>



myxml file is
<?xml version="1.0"?>
<root>
  <main id="283" select="selected">
    <question>qw</question>
    <ans right="true">qwq</ans>
    <ans>wqw</ans>
    <ans>qwq</ans>
    <ans>wqwqw</ans>
  </main>
  <main id="56" select="selected">
    <question>qwewqe</question>
    <ans right="true">qwe</ans>
    <ans>qweqwe</ans>
  </main>
  .........

  i want to see one quetion in one page...

Recommended Answers

All 2 Replies

You could use xpath() to match only the selected questions:

$xpath = $xml->xpath('//main[@select="selected"]');

So you can remove the IF statement:

if($main['select'] == "selected")

Then count to get the total and create a simple pagination system:

$count = count($xpath);
$page  = isset($_GET['page']) && $_GET['page'] > 0 ? $_GET['page'] : 1;
$numbr = $page - 1;

# load the current question
$main  = $xpath[$numbr];

Once you have $main, you don't need this loop:

foreach($xml->main as $main)

Then create the page links:

<ul class="nav">
    <?php
    for($i = 1; $i <= $count; $i++)
    {
        if($page == $i)
            echo "<li><a class=\"current\" href=\"question.php?page={$i}\">{$i}</a></li>";

        else
            echo "<li><a href=\"question.php?page={$i}\">{$i}</a></li>";
    }
    ?>
</ul>

And to your form code simply add the question id, which is the attribute id in the main object:

<input type="hidden" name="questionID" value="<?php echo $main->attributes()->id; ?>" />

Docs:

Tnxs Bro..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.