I am creating an Excel download code, in which I am merging the TH cells and looping through TR to put data under specific merged TH.
But all of data is reflecting on first TH row. Below is code:
$K=0;
while($ROW = $RESULT->fetch_assoc() )
{
if($ROW['type'] == $_GET['separate_id'])
{
$DATE[] = $ROW['end_time'];
$meta_values[] = $ROW['meta_values'];
$body = '
<table style="border:1px solid #000; border-collapse:collapse;">
<thead style="border:1px solid #000; padding:8px;width:150px; height:25px;font-size:15px">
';
foreach($DATE as $DATES)
{
$DATESVIEW = explode('T',$DATES);
$body .=' <th colspan="2">'.$DATESVIEW[0].'</th> ';
}
$body .= '
</thead>
';
foreach($meta_values as $VALS)
{
$META_VALS = explode(' | ',$VALS);
foreach($META_VALS as $VALUEs)
{
$REs = explode(' - ', $VALUEs);
$body .= '
<tbody>
<tr style="border:1px solid #000; padding:8px;width:150px; height:25px;font- size:15px">
<td style="border:1px solid #000; padding:8px; width:150px; height:25px;font-size:15px">'.$REs[0].'</td>
<td style="border:1px solid #000; padding:8px; width:150px; height:25px;font-size:15px">'.$REs[1].'</td>
</tr>
</tbody>
';
}
}
}
$K++;
}
$body .= '
</table>
';
Related
I have a question regarding hiding empty rows in my table.
I want to hide an entire row in the case a field says N/A which means the field is empty.
Example:
In the event that the field is empty or N/A, I want to hide the entire row. I will also welcome javascript solutions.
<tbody style="
white-space: nowrap;
">
<?php
foreach($student_subject_list AS $SSL)
{
$subject_name = $SSL["name"];
if(in_array($subject_name, array_keys($result)))
{
$total_score = 0;
$avg = count($result[$subject_name]);
$test_results = "";
$i = 1;
foreach($result[$subject_name] AS $SN)
{
if($i == 1)
{
$ie = "1<sup>st</sup>";
}
elseif($i == 2)
{
$ie = "<br>2<sup>nd</sup>";
}
elseif($i == 3)
{
$ie = "<br>3<sup>rd</sup>";
}
else
{
$ie = "<br>4<sup>th</sup>";
}
$test_results .= "$ie Test: $SN ";
$total_score += $SN;
$avg_score = $total_score/$avg;
$i++;
}
}
else
{
$total_score = $test_results = "N/A";
}
?>
<tr>
<td style="border: 1px solid black; font-size:11px;width:120px;white-space: nowrap;height:30px;"><?=$subject_name?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?=$test_results?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?=$avg_score?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?=$remark?></td>
</tr>
<?php
}
?>
</tbody>
Could you make the html where you create the row conditional on $test_results NOT being equal to "N/A"? Something like this:
<?php if(!$test_results === "N/A"){ ?>
<tr>
<td> etc etc
<td> etc etc
<td> etc etc
<td> etc etc
</tr>
<?php } ?>
So I need to fetch data such as section, item names and subtotal of every section from my database and get the grand total of all the items.
I stumble upon the code below and modify it to my requirements.
The code works perfectly but I need to display more data like Item description, Qty, Price, status and so on.
The code below works and have no problem whatsoever but i need more data (description, qty, price and so on) to display in my table but I can't figure out how to do it.
The data I need to display is also in the same row where projectscostbreakdown_areaname, projectscostbreakdown_itemname and projectscostbreakdown_totalcost are.
<?php
$projectsid = $_GET['projectrfpid'];
$itemdeleted = 1;
$itemfirstothers = 'OTHERS';
$query = $conn->prepare('SELECT projectscostbreakdown_id,
projectscostbreakdown_projectid,
projectscostbreakdown_areaname,
projectscostbreakdown_itemname,
projectscostbreakdown_itemdescription,
projectscostbreakdown_qty,
projectscostbreakdown_costpiece,
projectscostbreakdown_budgeted,
projectscostbreakdown_totalcost,
projectscostbreakdown_note,
projectscostbreakdown_addedby,
projectscostbreakdown_addeddate,
projectscostbreakdown_deleted,
projectscostbreakdown_lastedit,
projectscostbreakdown_lasteditby
FROM projectscostbreakdown WHERE projectscostbreakdown_projectid=:projectsid && projectscostbreakdown_deleted=:itemdeleted ORDER BY projectscostbreakdown_areaname=:itemfirstothers, projectscostbreakdown_areaname ASC, projectscostbreakdown_id DESC');
$query->bindParam(':projectsid', $projectsid, PDO::PARAM_INT);
$query->bindParam(':itemdeleted', $itemdeleted, PDO::PARAM_INT);
$query->bindParam(':itemfirstothers', $itemfirstothers, PDO::PARAM_INT);
$query->execute();
$data = array();
$data2 = array();
$numbering = 0; //for item count
$count = 1; //counter use for background color
while ( $row2 = $query->fetch() ) {
if ( empty($data[ $row2['projectscostbreakdown_areaname'] ]) ) {
$data[ $row2['projectscostbreakdown_areaname'] ]= array();
$data2[ $row2['projectscostbreakdown_itemdescription'] ][ $row2['projectscostbreakdown_qty'] ]= array();
}
if ( empty( $data[ $row2['projectscostbreakdown_areaname'] ][ $row2['projectscostbreakdown_itemname'] ] ) ) {
$data[ $row2['projectscostbreakdown_areaname'] ][ $row2['projectscostbreakdown_itemname'] ] = array();
$data2[ $row2['projectscostbreakdown_itemdescription'] ][ $row2['projectscostbreakdown_qty'] ]= array();
}
$data[ $row2['projectscostbreakdown_areaname'] ][ $row2['projectscostbreakdown_itemname'] ][] = $row2['projectscostbreakdown_totalcost'];
$data2[ $row2['projectscostbreakdown_itemdescription'] ][ $row2['projectscostbreakdown_qty'] ]= array();
}
print '<table width="100%" border="0"><tbody>';
$totalSum = 0;
foreach ( $data as $area => $item ) {
print '<tr style="background-color: white;"><td colspan="7" style="text-align: left;"><br /><br /><b><u>'. $area .'</u></b></td></tr>';
print '<tr style="background-color: #AAAAAA; text-align: center;">
<td width="20%""><b>Item name</b></td>
<td width="30%"><b>Description</b></td>
<td width="5%"><b>Qty.</b></td>
<td width="10%"><b>Cost/Piece</b></td>
<td width="10%"><b>Subtotal</b></td>
<td width="10%"><b>Budget</b></td>
<td width="15%"><b>Note /<br / >Entered by</b></td>
</tr>';
$totalArea = 0;
foreach ( $item as $item => $totalcost ) {
//while ( $data = $query->fetch() ) {
$numbering++;
$count++;
$class = ($count%2 == 0)? 'white': '#CCCCCC';
$sum = array_sum( $totalcost );
print '<tr style="background-color: '.$class.'">';
print '<td style="vertical-align: top; text-align: left;">'. $numbering .'. '. $item . '</td>';
print '<td style="vertical-align: top; text-align: left;">';
print_r($data2);
print '</td>';
print '<td style="vertical-align: top; text-align: right;"></td>';
print '<td style="vertical-align: top; text-align: right;"></td>';
print '<td style="vertical-align: top; text-align: right;">'. number_format($sum, 2,'.', ',') . '</td>';
print '<td style="vertical-align: top; text-align: right;"></td>';
print '<td style="vertical-align: top; text-align: left;"></td>';
print '</tr>';
$totalArea += $sum;
}
print '<tr style="background-color: lightgray; text-align: right;" ><td colspan="6">Section Total: </td><td>'. number_format($totalArea, 2,'.', ',') . '</td></tr>';
$totalSum += $totalArea;
}
print '<tr style="background-color: lightblue; text-align: right;" ><td colspan="6"><b>Grand Total: </b></td><td><b>'. number_format($totalSum, 2,'.', ',') . '</b></td></tr>';
echo '</tbody>
</table>';
?>
UPDATE
The output looks like this right now.
I need to include Description and Qty in the output but cant figure how to be done.
Section1
Item Name-----Description-----Qty-----Subtotal
item 1---------------------------------------------1000
item 2---------------------------------------------2000
----------------------------------Section Total: 3000
Section2
Item Name-----Description-----Qty-----Subtotal
item 3---------------------------------------------1000
----------------------------------Section Total: 1000
------------------------------------Grand Total: 4000
Complete code (more structured, fixed bugs):
<?php
$projectsid = $_GET['projectrfpid'];
$itemdeleted = 1;
$itemfirstothers = 'OTHERS';
$query = $conn->prepare('SELECT projectscostbreakdown_id,
projectscostbreakdown_projectid,
projectscostbreakdown_areaname,
projectscostbreakdown_itemname,
projectscostbreakdown_itemdescription,
projectscostbreakdown_qty,
projectscostbreakdown_costpiece,
projectscostbreakdown_budgeted,
projectscostbreakdown_totalcost,
projectscostbreakdown_note,
projectscostbreakdown_addedby,
projectscostbreakdown_addeddate,
projectscostbreakdown_deleted,
projectscostbreakdown_lastedit,
projectscostbreakdown_lasteditby
FROM projectscostbreakdown WHERE projectscostbreakdown_projectid=:projectsid && projectscostbreakdown_deleted=:itemdeleted ORDER BY projectscostbreakdown_areaname=:itemfirstothers, projectscostbreakdown_areaname ASC, projectscostbreakdown_id DESC');
$query->bindParam(':projectsid', $projectsid, PDO::PARAM_INT);
$query->bindParam(':itemdeleted', $itemdeleted, PDO::PARAM_INT);
$query->bindParam(':itemfirstothers', $itemfirstothers, PDO::PARAM_INT);
$query->execute();
$data = array();
$data2 = array();
$numbering = 0; //for item count
$count = 1; //counter use for background color
$itemData = $query->fetchAll();
$query->execute();
while ($row2 = $query->fetch()) {
if (empty($data[$row2['projectscostbreakdown_areaname']]) ) {
$data[$row2['projectscostbreakdown_areaname']]= array();
$data2[$row2['projectscostbreakdown_itemdescription']][$row2['projectscostbreakdown_qty']] = array();
}
if (empty($data[$row2['projectscostbreakdown_areaname']][$row2['projectscostbreakdown_itemname']])) {
$data[$row2['projectscostbreakdown_areaname']][ $row2['projectscostbreakdown_itemname']] = array();
$data2[$row2['projectscostbreakdown_itemdescription']][$row2['projectscostbreakdown_qty']]= array();
}
$data[$row2['projectscostbreakdown_areaname']][$row2['projectscostbreakdown_itemname']][] = $row2['projectscostbreakdown_totalcost'];
$data2[$row2['projectscostbreakdown_itemdescription']][$row2['projectscostbreakdown_qty']] = array();
}
print '<table width="100%" border="0"><tbody>';
$totalSum = 0;
foreach ( $data as $area => $item ) {
print '<tr style="background-color: white;"><td colspan="7" style="text-align: left;"><br /><br /><b><u>'. $area .'</u></b></td></tr>';
print '<tr style="background-color: #AAAAAA; text-align: center;">
<td width="20%""><b>Item name</b></td>
<td width="30%"><b>Description</b></td>
<td width="5%"><b>Qty.</b></td>
<td width="10%"><b>Cost/Piece</b></td>
<td width="10%"><b>Subtotal</b></td>
<td width="10%"><b>Budget</b></td>
<td width="15%"><b>Note /<br / >Entered by</b></td>
</tr>';
$totalArea = 0;
foreach ( $item as $item => $totalcost ) {
$count++;
echo $numbering.' ';
$class = ($count % 2 == 0) ? 'white' : '#CCCCCC';
$sum = array_sum($totalcost);
print '<tr style="background-color: '.$class.'">';
print '<td style="vertical-align: top; text-align: left;">'. $numbering .'. '. $item . '</td>';
print '<td style="vertical-align: top; text-align: left;">'.$itemData[$numbering]['projectscostbreakdown_itemdescription'].'</td>';
print '<td style="vertical-align: top; text-align: right;"></td>';
print '<td style="vertical-align: top; text-align: right;"></td>';
print '<td style="vertical-align: top; text-align: right;">'. number_format($sum, 2,'.', ',') . '</td>';
print '<td style="vertical-align: top; text-align: right;"></td>';
print '<td style="vertical-align: top; text-align: left;"></td>';
print '</tr>';
$numbering++;
$totalArea += $sum;
}
print '<tr style="background-color: lightgray; text-align: right;" ><td colspan="6">Section Total: </td><td>'. number_format($totalArea, 2,'.', ',') . '</td></tr>';
$totalSum += $totalArea;
}
print '<tr style="background-color: lightblue; text-align: right;" ><td colspan="6"><b>Grand Total: </b></td><td><b>'. number_format($totalSum, 2,'.', ',') . '</b></td></tr>';
echo '</tbody></table>';
?>
Basically, what I did (which might not be the best option but I couldn't think anything better) was I used $itemData = $query->fetchAll(); after executing $query->execute();. Now if we leave everything the same, table will be empty since pointer is at the last element. Therefore, we must execute again the same query by writing an additional $query->execute(); right after $itemData = $query->fetchAll();.
Now we have basically the same thing as in your example but with one additional variable $itemData with all information about items. To bring out description, quantity, etc. now we can use $itemData[$numbering]['projectscostbreakdown_itemdescription'] where $numbering stands for array's index number.
I want to fetch some data from database as xml and print them inside a html table using php.
here is my php code:
$xml=simplexml_load_string($ru) or die("Error: Cannot create object");
echo '<table style="border: 1px solid black; border-collapse: collapse;">';
foreach ($xml->employee->children() as $key=>$value){
if($key == 'PAYMSTR_SALHDNM' ){
echo '<tr>';
echo '<th style="border: 1px solid black; border-collapse: collapse;">'.$value.'</th>';
echo '<td style="border: 1px solid black; border-collapse: collapse;">'.$value.'</td>';
echo '</tr>';
}
else{
echo '<tr>';
echo '<th style="border: 1px solid black; border-collapse: collapse;">'.$key.'</th>';
echo '<td style="border: 1px solid black; border-collapse: collapse;">'.$value.'</td>';
echo '</tr>';
}
}
echo '</table>';
Now I am getting this output:
But want to make each value of tag PAYMSTR_SALHDNM as table head [its done ]and each value of tag PAYMSTR_AMOUNT as table data for tag PAYMSTR_SALHDNM
Like the following image:
i think if, somehow i can keep track of next $xml->employee->children() and its value,then i can bring such correct format.Please help me on that.Thanks
Updates: Here is my Table:
Here is my xml:
<employee><id>FMCSC00015</id><year>2016</year><month>1</month><paymstr_salhdnm>BASIC PAY</paymstr_salhdnm><paymstr_amount>35600</paymstr_amount><paymstr_salhdnm>ASSOCIATION SUBSCRIPTION</paymstr_salhdnm><paymstr_amount>240</paymstr_amount><paymstr_salhdnm>TELEPHONE ALLOWANCE</paymstr_salhdnm><paymstr_amount>800</paymstr_amount><paymstr_salhdnm>HOUSE RENT DEDUCTION</paymstr_salhdnm><paymstr_amount>2587.5</paymstr_amount><paymstr_salhdnm>MEDICAL ALLOWANCE</paymstr_salhdnm><paymstr_amount>700</paymstr_amount><paymstr_salhdnm>GAS BILL</paymstr_salhdnm><paymstr_amount>450</paymstr_amount><paymstr_salhdnm>DEARNESS ALLOWANCE</paymstr_salhdnm><paymstr_amount>6000</paymstr_amount><paymstr_salhdnm>LIFE INSURANCE PREMIUM (D)</paymstr_salhdnm><paymstr_amount>1718</paymstr_amount><paymstr_salhdnm>PF SUBSCRIPTION</paymstr_salhdnm><paymstr_amount>3560</paymstr_amount><paymstr_salhdnm>PF ADVANCE LOAN</paymstr_salhdnm><paymstr_amount>2796</paymstr_amount><paymstr_salhdnm>BENEVOLENT FUND</paymstr_salhdnm><paymstr_amount>1780</paymstr_amount><paymstr_salhdnm>HEALTH INSURANCE PREMIUM</paymstr_salhdnm><paymstr_amount>150</paymstr_amount><paymstr_salhdnm>STUDENT WELFARE</paymstr_salhdnm><paymstr_amount>3</paymstr_amount><paymstr_salhdnm>MUNICIPAL TAX & SWEAPER CH.</paymstr_salhdnm><paymstr_amount>18</paymstr_amount><paymstr_salhdnm>STAIR CASE LIGHT CHARGES</paymstr_salhdnm><paymstr_amount>1.25</paymstr_amount><paymstr_salhdnm>CLUB</paymstr_salhdnm><paymstr_amount>207</paymstr_amount><paymstr_salhdnm>TEACHER'S FAMILY WELFARE ASSOC</paymstr_salhdnm><paymstr_amount>10</paymstr_amount><paymstr_salhdnm>MOTOR GARAGE</paymstr_salhdnm><paymstr_amount>500.5</paymstr_amount><paymstr_salhdnm>RESEARCH ALLOWANCE</paymstr_salhdnm><paymstr_amount>1500</paymstr_amount><paymstr_salhdnm>SONALI BANK LOAN-3</paymstr_salhdnm><paymstr_amount>5728</paymstr_amount><paymstr_salhdnm>SONALI BANK LOAN-4</paymstr_salhdnm><paymstr_amount>23490</paymstr_amount></employee>
Here's a solution. The basic idea is to…
in a first loop extract all the header data(the first three header items to be pre-initialised),
in a second loop select data and build table, where the loop index is the index into the header table.
Code
<?php
$ru = "..."; // Has pre-defined content
$xml=simplexml_load_string($ru) or die("Error: Cannot create object");
// Collect and create headers
$headers = array('ID', 'YEAR', 'MONTH'); // Pre-define headers
foreach ($xml->paymstr_salhdnm as $key=>$header) {
$headers[] = (string)$header;
}
// Create data table
$hindex=0;
echo '<table style="border: 1px solid black; border-collapse: collapse;">';
foreach ($xml->children() as $key=>$value){
if (strtolower($key) == 'paymstr_salhdnm' ) continue; // header data, so ignore
echo '<tr>';
echo ' <td style="border: 1px solid black; border-collapse: collapse;">'.((isset($headers[$hindex])) ? $headers[$hindex] : ' ').'</td>';
echo ' <td style="border: 1px solid black; border-collapse: collapse;">'.$value.'</td>';
echo '</tr>';
$hindex++;
}
echo '</table>';
?>
Result
You can also output the key/value pairs as a definition list, using php like:
<?php
$ru = <<<XMLDATA
<employee><id>FMCSC00015</id><year>2016</year><month>1</month><paymstr_salhdnm>BASIC PAY</paymstr_salhdnm><paymstr_amount>35600</paymstr_amount><paymstr_salhdnm>ASSOCIATION SUBSCRIPTION</paymstr_salhdnm><paymstr_amount>240</paymstr_amount><paymstr_salhdnm>TELEPHONE ALLOWANCE</paymstr_salhdnm><paymstr_amount>800</paymstr_amount><paymstr_salhdnm>HOUSE RENT DEDUCTION</paymstr_salhdnm><paymstr_amount>2587.5</paymstr_amount><paymstr_salhdnm>MEDICAL ALLOWANCE</paymstr_salhdnm><paymstr_amount>700</paymstr_amount><paymstr_salhdnm>GAS BILL</paymstr_salhdnm><paymstr_amount>450</paymstr_amount><paymstr_salhdnm>DEARNESS ALLOWANCE</paymstr_salhdnm><paymstr_amount>6000</paymstr_amount><paymstr_salhdnm>LIFE INSURANCE PREMIUM (D)</paymstr_salhdnm><paymstr_amount>1718</paymstr_amount><paymstr_salhdnm>PF SUBSCRIPTION</paymstr_salhdnm><paymstr_amount>3560</paymstr_amount><paymstr_salhdnm>PF ADVANCE LOAN</paymstr_salhdnm><paymstr_amount>2796</paymstr_amount><paymstr_salhdnm>BENEVOLENT FUND</paymstr_salhdnm><paymstr_amount>1780</paymstr_amount><paymstr_salhdnm>HEALTH INSURANCE PREMIUM</paymstr_salhdnm><paymstr_amount>150</paymstr_amount><paymstr_salhdnm>STUDENT WELFARE</paymstr_salhdnm><paymstr_amount>3</paymstr_amount><paymstr_salhdnm>MUNICIPAL TAX & SWEAPER CH.</paymstr_salhdnm><paymstr_amount>18</paymstr_amount><paymstr_salhdnm>STAIR CASE LIGHT CHARGES</paymstr_salhdnm><paymstr_amount>1.25</paymstr_amount><paymstr_salhdnm>CLUB</paymstr_salhdnm><paymstr_amount>207</paymstr_amount><paymstr_salhdnm>TEACHER'S FAMILY WELFARE ASSOC</paymstr_salhdnm><paymstr_amount>10</paymstr_amount><paymstr_salhdnm>MOTOR GARAGE</paymstr_salhdnm><paymstr_amount>500.5</paymstr_amount><paymstr_salhdnm>RESEARCH ALLOWANCE</paymstr_salhdnm><paymstr_amount>1500</paymstr_amount><paymstr_salhdnm>SONALI BANK LOAN-3</paymstr_salhdnm><paymstr_amount>5728</paymstr_amount><paymstr_salhdnm>SONALI BANK LOAN-4</paymstr_salhdnm><paymstr_amount>23490</paymstr_amount></employee>
XMLDATA;
$xml = simplexml_load_string($ru) or die("Error: Cannot create object");
// Collect and create headers
$headers = array('ID', 'YEAR', 'MONTH');
foreach ($xml->paymstr_salhdnm as $key=>$header) {
$headers[] = (string)$header;
}
echo '<dl>';
$hindex=0;
foreach ($xml->children() as $key=>$value){
if (strtolower($key) == 'paymstr_salhdnm' ) continue;
echo (isset($headers[$hindex])) ? PHP_EOL . "<dt>{$headers[$hindex]}</dt>" : PHP_EOL . '<dt></dt>';
echo "<dd>$value</dd>";
$hindex++;
}
echo '</dl>';
And the outcome being a list of "definition-terms" with the matching "definition-value", like so:
https://jsfiddle.net/9c2xaa27/
Can't understand how correctly get all info from web, not just one row but all.
This script produces the output : Array Array Array Array and only one row.
<table id="rounded-corner" width=100%>
<tr>
<td>Nuotrauka</td>
<td>Pavadinimas</td>
<td>miestas</td>
<td>metai</td>
<td>kaina</td>
</tr>
<?
$url = "My Link";
$contents = file_get_contents($url);
preg_match_all("|<span class=\"ttitle2\">(.*?) </span>|U",$contents,$pavadinimas);
preg_match_all("|<span class=\"ttitle3\">(.*?)</span>|U",$contents,$miestas);
preg_match_all("|<span class=\"ttitle1\">(.*?)</span>|U",$contents,$metai);
preg_match_all("|<span class=\"ttitle1\" style='float: left;'>(.*?)<br />|U",$contents,$kaina);
preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $contents, $img_link);
$output = "<tr><td><img src=$img_link></td><td>$pavadinimas</td><td>$miestas</td><td>$metai</td><td>$kaina</td></tr>";
print_r($output);
?>
</table>
Try this. It is extracting data as required.
<table style="border: 1px solid black;" id="rounded-corner" width=100%>
<tr>
<td style="border: 1px solid black;">Nuotrauka</td>
<td style="border: 1px solid black;">Pavadinimas</td>
<td style="border: 1px solid black;">miestas</td>
<td style="border: 1px solid black;">metai</td>
<td style="border: 1px solid black;">kaina</td>
</tr>
<?php
$url = "put_your_url";
$contents = file_get_contents($url);
preg_match_all('/<span class="ttitle2".*?>(.*?)<\/span>/',$contents,$pavadinimas);
preg_match_all('/<span class="ttitle3".*?>(.*?)<\/span>/',$contents,$miestas);
preg_match_all('/<span class="ttitle1".*?>(.*?)<\/span>/',$contents,$metai_kaina);
foreach($metai_kaina[0] as $key=>$metai_kaina_val){
if($key%2==0)
$metai[] = strip_tags($metai_kaina_val);
else
$kaina[] = strip_tags($metai_kaina_val);
}
preg_match_all('/<img .*?(?=src)src=\"([^\"]+)\"/si', $contents, $img_link);
for($i=0; $i<count($pavadinimas[0]); $i++){
echo '<tr>
<td style="border: 1px solid black;"><img src="'.$img_link[1][$i+2].'"></td>
<td style="border: 1px solid black;">'.$pavadinimas[0][$i].'</td>
<td style="border: 1px solid black;">'.$miestas[0][$i].'</td>
<td style="border: 1px solid black;">'.$metai[$i].'</td>
<td style="border: 1px solid black;">'.$kaina[$i].'</td>
</tr>';
}
?>
</table>
The $matches parameter of the function will always be an array - you need to process the variable before adding them to the output string.
http://php.net/manual/en/function.preg-match-all.php
As far as I'm aware you can't print_r a mixture of string and array.
If all matchings return the same number of results, which seems to be implied by your $output structure, iterate over one of them:
$output = '';
foreach ($pavadinimas[1] as $index => $match) {
$output .= '<tr><td><img src=' . $img_link[1][$index] . '></td><td>' . $match . '</td><td>' . $miestas[1][$index] . '</td><td>' . $metai[1][$index] . '</td><td>' . $kaina[1][$index] . '</td></tr>';
}
preg_match_all makes deep complex search. Maximum it'll be two dimentional array. Read docs
how to correctly get all info?
For this to find out you just dump out all the result info:
print_r($pavadinimas);
print_r($miestas);
...
but i need that all info put in table
Be little more inventive; if $pavadinimas is 2d array:
echo '<table>';
foreach($p in $pavadinimas){
echo '<tr>';
foreach($item in $p){
echo '<td>' , $item , '</td>' ;
}
echo '</tr>';
}
echo '</table>';
Note
To invert the 2d array order you just use flag (PREG_PATTERN_ORDER or PREG_SET_ORDER) in the preg_match_all (see flag section in docs). Thus you will have your table with other order.
I have a script that when clients create a pdf file, it is placed in a folder named with there own code. This is in a main folder called 'destcerts' and the folder structure ends up looking like the illustration below. What I need to do as admin is have access to these files and the only way I can do that at the moment is to hard code the sub-directory in the code and that is not practical for obvious reasons.
What I thought I could do is, have a select dropdown with a list of clients which when selected could trigger perhaps a change event which would say loop for the chosen client and display the files. Unfortunately, my php or jquery is not good enough to achieve this and I would be grateful if someone could show me the way to go forward with this. Many thanks
php code
<div id="viewCerts" style="display:none;">
<?php
$sub = 'destcerts/';
// READ THE NAMES OF FILES IN THE SUB-DIRECTORY
$fff = new DirectoryIterator($sub);
$sss = array();
foreach ($fff as $filedata)
{
// SKIP THE "DOT" FILES
if ($filedata->isDot()) continue;
// ACTIVATE THIS LINE TO RESTRICT IT TO PDF FILES ONLY
if ($filedata->getExtension() != 'pdf') continue;
// CREATE LINKS TO THESE FILES
$nom = $filedata->getFilename();
$lnk
= '<img src="destcerts/PDF_icon_100.png" style="margin-bottom: 15px; margin-top:15px;"><br /><a href="'
. $sub
. '/'
. $nom
. '" style="color:#0099FF; text-decoration:none; font-size:12px; font-family: Verdana, Geneva, sans-serif;">'
. $nom
. '</a>'
;
// COLLECT THE LINKS HERE
$sss[] = $lnk;
}
// ACCUMULATE THE TABLE ROWS HERE
$trs = NULL;
// COLLECT GROUPS OF FOUR
If(!empty($sss)){
while (!empty($sss))
{
$td1 = array_shift($sss) or NULL;
$td2 = array_shift($sss) or NULL;
$td3 = array_shift($sss) or NULL;
$td4 = array_shift($sss) or NULL;
// USE HEREDOC TO INSERT THESE INTO A TABLE ROW
$tr = <<<EOD
<tr>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td1</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td2</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td3</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td4</td>
</tr>
EOD;
// APPEND THE TABLE ROW TO THE OTHER ROWS
$trs .= $tr;
}
}
else{
$msg = "There are no files to display";
$tr = <<<EOD
<tr>
<td style="text-align:center; padding: 10px !important; font-weight: normal;">$msg</td>
</tr>
EOD;
$trs .= $tr;
}
// USE HEREDOC TO INSERT THE TABLE ROWS INTO THE TABLE
$tab = <<<EOD
<table id="pdfDownload" width="97%" align="center" border="1" cellspacing="10" cellpadding="0" style="border:1px solid grey; padding-bottom: 10px; margin-bottom:20px;">
<th style="text-align:center; padding: 10px !important; padding-bottom: 20px; border:1px solid black; background-color: #3399FF; color: white; font-size: 18px !important;" colspan="4">Destruction Certificates Download</th>
<tr>
<th></th>
</tr>
$trs
</table>
EOD;
// SHOW THE WORK PRODUCT
echo $tab;
?>
</div>
directory structure
.
..
destcerts
client1
client2
client3
etc