How to print table from php file to TCPDF in following scenario? - php

I have a php file(add_member.php). I'm creating a dynamic table by executing SQL query. The code of this file is as follows:
$sql = SELECT * FROM member_details WHERE lname='ABC';
$n = new data();
$res = $n -> querySend($sql);
?>
<table border="1" cellpadding="3" cellspacing="1">
<tr>
<td align="center"></td>
<td align="center">First Name</td>
<td align="center">Last Name</td>
<td align="center">Caste</td>
<td align="center">Residence address</td>
<td align="center">Education</td>
</tr>
<?php
$i=1;
while($row = mysql_fetch_array($res))
{
$member_no = $row['member_no'];
$total_member = "SELECT COUNT(*) AS total_member FROM family_member_details WHERE member_no =" .$member_no;
$total_res = $n -> querySend($total_member);
$row_total = mysql_fetch_array($total_res);
?>
<tr>
<td align="center" rowspan="<?php echo $row_total['total_member']+1;?>"><?php echo $i;?></td>
<td align="center"><?php echo $row['fname'];?></td>
<td align="center"><?php echo $row['lname'];?></td>
<td align="center"><?php echo $row['caste'];?></td>
<td align="center"><?php echo $row['residence_addr'];?></td>
<td align="center"><?php echo $row['education'];?></td>
</tr>
<?php
$family_sql = "SELECT * from family_member_details WHERE member_no = $member_no";
$family_res = $n -> querySend($family_sql);
while($row1 = mysql_fetch_array($family_res))
{
?>
<tr>
<td align="center"><?php echo $row1['name']?></td>
<td align="center"><?php echo $row1['name']?></td>
<td align="center"><?php echo $row1['name']?></td>
<td align="center"><?php echo $row1['name']?></td>
<td align="center"><?php echo $row1['name']?></td>
</tr>
<?php }
$i++;
} ?>
</table>
Now upon clicking on a button I want to create the same table into PDF file. For this purpose I decided to use TCPDF library. But for it I've to provide the HTML content to TCPDF file. Now my issue is how should I get the HTML of a dynamically generated table from PHP file and write this content to the text file? Can anyone please help me in this regard? Any help would be highly appreciated.

Instead of displaying your table directly to the browser page, simply store the text in a variable and echo it...then you can send the var to the TCPDF library.
$dyn_table = '<table border="1" cellpadding="3" cellspacing="1"><tr><td align="center">/td><th align="center">First Name</th><th align="center">Last Name</th><th align="center">Caste</th><th align="center">Residence address</th><th align="center">Education</th></tr>';
$i = 1;
while ($row = mysql_fetch_array($res)) {
$member_no = $row['member_no'];
$total_member = "SELECT COUNT(*) AS total_member FROM family_member_details WHERE member_no =" . $member_no;
$total_res = $n->querySend($total_member);
$row_total = mysql_fetch_array($total_res);
$dyn_table .= '<tr><td align="center" rowspan="' . $row_total['total_member'] + 1 . '">' . $i . '</td><td align="center">' . $row['fname'] . '</td><td align="center">' . $row['lname'] . '</td><td align="center">' . $row['caste'] . '</td><td align="center">' . $row['residence_addr'] . '</td><td align="center">' . $row['education'] . '</td></tr>';
$family_sql = "SELECT * from family_member_details WHERE member_no = $member_no";
$family_res = $n->querySend($family_sql);
while ($row1 = mysql_fetch_array($family_res)) {
$dyn_table .= '<tr><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td></tr>';
}
$i++;
}
$dyn_table .= '</table>';
echo $dyn_table;
EDIT
In order to post this html to your TCPDF library, I would use AJAX to prevent another page request/load. I prefer to use JQuery as it simplifies this process immensely. Here is one way you could do it:
<input type="button" name="TCPDF" id="submitToTCPDF" />
<script type="text/javascript">
var url = 'php/script/to/handle/post';
var data = {'table_html': '<? echo $dyn_table; ?>'};
$('#TCPDF').click(function(){
$.ajax({
type: "POST",
url: url,
data: data,
success: function($result){
// Do whatever after html is submitted
}
});
});
</script>
You can read more about Jquery's AJAX post method in this StackOverflow question.

