PHP output echoed out of order - php

I'm having trouble with the output order of a stored procedure (MariaDB). I've tried this several different ways and each echoed result set exhibits the same problem. (I couldn't resolve it using procedural/mysqli, so remade the project in PDO, and now the same issue has arisen.) This is epic, so thanks to everyone who reads.
I'm trying to create a form-letter report for each member in the result set, with an introduction and a table of fees attributed to that member.
So I would expect to see:
Intro for Member 1,
Table for Member 1;
Intro for Member 2,
Table for Member 2; and so on.
The problem is that I am seeing:
Intro for Member 1,
Intro for Member 2,
Table for Member 1,
Intro for Member 3,
Table for Member 2,
Intro for Member 4,
Table for Member 3; and so on.
Called stored procedure:
SELECT * FROM `View_Accounts_report` WHERE `Date` >= parameter_start_date AND `Date` <= parameter_end_dated
Call to the output function within html:
if(isset($_REQUEST['submit'])) {
Accounts_by_date($_REQUEST['start_date'], $_REQUEST['end_date']);
}
php function Accounts_by_date method 1:
function Accounts_by_date($start_date, $end_date) {
require '../lib/pdo.php';
$prev = NULL;
$sql = "CALL accounts_report_dates (:start_date, :end_date)";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':start_date', $start_date);
$stmt->bindValue(':end_date', $end_date);
$stmt->execute();
$results = $stmt->fetchALL(PDO::FETCH_ASSOC);
var_dump($results);
$arrlength = count($results);
for ($row = 0; $row < $arrlength; $row++) {
$person = $results[$row]['Member_ID']; // tracking each row
if ($person != $prev) { // if the current member is different from the previous member, print header and row
// table headers
echo "<br><br>Member: " . $results[$row]['Member_name'] . "<br>";
echo "Dear " . $results[$row]['First_name'] . ",<br>";
echo "Please find your Belmont Pottery Group fees for the period " . $start_date . " to " . $end_date . " below. <br>";
echo "<br><table><tr><th>Total</th><th>Member_ID</th><th>Date</th><th>Transaction</th><th>Amount</th>";
echo "<th>First name</th><th>Email</th><th>Member name</th></tr>";
// table rows
echo "<tr><td>" . $results[$row]['Total'] . "</td>";
echo "<td>" . $results[$row]['Member_ID'] . "</td>";
echo "<td>" . $results[$row]['Date'] . "</td>";
echo "<td>" . $results[$row]['Transaction'] . "</td>";
echo "<td>$" . $results[$row]['Amount'] . "</td>";
echo "<td>" . $results[$row]['First_name'] . "</td>";
echo "<td>" . $results[$row]['Email'] . "</td>";
echo "<td>" . $results[$row]['Member_name'] . "</td></tr>";
$prev = $person;
}else{
echo "<tr><td>" . $results[$row]['Total'] . "</td>";
echo "<td>" . $results[$row]['Member_ID'] . "</td>";
echo "<td>" . $results[$row]['Date'] . "</td>";
echo "<td>" . $results[$row]['Transaction'] . "</td>";
echo "<td>$" . $results[$row]['Amount'] . "</td>";
echo "<td>" . $results[$row]['First_name'] . "</td>";
echo "<td>" . $results[$row]['Email'] . "</td>";
echo "<td>" . $results[$row]['Member_name'] . "</td></tr>";
$prev = $person;
}
}
}
Excerpt of var_dump from php function method 1:
array(15) { [0]=> array(8) { ["Total"]=> string(5) "36.00" ["Member_ID"]=> int(31) ["Date"]=> string(10) "2017-11-06" ["Transaction"]=> string(4) "Clay" ["Amount"]=> string(5) "18.00" ["First_name"]=> string(6) "Dummy " ["Email"]=> string(25) "dummy#dummy.com" ["Member_name"]=> string(12) "Dummy Nine " } [1]=> array(8) { ["Total"]=> string(5) "36.00" ["Member_ID"]=> int(31) ["Date"]=> string(10) "2017-11-13" ["Transaction"]=> string(4) "Clay" ["Amount"]=> string(5) "18.00" ["First_name"]=> string(6) "Dummy " ["Email"]=> string(25) "dummy#dummy.com" ["Member_name"]=> string(12) "Dummy Nine " } [2]=> array(8) { ["Total"]=> string(5) "40.20" ["Member_ID"]=> int(29) ["Date"]=> string(10) "2017-11-05" ["Transaction"]=> string(6) "Firing" ["Amount"]=> string(4) "3.70" ["First_name"]=> string(6) "Dummy " ["Email"]=> string(25) "dummy#dummy.com" ["Member_name"]=> string(13) "Dummy Seven " } [3]=> array(8) { ["Total"]=> string(5) "40.20" ["Member_ID"]=> int(29) ["Date"]=> string(10) "2017-11-05" ["Transaction"]=> string(10) "Attendance" ["Amount"]=> string(4) "4.00" ["First_name"]=> string(6) "Dummy " ["Email"]=> string(25) "dummy#dummy.com" ["Member_name"]=> string(13) "Dummy Seven " }
php function Accounts_by_date method 2:
function Accounts_by_date($start_date, $end_date) {
require '../lib/pdo.php';
$prev = NULL;
$sql = "CALL accounts_report_dates (:start_date, :end_date)";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':start_date', $start_date);
$stmt->bindValue(':end_date', $end_date);
$stmt->execute();
$stmt->bindColumn('Total', $total);
$stmt->bindColumn('Member_ID', $id);
$stmt->bindColumn('Date', $date);
$stmt->bindColumn('Transaction', $transaction);
$stmt->bindColumn('Amount', $amount);
$stmt->bindColumn('First_name', $fname);
$stmt->bindColumn('Email', $email);
$stmt->bindColumn('Member_name', $name);
while ($row = $stmt->fetch(PDO::FETCH_BOUND)) {
$person = $id;
if ($person != $prev) { // if the current member is different from the previous member, print header and row
// table headers
echo "<br><br>Member: " . $name . "<br>";
echo "Dear " . $fname . ",<br>";
echo "Please find your Belmont Pottery Group fees for the period " . $start_date . " to " . $end_date . " below. <br><br>";
echo "The total of your invoice is: $" . $total . "<br>";
echo "<br><table><tr><th>Total</th><th>Member_ID</th><th>Date</th><th>Transaction</th><th>Amount</th>";
echo "<th>First name</th><th>Email</th><th>Member name</th></tr>";
// table rows
echo "<tr><td>" . $total . "</td>";
echo "<td>" . $id . "</td>";
echo "<td>" . $date . "</td>";
echo "<td>" . $transaction . "</td>";
echo "<td>$" . $amount . "</td>";
echo "<td>" . $fname . "</td>";
echo "<td>" . $email . "</td>";
echo "<td>" . $name . "</td></tr>";
$prev = $person;
}else{
echo "<tr><td>" . $total . "</td>";
echo "<td>" . $id . "</td>";
echo "<td>" . $date . "</td>";
echo "<td>" . $transaction . "</td>";
echo "<td>$" . $amount . "</td>";
echo "<td>" . $fname . "</td>";
echo "<td>" . $email . "</td>";
echo "<td>" . $name . "</td></tr>";
$prev = $person;
}
}
}
var_dump of php function method 2 returns true.
Output (both methods):
Member: Dummy Nine
Dear Dummy ,
Please find your BPG fees for the period 2017-11-01 to 2017-11-30 below.
Member: Dummy Seven
Dear Dummy ,
Please find your BPG fees for the period 2017-11-01 to 2017-11-30 below.
Total Member_ID Date Transaction Amount First name Email Member name
36.00 31 2017-11-06 Clay $18.00 Dummy dummy#dummy.com Dummy Nine
36.00 31 2017-11-13 Clay $18.00 Dummy dummy#dummy.com Dummy Nine
Member: Dummy Six
Dear Dummy ,
Please find your BPG fees for the period 2017-11-01 to 2017-11-30 below.
Total Member_ID Date Transaction Amount First name Email Member name
40.20 29 2017-11-05 Firing $3.70 Dummy dummy#dummy.com Dummy Seven
40.20 29 2017-11-05 Attendance $4.00 Dummy dummy#dummy.com Dummy Seven
40.20 29 2017-11-19 Firing $11.60 Dummy dummy#dummy.com Dummy Seven
Member: Dummy Three
Dear Dummy ,
Please find your BPG fees for the period 2017-11-01 to 2017-11-30 below.
Total Member_ID Date Transaction Amount First name Email Member name
26.60 28 2017-11-02 Attendance $4.00 Dummy dummy#dummy.com Dummy Six
26.60 28 2017-11-06 Attendance $4.00 Dummy dummy#dummy.com Dummy Six
26.60 28 2017-11-03 Firing $17.00 Dummy dummy#dummy.com Dummy Six
26.60 28 2017-11-06 Firing $1.60 Dummy dummy#dummy.com Dummy Six

