MySQL php output result

I am constructing a db fed online questionnaire.
Work in progress link http://www.statusconsultinglimited.com/form-test1.php

As you will see each page shows the same question twice as opposed to showing the first and second question respectively. What I need is for the initial page to show the first and second questions then so an and so forth etc..

I have attached the php script that dives the page.
Help appreciated
N

<?php
// connect to db
  $conn = db_connect();
  // find information 
$query = "SELECT * FROM High_Altitude";  
 $result = $conn->query($query);
 $num_results = $result->num_rows;	 
 ;                                               if (!$result) {
     echo'<p>There was a problem retrieving data from the database. Please try again later</p>';
  } else {
 
  }
 for ($i=0; $i <$num_results; $i++) {
     $row = $result->fetch_assoc();
		echo '<div class="page">
				<h2>
					<strong>Hight Altitude Exam</strong>
					<em>Please answer the following questions:</em>
				</h2>';
				echo '<p>There are '; echo $num_results; echo ' question in this section</p>';
			echo'<ul>					
					<li class="required">
								<label>
							<strong>'; echo  $row['Q_No']; echo'</strong> '; echo  $row['Question'];
					 echo'<p>'; echo  $row['Answer_A']; echo'</p>
                            <p>';echo  $row['Answer_B'];echo'</p>
                            <p>';echo  $row['Answer_C'];echo'</p>
                            <p>';echo  $row['Answer_D']; echo'</p>
							<select name="'; echo  $row['Q_No ']; echo'">
								<option value="">-- select answer --</option>
								<option value="';echo  $row['Value_A'];echo'">a</option>
								<option value="';echo  $row['Value_B'];echo'">b</option>
								<option value="';echo  $row['Value_C'];echo'">c</option>
                                <option value="';echo  $row['Value_D'];echo'">b</option>
                            </select>
						</label>
					
						<li class="required">
								<label>
							<strong>'; echo  $row['Q_No']; echo'</strong> '; echo  $row['Question'];
					 echo'<p>'; echo  $row['Answer_A']; echo'</p>
                            <p>';echo  $row['Answer_B'];echo'</p>
                            <p>';echo  $row['Answer_C'];echo'</p>
                            <p>';echo  $row['Answer_D']; echo'</p>
							<select name="'; echo  $row['Q_No ']; echo'">
								<option value="">-- select answer --</option>
								<option value="';echo  $row['Value_A'];echo'">a</option>
								<option value="';echo  $row['Value_B'];echo'">b</option>
								<option value="';echo  $row['Value_C'];echo'">c</option>
                                <option value="';echo  $row['Value_D'];echo'">b</option>
                            </select>
						</label>
					<li  style="clear:both">
						<button type="button" class="next right">Proceed &raquo;</button>
					</li>
				</ul>
			</div>';
			}
  $result->free();
  $conn->close();
			?>

Open in new window

LVL 5
Neil_BradleyWeb UX/UI DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Greg AlexanderLead DeveloperCommented:
Here is a wild shot without testing :)
<?php
// connect to db
  $conn = db_connect();
  // find information 
$query = "SELECT * FROM High_Altitude";  
 $result = $conn->query($query);
 $num_results = $result->num_rows;       
 ;                                               if (!$result) {
     echo'<p>There was a problem retrieving data from the database. Please try again later</p>';
  } else {
 
  }

while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
	$data[] = $row;
}

$count = (count($data)/2);

$q=0;
for($i=0;$i<$count;$i++){
                echo '<div class="page">
                                <h2>
                                        <strong>Hight Altitude Exam</strong>
                                        <em>Please answer the following questions:</em>
                                </h2>';
                                echo '<p>There are '; echo $num_results; echo ' question in this section</p>';
                        echo'<ul>                                       
                                        <li class="required">
                                                                <label>
                                                        <strong>'; echo  $data[$q]['Q_No']; echo'</strong> '; echo  $data[$q]['Question'];
                                         echo'<p>'; echo  $data[$q]['Answer_A']; echo'</p>
                            <p>';echo  $data[$q]['Answer_B'];echo'</p>
                            <p>';echo  $data[$q]['Answer_C'];echo'</p>
                            <p>';echo  $data[$q]['Answer_D']; echo'</p>
                                                        <select name="'; echo  $data[$q]['Q_No ']; echo'">
                                                                <option value="">-- select answer --</option>
                                                                <option value="';echo  $data[$q]['Value_A'];echo'">a</option>
                                                                <option value="';echo  $data[$q]['Value_B'];echo'">b</option>
                                                                <option value="';echo  $data[$q]['Value_C'];echo'">c</option>
                                <option value="';echo  $data[$q]['Value_D'];echo'">b</option>
                            </select>
                                                </label>
                                        
                                                <li class="required">
                                                                <label>
                                                        <strong>'; echo  $data[$q+1]['Q_No']; echo'</strong> '; echo  $data[$q+1]['Question'];
                                         echo'<p>'; echo  $data[$q+1]['Answer_A']; echo'</p>
                            <p>';echo  $data[$q+1]['Answer_B'];echo'</p>
                            <p>';echo  $data[$q+1]['Answer_C'];echo'</p>
                            <p>';echo  $data[$q+1]['Answer_D']; echo'</p>
                                                        <select name="'; echo  $data[$q+1]['Q_No ']; echo'">
                                                                <option value="">-- select answer --</option>
                                                                <option value="';echo  $data[$q+1]['Value_A'];echo'">a</option>
                                                                <option value="';echo  $data[$q+1]['Value_B'];echo'">b</option>
                                                                <option value="';echo  $data[$q+1]['Value_C'];echo'">c</option>
                                <option value="';echo  $data[$q+1]['Value_D'];echo'">b</option>
                            </select>
                                                </label>
                                        <li  style="clear:both">
                                                <button type="button" class="next right">Proceed &raquo;</button>
                                        </li>
                                </ul>
                        </div>';
						
						$q++;$q++;
                        }
  $result->free();
  $conn->close();
                        ?>

