I need to export only those records whose column satisfies defined value.
Eg.
if(null !== ($this->f3->get('SESSION.userStatus')))
{
$userStatus = $this->f3->get('SESSION.userStatus');
}
This is how I tried to set cell values depending on set value:
$rowID = 5;
foreach ($results as $result)
{
if($result['status'] == $userStatus)
{
$objPHPExcel->getActiveSheet()->setCellValue('A'.$rowID, $result['fullname']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$rowID, $result['checkin_date']);
}
else
{
$objPHPExcel->getActiveSheet()->removeRow($rowID);
}
$rowID++;
}
The file is exported with only those records that have defined "status" value.
But the problem is that rows whose criteria doesn't meet are filled with spaces and still occupies rows.
Here is how output looks like:
A | B | C | D
1 | | |
.
.
174 | | |
175 | | |
176 | | |
177 | | |
178 John | 2014| xyz | dfdf
179 Jack | 2015| jkl | dfdf
180 | | |
.
.
How can I fix it to get records starting with top row?
Eg.
1 | John | 2014 | xyz | dfdf
2 | Jack | 2015 | jkl | dfds
between
}
$rowID++;
put this
else
{
$objPHPExcel->getActiveSheet()->removeRow($rowID);
}
That will delete your blank rows.
Note, that you start with row 5 for some reason and do not test for max rows. Your code will still need some fixing up
I wanted to check if the next TWO rows or ID is not NULL.
because if there is no succeeding TWO rows or ID then the income remains zero.
foreach ($data as $row)
{
if(($row->id + 2) != NULL) //I don't know what is the correct statement here
{
echo "<tr>";
echo "<td>".$row->id."</td>";
echo "<td>".$row->username."</td>";
echo "<td>"."650.00"."</td>";
echo "<td>".$row->remarks."</td>";
echo "<tr>";
}
else
{
echo "<tr>";
echo "<td>".$row->id."</td>";
echo "<td>".$row->username."</td>";
echo "<td>".$row->income."</td>";
echo "<td>".$row->remarks."</td>";
echo "<tr>";
}
}
Here is the table I want to achieve.
==================================
|ID | Username | Income | Remarks|
| 2 | user1 | 650.00 | |
| 3 | user2 | 650.00 | |
| 4 | user3 | 0.00 | |
| 5 | user4 | 0.00 | |
==================================
If I add a username then the next output will be this:
==================================
|ID | Username | Income | Remarks|
| 2 | user1 | 650.00 | |
| 3 | user2 | 650.00 | |
| 4 | user3 | 650.00 | |
| 5 | user4 | 0.00 | |
| 6 | user5 | 0.00 | |
==================================
You need to change your foreach to get the index.
foreach ($data as $index => $row)
Then you can address all rows relative to your current row with:
$row_two_ahead = $data[$index + 2];
you should check however if that row exists before you try to use it or you will get index out of range exceptions:
if (isset($data[$index + 2])) {
}
I solved the problem...
this is the code from my controller:
public function addUsername()
{
$data['id'] = NULL;
$data['username'] = $this->input->post('username');
$data['reward'] = 0.00;
$data['remarks'] = ' ';
$this->Matrix_model->addUsername($data);
$last = $this->Matrix_model->getLast();
$index = $last - 2; //I minus the last index with two
$this->Matrix_model->updateIncome($index); //and updated the income of that index
redirect('http://localhost/matrix/index.php/matrix');
}
and this is the code from my model:
public function addUsername($data)
{
$this->db->insert("income", $data);
}
public function getLast()
{
return $this->db->insert_id(); //this is the method to access the last id inserted
}
public function updateIncome($id)
{
$this->db->set("reward", 650);
$this->db->where("id", $id);
$this->db->update("income");
}
thank you and I'm sorry if other programmers didn't understand my questions
I need If Statement (php) to :
if (Modem = ModemNotReceived) and (CheckModem = Done)
Total + 50
And
if (Modem = ModemReceived) and (CheckModem = Done)
Total - 50
How i can do that in one if statement?
My Table :
| ID | Modem | CheckModem | Total
------------------------------------------------
| 1 | ModemReceived | Done | 120
------------------------------------------------
| 2 | ModemNotReceived | Null | 90
------------------------------------------------
| 3 | ModemReceived | Null | 100
If I understand what you're saying correctly, this code is what you mean:
<?php
if ($Modem == 'ModemNotReceived' && $CheckModem == 'Done') {
$Total += 50;
} else if ($Modem == 'ModemReceived') && $CheckModem == 'Done' {
$Total -= 50;
}
?>
I have been messing with this for 3 days now, researching and experimenting: it's time to ask for some help.
I have a bit of code for a schedule display page that loops over a database table, once per appointment type. It gathers all appointments of that type for the week, and returns them in a row, like this:
+------+------+-----+-----+-----+-----+-----+-----+-----+------+
| Name | Year | Mon | Tue | Wed | Thu | Fri | Sat | Sun | Purp |
+------+------+-----+-----+-----+-----+-----+-----+-----+------+
| IST | NA | 9-4 | | | | | | | ABC |
+------+------+-----+-----+-----+-----+-----+-----+-----+------+
| OT | NA | | 8-2 | 8-2 | 8-2 | 8-2 | | | DEF |
+------+------+-----+-----+-----+-----+-----+-----+-----+------+
My code works exactly as I want it to, with one fatal flaw. If there's more than one row for a "Name", I get this as my row:
+------+------+-----+-----+-----+-----+-----+-----+-----+------+
| Name | Year | Mon | Tue | Wed | Thu | Fri | Sat | Sun | Purp |
+------+------+-----+-----+-----+-----+-----+-----+-----+------+
| IST | NA | 9-4 | | | | | | | ABC |
+------+------+-----+-----+-----+-----+-----+-----+-----+------+
| OT | NA | | 8-2 | 8-2 | 8-2 | 8-2 | | | DEF |
+------+------+-----+-----+-----+-----+-----+-----+-----+------+-+-----+-+-+-+-+-+-----+
| MTG | | 1-2 | | | | | | | | | 1-2 | | | | | | GHI |
| | | | | | | | | | | | | | | | | | JKL |
+------+------+-----+-----+-----+-----+-----+-----+-----+------+-+-----+-+-+-+-+-+-----+
So instead of inserting into the appropriate <td>, it makes a whole new set of them. It's really frustrating because I'm sure it's something simple, but I can't see it... :(
Code:
$apptnamestop = array("IST", "OT", "MTG", "TR-CN", "EVENT", "EN", "REC", "TO");
$daysofweek = array("1", "2", "3", "4", "5", "6", "0");
foreach ( $apptnamestop as $name) {
print str_repeat($tab, 8) . "<tr>\n";
print str_repeat($tab, 9) . "<td class=\"td1s\">$name</td>\n";
print str_repeat($tab, 9) . "<td class=\"td1s\">N/A</td>\n";
$getdataquery = "SELECT appointmentName as name
, appointmentType as type, appointmentStartDateTime as sdt
, appointmentEndDateTime as edt, appointmentPurpose as purp
from tblAppointments
where appointmentStaffIDsToAttend like '%{$_SESSION['user_id']}%'
and appointmentName = '$name'
and (appointmentStartDateTime >= DATE_ADD(CURDATE(), INTERVAL (9 - IF(DAYOFWEEK(CURDATE())=1, 8, DAYOFWEEK(CURDATE()))) DAY)
and appointmentEndDateTime < DATE_ADD(CURDATE(), INTERVAL (16 - IF(DAYOFWEEK(CURDATE())=1, 8, DAYOFWEEK(CURDATE()))) DAY))
";
$getdataqueryresults = $mysqli->query($getdataquery)
or trigger_error("<p class=\"error\">We're very sorry, but an error has occurred when interacting with the CHAIRS database. Please try again and see if the error repeats. If it does, please get the following information in its entirety to your database adminapptrator so the CHAIRS developer can get the error resolved.<br />Error Message: " . $mysqli->error, E_USER_ERROR);
$datarowcnt = $getdataqueryresults->num_rows;
if ($datarowcnt > 0) {
while ($row = $getdataqueryresults->fetch_row()) {
$rows[] = $row;
}
foreach ($rows as $row) {
$title = $row[0];
$type = $row[1];
$sdt = $row[2];
$edt = $row[3];
$purp = $row[4];
$c=7;
if ($type == 1) {
$typew = "Mandatory";
} else {
$typew = "Elective";
}
$sparts = explode(" ", $sdt);
$eparts = explode(" ", $edt);
$tdiff = getTimeDiff($sparts[1], $eparts[1]);
foreach ($daysofweek as $day) {
if ($title == $name) {
if ($day == date('w', strtotime("$sparts[0]"))) {
if ($sparts[0] == $eparts[0]) {
print str_repeat($tab, 9) . "<td class=\"td1s\">$sparts[1] - $eparts[1]<br />($tdiff) - $typew</td>\n";
$c--;
} else {
$s = strtotime("$sparts[0]");
$e = strtotime("$eparts[0]");
for ($i=$s; $i<=$e; $i+=86400) {
print str_repeat($tab, 9) . "<td class=\"td1s\">$sparts[1] - $eparts[1]<br />($tdiff) - $typew</td>\n";
$c--;
}
}
}
if ( $c > 0) {
$c--;
print str_repeat($tab, 9) . "<td class=\"td1s\"></td>\n";
}
}
}
}
$rc++;
} else {
foreach ($daysofweek as $day) {
print str_repeat($tab, 9) . "<td class=\"td1s\"></td>\n";
}
}
print str_repeat($tab, 9) . "<td class=\"td1s\">$purp</td>\n";
$purp = "";
print "</tr>\n";
}
The new rows are started here:
foreach ( $apptnamestop as $name) {
print str_repeat($tab, 8) . "<tr>\n";
print str_repeat($tab, 9) . "<td class=\"td1s\">$name</td>\n";
As you can see, this new row is started once per element in $apptnamestop, rather than once per row from the database. The code doesn't care how many matching rows it finds in the table, it is only going to insert one new tr per name.
There are a couple of ways to improve this situation, depending on your needs.
If you need to always insert a row per name, regardless of whether there is a matching row in the database, keep lines 4, 5 and 6 the same as they are now. We will need to modify the inner loop (foreach ($rows as $row)) to output the new table rows for every loop beyond the first one:
$rowCounter = 0;
foreach ($rows as $row) {
if ($rowCounter > 0) {
print "</tr>\n"; // Close the previous table row
print str_repeat($tab, 8) . "<tr>\n";
print str_repeat($tab, 9) . "<td class=\"td1s\">$name</td>\n";
print str_repeat($tab, 9) . "<td class=\"td1s\">N/A</td>\n";
}
$rowCounter++;
// Remainder of the loop code goes here
...
}
If you prefer to skip creation of the table row when no matching database row is found, move the row creation statements into the inner loop entirely; this way it will run only if database rows are found.
As a final note, consider implementing a view templating system; by separating your logic code from your display code, you can easily modify either one without breaking the other. Your code becomes much simpler to read and modify in the future.
I am trying to print a 3-dimensional array into a table. But the indexes are kinda fked up. When I use the following (psuedo)code:
...
<<print headers and stuff>>
for ( $i = 0; $i < count( $array ); i++) {
$itemArray = $array[i];
for ( $j = 0; $j < count( $itemArray; j++) {
$innerItem = $itemArray[j];
echo <<tr start + both indexes in td>>
foreach ($innerItem as $spec) {
echo <<td with item>>
}
echo <<tr stop>>
}
}
In this example I am using i as index for the outer array and j as an index for the inner array (pretty obvious).
The result I am getting from this is as follows:
| index i | index j | title1 | title2 |
| 0 | 0 | | |
| 1 | 0 | | |
| 2 | 0 | | |
| ... | ... | | |
Whilst I would expect:
| index i | index j | title1 | title2 |
| 0 | 0 | | |
| 0 | 1 | | |
| 1 | 0 | | |
| 1 | 1 | | |
| 1 | 2 | | |
| 2 | 0 | | |
| ... | ... | | |
The (original) full code is:
echo "<h1>Combat analysis</h1>";
echo '<table cellspacing="0" cellpadding="4" border="1"><tbody>';
echo "<tr><td>#Mon</td><td>#Att</td><td>DungLVL</td><td>CharLVL</td><td>Health</td><td>Weapon</td><td>No. potions</td></tr>";
for ($battleIndex = 0; $battleIndex < count($this->combatLog); $battleIndex++) {
$battle = $this->combatLog[$battleIndex];
for ($attackIndex = 0; $attackIndex < sizeof($battle); $attackIndex++) {
$attack = $battle[$attackIndex];
echo "<tr><td>" . $battleIndex . "</td><td>" . $attackIndex . "</td>";
foreach ($attack as $stat) {
echo "<td>" . $stat . "</td>";
}
echo "</tr>";
}
}
echo "</tbody></table>";
What is going wrong?
Tested your code and runs as expected. You should do a echo '<pre>'.print_r($this->combatLog).'</pre>'; and debug the array contents.
Also I would recommend you the following:
1) You can use foreach instead of for, example: foreach ($this->combatLog as $battleIndex => $battle)
2) If you're not sure that a array contains values you should first do a: if (is_array($this->combatLog) && count($this->combatLog) > 0)
3) For simplicity and code maintenance I would first loop the multi-dimensional array and turn it into a one dimension called $attacks containing a array per each attack indexed by keys that you can recognize, ej:
$attacks=array();
$attacks[]=array(
'Mon'=>$battleIndex,
'Att'=>$attackIndex,
'DungLVL'=>isset($stat[0])?$stat[0]:null,
'CharLVL'=>isset($stat[1])?$stat[1]:null,
'Health'=>isset($stat[2])?$stat[2]:null,
'Weapon'=>isset($stat[3])?$stat[3]:null,
'Potions'=>isset($stat[4])?$stat[4]:null,
);
Then you could define some columns for example:
$columns=array(
'Mon',
'Att',
'DungLVL',
'CharLVL',
'Health',
'Weapon',
'Potions',
);
Then print the table header like this:
echo '<tr>';
foreach ($columns as $column) {
echo '<td>'.$column.'</td>';
}
echo '</tr>';
And print rows like this:
foreach ($attacks as $attack) {
echo '<tr>';
foreach ($columns as $column) {
echo '<td>'.$attack[$column].'</td>';
}
echo '</tr>';
}