I would start by making sure you are closing your tables, you can add the code below:
if ($person != $prev) {
if ($row > 0) echo "</table>"; /*added line of code*/
That should fix some issues with multiple table header cells in the same table. The multiple 's in one table are pushing your intro's up to the top.
Don't forget to add a
</table>
to the end of your code as well after the while loop is done for proper html

Related

PHP display image based on positive or negative number

I need to display:
/images/image1.jmp if the number is positive
or
/images/image2.jpg if the number is negative
or
/images/image3 if the number is 0.
$stmt = sqlsrv_query($conn,$sql);
echo "<table border='1'><tr><th>Offense</th><th>Previous Date Range</th><th>Current Date Range</th><th>Difference</th><th>Percentage Difference</><th>Up or down image in this column</></tr>";
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>" . $row['Offense']. "</td>";
echo "<td>" . $row['PreviousDateRange']."</td>";
echo "<td>" . $row['DateRange']."</td>";
echo "<td>" . $row['difference1']."</td>";
echo "<td>" . $row['percentchange']."</td>";
echo "<td>" . $row['']. "</td>";
}
echo "</table>";
?>
I found this code and have tried different ways if incorporating it in the echo "<td>" . $row['']. "</td> but not having any luck.
I get the code but can not manipulate it to fit what it needs to do. I'm sure it's a simple solution. Just frustrated.
switch ($myNumber) {
case 0:
echo "Zero is not a valid value.";
break;
case $myNumber < 0:
echo "Negative numbers are not allowed.";
break;
default:
echo "Great! Ready to make calculations.";
break;
}
Thanks for the help guys. Got the answer that worked.
I think this is a more readable solution.
I use an array to hold the links and use a calculation to see if it is negative or positive.
$img = ["-1" => "/images/image2.jpg",
"0" => "/images/image3.jpg",
"1" => "/images/image1.jpg"];
$number = 0;
Echo ($number == 0 ? $img[$number] : $img[$number/abs($number)]);
https://3v4l.org/JstZl
If the number is positive the calculation will be 15/15 => 1.
If the number is negative -10/10 => -1.
Edit:
$stmt = sqlsrv_query($conn,$sql);
echo "<table border='1'><tr><th>Offense</th><th>Previous Date Range</th><th>Current Date Range</th><th>Difference</th><th>Percentage Difference</><th>Up or down image in this column</></tr>";
$img = ["-1" => "/images/image2.jpg",
"0" => "/images/image3.jpg",
"1" => "/images/image1.jpg"];
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>" . $row['Offense']. "</td>";
echo "<td>" . $row['PreviousDateRange']."</td>";
echo "<td>" . $row['DateRange']."</td>";
echo "<td>" . $row['difference1']."</td>";
echo "<td>" . $row['percentchange']."</td>";
Echo '<td><img src="' . ($row['percentchange'] == 0 ? $img[$row['percentchange']] : $img[$row['percentchange']/abs($row['percentchange'])]) . '"></td>';
}
echo "</table>";
You didn't show us where $myNumber comes from so if the variable name is different just modify it in the code below.
echo '<td>/images/image' . ($myNumber == 0 ? '3' : ($myNumber >= 1 ? '1' : '2')) . '.jpg</td>';
Try:
if($myNumber < 0) {
//show /images/image2.jpg
}
if($myNumber == 0) {
//show /images/image3
}
if($myNumber > 0) {
//show /images/image1.jmp
}
In your code:
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>" . $row['Offense']. "</td>";
echo "<td>" . $row['PreviousDateRange']."</td>";
echo "<td>" . $row['DateRange']."</td>";
echo "<td>" . $row['difference1']."</td>";
echo "<td>" . $row['percentchange']."</td>";
echo "<td>";
if($row['percentchange'] < 0) {
//show /images/image2.jpg
}
if($row['percentchange'] == 0) {
//show /images/image3
}
if($row['percentchange'] > 0) {
//show /images/image1.jmp
}
echo "</td>";
}
This is assuming, based off of your other comments, that $row['percentchange'] is the number in particular that you care about checking against.

