I have this code:
<?php
if( isset($_POST['groups'])){
$groups = $_POST['groups'];
$subject = $_POST['subject'];
$sql="SELECT
a.groupcode, a.groupstudents, a.studentid,
b.groupcode, b.coursename, b.studentid, b.date, b.class1, b.attend, b.attendno
FROM table_1 a, table_2 b
WHERE b.groupcode = '$groups' AND b.coursename = '$subject' AND
(a.studentid = b.studentid AND a.groupcode = b.groupcode)";
$result=mysqli_query($GLOBALS["___mysqli_ston"], $sql); ?>
<table width="100%" border="1" cellspacing="0" cellpadding="3" >
<tr>
<td align="center"><strong><font size="2">Students</font></strong></td>
<td align="center"><strong><font size="2">Date</font></strong></td>
<td align="center"><strong><font size="2">Attendance</font></strong> </td>
</tr>
<?php
while($rows=mysqli_fetch_array($result)){
$date = $rows['date']; $date2 = date("d-F-Y", strtotime($date));
$class1 = $rows['class1'];
if ($class1 == 0) $class1 = "No Class"; if ($class1 == 1) $class1 = "Absent";
if ($class1 == 3) $class1 = "Present"; if ($class1 == 2) $class1 = "Late";
?>
<tr>
<td align="center"><font size="2"><?php echo $rows['groupstudents']; ?></font> </td>
<td align="center"><strong><font size="2"><?php echo $date2; ?></font></strong> </td>
<td align="center"><font size="2"><?php echo $class1; ?></font></td>
</tr>
<?php
}
?>
which gives the below output.
Now my question is how to modify my code (use nested loops?!) so the output is:
Thank you kindly.
NB: Sorry, I do not have enough reputation to attach images. I have uploaded them on an external site.
It may not be the best solution, but I cannot think of something better right now.
In the pre-execution I create the grid you want, and in the layout this grid-array is displayed.
<?php
if( isset($_POST['groups'])){
$groups = $_POST['groups'];
$subject = $_POST['subject'];
$sql="SELECT
a.groupcode, a.groupstudents, a.studentid,
b.groupcode, b.coursename, b.studentid, b.date, b.class1, b.attend, b.attendno
FROM table_1 a, table_2 b
WHERE b.groupcode = '$groups' AND b.coursename = '$subject' AND
(a.studentid = b.studentid AND a.groupcode = b.groupcode)";
$result=mysqli_query($GLOBALS["___mysqli_ston"], $sql);
$dates = array();
$display = array();
while ($rows=mysqli_fetch_array($result)) {
if (!isset($display[$rows['groupstudents']])) {
$display[$rows['groupstudents']] = array();
}
if (!isset($dates[strtotime($rows['date'])])) {
$dates[strtotime($rows['date'])] = count($dates);
}
$class1 = $rows['class1'];
if ($class1 == 0) $class1 = "No Class"; if ($class1 == 1) $class1 = "Absent";
if ($class1 == 3) $class1 = "Present"; if ($class1 == 2) $class1 = "Late";
$display[$rows['groupstudents']][$dates[strtotime($rows['date'])]] = $class1;
}
echo '<table width="100%" border="1" cellspacing="0" cellpadding="3">';
echo '<tr>';
echo '<td align="center"><strong><font size="2">Students</font></strong></td>';
foreach ($dates as $date => $reversedIndex) {
echo '<td align="center"><strong><font size="2">' . date("d-F-Y", $date) . '</font></strong></td>';
}
echo '</tr>';
foreach ($display as $student => $row) {
echo '<tr>';
echo '<td align="center"><font size="2">' . $student . '</font></td>';
foreach ($dates as $date => $index) {
echo '<td align="center"><font size="2">';
if (isset($row[$index])) {
echo $row[$index];
} else {
echo '';
}
echo '</font></td>';
}
echo '</tr>';
}
echo '</table>';
?>
Related
I am very new at using PHP and am trying to populate a table with data from a MySQL database, but the results that display are dotted all over the table and are not in their relevant columns at the top of the table.
Attached is a screenshot of where I need the data to be.
Here's the code.
Any help is appreciated.
<?php
$server = mysql_connect("xxxx", "xxxx", "xxxxx");
$db = mysql_select_db("xxxxxx", $server);
$query = mysql_query("SELECT * FROM stores");
?>
<html>
<head>
<title>Stores Tracker Dashboard</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p align="center"><font face="Arial"><strong>Stores Tracker Dashboard</strong></font></p>
<table align="center" border="1" width="90%">
<tr>
<td align="center"><font face="Arial"><strong>Not Started</strong></font></td>
<td align="center"><font face="Arial"><strong>In Transit</strong></font></td>
<td align="center"><font face="Arial"><strong>Awaiting Build</strong></font></td>
<td align="center"><font face="Arial"><strong>Being Built</strong></font></td>
<td align="center"><font face="Arial"><strong>On Return</strong></font></td>
<td align="center"><font face="Arial"><strong>Received Back</strong></font></td>
</tr>
<?php
while ($row = mysql_fetch_array($query)) {?>
<tr>
<td align="center"><font face="Arial"><?php if ($row['status'] == "Not Started") { echo $row['ticket'], ' (', $row['site'], ')'; }?></font></td>
<td align="center"><font face="Arial"><?php if ($row['status'] == "In Transit") { echo $row['ticket'], ' (', $row['site'], ')'; }?></font></td>
<td align="center"><font face="Arial"><?php if ($row['status'] == "Awaiting Build") { echo $row['ticket'], ' (', $row['site'], ')'; }?></font></td>
<td align="center"><font face="Arial"><?php if ($row['status'] == "Being Built") { echo $row['ticket'], ' (', $row['site'], ')'; }?></font></td>
<td align="center"><font face="Arial"><?php if ($row['status'] == "On Return from CFC2") { echo $row['ticket'], ' (', $row['site'], ')'; }?></font></td>
<td align="center"><font face="Arial"><?php if ($row['status'] == "Received") { echo $row['ticket'], ' (', $row['site'], ')'; }?></font></td>
</tr>
<?php } ?>
This one uses PDO.
Please take your time and learn what is going on there!
<?php
$host = "set_hostname_here";
$daba = "set_database_here";
$user = "set_loginname_here";
$pass = "set_password_here";
$notstarted = array();
$intransit = array();
$awaiting = array();
$built = array();
$cfc2 = array();
$received = array();
try {
//open the PDO-connection to a MySQL-host
$DBH = new PDO("mysql:host=$host;dbname=$daba", $user, $pass);
//error mode set to EXCEPTION (ERRMODE_SILENT or ERRMODE_WARNING are also possible)
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
//execute the query
$STH = $DBH->query('SELECT status, ticket, site FROM stores');
//set default fetch mode to FETCH_ASSOC (if you want to try FETCH_OBJ)
$STH->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
echo $e->getMessage();
}
//go through all results and assign them to the related arrays
while($row = $STH->fetch()) {
$data = $row['ticket'] . ' (' . $row['site'] . ')';
if ($row['status'] == "Not Started") { $notstarted[] = $data; }
if ($row['status'] == "In Transit") { $intransit[] = $data; }
if ($row['status'] == "Awaiting Build") { $awaiting[] = $data; }
if ($row['status'] == "Being Built") { $built[] = $data; }
if ($row['status'] == "On Return from CFC2") { $cfc2[] = $data; }
if ($row['status'] == "Received") { $received[] = $data; }
}
//close the connection
$DBH = null;
?>
<html>
<head>
<title>Stores Tracker Dashboard</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p align="center"><font face="Arial"><strong>Stores Tracker Dashboard</strong></font></p>
<table align="center" border="1" width="90%">
<tr>
<td align="center"><font face="Arial"><strong>Not Started</strong></font></td>
<td align="center"><font face="Arial"><strong>In Transit</strong></font></td>
<td align="center"><font face="Arial"><strong>Awaiting Build</strong></font></td>
<td align="center"><font face="Arial"><strong>Being Built</strong></font></td>
<td align="center"><font face="Arial"><strong>On Return</strong></font></td>
<td align="center"><font face="Arial"><strong>Received Back</strong></font></td>
</tr>
<?php
for ($i = 0; $i < max(count($notstarted),count($intransit),count($awaiting),count($built),count($cfc2),count($received)); $i++) {
echo "<tr>";
echo "<td align=\"center\"><font face=\"Arial\">";
if (count($notstarted) > $i) echo $notstarted[$i];
echo "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\">";
if (count($intransit) > $i) echo $intransit[$i];
echo "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\">";
if (count($awaiting) > $i) echo $awaiting[$i];
echo "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\">";
if (count($built) > $i) echo $built[$i];
echo "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\">";
if (count($cfc2) > $i) echo $cfc2[$i];
echo "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\">";
if (count($received) > $i) echo $received[$i];
echo "</font></td>";
echo "</tr>";
}
?>
</table
</body>
</html>
I used the example of the answer by manassehkatz.
Most of the code relating the PDO connection is nearly copy/pasted from the link given by tadman ...
Good luck for your project.
Each row from the mysql query (yes, should be replaced with PDO, etc. but that is not directly related to the problem) reports the status of one stores record. The table is designed to report the values of several on each line - one of each status type. There are a few ways to do this. One way is to populate a set of arrays for the different status codes and then print out the arrays, once completed, as an HTML table. Something like this:
$notstarted = array();
$intransit = array();
$awaiting = array();
$built = array();
$cfc2 = array();
$received = array();
// Get all the data into the arrays
while ($row = mysql_fetch_array($query)) {
$data = $row['ticket'] . ' (' . $row['site'] . ')';
if ($row['status'] == "Not Started") { $notstarted[] = $data; };
if ($row['status'] == "In Transit") { $intransit[] = $data; };
if ($row['status'] == "Awaiting Build") { $awaiting[] = $data; };
if ($row['status'] == "Being Built") { $built[] = $data; };
if ($row['status'] == "On Return from CFC2") { $cfc2[] = $data; };
if ($row['status'] == "Received") { echo $received[] = $data; };
};
// Loop through all the arrays at once. # of rows will be max of size of all arrays
for ($i = 0; $i < max(count($notstarted),count($intransit),count($awaiting),count($built),count($cfc2),count($received)); $i++) {
print "<tr>";
print "<td align="center"><font face="Arial">";
if (count($notstarted) > $i) { print $notstarted[$i]; };
print "</font></td>";
print "<td align="center"><font face="Arial">";
if (count($intransit) > $i) { print $intransit[$i]; };
print "</font></td>";
print "<td align="center"><font face="Arial">";
if (count($awaiting) > $i) { print $awaiting[$i]; };
print "</font></td>";
print "<td align="center"><font face="Arial">";
if (count($built) > $i) { print $built[$i]; };
print "</font></td>";
print "<td align="center"><font face="Arial">";
if (count($cfc2) > $i) { print $cfc2[$i]; };
print "</font></td>";
print "<td align="center"><font face="Arial">";
if (count($received) > $i) { print $received[$i]; };
print "</font></td>";
print "</tr>";
};
I am working on a function which selects a list of dates, and highlight the next event. I have managed to highlight all future events, however I would like to only highlight the nearest future event. How can this be done?
<?php
function racesbydate($sql) {
include 'connect.php';
$year = $_GET['year'];
$get = $year.'%';
$select = $conn->prepare($sql);
$select->bind_param('s', $get);
$select->execute();
$select->store_result();
if ($select->num_rows > 0) {
echo "<div class='races' id='$year' style='display:block'>
<table>\n<th colspan='5'>Cycling Season $year </th>\n
<tr id='information'>\n
<th id='date'>Date</th>\n
<th id='race'>Race</th>\n
<th id='route'>Route</th>\n
<th id='info'>Entry</th>\n
<th id='rizultz'>Results</th>";
$meta = $select->result_metadata();
while ($field = $meta->fetch_field()) {
$params[] = &$row[$field->name];
}
call_user_func_array(array($select, 'bind_result'), $params);
while ($select->fetch()) {
$today = date("Y-m-d");
$date = new DateTime($row['date']);
$formatted_date = $date->format('d/m/Y');
if ($row['date'] > $today) {
if ($row['infoID'] != "0") {
echo "<tr class='racedetails' id='nextrace' >\n
<td class='dark' id='date'>".$formatted_date."</td>\n
<td class='light' id='race'>".$row["title"]."</td>\n
<td class='dark' id='route'>".$row["route"]."</td>\n
<td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
<td class='dark' id='rizults'>N/A</td>\n</tr>\n";
}
} else {
if (($row['infoID'] != "0") && ($row['resultID'] !="0" )) {
echo "<tr class='racedetails' id='race' >\n
<td class='dark' id='date'>".$formatted_date."</td>\n
<td class='light' id='race'>".$row["title"]."</td>\n
<td class='dark' id='route'>".$row["route"]."</td>\n
<td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
<td class='dark' id='rizults'><a href='".$row["resultID"]."'>Results</a></td>\n</tr>\n";
}
}
}
}
echo "</table>\n</div>";
$select->close();
}
current result
<tr class='racedetails' id='nextrace' ><td class='dark' id='date'>06/01/2016</td></tr>
<tr class='racedetails' id='nextrace' ><td class='dark' id='date'>08/02/2016</td></tr>
goal results
<tr class='racedetails' id='nextrace' ><td class='dark' id='date'>06/01/2016</td></tr>
<tr class='racedetails' id='race' ><td class='dark' id='date'>08/02/2016</td></tr>
If I understood you correctly, you only need a flag to store the information whether the next race has already been seen or not:
:
call_user_func_array(array($select, 'bind_result'), $params);
$nextRace = true;
while ($select->fetch()) {
$today = date("Y-m-d");
$date = new DateTime($row['date']);
$formatted_date = $date->format('d/m/Y');
if ($row['date'] > $today) {
if ($row['infoID'] != "0") {
$cssId = $nextRace ? 'nextrace' : 'race';
$nextRace = false;
echo "<tr class='racedetails' id='".$cssId."' >\n
<td class='dark' id='date'>".$formatted_date."</td>\n
<td class='light' id='race'>".$row["title"]."</td>\n
<td class='dark' id='route'>".$row["route"]."</td>\n
<td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
<td class='dark' id='rizults'>N/A</td>\n</tr>\n";
}
} else {
:
This assumes that the order of the races is ascending by date (which your examples suggest).
I am creating a component for teachers where in teacher can generate pdf for all the students who have completed the course.
Checking all the students and pdfs should be generated and saved on disk. After which a download link is provided to download the zip of all the pdfs generated. This is what i want to achieve. I am using fpdf for generating pdf.
Any suggestions ?
Below is the form that is posted and students id-
<form
action="<?php echo JRoute::_('index.php?option=com_mentor&view=download_certificate&cid=' . $cid . '&Itemid=529') ?>"
name="download_certificate" method="post" id="download_certificate">
<table class="adminlist" border="1" cellpadding="0" cellspacing="0"
style="table-layout: fixed" id="content">
<thead>
<tr>
<th class="nowrap" style="width: 35px">
<input type="checkbox" name="selectall" id="selectall">
</th>
<th class="nowrap" align="center">
<?php echo JText::_('COM_MENTOR_USER_NAME'); ?>
</th>
<th class="nowrap" style="width: 140px">
<?php echo JText::_('COM_MENTOR_COURSE_STATUS'); ?>
</th>
<th class="nowrap" style="width: 140px">
<?php echo JText::_('COM_MENTOR_ENROLLMENT_DATE'); ?>
</th>
<th class="nowrap" style="width: 140px">
<?php echo JText::_('COM_MENTOR_ACTIVITY'); ?>
</th>
<th class="nowrap" style="width: 50px">
<?php echo JText::_('COM_MENTOR_SCORE'); ?>
</th>
<th class="nowrap" style="width: 50px">
<?php echo JText::_('COM_MENTOR_RESULT'); ?>
</th>
</tr>
</thead>
<tbody>
<?php
//echo '<pre>';print_r($this->mentor_details); die;
foreach ($this->mentor_details as $students) {
$cid = $this->mentor_details['cid'];
$i = 1;
foreach ($students['students'] as $student) {
$userid = $student['id'];
// echo '<pre>';
// print_r($student);
// die;
?>
<tr class="status" id="<?php echo $userid ?>">
<td align="center">
<input type="checkbox" id="<?php echo $userid ?>" name="check[]" class="checkbox1"
value="<?php echo $userid ?>">
</td>
<td>
<a href="<?php echo JRoute::_('index.php?option=com_mentor&view=grader&cid=' . $cid . '&uid='
. $userid . $itemid) ?>">
<?php echo $student['username']; ?>
</a>
</td>
<!-- <td>
<?php// echo $student['email']; ?>
</td> -->
<td align="center">
<?php
$incomplete = $completed = $not_started = 0;
for ($k = 0; $k < count($student['elements']); $k++) {
foreach ($student['elements'] as $elements) {
if ($elements['userid'] == $userid) {
// echo '<pre>';print_r($elements); die;
if ($elements['element']['cmi.core.lesson_status'] == 'incomplete') {
$incomplete++;
} else {
$completed++;
}
}
}
}
if ($incomplete == 0 && $completed == 0) {
echo 'Not yet started';
} else {
if ($completed == count($student['elements'])) {
echo 'Completed';
} else {
echo 'Incomplete';
}
}
?>
</td>
<td align="center">
<?php
if (!empty($student['timestart'])) {
$date = date('d-m-Y H:i', $student['timestart']);
echo $date;
} else {
echo "Not yet started";
} ?>
</td>
<td align="center">
<?php
if (!empty($student['activity']['lasttime']) && (!empty($student['activity']['starttime']))) {
$start_date = date('d-m-Y H:i', $student['activity']['starttime']);
$last_date = date('d-m-Y H:i', $student['activity']['lasttime']);
echo $start_date . '<br/>' . $last_date;
} else {
echo "-";
} ?>
</td>
<td align="center">
<?php
$grades = $student['grades'];
$total_grade = array();
$j = 0;
//for ($j = 0; $j < count($grades); $j++) {
// $total_grade[$j] = $grades[$j]['finalgrade'];
//}
//print_r($total_grade);die;
if (!empty($grades)) {
//echo number_format(array_sum($total_grade), 2);
$total_grade[$j] = $grades[$j]['finalgrade'];
echo number_format($total_grade[$j], 2);
} else {
echo '-';
}
//echo '<pre>';
//print_r($student['grades']);
//die;
?>
</td>
<td align="center">
<?php
//echo '<pre>';print_r($student);die;
if (!empty($student['scores'])) {
if (isset($grades[$j]['feedbacktext'])) {
echo $grades[$j]['feedbacktext'];
} else {
echo '-';
}
} else {
echo '-';
}
?>
</td>
</tr>
<?php $i++;
}
} ?>
</tbody>
</table>
</form>
<script>
function checked_value() {
var checkedValue = [];
var $len = $(".checkbox1:checked").length;
if ($len == 0) {
alert('Please select user');
}
// else if ($len > 1) {
// alert('Please select a single user only.');
// }
else {
$(".checkbox1").each(function () {
var $this = $(this);
if ($this.is(":checked")) {
checkedValue.push($this.attr("id"));
}
});
$("#download_certificate").submit();
</script>
On Clicking image tag, form is submitted with the students id and I am getting students data, his name, grades, course,
<img src="/components/com_mentor/images/certificate_blue.png" class="certificate-ico right"
title="Download Certificate" onclick="checked_value();"/>
After this processing, page is redirected to pdf.php page
require_once('/wamp/opt/bitnami/apache2/htdocs/lms/lib/fpdf/fpdf.php');
$pdf = new FPDF(); $pdf->SetFont('times', '', 12);
$pdf->SetTextColor(50, 60, 100); $pdf->AddPage('L');
$pdf->SetDisplayMode(real, 'default'); $pdf->SetXY(10, 60);
$pdf->SetFontSize(12);
$pdf->Write(5, 'Dear Ms.XYX');
$filename = "test.pdf";
$dir = "/assets/";
$pdf->Output($dir . $filename, 'F');
Thanks guys for your help.. Solved my question.
Looped through the pdf function for n no. of users.
I know this has been asked before and I have got it working using the following code:
<?php
$maxcols = 8; $i = 0;
echo "<table id='table1'><tr>";
foreach ($id as $k => $v) {
echo "<td id='0'><div id='{$k}' class='drag t1'>{$v}</div></td>"; $i++;
if ($i == $maxcols) { $i = 0; echo "</tr><tr>"; }
} $i++;
while ($i <= $maxcols) {
$i++; echo "<td></td>";
}
echo "</tr></table>";
?>
This results in a table that looks like this :
I'd like to add headers to this so the end result looks like this:
I'd like to do it dynamically so if I create a table that is only 5 columns wide I'd get on the first header row ID01 - ID05 and on the second header row ID06 - ID10
I want to limit the header ID values to be no more than $maxid any extra header fields should be blank, like this : If $maxid = 12; then :
I need the header rows are made as follows and not using <TH>
<td class="mark">
I'm using some javascript to allow the movement of cell data.
The class is used to set the formatting on the header and stop items from being dragged into the fields.
Can anyone point me in the right direction on how to do this.
This should help you.
$maxcols = 8;
$maxid = 12;
$startid = 1;
echo "<table id='table1'>\n";
for ($i = 1;$i<=ceil($maxid/$maxcols);$i++) {
echo "<tr>\n";
for ($j=1;$j<=$maxcols;$j++)
if ($startid <= $maxid)
echo " <td class='mark'>ID".$startid++."</td>\n";
else
echo " <td> </td>\n";
echo "</tr>\n<tr>\n";
for ($j=1;$j<=$maxcols;$j++)
echo "<td>Content</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
Generates
<table id='table1'>
<tr>
<td class='mark'>ID1</td>
<td class='mark'>ID2</td>
<td class='mark'>ID3</td>
<td class='mark'>ID4</td>
<td class='mark'>ID5</td>
<td class='mark'>ID6</td>
<td class='mark'>ID7</td>
<td class='mark'>ID8</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td class='mark'>ID9</td>
<td class='mark'>ID10</td>
<td class='mark'>ID11</td>
<td class='mark'>ID12</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
</table>
try this. It will work in the same way as you desired
<?php
$id= array("1","2","3","4","5","6","7","8","9","10","11","12");
$maxcols = 8; $i = 0;$j=0;$t=0;$s=0;
$maxid = count($id);
echo "<table id='table1'><tr>";
foreach ($id as $k => $v)
{
if($t == 0)
{
while ($t <= $maxcols-1) {
if($s < $maxid)
{
$s++;$t++; echo "<td class='mark'>id$s</td>";
}
else
{
echo "<td class='mark'></td>";$t++;$s++;
}
}
echo "</tr><tr>";
}
else
{
}
echo "<td id='0'><div id='{$k}' class='drag t1'>{$v}</div></td>"; $i++;
if ($i == $maxcols)
{
echo "</tr><tr>";
if($j == 0)
{
while ($j <= $maxcols-1) {
if($s < $maxid)
{
$s++;$j++; echo "<td class='mark'>id$s</td>";
}
else
{
echo "<td class='mark'></td>";$j++;$s++;
}
}
echo "</tr><tr>";
}
$i=0;
}
}
echo "</tr></table>";
?>
Output
Hi You can Use My Library:
class generate{
private $row = "<tr>{columns}</tr>";
private $td = "<td {attr}>{data}</td>";
private $attributeTR="";
private $attributeTD="";
private $tdBuilder="";
public function addCol($ColumValsArr=array("class='motota'"=>"Example")){
foreach($ColumValsArr as $key=>$val){
$newCol = str_replace("{data}",$val,$this->td);
$newCol = str_replace("{attr}",$key,$newCol);
$this->tdBuilder .= str_replace("{data}",$key,$newCol);
}
}
public function getRow(){
return str_replace("{columns}",$this->tdBuilder,$this->row);
}
}
<?php
function TableFunc($Data)
{
$Table = "<table>" . PHP_EOL;
foreach ($Data as $tags => $array) {
$Table .= "<$tags>" . PHP_EOL;
foreach ($array as $thead) {
$tag=$tags==="tbody"?"td":"th";
$Table .= "<tr>" . PHP_EOL;
if (is_array($thead)) {
foreach ($thead as $theadItem) {
if (is_array($theadItem))
$Table .= "<$tag colspan='$theadItem[1]'>$theadItem[0]</$tag>" . PHP_EOL;
else
$Table .= "<$tag>$theadItem</$tag>" . PHP_EOL;
}
}
$Table .= "</tr>" . PHP_EOL;
}
$Table .= "</$tags>" . PHP_EOL;
}
$Table .= "</table>" . PHP_EOL;
return $Table;
}
$Data = array(
"thead" => [
[["GENEL BİLGİ", 2], ["KALORİMETRE (ISINMA)", 2], ["HESAPLAMA", 2]],
["NO", "AD SOYAD", "FARK", "TUTAR", "OKUMA", "ÖDENECEK"]
],
"tbody"=>array(
array("1","MURAT DURAN","100","100.00","10.00","110.00"),
array("1","MURAT DURAN","100","100.00","10.00","110.00"),
array("1","MURAT DURAN","100","100.00","10.00","110.00"),
array("1","MURAT DURAN","100","100.00","10.00","110.00"),
),
"tfoot" => [["NO", "AD SOYAD", "M2", "MAHSUP", "SAYAÇ", "15°", "FARK", "TUTAR", "ORTAK ALAN", "EKSTRA", "MUAFİYET", "OKUMA", "ÖDENECEK"]]
);
echo TableFunc($Data);
I'm trying to write some sort of forum homepage, based off work someone else did. Here's what I've got so far:
Forums
<?php
$crumbs = explode(",", $user['data']['depts']);
foreach ($crumbs as &$value) {
$data = $db->query("SELECT * FROM tbl_depts WHERE id = '" . $value . "'");
$crumb = $data->fetch_assoc();
$data = $db->query("SELECT * FROM tbl_forums WHERE deptid = '" . $value . "'");
$forumcount = $data->num_rows;
$forum = $data->fetch_assoc();
?>
<div class="h3top"><?php echo $crumb['name']; ?></div>
<div class="info2alt">
<?php
while (($row = $data->fetch_array()) !== FALSE) {
$forum[] = $row;
}
foreach ($forum as $row) {
if ($forumcount >= 1) {
if ($forum['lastpost'] == "") {
$forum['lastpost'] = "--";
}
?>
<div class="info4">
<table width="100%" border="0" cellspacing="1" cellpadding="4">
<tr>
<td width="55%" align="left" valign="middle" class="zebraodd"><?php echo $row['name']; ?></td>
<td width="10%">Threads: <?php echo $forum['threadcount']; ?><br />Posts: <?php echo $forum['postcount']; ?></td>
<td width="35%">Last Post: <?php echo $forum['lastpost']; ?></td>
</tr>
</table>
</div>
<?php
}
?>
<?php if ($forumcount >= 1) { ?>
<div class="info3bottom"></div>
<?php
}
}
?>
</div>
<?php
}
?>
This is the current data in the table:
Now, when I try to use this code, I get this:, such that the first one is shown so many times, however, the next one does not appear.
How do I fix this?
you want to use $value['threadcount'] etc. $forum is the array containing all the rows. i wonder that you get some output at all …
fetch_assoc() will only fetch a single row from the resultset. you'd have to use a loop to fill an array:
while(($row = $data->fetch_assoc()) !== FALSE) {
$forum[] = $row;
}
you can then iterate over your $forum array with a foreach loop:
foreach($forum as $row) {
echo htmlspecialchars($row['threadcount']);
// etc.
}
trying to fix this mess …
<?php
$crumbs = explode(",", $user['data']['depts']);
foreach ($crumbs as $crumb) { ?>
<div class="h3top"><?php echo htmlspecialchars($crumb['name']);?></div>
<?
}
$result = $db->query("SELECT * FROM tbl_forums WHERE deptid = " . (int)$id . "");
$forumcount = $result->num_rows;
while(($row = $data->fetch_assoc()) !== FALSE) {
if ($row['lastpost'] == "") { $row['lastpost'] = "--";}
?>
<div class="info2alt">
<div class="info4">
<table width="100%" border="0" cellspacing="1" cellpadding="4">
<tr>
<td width="55%" align="left" valign="middle" class="zebraodd"><?php echo htmlspecialchars($forum['name']); ?></td>
<td width="10%">Threads: <?php echo htmlspecialchars($forum['threadcount']); ?><br />Posts: <?php echo htmlspecialchars($forum['postcount']); ?></td>
<td width="35%">Last Post: <?php echo htmlspecialchars($forum['lastpost']); ?></td>
</tr>
</table>
</div>
<div class="info3bottom"></div>
<?php
}
?>