Related

Display mysql query results on a new page from links

I create a table and display entries from MySQL in a brief table report format. Each of the entries in the table have further properties that I would like to display on a separate page when I click on the link in their names.
<?php
require_once 'includes/db.php';
connect();
$query = "SELECT * FROM persons order by date_entered DESC LIMIT 5";
$response = mysqli_query($con, $query);
if($response){
echo '<div id="frm"> <h2> Last 5 candidates entered </h2> <table align="left">
<tr>
<td align="left"><b>First Name</b></td>
<td align="left"><b>Email</b></td>
<td align="left"><b>Sex</b></td>
<td align="left"><b>City</b></td>
<td align="left"><b>Phone Number</b></td>
<td align="left"><b>Education</b></td>
<td align="left"><b>Salary</b></td>
</tr>';
while($row = mysqli_fetch_array($response)){
echo '<tr>
<td align="left">' . '' . $row["first_name"] . " " .$row['last_name'] . '' . '</td>
<td align="left">' . $row['email'] . '</td>
<td align="left">' . $row['sex'] . '</td>
<td align="left">' . $row['city'] . '</td>
<td align="left">' . $row['phone'] . '</td>
<td align="left">' . $row['education'] . '</td>
<td align="left">' . $row['salary'] . '</td>';
echo '</tr>';
}
echo '</table>';
} else {
echo "Couldn't issue database query<br />";
echo mysqli_error($con);
}
mysqli_close($con);
?>
This gives me a table report with links in the names:
brief report with link in names
My question is: if I click Mary Swanson in the table, what variables do I use in person.php so I can retrieve the MySQL data for that record?
Thanks.
You could pass this id trough the link as a GET variable and use it on the other side to query the database for that specific record like this:
<?php
require_once 'includes/db.php';
connect();
$query = "SELECT * FROM persons order by date_entered DESC LIMIT 5";
$response = mysqli_query($con, $query);
if($response){
echo '<div id="frm"> <h2> Last 5 candidates entered </h2> <table align="left">
<tr>
<td align="left"><b>First Name</b></td>
<td align="left"><b>Email</b></td>
<td align="left"><b>Sex</b></td>
<td align="left"><b>City</b></td>
<td align="left"><b>Phone Number</b></td>
<td align="left"><b>Education</b></td>
<td align="left"><b>Salary</b></td>
</tr>';
while($row = mysqli_fetch_array($response)){
echo '<tr>
<td align="left">' . '' . $row["first_name"] . " " .$row['last_name'] . '' . '</td>
<td align="left">' . $row['email'] . '</td>
<td align="left">' . $row['sex'] . '</td>
<td align="left">' . $row['city'] . '</td>
<td align="left">' . $row['phone'] . '</td>
<td align="left">' . $row['education'] . '</td>
<td align="left">' . $row['salary'] . '</td>';
echo '</tr>';
}
echo '</table>';
} else {
echo "Couldn't issue database query<br />";
echo mysqli_error($con);
}
mysqli_close($con);
?>
Then you can access the variable on the other side like this $_GET["id"])
<?php
require_once 'includes/db.php';
connect();
$query = "SELECT * FROM persons where id = " . $_GET["id"]);
$response = mysqli_query($con, $query)
After this you can print the values returned by the query as if where a multiple row select, but it will only come back with one row, you still need to do the while since what it returns is an array.

How to convert this echo? using this approach