Open in new window

0
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
It looked like a winner but no go..
0
Greg AlexanderLead DeveloperCommented:
Did it give you an error?
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
Yes, I loaded up your answer onto this test page so you can see
http://www.statusconsultinglimited.com/form-test3.php
0
Greg AlexanderLead DeveloperCommented:
The problem is that I can't see your db functions so not sure how to test... try a basic connection like the atttached
<?php
	// connect to db
	$conn = db_connect();
	// find information 
	$query = "SELECT * FROM High_Altitude";  
	$result = mysql_query($query);
	$num_results = $result->num_rows;       
	if (!$result) {
		echo'<p>There was a problem retrieving data from the database. Please try again later</p>';
  	}else{}

while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
        $data[] = $row;
}

$count = (count($data)/2);

$q=0;
for($i=0;$i<$count;$i++){
                echo '<div class="page">
                                <h2>
                                        <strong>Hight Altitude Exam</strong>
                                        <em>Please answer the following questions:</em>
                                </h2>';
                                echo '<p>There are '; echo $num_results; echo ' question in this section</p>';
                        echo'<ul>                                       
                                        <li class="required">
                                                                <label>
                                                        <strong>'; echo  $data[$q]['Q_No']; echo'</strong> '; echo  $data[$q]['Question'];
                                         echo'<p>'; echo  $data[$q]['Answer_A']; echo'</p>
                            <p>';echo  $data[$q]['Answer_B'];echo'</p>
                            <p>';echo  $data[$q]['Answer_C'];echo'</p>
                            <p>';echo  $data[$q]['Answer_D']; echo'</p>
                                                        <select name="'; echo  $data[$q]['Q_No ']; echo'">
                                                                <option value="">-- select answer --</option>
                                                                <option value="';echo  $data[$q]['Value_A'];echo'">a</option>
                                                                <option value="';echo  $data[$q]['Value_B'];echo'">b</option>
                                                                <option value="';echo  $data[$q]['Value_C'];echo'">c</option>
                                <option value="';echo  $data[$q]['Value_D'];echo'">b</option>
                            </select>
                                                </label>
                                        
                                                <li class="required">
                                                                <label>
                                                        <strong>'; echo  $data[$q+1]['Q_No']; echo'</strong> '; echo  $data[$q+1]['Question'];
                                         echo'<p>'; echo  $data[$q+1]['Answer_A']; echo'</p>
                            <p>';echo  $data[$q+1]['Answer_B'];echo'</p>
                            <p>';echo  $data[$q+1]['Answer_C'];echo'</p>
                            <p>';echo  $data[$q+1]['Answer_D']; echo'</p>
                                                        <select name="'; echo  $data[$q+1]['Q_No ']; echo'">
                                                                <option value="">-- select answer --</option>
                                                                <option value="';echo  $data[$q+1]['Value_A'];echo'">a</option>
                                                                <option value="';echo  $data[$q+1]['Value_B'];echo'">b</option>
                                                                <option value="';echo  $data[$q+1]['Value_C'];echo'">c</option>
                                <option value="';echo  $data[$q+1]['Value_D'];echo'">b</option>
                            </select>
                                                </label>
                                        <li  style="clear:both">
                                                <button type="button" class="next right">Proceed &raquo;</button>
                                        </li>
                                </ul>
                        </div>';
                                                
                                                $q++;$q++;
                        }
  $result->free();
  $conn->close();
                        ?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
Here is the current bd function.
<?php

function db_connect() {
   $result = new mysqli('localhost', 'demo', 'pass', 'demo');
   if (!$result) {
     throw new Exception('Could not connect to database server');
   } else {
     return $result;
   }
}

?>
If it helps I can change it to suit..
0
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
Thanks for that. I used your solution but with a different connection type. Works perfectly!
Cheers,
N
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.