Layout issue after adding a new array to table

I added a new array to an existing array, but the layout is broken.
The code to output the array is:
for ($i=1; $i<=$comp_value_count; $i++) {
$compPro = 'pro'.$i;
$attrCount = $result[$compPro][11];//A count of how many options there are per product
$compDescription .= '<td>' . $result[$compPro][2] . '</td>';
$compModel .= '<td>' . $result[$compPro][3] . '</td>';
$compWeight .= '<td>' . $result[$compPro][4] . '</td>';
$compQuantity .= '<td>' . $result[$compPro][5] . '</td>';
$compManufacturer .= '<td>' . $result[$compPro][7] . '</td>';
$compattr1 .= '<td>';
for ($c=0; $c<=$attrCount; $c++){
$compattr2 .= $result[$compPro][8][$c] . $result[$compPro][9][$c] . "\n";
}
$compattr3 .= '</td>';
}
// create the display
echo '<tr class="rowEven"><th>' . COMPARE_QUANTITY . '</th>' . $compQuantity . '</tr>';
echo '<tr class="rowOdd"> <th>' . COMPARE_MODEL . '</th>' . $compModel . '</tr>';
echo '<tr class="rowEven"><th>' . COMPARE_WEIGHT . '</th>' . $compWeight . '</tr>';
echo '<tr class="rowOdd"> <th>' . COMPARE_MANUFACTURER . '</th>' . $compManufacturer . '</tr>';
echo '<tr class="rowEven"><th>' . COMPARE_DESC . '</th>' . $compDescription . '</tr>';
echo '<tr class="rowOdd"> <th>' . COMPARE_OPTNAME . '</th>' . $compattr1 . $compattr2 . $compattr3 . '</tr>';
echo '</table>';
}
A fiddle showing the layout this code generates is https://jsfiddle.net/uzmk61v7/2/
And another showing what I wanted it to look like is here https://jsfiddle.net/85wztu3z/1/
var_dump($result) output is below
array(2) {
["pro1"]=> array(12) {
[0]=> string(191) "01 button stainless steel audio panel - surface"
[1]=> string(612) "01 button stainless steel audio panel - surface"
[2]=> string(144) "5101S - 1 button audio door entry phone intercom panel SRS surface mounted, vandal resistant 1 way traditional audio, BS316 stainless steel,..."
[3]=> string(5) "5101S"
[4]=> string(1) "0"
[5]=> string(1) "1"
[6]=> string(171) "£147.74 Inc VAT £123.12 Ex VAT"
[7]=> string(3) "SRS"
[8]=> array(4) {
[0]=> string(12) "Call Buttons"
[1]=> string(8) "Mounting"
[2]=> string(6) "Finish"
[3]=> string(5) "Range"
}
[9]=> array(4) {
[0]=> string(20) "1 call button"
[1]=> string(20) "Surface mount"
[2]=> string(35) "Stainless Steel VR (brushed)"
[3]=> string(15) "SRS 5000"
}
[10]=> string(73) "remove"
[11]=> string(1) "4"
}
["pro2"]=> array(12) {
[0]=> string(200) "00 way VR brass video panel, size D"
[1]=> string(576) "00 way VR brass video panel, size D"
[2]=> string(148) "6600 - 0 button video door entry phone intercom panel Size D SRS vandal resistant 0 way traditional video, polished brass, engravable, door entry..."
[3]=> string(4) "6600"
[4]=> string(1) "0"
[5]=> string(1) "0"
[6]=> string(171) "£155.95 Inc VAT £129.96 Ex VAT"
[7]=> string(3) "SRS"
[8]=> array(4) {
[0]=> string(8) "Mounting"
[1]=> string(6) "Finish"
[2]=> string(5) "Range"
[3]=> string(7) "Cabling"
}
[9]=> array(4) {
[0]=> string(18) "Flush mount"
[1]=> string(21) "Polished brass"
[2]=> string(15) "SRS 4000"
[3]=> string(34) "SRS Video C + 6 + n cabling"
}
[10]=> string(73) "remove" [11]=> string(1) "4"
}
}
Any suggestions on how I can fix this?
Just add one more <td></td>
for ($i=1; $i<=$comp_value_count; $i++) {
$compPro = 'pro'.$i;
$attrCount = $result[$compPro][11];//A count of how many options there are per product
$compDescription .= '<td>' . $result[$compPro][2] . '</td>';
$compModel .= '<td>' . $result[$compPro][3] . '</td>';
$compWeight .= '<td>' . $result[$compPro][4] . '</td>';
$compQuantity .= '<td>' . $result[$compPro][5] . '</td>';
$compManufacturer .= '<td>' . $result[$compPro][7] . '</td>';
$compattr1 .= '<td>';
for ($c=0; $c<=$attrCount; $c++){
$compattr1 .= $result[$compPro][8][$c] . "\n";
}
$compattr1 .= '</td>';
$compattr2 .= '<td>';
for ($c=0; $c<=$attrCount; $c++){
$compattr2 .= $result[$compPro][9][$c] . "\n";
}
$compattr2 .= '</td>';
}
// create the display
echo '<tr class="rowEven"><th>' . COMPARE_QUANTITY . '</th>' . $compQuantity . '</tr>';
echo '<tr class="rowOdd"> <th>' . COMPARE_MODEL . '</th>' . $compModel . '</tr>';
echo '<tr class="rowEven"><th>' . COMPARE_WEIGHT . '</th>' . $compWeight . '</tr>';
echo '<tr class="rowOdd"> <th>' . COMPARE_MANUFACTURER . '</th>' . $compManufacturer . '</tr>';
echo '<tr class="rowEven"><th>' . COMPARE_DESC . '</th>' . $compDescription . '</tr>';
echo '<tr class="rowOdd"> <th>' . COMPARE_OPTNAME . '</th>' . $compattr1 . $compattr2 . '</tr>';
echo '</table>';

