I run a query, the result is good, but while putting into PHP result, the resuts are duplicated per row, here is the code:
$selectSi = 'SELECT * FROM series LEFT JOIN series_join on series.ids = series_join.id_sil GROUP BY series.ids';
$querySi = $connexion->query($selectSi);
$resultSi = $querySi->fetchAll();
$displaying = 5;
// building table here
foreach($resultSi as $siK=>$siV)
{
$ids = $siV['ids'];
$silsila_en = $siV['silsila_en'];
$id_shks = $siV['id_shks'];
// get singer
$sSel = 'SELECT shk_fname, shk_lname, shk_tran FROM shk_tbl WHERE id_shk = "'.$id_shks.'"';
$sReq = $connexion->query($sSel);
$sRes = $sReq->fetchAll();
$listShk = '';
foreach($sRes as $vS)
{
$shk_fname = $vS['shk_fname'];
$shk_lname = $vS['shk_lname'];
$shkFullName = $shk_fname.' '.$shk_lname;
$shkFullNameLink = '<a href="">';
$shkFullNameLink .= $shkFullName;
$shkFullNameLink .= '</a> - ';
$listShk .= $shkFullNameLink;
$listSheikhs = substr($listShk, 0,-2).'<br />';
}
// putting result into a table
echo '<tr class="multiColor">';
echo '<td>'.$silsila_en.'</td>';
echo '<td>'.$listSheikhs.'<br /></td>';
echo '</tr>';
}
echo '</table>';
and the tables are like this
PS: Instead of numbers result, it is singers name
I am looking for a result like:
Conciseness of speech: 114 - 136
The eternal journey: 325 - 326 - 327
Night Mare: 303 - 332 - 331 - 330 - 329 - 328 - 282 - 306 - 281 - 126 - 120
but I am getting something like:
Conciseness of speech: 114 - 136
The eternal journey: 114 - 136 - 325 - 326 - 327
Night Mare: 114 - 136 - 325 - 326 - 327 - 303 - 332 - 331 - 330 - 329 - 328 - 282 - 306 - 281 - 126 - 120
so the first row is duplicated to second, and second is duplicated to third
Thanks in advance
Related
This question already has answers here:
Getting data from text file and display it in html table
(3 answers)
Reading from comma or tab delimited text file
(7 answers)
Closed 3 years ago.
I have to load a text file into an HTML table and have no idea how to do it.
This is my document I have to put the table in and an example of the text document.
<?php # Script 3.4 - index.php
$page_title = 'Climate Data For All Cities';
include ('./includes/header.html');
?>
<h1 id="mainhead">Climate Data For All Cities</h1>
<p>There are currently 80 cities.</p>
(table will go here)
<?php
include ('./includes/footer.html');
?>
Text document:
Lander WY 5557 99 -35 109 140 67
Milwaukee WI 672 98 -7 79 177 124
Seattle WA 400 94 12 55 233 163
Spokane WA 2356 98 -16 95 187 112
Burlington VT 332 91 6 54 217 168
Norfolk VA 24 98 19 85 145 117
Richmond VA 164 101 16 105 165 115
Salt Lake City UT 4221 103 -11 129 152 86
Dallas TX 551 106 10 130 152 89
Houston TX 96 104 19 73 166 94
San Antonio TX 788 102 16 90 165 83
Memphis TN 258 101 12 121 151 112
Huron SD 1281 100 -30 121 147 80
Rapid City SD 3162 106 -30 106 127 95
I am more of a JS sort of person but you can integrate this into PHP to work seamlessly. You will need to add the following to your head tags
<script
src="https://code.jquery.com/jquery-3.4.0.min.js"
integrity="sha256-BJeo0qm959uMBGb65z40ejJYGSgR7REI4+CW1fNKwOg="
crossorigin="anonymous">
</script>
<script src="text.js" charset="utf-8"></script>
The Text file you provided was saved as "text.txt" located at the same level as index html, and the following text.js script.
text.txt contains the following
Lander WY 5557 99 -35 109 140 67
Milwaukee WI 672 98 -7 79 177 124
Seattle WA 400 94 12 55 233 163
Spokane WA 2356 98 -16 95 187 112
Burlington VT 332 91 6 54 217 168
Norfolk VA 24 98 19 85 145 117
Richmond VA 164 101 16 105 165 115
Salt Lake City UT 4221 103 -11 129 152 86
Dallas TX 551 106 10 130 152 89
Houston TX 96 104 19 73 166 94
San Antonio TX 788 102 16 90 165 83
Memphis TN 258 101 12 121 151 112
Huron SD 1281 100 -30 121 147 80
Rapid City SD 3162 106 -30 106 127 95
You should have the following HTML in your body (can do it in php)
<h1>Text File reader</h1>
<button type="button" name="button" id="fileReadButton">LOAD</button>
<table id="textFileContentTable">
<thead>
<th>Location</th>
<th>State</th>
<th>High</th>
<th>Low</th>
<th>Days Clear</th>
<th>Days Cloudy</th>
<th>Days with Precip</th>
<th>Days With Snow</th>
</thead>
</table>
and finally, here is the script to get the data from the text file on click of the button, to fill in the table.
$().ready(function(){
function populateTable(filePath){
// get request for the file
$.get(filePath, function(response){
let rows = response.split("\n");
// getting each row of the text file
for(var i = 0; i < rows.length; i++){
var validRowData = [];
var rowData = rows[i].split(" ");
// getting the data for the row
for (var z = 0; z < rowData.length; z++) {
if(rowData[z] == ""){
}else{
validRowData.push(rowData[z]);
}
// sanitising the strings like San Antonio and Rapid City
if(validRowData.length == 9){
validRowData[0] = validRowData[0] + " " + validRowData[1];
validRowData.splice(1, 1);
}
}
// creating the row template, iterating through the valid data to create TDs
var rowTemplate = "<tr>";
for (var j = 0; j < validRowData.length; j++) {
rowTemplate += " <td> " + validRowData[j] + " </td> ";
}
rowTemplate += " </tr>";
// appending it to the table
$('#textFileContentTable').append(rowTemplate);
}
});
}
// onclick of the button, load the table
$('#fileReadButton').click(function(){
populateTable('./text.txt');
});
});
Running the following PHP script:
<?php
function fixCities($better_row){
$res = trim(preg_replace('/\s+/', ' ',$better_row));
$pieces = explode(" ",$res);
$num = count($pieces);
switch( $num )
{
case 9:
$temp = array($pieces[0]." ". $pieces[1]);
array_splice($pieces, 0, 2,$temp);
break;
case 10:
$temp = array($pieces[0]." " .$pieces[1] . " " .$pieces[2]);
array_splice($pieces,0,3,$temp);
break;
default:
// 8 col table;
}
return $pieces;
}
$arr = file("whspdel.txt");
foreach($arr as $e){
$lines[] = preg_replace('!\s!', ',', $e);
}
echo <<<THEAD
<table cellpadding=0 cellspacing=0>
<thead><th>Location</th><th>State</th><th>High</th><th>Low</th><th>Days Clear</th>
<th>Days Cloudy</th><th>Days with Preceipitation</th><th>Days with Snow</th>
THEAD;
$data = array_map("str_getcsv", $lines);
foreach($data as $row) {
$i=0;
echo "<tr>\n";
$better_row = join(" ",$row);
$split = fixCities($better_row);
foreach ($split as $e) {
if ($e == "") continue;
if ($i==0){
echo "<td class='left'>",$e,"</td>";
$i++;
}
else
{
echo "<td class='phpcolor'>",$e,"</td>";
}
}
echo "</tr>\n";
}
echo "</table>\n";
?>
converts the OP's text file into an HTML table, as depicted below:
<html>
<head>
<style>
table {
width:80%;
border:1px solid #fff;
box-shadow: -18px -8px 20px #ccc;
margin-left:13%;
}
tr {
width: 100%;
}
td {
background:transparent;
width:10%;
border-top:1px solid #003;
border-right:none;
border-left:none;
font: 62% Arial,Helvetica;
padding:6px;
text-align:right;
}
th {
font: 62% Arial,Helvetica;
}
.left {
text-align:left;
background:#ffdede;
}
.phpcolor {
background:#ccccff;
}
</style>
</head>
<body>
<table cellpadding=0 cellspacing=0>
<thead><th>Location</th>
<th>State</th><th>High</th><th>Low</th><th>Days Clear</th>
<th>Days Cloudy</th><th>Days with Preceipitation</th><th>Days with Snow</th>
<tr>
<td class='left'>Lander</td><td class='phpcolor'>WY</td><td class='phpcolor'>5557</td><td class='phpcolor'>99</td><td class='phpcolor'>-35</td><td class='phpcolor'>109</td><td class='phpcolor'>140</td><td class='phpcolor'>67</td></tr>
<tr>
<td class='left'>Milwaukee</td><td class='phpcolor'>WI</td><td class='phpcolor'>672</td><td class='phpcolor'>98</td><td class='phpcolor'>-7</td><td class='phpcolor'>79</td><td class='phpcolor'>177</td><td class='phpcolor'>124</td></tr>
<tr>
<td class='left'>Seattle</td><td class='phpcolor'>WA</td><td class='phpcolor'>400</td><td class='phpcolor'>94</td><td class='phpcolor'>12</td><td class='phpcolor'>55</td><td class='phpcolor'>233</td><td class='phpcolor'>163</td></tr>
<tr>
<td class='left'>Spokane</td><td class='phpcolor'>WA</td><td class='phpcolor'>2356</td><td class='phpcolor'>98</td><td class='phpcolor'>-16</td><td class='phpcolor'>95</td><td class='phpcolor'>187</td><td class='phpcolor'>112</td></tr>
<tr>
<td class='left'>Burlington</td><td class='phpcolor'>VT</td><td class='phpcolor'>332</td><td class='phpcolor'>91</td><td class='phpcolor'>6</td><td class='phpcolor'>54</td><td class='phpcolor'>217</td><td class='phpcolor'>168</td></tr>
<tr>
<td class='left'>Norfolk</td><td class='phpcolor'>VA</td><td class='phpcolor'>24</td><td class='phpcolor'>98</td><td class='phpcolor'>19</td><td class='phpcolor'>85</td><td class='phpcolor'>145</td><td class='phpcolor'>117</td></tr>
<tr>
<td class='left'>Richmond</td><td class='phpcolor'>VA</td><td class='phpcolor'>164</td><td class='phpcolor'>101</td><td class='phpcolor'>16</td><td class='phpcolor'>105</td><td class='phpcolor'>165</td><td class='phpcolor'>115</td></tr>
<tr>
<td class='left'>Salt Lake City</td><td class='phpcolor'>UT</td><td class='phpcolor'>4221</td><td class='phpcolor'>103</td><td class='phpcolor'>-11</td><td class='phpcolor'>129</td><td class='phpcolor'>152</td><td class='phpcolor'>86</td></tr>
<tr>
<td class='left'>Dallas</td><td class='phpcolor'>TX</td><td class='phpcolor'>551</td><td class='phpcolor'>106</td><td class='phpcolor'>10</td><td class='phpcolor'>130</td><td class='phpcolor'>152</td><td class='phpcolor'>89</td></tr>
<tr>
<td class='left'>Houston</td><td class='phpcolor'>TX</td><td class='phpcolor'>96</td><td class='phpcolor'>104</td><td class='phpcolor'>19</td><td class='phpcolor'>73</td><td class='phpcolor'>166</td><td class='phpcolor'>94</td></tr>
<tr>
<td class='left'>San Antonio</td><td class='phpcolor'>TX</td><td class='phpcolor'>788</td><td class='phpcolor'>102</td><td class='phpcolor'>16</td><td class='phpcolor'>90</td><td class='phpcolor'>165</td><td class='phpcolor'>83</td></tr>
<tr>
<td class='left'>Memphis</td><td class='phpcolor'>TN</td><td class='phpcolor'>258</td><td class='phpcolor'>101</td><td class='phpcolor'>12</td><td class='phpcolor'>121</td><td class='phpcolor'>151</td><td class='phpcolor'>112</td></tr>
<tr>
<td class='left'>Huron</td><td class='phpcolor'>SD</td><td class='phpcolor'>1281</td><td class='phpcolor'>100</td><td class='phpcolor'>-30</td><td class='phpcolor'>121</td><td class='phpcolor'>147</td><td class='phpcolor'>80</td></tr>
<tr>
<td class='left'>Rapid City</td><td class='phpcolor'>SD</td><td class='phpcolor'>3162</td><td class='phpcolor'>106</td><td class='phpcolor'>-30</td><td class='phpcolor'>106</td><td class='phpcolor'>127</td><td class='phpcolor'>95</td></tr>
</table>
</body>
</html>
One of the challenges with the data is that it was not in a CSV format. Some of the items were followed by multiple white space characters while others with one or two. So, my first thought was to reduce those characters and then convert the document to a CSV format.
Using array_map() provides a great convenience: avoids having to construct a for-loop and it speedily applies str_getcsv() to every line of the text file.
preg_replace() very helpful in replacing the excessive whitespace characters and the HTML entity came in handy for use with the city names consisting of more than one word.
I also use a heredoc to good advantage
Updated:
fixCities() now provides a more useful fix for cities out of alignment because their names consist of two or three words. Took a cue from #Chizzele as regards splicing to get the city names into proper alignment.
I read bytes from a file and process them. Afterwards I would like to save the packed bytes.
What is the recommended+generic way to convert an array with mixed objects/types to a byte string? In my case: array with int and string, pack types a,C,x.
A simplified example:
// $bytes = fread($handle, 100);
$bytes = "437XYZ25.011001DBEFORE ....";
$unpackString = "a3CPN/x8spare/CDSC/x4spare/a32OPT";
$unpacked = unpack($unpackString, $bytes);
var_dump($unpacked);
/*
array(3) {
["CPN"]=> string(3) "437"
["DSC"]=> int(49)
["OPT"]=> string(32) "BEFORE "
}
*/
// example of processing
$unpacked["DSC"] = 12;
$unpacked["OPT"] = "AFTER ";
// pack + write the result
// $packString = "a3x8Cx4a32";
$packTypes = ["a3","x8","C","x4","a32"];
$packFields = [ $unpacked["CPN"], null, $unpacked["DSC"], null, $unpacked["OPT"] ];
// ...
update: in the simplified example I have replaced $packString with $packTypes and $packFields to make sure it is clear what content belongs where and with what type.
I suppose what you're looking for is a way to call pack, which accepts arguments with an associative array as you have in your example. For this, we can use call_user_func_array which calls a function by its name and provides its arguments from a given array.
$bytes = "437XYZ25.011001DBEFORE ....";
$unpackString = "a3CPN/x8spare/CDSC/x4spare/a32OPT";
$unpacked = unpack($unpackString, $bytes);
// example of processing
$unpacked["DSC"] = 12;
$unpacked["OPT"] = "AFTER ";
// pack + write the result
$packTypes = ["a3", "x8", "C", "x4", "a32"];
$packFields = [$unpacked["CPN"], null, $unpacked["DSC"], null, $unpacked["OPT"]];
$packString = "";
$packArguments = [];
for ($i = 0; $i < count($packTypes); $i++){
$packString .= $packTypes[$i];
if ($packFields[$i] !== null){
// the null bytes don't use an argument
$packArguments[] = $packFields[$i];
}
}
// put packString as the first argument
array_unshift($packArguments, $packString);
$output = call_user_func_array("pack", $packArguments);
And $output would then be:
00000000 34 33 37 00 00 00 00 00 00 00 00 0c 00 00 00 00 |437.............|
00000010 41 46 54 45 52 20 20 20 20 20 20 20 20 20 20 20 |AFTER |
00000020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 | |
00000030
The script is supposed to output a list of profiles who have a certain item in their backpack, for example item "125" At the moment this is the output:
Defindex: 125 - 76561197992146126
Defindex: 56 - 76561197992146126
Defindex: 60 - 76561197992146126
Defindex: 115 - 76561197992146126
Defindex: 170 - 76561197992146126
Defindex: 182 - 76561197992146126
Defindex: 140 - 76561197992146126
Defindex: 261 - 76561197992146126
Defindex: 278 - 76561197992146126
Defindex: 277 - 76561197992146126
Defindex: 266 - 76561197992146126
Defindex: 295 - 76561197992146126
Full list at http://pastebin.com/G6bjzAwM.
Below is the desired output
Defindex: 125 - 76561197992146126
Defindex: 125 - 76561197995524521
Defindex: 125 - 76561197998542532
To do this all I need is a good IF function which will parse the results and IF (example) "125" then echo "Defindex: 125 - $profile"
<?php
$APIkey = 'MyAPIKey';
$profile = '76561197992146126';
$backpackURL = "http://api.steampowered.com/ITFItems_440/GetPlayerItems/v0001/?key=" . $APIkey . "&SteamID=" . $profile . "&format=json";
$userBackpack = json_decode(file_get_contents($backpackURL), true);
$result = $userBackpack['result'];
$items = $result['items'];
foreach($items['item'] as $ind=>$item) {
$defindex = $item['defindex'];
echo "Defindex: $defindex - $profile<br/>";
}
?>
I can make the script loop through a file, so that's not a big deal.
Well since you didn't give me any profiles to loop through I am going to assume you have that part figured out. For your foreach just use the code I have below.
foreach($items['item'] as $ind => $item) {
if ($item['defindex'] == 125) {
echo "Defindex: $defindex - $profile<br/>";
}
}
Is there something else that you needed to do? This seems pretty straightforward to me.
This should work
foreach($items['item'] as $ind => $item) {
if ($item['defindex'] == 125) {
echo "Defindex: {$item['defindex']} - $profile<br/>";
}
}
His works in theory, but in application he had a small issue
edit: did notice his comment, oh well incase someone forgets to read that, this is here.
i am creating a student management system and i want to be able to generate a pdf report that will contain every students data in its own page i.e . studentId, Math, English, Science, Class, totals, Rank, myClass and myTotals.for example, in the table below i would expect the pdf to have 6 pages. each containing only details of a particular student. how do i go about doing this?
Thank you in advance
studentId Math English Science Class totals Rank myClass myTotals
2 75 83 84 3p1 242 1 3p1 242
5 88 77 77 3p1 242 1 3p1 242
1 80 66 85 3p1 231 2 3p1 231
6 92 97 96 5p2 285 1 5p2 285
3 70 88 90 5p2 248 2 5p2 248
4 50 82 50 5p2 182 3 5p2 182
loop through the rows and create a new page for every row. How to do that exactly depends on with what you create the pdf.
You could output the information to a LaTeX file (assuming the machine has it installed). I assume you get all your student details from a database into an array of arrays called students. If you are using mySQL or similar to store the data, this should be simple enough.
This should generate a report.tex file and then execute pdflatex to generate report.pdf file.
<?
$f = fopen('report.tex','w');
$out = '\documentclass{article}
\usepackage{a4wide}
\begin{document}
';
fwrite($f,$out);
//Example array
$students = array(array('studentId'=> 1, 'Math'=> 1, 'English'=> 5 , 'Science' => 5, 'Class' =>6, 'totals'=>6 , 'Rank'=>7 , 'myClass' =>7, 'myTotals'=>9));
foreach($students as $x){
$out = '\begin{table}[htbp]'."\n".' \centering'."\n";
$out .= '\begin{tabular}{|r|r|r|r|r|r|r|r|r|}'."\n";
$out .= '\hline'."\n";
$out .= 'studentId & Math & English & Science & Class & totals & Rank & myClass & myTotals \\\\ '."\n \\hline \n";
$out .= "{$x['studentId']} & {$x['Math']} & {$x['English']} & {$x['Science']} & {$x['Class']} & {$x['totals']} & {$x['Rank']} & {$x['myClass']} & {$x['myTotals']} ";
$out .= '\\\\'."\n \\hline";
$out .= '\end{tabular} '."\n".' \end{table}'."\n".'\newpage' ."\n";
fwrite($f,$out);
}
fwrite($f,"\n".'\end{document}');
fclose($f);
echo (exec('pdflatex report.tex'));
echo "\ndone";
?>
The script works properly now, that produces a correct file. Having the server send the pdf to you shouldn't be too difficult.
I am to import a file, say june.txt that would have data such as the following data:
Sandy,820,384,133,18,408
Wanda,120,437,128,807,595
Jane,631,415,142,687,600
Andrea,179,339,349,594,986
Wanda,803,191,6,807,322
Jane,741,975,34,15,832
Jane,239,714,250,94,497
Andrea,219,188,411,584,713
And then the PHP would parse it into 2 difference ways:
The first way being all the names bundled together with totals, such as:
Sandy 820 384 133 18 408
Total 820 384 133 18 408
Jane 631 415 142 687 600
Jane 741 975 34 15 832
Jane 239 714 250 94 497
Total 1611 2104 426 796 497
Andrea 179 339 349 594 986
Andrea 219 188 411 584 713
Total 398 527 760 1178 1699
Wanda 120 437 128 807 595
Wanda 803 191 6 807 322
Total 923 628 134 1614 917
The second way would total and add the names together in a big list, such as
Sandy 820 384 133 18 408
Jane 1611 2104 426 796 497
Andrea 398 527 760 1178 1699
Wanda 923 628 134 1614 917
Any logic or suggestions would be helpful, I am new to PHP and not sure how this could even be done. My plan is to eventually display the results in HTML tables and have them sortable, but I can tackle that at a later date, Unless someone feels obligated to just add the and such for me in the parsing.
I think something useful for you would be the explode function.
As far as creating these views I'd start by loading all this data into an associative array of arrays based on the name, then iterate as necessary:
$datafile = file("filename.txt");
// reads lines into an associative array (key is the name) of arrays
// where each sub-array is a list of the records for each name
$arr = array();
foreach($datafile as $line){
$temp = explode(',', $line);
$arr[$temp[0]][] = $temp;
}
// iterate over each person
foreach($arr as $person_set){
// create an array to hold the sum of each column
// (and the name in the first column)
$totals = array();
$totals[0] = $person_set[0][0];
for($i = 1; $i < length($record); $i++){
$totals[$i] = 0;
}
// now iterate over each record for this person
foreach($person_set as $record){
// print a particular record
echo implode(' ', $record) . '<br>';
// add each column (1..end) to the totals array
for($i = 1; $i < length($record); $i++){
$totals[$i] += $record[$i];
}
}
// print out the totals line
echo implode(' ', $totals) . '<br><br>';
}
I'll leave formatting this data into a table as an exercise.
Well, to start, I'd real a file like that with PHP's fgetcsv(). Docs.
You could try a script like this:
<?php
echo "<table>";
$data = file("june.txt");
foreach($data as $month) {
$line = explode(',', $data);
echo '<tr><td>', implode('</td><td>', $line), '</td></tr>';
}
echo "</table>";
Edit:
My bad, didn't notice that you were sorting/grouping/totaling. This should set you on the right track, though. The key is to use $line as your source of information. Just compile it into an array and output later (instead of right in the loop).