Multidimensional PHP array from a MySQLi table to HTML table - php

I have a portion of my website where I can give awards to members. Right now I am working on creating individual description pages for each award. On that page I want to include which members have already received this award and display this data in an HTML table.
I have already passed the data into a Multidimensional array as seen below.
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$currentAward = $awardid; // Current Page Award. Local value to the page.
$result = $conn->query("SELECT * FROM vms_awardsgranted WHERE awardid='$currentAward'");
$awardsList = array();
while($pilotsList = $result->fetch_assoc()){
$awardsList[ $pilotsList["id"] ] = $pilotsList;
}
echo nl2br("DEBUG: $currentAward \n");
print_r(array_values($awardsList));
$conn->close();
?>
Example Result
DEBUG: 8
Array ( [0] => Array ( [id] => 28 [awardid] => 8 [pilotid] => 4 [dateissued] => 2015-10-14 20:12:21 ) [1] => Array ( [id] => 32 [awardid] => 8 [pilotid] => 1 [dateissued] => 2015-10-14 20:14:14 ) )
From here I am trying to parse this info and add it into the HTML table below but I honestly can't find the correct way of doing this with a Multidimensional array. Can anyone out here give me some insight? My HTML tables looks like below.
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="ocean_table">
<thead>
<tr>
<th>Pilot ID</th>
<th>Pilot Name</th>
<th>Date Awarded</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">
</td>
<td align="center">
</td>
<td align="center">
</td>
</tr>
</tbody>
</table>

Basically, to loop through the result, your code could look like this:
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="ocean_table">
<thead>
<tr>
<th>Pilot ID</th>
<th>Pilot Name</th>
<th>Date Awarded</th>
</tr>
</thead>
<tbody>
<?php foreach($awardsList as $member):?>
<tr>
<td align="center">
<?=$pilot['pilotid']?>
</td>
<td align="center">
</td>
<td align="center">
<?=$pilot['dateissued']?>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
A couple of remarks, though.
You don't return the pilot name from the query. You should join on the table of members/pilots to get their name.
Your naming is confusing. $pilotList suggests a list of pilots, but instead that variable contains only the reference/junction between the award and one pilot. Dito for awardsList which actually contains a list of people that won the award.

Something like
<tbody>
<?php foreach($awardsList as $record){ ?>
<tr>
<td align="center">
<?php echo $record['id']; ?>
</td>
<td align="center">
<?php echo $record['awardid']; ?>
</td>
<td align="center">
<?php echo $record['pilotid']; ?>
</td>
</tr>
<?php } ?>
</tbody>

Related

Scrape multiple table to array using php

I am using php DomDocument to scrape multiple tables but i don't understand how to i output multiple tables data as array, like below example.
Example:
[0] =>
[Network] =>
[Technology]=>[GSM / HSPA / LTE]
[...]=>[...],
[...]=>[...]
[1] =>
[...] =>
[...]=>[...],
[...]=>[...],
[...]=>[...]
My code is below:
$doc = new DomDocument();
$doc->preserveWhiteSpace = false;
#$doc->loadHTML($responseBody);
$xpath = new DOMXPath($doc);
$th = $xpath->query('//table//th');
$tdFirst = $xpath->query('//table//td[contains(#class, "ttl")]');
$tdSecond = $xpath->query('//table//td[contains(#class, "nfo")]');
i get output from below code but i don't want this type output. I need to output like my example. I want to display outputted data on my own table design, so i need it.
foreach($th as $rows) {
echo $rows->nodeValue.'<br>';
}
foreach($tdFirst as $rows) {
echo $rows->nodeValue.'<br>';
}
foreach($tdSecond as $rows) {
echo $rows->nodeValue.'<br>';
}
Sorry for not good english. Thanks
I want to scrape data from multiple table like below table (i just copy and pasted first table to many times for understand you, my targeted scraping site tables are look same ):
<table>
<tbody>
<tr>
<th scope="col" colspan="2">Network</th>
</tr>
<tr>
<td class="ttl"><a>Technology</a></td>
<td class="nfo" >GSM / HSPA / LTE</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<th scope="col" colspan="2">Network</th>
</tr>
<tr>
<td class="ttl"><a>Technology</a></td>
<td class="nfo" >GSM / HSPA / LTE</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<th scope="col" colspan="2">Network</th>
</tr>
<tr>
<td class="ttl"><a>Technology</a></td>
<td class="nfo" >GSM / HSPA / LTE</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<th scope="col" colspan="2">Network</th>
</tr>
<tr>
<td class="ttl"><a>Technology</a></td>
<td class="nfo" >GSM / HSPA / LTE</td>
</tr>
</tbody>
</table>

