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?
 
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
 
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Greg AlexanderLead DeveloperCommented:
Did it give you an error?
0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.