I am trying to get my data to look like the output below. I have a SQL with a Inner Join to
connect two tables so they will output the data so it looks like the below. I am not sure how to do this could somebody give me a example
This is what the output looks like now
First American
2014-03-17 Blackwell Vancouver 115
First American
2014-04-21 Kurth Vancouver 85
Title Source
2014-01-04 Kurth Vancouver 85
Title Source
2014-03-11 Kurth Vancouver 85
Title Source
2014-03-17 Kurth Vancouver 85
This is what I would like it to look like
First American
2014-03-17 Blackwell Vancouver 115
2014-04-21 Kurth Vancouver 85
Title Source
2014-01-04 Kurth Vancouver 85
2014-03-11 Kurth Vancouver 85
2014-03-17 Kurth Vancouver 85
$result = $db->query("SELECT customers.name,signings.pid,signings.done,signings.signstart,
signings.lname,signings.ordern, signings.city,signings.samount,signings.prtamount,
signings.faxamount FROM customers Inner Join signings On signings.custid = customers.custid
WHERE signings.pid = 6 AND signings.done = 1 GROUP BY customers.name, signings.pid,
signings.done,signings.signstart");
foreach($result as $key => $income) {
echo $income['name'];
echo '<br>';
$total = $income['samount'] + $income['prtamount'] + $income['faxamount'];
echo $income['signstart'] . ' ' .$income['lname']. ' ' .$income['city'] . ' ' .$total;
echo '<br>';
}
You can modify your PHP like this:
$current=null;
foreach($result as $key => $income) {
if($current!=$income['name']){
echo $income['name'];
$current = $income['name'];
}
//other stuff
}
Hope this solves it!
Related
I have query returned from MySQL data that gets written to a html as a report, it looks very much like this. I would like to set a background color for the first row of each 'Dist' value, in this case rows 1,5,and 9 using CSS. But I am having no luck determining which row that is coming out of MySQL.
NO CALL First St, CO Dist Count
1 KD0NBH John MO Clay A 13
2 K0KEX Rick MO Platte A 12
3 N0SAX Jack MO Carroll A 12
4 W0NRP Neil MO Jackson A 12
5 WB0HLW Charles MO Macon B 8
6 KD0HHN Donald MO Marion B 4
7 AC0OK Sam MO Sullivan B 2
8 N9MAF Alan MO Marion B 1
9 KF0CTR Jeffrey MO St. Louis C 13
10 K0TPY Paul MO Franklin C 12
11 KA0P David MO St. Louis C 10
12 KD0CIV Dolores MO St. Louis C 9
$firstdist = ' ';
$liteitup = 'N';
foreach($db_found->query($sql) as $row) {
if ('$row[district])' <> '$firstdist')
{$liteitup = "Y";}
else {$liteitup = "N";}
$rowno = $rowno + 1;
$netcallsign = '$row[callsign]';
$Fname = ucfirst(strtolower('$row[Fname]'));
$Lname = '$row[Lname]';
$listing .= "<tr class='$liteitup'>
<TD>$rowno</td> <td>$row[callsign]</td>
<td>$row[Fname]</td> <td>$row[Lname]</td>
<td>$row[place]</td> <td>$row[cnt_call]</td>
</tr>";
$firstdist = "$row[district]";
} // End foreach
The class related to $liteitup would then be defined elsewhere. But to do this I need a way to determine which row of each district is the first row. Would some one please help me figure out the loop I need to do this?
Declare a variable before your foreach like $lastDist = null. Then inside the loop do something like
if($lastDist !== $currentDist) {
// set background color
$lastDist = $currentDist;
}
That's all there is to it. Whenever the dist changes between iterations that will trigger. null won't match anything so it'll hit on the first iteration too.
I've got a project that involves getting flight information from a database.
I want the results to look like:
01 Nov 2017 - Eastern Airways 7601
Departing from Southampton, Eastleigh (SOU) at 0840
However my code seems to bring up the below with a mysterious live of nonsense as per below
01 Nov 2017 - Eastern Airways 7601
Departing from , () at 084001 Nov 2017 - 7601 //this line I don't want
Departing from Southampton, Eastleigh (SOU) at 0840
$link = mysqli_connect('xxx', 'xxx', 'xxx', 'xxx' );
foreach ($flights as $b) {
$query = "SELECT * FROM `airlines` WHERE `iatacode` = '$airline' LIMIT 0 , 30;";
$query .= "SELECT * FROM `airports` WHERE `Airport Code` = '$depdest' LIMIT 0 , 30;";
/* Execute queries */
if (mysqli_multi_query($link, $query)) {
do {
/* store first result set */
if ($result = mysqli_store_result($link)) {
while ($row = mysqli_fetch_array($result))
/* print results */
{
$date=date_create($depdate);
echo "<b>" . date_format($date,"d M Y"). " - ". $row['airlinename']." ".$flightno. "</b><br/>";
echo "Departing from " .$row['City Name'].", ".$row['Airport Name']." (".$row['Airport Code'].") at " . $deptime;
}
mysqli_free_result($result);
}
} while (mysqli_next_result($link));
}
Can anyone see what I'm doing wrong?
Data base tables are as below
table 1 - Name "airports"
Airport ID City name Airport Code Airport Name Country name Country Abbrev. World Area Code
1, 108 Mile Ranch, ZMH , 108 Mile Ranch , Canada, CA, 906,
2, Aachen, AAH Aachen/Merzbruck, Germany, DE, 429,
3, Aachen, ZIU, Railway, Germany, DE, 429,
4, Aalborg, AAL, Aalborg, Denmark, DK, 419,
etc
table 2 - Name "airlines"
airline id, Iata, Airline Name
1, 0A, Amber Air,
2, 0B, Blue Air,
3, 0C, IBL Aviation,
etc
Thanks!
If I was to guess, I would say that there is a problem with the $b variable.
It seems your output is being written twice: the value "084001" in the middle of the second line looks to be the 0840 flight time from one entry, and then immediately starting the next entry as the date.
I would guess that the foreach is looping twice, instead of once, causing two outputs.
Could you do an output of the $b variable?
echo "<pre>";
print_r($b);
echo "</pre>";
I have text file having the following format
00151422 N8X 3V6 2013-11-11 00:19:00.000 IN patricksoong#hotmail.com E 200-2462 Howard Avenue Windsor ON CAN N8X3V6 M Dr. Patrick Soong
00331448 T6E 2R1 2010-03-01 00:00:00.000 IN atirlea#yahoo.com E 9743 88 Ave NW Edmonton AB CAN T6E2R1 Alina Tirlea Engstrom
00364578 K7N 1A3 2011-01-12 00:00:00.000 IN E 4463 Bath Rd Amherstview ON CAN K7N1A3 M Mr. Martin Kandler
The above positional text file contains 3 records and 20 fields in each record. Also I now the size for each column. How will i read records and fields with in a record using PHP?
Size of fields are
f1=8;f2=10;f3=10;f4=10;f5=255;f6=50;f7=255;f8=10;f9=10;f10=50;f11=50;f12=1;f13=20;f14=50;f15=50;f16=60;f17=10;f18=20;f19=20;f20=1;
Use a substr() inside some kind of loop. Untested, but should give you an idea:
$lengths = [8,10,10]; // define all the lengths
function fixed_width_data_to_array($data, $lengths) {
foreach($rows as $row) {
$position = 0; // start at the beginning of the row
foreach($lengths as $length) {
// add current field to array
$my_data[] = trim(substr($row, $position, $length));
// move the 'pointer' to the start of the next field
$position += $length;
}
// add current row to an array
$my_array[] = $my_data;
}
return $my_array;
}
I was given a file that contains something similar to this kind of structure:
12345 ABC 100M 001 2.0 ABC 1010 4510 A01 451 Apple, Johnny A 150
12345 ABC 100M 011 2.0 ABC 1010 4510 A01 451 Apple, Johnny A 80
12345 ABC 100 011 2.0 ABC 1010 4510 A01 451 Apple, Johnny A 80
I need to grab the following sections from this file:
Group together the third column (ie. 100M) if they are similar
Add together the fourth column (if they are in the same group as the third column)
Add up the last column depending on the fourth column
I've managed to do the following:
$List1 = array();
$grab = fopen("file.txt", "r") or die("Can't open file");
$check = fgets($grab);
while(!feof($grab)) {
if (ereg("^[[:digit:]]{5} +ABC +([[:digit:]]{3}[[:alpha:]]?)+ ([[:digit:]]{3})",
$check, $output)) {
if (!in_array($output[1], $List1)) {
array_push($List1, $output[1]);
}
if (!in_array($output[2], $List1)) {
array_push($List1, $output[2]);
}
}
$check = fgets($grab);
}
fclose($grab);
foreach ($List1 as $list) {
print "$list <br/>";
}
I have managed to somehow group together the third column.
The fourth column is being displayed, but I'm not sure how to group it together into the third column if it's under the same group.
And I'm not sure how to easily grab the last bit in the file/array.
Is there a shortcut to getting the last in a file and adding them up?
Thanks in advance for anyone who can help me.
This should do it:
$string = '12345 ABC 100M 001 2.0 ABC 1010 4510 A01 451 Apple, Johnny A 150
12345 ABC 100M 011 2.0 ABC 1010 4510 A01 451 Apple, Johnny A 80
12345 ABC 100 011 2.0 ABC 1010 4510 A01 451 Apple, Johnny A 80';
$third = array();
$fourth = array();
foreach (explode("\n", $string) as $line)
{
// Skip empty lines.
if (empty($line))
continue;
// Clean up any excessive white space.
$line = trim(preg_replace('~[\s]{2,}~', ' ', $line));
$info = explode(' ', $line);
if (!isset($third[$info[2]]))
$third[$info[2]] = array();
$third[$info[2]][] = $info;
if (!isset($fourth[$info[3]]))
$fourth[$info[3]] = 0;
$fourth[$info[3]] += (int) end($info);
}
print_r(array(
'third' => $third,
'fourth' => $fourth,
));
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.