PHP Nesting do-while loop

Im trying to teach myself php.... I have created a database with 4 elements... region, details, store_name, web_address.
I have created ado-while loop that displays all the records in a 'region'... it displays 'store_name', then 'web_address', then 'details' sorted so all the records with the same 'details' are grouped together;
<table width="600" align="center" border="0" cellspacing="4" >
<tr>
<td colspan="2"><h2>Web Stockists: <strong></strong></h2></td>
<td width="251" colspan="2" align="right"><h3>Region: <?php echo $region_text ?></h3></td>
</tr>
<?php do { ?>
<tr>
<td colspan="2"><strong><?php echo $row_RegionList['store_name']; ?></strong></td>
<td colspan="2" rowspan="2" align="center"> <?php echo '' . $row_RegionList['web_address'] . ''; ?></td>
</tr>
<tr>
<td width="14">
<td width="313"><?php echo $row_RegionList['details']; ?> </tr>
<?php } while ($row_RegionList = mysql_fetch_assoc($RegionList)); ?>
</table>
I now want to make it so every record with the same 'details' value get listed under a 'details' sub heading.
This is my attempt;
<table width="600" align="center" border="0" cellspacing="4" >
<tr>
<td colspan="2"><h2>Web Stockists: <strong></strong></h2></td>
<td width="251" colspan="2" align="right"><h3>Region: <?php echo $region_text ?></h3></td>
</tr>
<?php $details = $row_RegionList['details'];
do { ?>
<tr>
<td width="313"> <h3><?php echo $row_RegionList['details']; ?> </h3></td>
</tr>
<?php do { ?>
<tr>
<td colspan="2"><strong><?php echo $row_RegionList['store_name']; ?></strong></td>
<td colspan="2" rowspan="2" align="center"> <?php echo '' . $row_RegionList['web_address'] . ''; ?></td>
</tr>
<?php } while ($row_RegionList['details'] == $details);
$details = $row_RegionList['details'];
} while ($row_RegionList = mysql_fetch_assoc($RegionList)); ?>
</table>
This makes a heading of 'region' with a sub heading of 'details' but then creates an eternal loop of the first record in the database. can someone show me what I have done wrong?

how to retrieve data from view table in page 1 to display on page 2?

i want to pass view data from shown table on page 1 to page 2. for example, i have table filter used from javascript function. so, after filter the data i wanna it displayed on page 2 also. page 2 is for print and save in pdf file. how to get it parameter so that it displays the data from current table showned?
page1.blade.php (am not sure it is right or not)
<form target="_blank" method="post" action="print">
<table class=" table-autosort table-autofilter " border="1" width="100%" id="save">
<thead>
<tr>
<td>bil</td>
<td class="table-filterable table-sortable:numeric table-sortable"> faculty</td>
<td class="table-filterable table-sortable:numeric table-sortable"> programme</td>
</tr>
</thead>
<tbody class="bottom" >
#foreach($profiles as $profile)
<tr>
<td class="number" width="7%%"></td>
<td class="faculty" width="40%">{{$profile->faculty}} </td>
<td class="program" width="53%%"> {{$profile->programme}}</td>
</tr>
#endforeach
</tbody>
</table>
page2.blade.php
<table width="100%" class="printTable">
<thead>
<tr>
<td>bil</td>
<td>faculty</td>
<td>programme</td>
<td>student id</td>
<td>student name</td>
</tr>
</thead>
<tbody>
//possible code here //i dont know how to get it parameter
</tbody>

PHP MySQL populating values from database

