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>';
Related
I have an array list that I wish to implode as I want to display each array with indexing to target that element. I'm trying but it is not displaying correctly. What am I doing wrong?
<?php
/*
var_dump($names); // below
array(4) {
[0]=>
string(22) "David Kaul"
[1]=>
string(23) "Julius Kaul"
[2]=>
string(22) "Robert Kaul"
[3]=>
string(22) "Juohn Kaul"
} */
for ($i = 0; $i <= 10; $i+=1) {
echo '<li class="item-' . $i . '">' . implode('</li><li class="item-'.$i.'">', $names) . '</li>';
}
?>
I think you just want a simple foreach over the $names array:
$names = array("David Kaul","Julius Kaul","Robert Kaul","Juohn Kaul");
foreach ($names as $i => $name) {
echo '<li class="item-' . $i . '">' . $name. '</li>' . "\n";
}
Output:
<li class="item-0">David Kaul</li>
<li class="item-1">Julius Kaul</li>
<li class="item-2">Robert Kaul</li>
<li class="item-3">Juohn Kaul</li>
Demo on 3v4l.org
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
I have an array of arrays and accessing the data is fine, but when I try to create separate tables based on a key, which is set to the numeric month that the data pulled from the database falls under.
I have:
array(1) {
[5]=>
array(12) {
["ID"]=>
string(5) "1001"
["created_timestamp"]=>
string(26) "2015-06-11 19:10:00"
["firstname"]=>
string(4) "John"
["lastname"]=>
string(3) "Doe"
}
}
array(1) {
[5]=>
array(12) {
["ID"]=>
string(5) "1002"
["created_timestamp"]=>
string(26) "2015-06-12 19:10:00"
["firstname"]=>
string(4) "Jane"
["lastname"]=>
string(3) "Doe"
}
}
array(1) {
[6]=>
array(12) {
["ID"]=>
string(5) "1003"
["created_timestamp"]=>
string(26) "2015-07-16 19:20:00"
["firstname"]=>
string(4) "John"
["lastname"]=>
string(4) "John"
}
}
The [5] Represents the current month -1. What I want to do is create a table and headers but only once, then populate the table with first and last name.
foreach($m as $key => $value)
{
if(key($m) != $currentKey)
{
$currentKey = key($m);
}
if($key == date("n", strtotime($value['created_timestamp']))-1);
{
for($i=$currentKey; $i<=$lastkey; $i++) {
echo "<br>".count($key);
$table .= '<table id=' . date("F-Y", strtotime($m[$currentKey]['created_timestamp'])) . ' class="tblclass">
<thead><tr><th>Month</th><th>ID</th><th>Customer</th></tr></thead>';
$table .= '<tbody>';
$table .= '<tr><td>'.date("F Y", strtotime($m[$currentKey]['created_timestamp'])).'</td><td>' . $value['ID'] . '</td><td>' . $value['lastname'] . ' ' . $value['firstname'] . '</td></tr>';
$table .= '</tbody>';
$table .= '</table>';
}
}
}
Whenever I run the code it creates a table for each separate array. I want to create one table for each separate month and populate it with anything with the same key. I feel like I'm missing something though. Any help would be greatly appreciated!
A dirty solution. Since info is lacking
Here are a few assumptions:
- Array is sorted in ASC order
- There is only one item in each array [ID, Name, Timestamp]
- IF there are more items, then you can just add a loop in there
- $m is the multi-dimensional array.
NOTE: Not tested.
$currentKey = 0;
$count= 0;
$lastKey = count($m) - 1;
foreach($m as $key => $value) {
if(key($m) != $currentKey) {
$currentKey = key($m);
if($count== 0) {
echo "<br>".count($key);
$table .= '<table id=' . date("F-Y", strtotime($m[$currentKey]['created_timestamp'])) . ' class="tblclass">
<thead><tr><th>Month</th><th>ID</th><th>Customer</th></tr></thead>';
$table .= '<tbody>';
} else {
$table .= '</tbody>';
$table .= '</table>';
$table .= '<table id=' . date("F-Y", strtotime($m[$currentKey]['created_timestamp'])) . ' class="tblclass">
<thead><tr><th>Month</th><th>ID</th><th>Customer</th></tr></thead>';
$table .= '<tbody>';
}
}
if($key == date("n", strtotime($value['created_timestamp']))-1) {
$table .= '<tr><td>'.date("F Y", strtotime($value['created_timestamp'])).'</td><td>' . $value['ID'] . '</td><td>' . $value['lastname'] . ' ' . $value['firstname'] . '</td></tr>';
}
if($lastKey == $count) {
$table .= '</tbody>';
$table .= '</table>';
}
$count++;
}
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;
}
i got this array.
array(24) {
[0]=> array(3) {
["id"]=> string(1) "1"
["category"]=> string(5) "Alles"
["description"]=> string(0) ""
}
[1]=> array(3) {
["id"]=> string(2) "11"
["category"]=> string(6) "Cinema"
["description"]=> string(0) ""
}
[2]=> array(3) {
["id"]=> string(1) "8"
["category"]=> string(8) "Computer"
["description"]=> string(0) ""
}
}
And i dont know how to show this informations:
I tried with:
for ($x = 0; $x < sizeof($array); ++$x)
{
echo "key: ".key($array)."<br>value: ".current($array)."<br>";
next($array);
}
The Key value is now right, but the current-value is also an array :(
I hope someone can help.
Should be something like
foreach($array as $key => $value)
{
echo "key: " . ' '.$key .', '.
"category: " . ' ' . $value['category'].',
"description: " . ' ' . $value['description'];
}
Use nested foreach loops i.e.
foreach ($array as $key => $value){
echo "[ ".$key." ]<br/>";
foreach ($value as $innerKey => $innerValue){
echo $innerKey." => ".$innerValue."<br/>";
}
}
You could iterate over this array as follows:
foreach($sourceArray as $key => $value) {
echo 'Item: ' . $value . '<br />';
foreach($value as $subKey => $subValue) {
echo $subKey.' -> '.$subValue.'<br />';
}
}