Retrive information from list associative array in php

Hy,
how I retrieve information from an associative array like list in php.
var_dump shows something like this:
array(2) { [1]=> string(1) "1"
[2]=> string(1) "2" }
array(2) { [1]=> string(15) "gica_craioveanu"
[2]=> string(14) "alexandra_minu" }
array(2) { [0]=> string(10) "craiovaMAX"
[1]=> string(10) "alexMinu10" }
First array containd the id's, second the user name and third the passwords.
My attempt:
foreach ($row as $i => $id,$user_name,$user_pass) {
echo "<tr>\n" .
" <td>".var_dump($id)."</td>\n".
" <td>".var_dump($user_name)."</td>\n".
" <td>".var_dump($user_pass)."</td>\n".
" </tr>\n"
}
one of php file contains the script to construct the list:
while($row = mysqli_fetch_row($result))
{
/*$id[]=$row['user_id'];
$user_name[]=$row['user_name'];
$user_pass[]=$row['user_pass'];
$i=$i+1;*/
list($id[$i],$user_name[$i],$user_pass[$i++])=$row;
}
/*for($no=0; $no<$i; $no++){
echo $row[$i]."/".$row[$i]."/";
}*/
include 'user.html.php';
}
?>
and the user.html.php
to view the rows:
<?php
// while (list($id, $user_name, $user_pass)= each($row)) {
foreach($row as $i => $id,$user_name,$user_pass){
echo " <tr>\n" .
" <td>".$id."</td>\n".
" <td>".$user_name."</td>\n".
" <td>".$user_pass."</td>\n".
" </tr>\n";
/*echo "<tr>\n" .
" <td>".var_dump($id)."</td>\n".
" <td>".var_dump($user_name)."</td>\n".
" <td>".var_dump($user_pass)."</td>\n".
" </tr>\n"
*/
/*for($no=0; $no<$i; $no++){
echo "<tr>".
"<td>".$row[$i]."</td>".
"<td>".$row[$i]."</td>".
"<td>".$row[$i]."</td>".
"</tr>";*/
}
?>
while($row = mysqli_fetch_row($result)) {
list($id[$i],$user_name[$i],$user_pass[$i++])=$row;
}
...
for($i = 1; $i < count($id) + 1; $i++) {
echo " <tr>\n" .
" <td>".$id[$i]."</td>\n".
" <td>".$user_name[$i]."</td>\n".
" <td>".$user_pass[($i - 1)]."</td>\n".
" </tr>\n";
}
$id = array("1", "2");
$user_name = array("gica_craioveanu", "alexandra_minu");
$user_pass = array("craiovaMAX", "alexMinu10");
We presume that 3 arrays are always the same size :
// You make a loop on first array
for ( $i = 0 ; $i < count($id) ; $i++ )
{
// You use index $i to access to 3 arrays.
echo 'ID: '.$id[$i].'<br />';
echo 'Name: '.$user_name[$i].'<br />';
echo 'Password: '.$user_pass[$i].'<br />';
}
for($no=1; $no<$i+1; $no++)
{
echo "<tr>".
"<td>".$id[$no]."</td>".
"<td>".$user_name[$no]."</td>".
"<td>".$user_pass[($no-1)]."</td>".
"</tr>";
}
Inspired by Typoheads