lets say i retrieve all of the values where their position belongs to top8.I populate them out in a table and instead of displaying different kinds of values , it displays 3 tables with 3 different values, how is this so? any help so that different values belonging to certain values will all be displayed out? i only need one table with 3 different values.
<?
$facebookID = "top8";
mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select_db("schoutweet") or ie(mysql_error());
$data= mysql_query("SELECT schInitial FROM matchTable WHERE position='".$facebookID."'")
or die(mysql_error());
while($row = mysql_fetch_array($data))
{
?>
<center>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_bracket">
<tr>
<td class="brack_under cell_1"><a href="www.facebook.com"/>team 1.1><?= $row['schInitial']?><a/></td>
<td class="cell_2"> </td>
<td class="cell_3"> </td>
<td class="cell_4"> </td>
<td class="cell_5"> </td>
<td class="cell_6"> </td>
</tr>
<tr>
<td class="brack_under_right_up">team 1.2><?= $row['schInitial']?></</td>
<td class="brack_right"><!--1.2.1--></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td class="brack_right"><!--2.1--></td>
<td class="brack_under"><!--3.1--></td>
<td><!--here?--></td>
<td><!--there?--></td>
<td><!--everywhere?--></td>
</tr>
</table>
</center>
<?
}
?>
</body>
That's because your <table> tag is within the loop! Place the <table> tag outside the while loop.
place your table tags outside the while loop
Because your writing the table tag inside the while loop. Everything inside the loop is done each loop cycle. If you only want to have one table in the output, you'll have to open and close the table outside of the loop, like this:
$data= mysql_query("SELECT schInitial FROM matchTable WHERE position='".$facebookID."'")
or die(mysql_error());
?>
<center>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_bracket">
<?
while($row = mysql_fetch_array($data))
{
?>
<tr>
<td class="brack_under cell_1"><a href="www.facebook.com"/>team 1.1><?= $row['schInitial']?><a/></td>
<td class="cell_2"> </td>
<td class="cell_3"> </td>
<td class="cell_4"> </td>
<td class="cell_5"> </td>
<td class="cell_6"> </td>
</tr>
<tr>
<td class="brack_under_right_up">team 1.2><?= $row['schInitial']?></</td>
<td class="brack_right"><!--1.2.1--></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td class="brack_right"><!--2.1--></td>
<td class="brack_under"><!--3.1--></td>
<td><!--here?--></td>
<td><!--there?--></td>
<td><!--everywhere?--></td>
</tr>
<?
}
?>
</table>
</center>
That will, however, print three rows per loop and therefore per record (but you have references to the table contents in two of them, so I suppose that's what you want?).
Also take care about some not well-formed HTML you have there (e.g. the > character in the expression team 1.1> / team 1.2>. If you want to print the > character to the browser, encode it as HTML entity (> for this case). You also have a probably superfluous </ in the first column of the second row (</</td>).
you need to echo the HTML part as well in the while loop like
echo '<table>';

How can i get the entire HTML of an element using regex?

i'm learning Regex but can't figure it out.... i want to get the entire HTML from a DIV, how to procced?
already tried this;
/\< td class=\"desc1\"\>(.+)/i
it returns;
Array
(
[0] => < td class="desc1">
[1] =>
)
the code that i'm matching is this;
<table id="profile" cellpadding="1" cellspacing="1">
<thead>
<tr>
<th colspan="2">Jogador TheInFEcT </th>
</tr>
<tr>
<td>Detalhes</td>
<td>Descrição:</td>
</tr>
</thead><tbody>
<tr>
<td class="empty"></td><td class="empty"></td>
</tr>
<tr>
<td class="details">
<table cellpadding="0" cellspacing="0">
<tbody><tr>
<th>Classificação</th>
<td>11056</td>
</tr>
<tr>
<th>Tribo:</th>
<td>Teutões</td>
</tr>
<tr>
<th>Aliança:</th>
<td>-</td>
</tr>
<tr>
<th>Aldeias:</th>
<td>1</td>
</tr>
<tr>
<th>População:</th>
<td>2</td>
</tr><tr>
<td colspan="2" class="empty"></td>
</tr>
<tr>
<td colspan="2"> » Alterar perfil</td>
</tr>
</tbody></table>
</td>
<td class="desc1">
<div>STATUS: OFNAaaaAA</div>
</td>
</tr>
</tbody>
</table>
i need to get the entire code inside the < td class="desc1">, like that;
<div >STATUS: OFNAaaaAA< /div>
</td>
</tr>
</tbody>
</table>
Could someone help me out?
Thanks in advance.
I usually use
$dom = DOMDocument::load($htmldata);
for converting HTML code to XML DOM. And then you can use
$node = $dom->getElementsById($id);
/* or */
$nodes = $dom->getElementsByTagName($tag);
to get your HTML/XML node.
Now, use
$node->textContent
to get data inside node.
try this, it does not cover all possible cases but it should work:
/<td\s+class=['"]\s*desc1\s*['"]\s*>((.|\n)*)<\/td>/i
tested with: http://www.pagecolumn.com/tool/pregtest.htm
edit: improved solution suggested by Alan Moore
/<td\s+class=['"]\s*desc1\s*['"]\s*>(.*?)<\/td>/s

Categories