how to convert this?
<td><a style="font weight: 700; font-size:1.2em;" href="preview.php? app_id=<?php echo $row['app_id']; ?>name=<?php echo $row['name']; ?>">PRINT</a> <!-- Delete --></td>
into this?
echo '<td>' . $row['curdate'] . '</td>'
here's the full code
<tr class="success">
<td><?php echo $row['app_id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['addr']; ?></td>
<td><?php echo $row['contact']; ?></td>
<td><?php echo $row['curdate']; ?></td>
<td><a style="font weight: 700; font-size:1.2em;" href="preview.php? app_id=<?php echo $row['app_id']; ?>name=<?php echo $row['name']; ?>">PRINT</a> <!-- Delete --></td>
</tr>
into this?
echo '<tr>';
echo '<td>' . $row['app_id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['addr'] . '</td>';
echo '<td>' . $row['contact'] . '</td>';
echo '<td>' . $row['curdate'] . '</td>';
echo 'PRINT CODE GOES HERE</td>';
You can try something like this :
$html = '<tr class="success">
<td>'. $row['app_id'].'</td>
<td>'.$row['name'].'</td>
<td>'.$row['addr'].'</td>
<td>'.$row['contact'].'</td>
<td>'.$row['curdate'].'</td>
<td><a style="font weight: 700; font-size:1.2em;" href="preview.php?app_id='.$row['app_id'].'name='.$row['name'].'">PRINT</a> <!-- Delete --></td>
</tr>';
echo $html;
You can try this :
echo '<tr class="success">
<td>'. $row['app_id'].'</td>
<td>'.$row['name'].'</td>
<td>'.$row['addr'].'</td>
<td>'.$row['contact'].'</td>
<td>'.$row['curdate'].'</td>
<td><a style="font weight: 700; font-size:1.2em;" href="preview.php?app_id='.$row['app_id'].'&name='.$row['name'].'">PRINT</a> </td>
</tr>';
once again, so "many ways to skin a cat"! (it's a Welsh idiom!)
currently I prefer this:
echo <<<HTML
<tr class="success">
<td>{$row['app_id']}</td>
<td>{$row['name']}</td>
<td>{$row['addr']}</td>
<td>{$row['contact']}</td>
<td>{$row['curdate']}</td>
<td><a style="font weight: 700; font-size:1.2em;" href="preview.php?app_id={$row['app_id']}&name={$row['name']}">PRINT</a> <!-- Delete --></td>
</tr>
HTML;
this way you don't have to worry about: dropping in and out of php (<?php, ?>); multiple echo statements; string concatenation with '. and .'; escaping any ' or " depending on which you used for your echo.

SQL query doesn't give the right result

I have a form where people can search the database for four values:
Location, Period, Day and Service. I always do not get the results that I want.
If I use AND, people need to fill in everything. If I use OR I get the complete database. I want to be able to search the database for those one to 4 things. Is there a way how I can do this?
Is there maybe a way to check which fields are filled in, and that the query is automatically changed with the filled in fields?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Zoeken</title>
</head>
<body>
<p><img src="add.png" width="20px" height="20px"/> | <img src="search.png" width="20px" height="20px"/> | <img src="number.png" width="20px" height="20px"/> </p>
<form action="" method="post">
<div>
<table>
<tr><td><strong>Locatie: </strong></td><td><input type="text" name="Locatie" value="" /></td> </tr>
<tr><td><strong>Periode: </strong></td><td><input type="text" name="Periode" value="" /></td> </tr>
<tr><td><strong>Dag: </strong></td><td><input type="text" name="Dag" value="" /></td> </tr>
<tr><td><strong>Dienst: </strong></td> <td><input type="text" name="Dienst" value="" /></td></tr>
<tr><td></td><td><input type="submit" name="zoeken" value="Zoeken"></td></tr>
</table>
</div>
</form>
<p><img src="add.png" width="20px" height="20px"/> | <img src="search.png" width="20px" height="20px"/> | <img src="number.png" width="20px" height="20px"/> </p>
</body>
</html>
<?php
if (isset($_POST['zoeken']))
{
include('connect-db.php');
$Locatie = $_POST['Locatie'];
$Periode = $_POST['Periode'];
$Dag = $_POST['Dag'];
$Dienst = $_POST['Dienst'];
// get results from database
$result = mysql_query("SELECT * FROM WMC_DeLijn WHERE Locatie='$Locatie' ANY Periode='$Periode' ANY Dag='$Dag'ANY Dienst='$Dienst' ")
or die(mysql_error());
// display data in table
echo "<h2>Resultaten:</h2><p>";
echo "<table border='1' cellpadding='10'>";
echo "<table><tr><th>ID</th><th>Locatie</th><th>Periode</th><th>Dag</th><th>Dienst</th><th>Delen</th><th>Geleed</th><th>Start 1</th><th>Eind 1</th><th>Start 2</th><th>Eind 2</th><th>Lijnen</th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td align="center">' . $row['id'] . '</td>';
echo '<td align="center">' . $row['Locatie'] . '</td>';
echo '<td align="center">' . $row['Periode'] . '</td>';
echo '<td align="center">' . $row['Dag'] . '</td>';
echo '<td align="center">' . $row['Dienst'] . '</td>';
echo '<td align="center">' . $row['Delen'] . '</td>';
echo '<td align="center">' . $row['Geleed'] . '</td>';
echo '<td align="center">' . $row['Start1'] . '</td>';
echo '<td align="center">' . $row['Eind1'] . '</td>';
echo '<td align="center">' . $row['Start2'] . '</td>';
echo '<td align="center">' . $row['Eind2'] . '</td>';
echo '<td align="center">' . $row['Lijnen'] . '</td>';
//Link to edit record
echo '<td align="center"><img src="edit.png" width="20px" height="20px"/></td>';
// Link to delete record
echo '<td align="center"><img src="delete.png" width="20px" height="20px"/></td>';
//Link to Add Event to Google Calendar
echo '<td align="center"><img src="proceed.png" width="20px" height="20px"/></td>';
echo "</tr>";
}}
// close table>
echo "</table>";
?>
You can either build the query string dynamically, only adding WHERE clause statements if the parameter is not falsy, or add conditions in the SQL itself like so: WHERE (col = ? OR '' = ?) AND (col2 = ? OR '' = ?).
Change your query to have either a AND condition or a OR condition like
SELECT * FROM WMC_DeLijn
WHERE Locatie='$Locatie'
AND Periode='$Periode'
AND Dag='$Dag'
AND ienst='$Dienst';
(OR)
SELECT * FROM WMC_DeLijn
WHERE Locatie='$Locatie'
OR Periode='$Periode'
OR Dag='$Dag'
OR ienst='$Dienst';

making hyperlink of a value stored in table column

How to make the values inserted in table column($row['survey_name']) a hyperlink. this is my code which I have tried in php:
while($row=mysql_fetch_array($result1))
{
$content .= "
<tr id=\"special\" style=\"background:;\">
<td>". $row['survey_name'] . "</td>
<td>" . $row['date'] . "</td>
</tr>
";
?>
<script
$('#special').onclick(function(){window="http://urllinkto.com/x/y/z";})
</script>
<?php }//end of while
if ($content) {
// See note on the "quote switch" here
echo '
<table border= "5" cellpadding="2" cellspacing="2" width="100%">
<tr>
<td><strong>Survey Name</strong></td>
<td><strong>Date Created</strong></td>
/tr>' .
$content . '
</table> ';
Chage
<td>". $row['survey_name'] . "</td>
to:
<td><a href='URL HERE'>". $row['survey_name'] . "</a></td>
Try to declare variables first.
while($row=mysql_fetch_array($result1)){
$surveyname = $row['survey_name']
$date = $row['date']
$content .= "
<tr id=\'special\' style=\"background:;\">
<td><a href='#'>echo $surveyname</a> </td>
<td><a href='#'>echo $date</a></td>
</tr>
";
}

Build Every 4 columns a row

How can I make every 4 columns it's own row in a table? Please don't tell me I should do them as div's, this needs to be a table...
Here's my code:
$tmpRet = array_filter($ret, function($e){
return ($e['categoryID'] == 6) ? $e : null;
});
$tmpRet = array_values($tmpRet);
$tmpCt = count($tmpRet);
$counter = 0;
echo '<table width="100%" cellpadding="10" cellspacing="5">' . PHP_EOL;
for($i = 0; $i < $tmpCt; ++$i){
$counter ++;
echo ($i == 0 || $i % 4 === 0) ? ' <tr>' . PHP_EOL : null;
echo '<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></td>' . PHP_EOL;
echo ($i == 0 || $i % 4 === 0) ? ' </tr>' . PHP_EOL : null;
}
echo '</table>';
And It's spitting out:
<table width="100%" cellpadding="10" cellspacing="5">
<tr>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/ADP_logo_tag_R_rgb 4-11-13.jpg" alt="ADP" title="ADP" style="max-height:85px;" /></td>
</tr>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Ascensus - color.jpg" alt="Ascensus" title="Ascensus" style="max-height:85px;" /></td>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Bridgepoint 2012.JPG" alt="Bridgepoint" title="Bridgepoint" style="max-height:85px;" /></td>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/BrightScope_Logo JPEG 10-10-12 approved.jpg" alt="BrightScope" title="BrightScope" style="max-height:85px;" /></td>
<tr>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Corporate Insight Logo JPEG 9-26-12.JPG" alt="Corporate Insight" title="Corporate Insight" style="max-height:85px;" /></td>
</tr>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Guardian Logo from 2011 OK.JPG" alt="Guardian" title="Guardian" style="max-height:85px;" /></td>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Infosys McCamish_natural_vert_nt jpeg.JPG" alt="Infosys McCamish" title="Infosys McCamish" style="max-height:85px;" /></td>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/INGC300.JPG" alt="ING" title="ING" style="max-height:85px;" /></td>
<tr>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/JANUS logoJPEG created 9-20-12.jpg" alt="Janus Capital Group" title="Janus Capital Group" style="max-height:85px;" /></td>
</tr>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/JP Morgan Logo for infotree.JPG" alt="JP Morgan" title="JP Morgan" style="max-height:85px;" /></td>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Newkirk a DST Co 4-23-13.JPG" alt="Newkirk" title="Newkirk" style="max-height:85px;" /></td>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Oculus_logo_v4-vert.jpg" alt="Oculus" title="Oculus" style="max-height:85px;" /></td>
<tr>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Pioneer Investments Jpeg ogo.JPG" alt="Pioneer Investments" title="Pioneer Investments" style="max-height:85px;" /></td>
</tr>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/ProcessUnity logo JPEG created 9-20-12.jpg" alt="ProcessUnity" title="ProcessUnity" style="max-height:85px;" /></td>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Prudential Blue Gray for Lanyards.JPG" alt="Prudential" title="Prudential" style="max-height:85px;" /></td>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/RR Donnelley Logo for Mints.JPG" alt="RR Donnelley" title="RR Donnelley" style="max-height:85px;" /></td>
<tr>
<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/sp_capitaliq_fc_logo.jpg" alt="S&P Capital IQ" title="S&P Capital IQ" style="max-height:85px;" /></td>
</tr>
</table>
Try replace the relevant lines in your code with these:
// At column 0 you create a new row <tr>
echo $i % 4 == 0 ? "<tr>\n" : "";
// At column 3 you end the row </tr>
echo $i % 4 == 3 ? "</tr>\n" : "";
This creates 4 columns per row.
Edit to accommodate cases where there may be coluumns that are not a multiple of 4
echo "<table>\n";
$colSpan = 4;
$rows = 0;
for($i = 0; $i < 5; $i++) {
// At column 0 you create a new row <tr>
if($i % $colSpan == 0) {
$rows++;
echo "<tr>\n";
}
echo "<td>" . ($i + 1) . "</td>\n";
// At column 3 you end the row </tr>
echo $i % $colSpan == 3 ? "</tr>\n" : "";
}
// Say you have 5 columns, you need to create 3 empty <td> to validate your table!
for($j = $i; $j < ($colSpan * $rows); $j++) {
echo "<td></td>\n";
}
// Add the final <tr>
if(($colSpan * $rows) > $i) {
echo "</tr>\n";
}
echo "</table>\n";
And this should create near enough perfect <table></table> structure! Enjoy.
What you want to echo a </tr> right before you echo a <tr> unless it's the first time through.
echo "<tr>";
for($i = 0; $i < $tmpCt; ++$i){
$counter ++;
// echo a row close right before you row open unless it's the first one
echo ($i != 0 || $i % 4 === 0) ? ' </tr><tr>' . PHP_EOL : null;
echo '<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></td>' . PHP_EOL;
}
// close out the last row
echo "</tr>";
Try it with nested for loops:
for($i = 0; $i < $tmpCt; ++$i){
$counter ++;
echo ' <tr>' . PHP_EOL;
for($j = 0; $j < 4 && $i < $tmpCt; ++$j, ++$i){
echo '<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></td>' . PHP_EOL;
}
echo ' </tr>' . PHP_EOL;
}
One thing that might go wrong is that maybe PHP doesn't support for loops where two incrementations happen (haven't tested it), so if it doesn't, just put the ++$i at the end of the inner for loop.
This will go through 4 columns or until the last column is found. This way $j is a simple counter to 4, and $i is still the reference for the array. Now in pseudocode, it's
for(every column){
start row;
for(4 iterations){
print or use the contents of a column;
}
end row;
}
EDIT
As OrangePill suggested, this would also work
for($i = 0; $i < $tmpCt; $i+=4){
$counter ++;
echo ' <tr>' . PHP_EOL;
for($j = 0; $j < 4 && $i+$j < $tmpCt; ++$j){
echo '<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i+$j]['showType']) . '/images/sponsors/' . $tmpRet[$i+$j]['sponFileName'] . '" alt="' . $tmpRet[$i+$j]['sponName'] . '" title="' . $tmpRet[$i+$j]['sponName'] . '" style="max-height:85px;" /></td>' . PHP_EOL;
}
echo ' </tr>' . PHP_EOL;
}
echo '<table width="100%" cellpadding="10" cellspacing="5">' . PHP_EOL . '<tr>' . PHP_EOL;
for($i = 1; $i <= $tmpCt; ++$i){
echo '<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></td>' . PHP_EOL;
echo ($i % 4 === 0) ? ' <tr>' . PHP_EOL .'</tr>' . PHP_EOL : null;
}
for($j = 1; $j <= (4 - $tmpCT % 4); $j++){
echo '<td width="25%"> </td>' . PHP_EOL;
}
echo '</tr>' . PHP_EOL . '</table>';
You've got a couple issues.
First, you're wrapping you first and every fourth cell in a tr.
Try changing this:
echo '<table width="100%" cellpadding="10" cellspacing="5">' . PHP_EOL;
for($i = 0; $i < $tmpCt; ++$i){
$counter ++;
echo ($i == 0 || $i % 4 === 0) ? ' <tr>' . PHP_EOL : null;
echo '<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></td>' . PHP_EOL;
echo ($i == 0 || $i % 4 === 0) ? ' </tr>' . PHP_EOL : null;
}
echo '</table>';
To this:
echo '<table width="100%" cellpadding="10" cellspacing="5">' . PHP_EOL;
echo '<tr>';//start first row
for($i = 0; $i < $tmpCt; ++$i){
$counter ++;
echo '<td width="25%" align="center" valign="middle"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></td>' . PHP_EOL;
echo ($i == 0 || $i % 4 === 0) ? '</tr><tr>' . PHP_EOL : null;//end row after every fourth cell and start a new one
}
echo '</tr>';//close out last row
echo '</table>';
Depending on how your page is laying out, you may have to insert some empty table cells in the last row, in case your record count isn't evenly divisible by 4.

Categories