Try to put a title on a table column in PHP

I can't seem to add the title Bonus % to the 5th column, what am I doing wrong?
Here is my code, I have tried to add the title the way I did for the other columns but it won't work.
<?php
for($i=0; $i<33; $i++) {
$staff[$i][0] = $i + 1;
$staff[$i][1] = rand(100000, 600000);
$staff[$i][2] = rand(14000, 40000);
$staff[$i][4] = rand(1,7);
if (rand(14000, 40000) >= 20000)
$staff[$i][3] = "yes";
else
$staff[$i][3] = "no";
}
print "<table border=1><tr><th>" . "Number of Staff" . "</th><th>" . "Staff Number" . "</th><th>" . "Salary" . "</th><th>" . "Bonus?" . "</th></tr>". "Bonus %" . "</th></tr>";
for($j=0; $j<33; $j++) {
print "<tr>";
for ($k=0; $k<5; $k++) {
print "<td>" . $staff[$j][$k] . "</td>";
}
print "</tr>";
}
print "</table>"
?>
You table header th formatting is not correct. Can try like below. Also missing a terminating semi clone ; at last line print "</table>".
print "<table border='1'>
<tr>
<th>" . "Number of Staff" . "</th>
<th>" . "Staff Number" . "</th>
<th>" . "Salary" . "</th>
<th>" . "Bonus?" . "</th>
<th>". "Bonus %" . "</th>
</tr>
";
for($j=0; $j<33; $j++) {
print "<tr>";
for ($k=0; $k<5; $k++) {
print "<td>" . $staff[$j][$k] . "</td>";
}
print "</tr>";
}
print "</table>";
Replace
"</th></tr>". "Bonus %" . "</th></tr>"
By
"</th><th>". "Bonus %" . "</th></tr>"
You close tr before end of line

