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

Edited by Vivek_13

2
Contributors
2
Replies
22
Views
2 Years
Discussion Span
Last Post by Vivek_13
1

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:

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.