How do I "loop" through JSON array inside foreach loop?

I have a problem, were I need to loop through an array inside a "foreach-loop". The PHP code below is per now looping through the JSON and looking for ["Question"], and looping these correctly.
But these questions have different amounts of ["Answers"]. Also this JSON is made dynamically so I don't know from before how many questions/answers there'll be.
So what I need is the ["Answers"] should be looped withing the <li> inside each question.
Would be really greatful for some expertise here!
JSON
["Questions"]=>
array(2) {
[0]=>
object(stdClass)#13 (2) {
["Question"]=>
string(30) "My first questions is this...."
["Answers"]=>
array(2) {
[0]=>
object(stdClass)#14 (2) {
["Answers"]=>
string(18) "I like this answer"
["Correct"]=>
bool(true)
}
}
}
[1]=>
object(stdClass)#16 (2) {
["Question"]=>
string(22) "Another funny question"
["Answers"]=>
array(2) {
[0]=>
object(stdClass)#17 (2) {
["Answers"]=>
string(9) "Or is it?"
["Correct"]=>
bool(false)
}
[1]=>
object(stdClass)#18 (2) {
["Answers"]=>
string(10) "Yes it is!"
["Correct"]=>
bool(true)
}
}
}
}
PHP
$questions = array();
$i = 0;
foreach($question as $key => $items) {
$i++;
if ($i>1) {$hide=' hide';}
$questions[] = "
<div class='question-holder" . $hide . "' id='q" . $i . "'>
<h2>" . $items->Question . "</h2>
<ul class='answers' id='quiz-answers" . $i . "'>
<li>
<input tabindex='1' type='radio' name='txtAnswer" . $i . "' id='txtAlt1' value='sdsds'>
<label for='txtAlt1'><span>" . $items->Answers[0]->Answers . "</span></label>
</li>
</ul>
</div>";
}
Use a nested loop for the answers.
$tab = 0;
foreach ($question as $items) {
$i++;
$j = 0;
if ($i > 1) { $hide = ' hide'; }
$this_q = "<div class='question-holder" . $hide . "' id='q" . $i . "'>
<h2>" . $items->Question . "</h2>
<ul class='answers' id='quiz-answers" . $i . "'>
";
foreach ($items->Answers as $ans) {
$tab++;
$j++;
$qa = "$i-$j";
$this_q .= "<li>
<input tabindex='" . $tab . "' type='radio' name='txtAnswer" . $qa . "' id='txtAlt" . $qa . "' value='sdsds'>
<label for='txtAlt" . $qa . "'><span>" . $ans->Answers . "</span></label>
</li>
";
}
$this_q .= "</ul>
</div>";
$questions[] = $this_q;
}

Categories