issue with table in php - php

I'm trying to create a table in php that would show the data on the mysql database based on the check box that is checked by the user.
As you can see in this screen shot, it will have problems when you did not check on a checkbox before the one that will be the last:
http://www.mypicx.com/04282010/1/
Here is my code:
if($_POST['general'] == 'ADDRESS'){
$result2 = mysql_query("SELECT * FROM student WHERE ADDRESS='$saddress'");
?>
<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
<th>SECTION</th>
<?php if ( $ShowLastName )
echo "<th>LASTNAME</th>" ?>
<?php if ( $ShowFirstName )
echo "<th>FIRSTNAME</th>" ?>
<?php if ( $ShowMidName )
echo "<th>MIDNAME</th>" ?>
<?php if ( $ShowAddress )
echo "<th>ADDRESS</th>" ?>
<?php if ( $ShowGender )
echo "<th>GENDER</th>" ?>
<?php if ( $ShowReligion )
echo "<th>RELIGION</th>" ?>
<?php if ( $ShowBday )
echo "<th>BIRTHDAY</th>" ?>
<?php if ( $ShowContact )
echo "<th>CONTACT</th>" ?>
</tr>
<?php
while($row = mysql_fetch_array($result2))
{?>
<tr>
<td><?php echo $row['IDNO']?> </td>
<td><?php echo $row['YEAR'] ?> </td>
<td><?php echo $row['SECTION'] ?></td>
<td><?php
if ( $ShowLastName )
echo $row['LASTNAME'] ?></td>
<td><?php
if ( $ShowFirstName )
echo $row['FIRSTNAME'] ?></td>
<td><?php
if ( $ShowMidName )
echo $row['MI'] ?></td>
<td><?php
if ( $ShowAddress )
echo $row['ADDRESS'] ?></td>
<td><?php
if ( $ShowGender )
echo $row['GENDER'] ?></td>
<td><?php
if ( $ShowReligion )
echo $row['RELIGION'] ?></td>
<td><?php
if ( $ShowBday )
echo $row['BIRTHDAY'] ?></td>
<td><?php
if ( $ShowContact )
echo $row['S_CONTACTNUM'] ?></td>
</tr>
<?PHP } ?>
</table>
<?PHP }
mysql_close($con);
?>
What can you recommend so that the output will not look like this when you one of the checkbox before a checkbox is not clicked: http://www.mypicx.com/04282010/2/

instead of
<td><?php
if ( $ShowGender )
echo $row['GENDER'] ?>
</td>
you should do something like
<?php
if ( $ShowGender )
echo "<td>".$row['GENDER']."</td>" ?>
So that the <td> tags only appears if the "if" statement is true.

Ok first thing's first, let's clean your code up, because it's so difficult to read in it's current format:
<?php
if($_POST['general'] == 'ADDRESS'){
$result2 = mysql_query("SELECT * FROM student WHERE ADDRESS='$saddress'");
?>
<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
<th>SECTION</th>
<?php if ( $ShowLastName ) { ?><th>LASTNAME</th><?php } ?>
<?php if ( $ShowFirstName ) { ?><th>FIRSTNAME</th><?php } ?>
<?php if ( $ShowMidName ) { ?><th>MIDNAME</th><?php } ?>
<?php if ( $ShowAddress ) { ?><th>ADDRESS</th><?php } ?>
<?php if ( $ShowGender ) { ?><th>GENDER</th><?php } ?>
<?php if ( $ShowReligion ) { ?><th>RELIGION</th><?php } ?>
<?php if ( $ShowBday ) { ?><th>BIRTHDAY</th><?php } ?>
<?php if ( $ShowContact ) { ?><th>CONTACT</th><?php } ?>
</tr>
<?php while($row = mysql_fetch_array($result2)) {?>
<tr>
<td><?php echo $row['IDNO']?> </td>
<td><?php echo $row['YEAR'] ?> </td>
<td><?php echo $row['SECTION'] ?></td>
<?php if ( $ShowLastName ) { echo('<td>'.$row['LASTNAME'].'</td>'); } ?></td>
<?php if ( $ShowFirstName ) { echo('<td>'.$row['FIRSTNAME'].'</td>'); } ?>
<?php if ( $ShowMidName ) { echo('<td>'.$row['MI'].'</td>'); } ?>
<?php if ( $ShowAddress ) { echo('<td>'.$row['ADDRESS'].'</td>'); } ?>
<?php if ( $ShowGender ) { echo('<td>'.$row['GENDER'].'</td>'); } ?>
<?php if ( $ShowReligion ) { echo('<td>'.$row['RELIGION'].'</td>'); }?>
<?php if ( $ShowBday ) { echo('<td>'.$row['BIRTHDAY'].'</td>'); }?>
<?php if ( $ShowContact ) { echo('<td>'.$row['S_CONTACTNUM'].'</td>'); }?>
</tr>
<?php } ?>
</table>
<?php }
mysql_close($con);
?>
Your best bet would be to try putting this code in and telling us if this improves things?
EDIT
Ah, as the others have said your <td> tags are sitting outside of your condition, still, the above code is much easier to read and will help future debugging :-)

You only print table header elements (<th>) if the corresponding $isField variable is set, but you print all table cells, only testing whether or not to print the cell contents.
Instead of all that, loop over the fields to be printed out. No need to test each and every field.
Example form:
<form action="..." method="POST">
<h4>Student Information</h4>
<?php foreach ($studentFields as $key => $label) { ?>
<input type="checkbox" name="show[<?php echo $key; ?>]" id="show_<?php echo $key; ?>"/><label for="show_<?php echo $key; ?>"><?php echo $label; ?></label>
<?php } ?>
<h4>Parent Information</h4>
<?php foreach ($parentFields as $key => $label) { ?>
<input type="checkbox" name="show[<?php echo $key; ?>]" id="show_<?php echo $key; ?>"/><label for="show_<?php echo $key; ?>"><?php echo $label; ?></label>
<?php } ?>
</form>
Form handler:
<table>
<thead><tr>
<?php foreach ($fields as $key => $label) { ?>
<th><?php echo $label; ?></th>
<?php } ?>
</tr></thead>
<tbody>
<?php foreach ($results as $row) { ?>
<tr>
<?php foreach ($fields as $key => $label) { ?>
<td><?php echo $row[$key]; ?></td>
<?php } ?>
</tr>
<?php ?>
</tbody>
The foreach ($results as $row) { needs to be rewritten as a while loop if you stick with the outdated mysql driver, but works with PDOStatement. Switching to PDO also makes it easier to injection vulnerabilities, as prepared statement parameters are invulnerable to them. You can also rewrite that SELECT * to only fetch the requested columns, reducing DB load.
$validFields = array('last' => 'Last Name', 'first' => 'First Name', 'stAddr' => 'Address', ...);
$fields = array_intersect($validFields, $_POST['show']);
You could even make it self-configuring by constructing the $validFields array by inspecting the DB table(s), though this would incur an extra table query.

Yeah, do it the same way as you've done the th tags, with the if statement around the td tags, rather than inside them. Way you've done it now will always show 9 columns, no matter what check boxes are selected.

Your 're printing the cells in the iteration, but only it's content depends on the condition.
<?php
if ( $ShowContact )
echo '<td>' . $row['S_CONTACTNUM'] . '</td>' ?>

Related

How to merge tabular data in PHP

I have a table with weeks of a year and some data adjacent to each week, and i can't seem to merge the values correctly. Week rows with no data should stay empty
This is where i list the weeks
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td><?php echo $leadCount->theweek ?>
/ <?php echo $leadCount->theyear ?></td>
</tr>
<?php }
And this is where i try to merge
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td style="text-align: center">
<?php if ($getFtdCountDMm[$index]->theweek == $leadCount->theweek && !empty($getFtdCountDMm[$index]->ftdcount)) {
echo $getFtdCountDMm[$index]->ftdcount;
} else {
echo '0';
} ?>
</td>
</tr>
<?php } ?>
it should look like this
but it looks like this
Instead of running two loops run one loop like this.
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td><?php echo $leadCount->theweek ?>
/ <?php echo $leadCount->theyear ?>
</td>
<td style="text-align: center">
<?php if ($getFtdCountDMm[$index]->theweek == $leadCount->theweek && !empty($getFtdCountDMm[$index]->ftdcount)) {
echo $getFtdCountDMm[$index]->ftdcount;
} else {
echo '0';
} ?>
</td>
</tr>
<?php }

Filtering an array result in a for each loop

I have this code to create my array from file:
<?php
$servers = array();
$handle = #fopen("data/data.txt", "r");
if ($handle) {
while (($buffer = fgets($handle)) !== false) {
$line = explode("|", $buffer);
$servers[] = array(
"name" => $line[0],
"ip" => $line[1],
"type" => $line[2],
);
}
fclose($handle);
}
?>
then i have this code to display the array:
<?php
foreach ($servers as $name => $servers): ?>
<td style="width:340px;"> <?php echo $servers['name']; ?></td>
<td style="width:240px;"><?php echo $servers['ip']; ?></td>
</tr>
<?php endforeach; ?>
this is the array sample:
Array(
[0] => Array
(
[name] => aaa
[ip] => 123
[type] => good
)
[1] => Array
(
[name] => bbb
[ip] => 345
[type] => good
)
)
suppose i need to filter the result with array type is good,
im trying with this code but it only returns the last array:
<?php
foreach ($servers as $name => $servers): ?>
<?php if($servers['type']=="good"){?>
<td style="width:340px;"> <?php echo $servers['name']; ?></td>
<td style="width:240px;"><?php echo $servers['ip']; ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
The error is in the variable name in foreach loop (use $server instead of $serves as $servers already exists and contains your data)
<?php foreach ($servers a $server): ?>
<?php if($server['type']=="good"){?>
<tr>
<td style="width:340px;"> <?php echo $server['name']; ?></td>
<td style="width:240px;"><?php echo $server['ip']; ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
Edit 1
Filter the array, then print it
<?php
//Filter the array
$goodValues = array_filter($servers, function($e){
return $e['type'] == "good";
//Use this to be sure
//return strtolower($e['type']) == "good";
});
//Print the values
foreach ($goodValues as $value): ?>
<tr>
<td style="width:340px;"> <?php echo $value['name']; ?></td>
<td style="width:240px;"><?php echo $value['ip']; ?></td>
</tr>
<?php endforeach; ?>
As this is just numaric array and you're not using the key anywhere inside the body of the loop, you don't need to use as $key => $value and only as $value will suffice. Also notice the different variable names.
<?php foreach ($servers as $server): ?>
<?php if($server['type']=="good"){?>
<tr>
<td style="width:340px;"> <?php echo $server['name']; ?></td>
<td style="width:240px;"><?php echo $server['ip']; ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>

How to avoid duplicates in nested foreach loop php

Why are values are getting printed multiple times (see images below). I need to print them only once.
<?php foreach($patchData3 as $tes3){?>
<?php foreach($patchData1 as $tes){?>
<tr class="<?php if($tes->PATCH == $tes3->PATCH) {echo "red_color"; } ?>">
<td><?php echo $tes->HOSTNAME;?></td>
<td><?php echo $tes->VERSION;?></td>
<td><?php echo $tes->PATCH;?></td> <!--bgcolor="#FF0000"-->
</tr>
<?php } ?>
<?php }?>
<?php foreach($patchData1 as $tes){ ?>
<tr class="<?php if(checkfunction($tes->PATCH,$patchData3) == TRUE) { echo "red_color"; } ?>">
<td><?php echo $tes->HOSTNAME;?></td>
<td><?php echo $tes->VERSION;?></td>
<td><?php echo $tes->PATCH;?></td> <!--bgcolor="#FF0000"-->
</tr>
<?php } ?>
<?php
function checkfunction($patch,$patchData3){
foreach($patchData3 as $tes3){
if($patch == $tes3->PATCH){
return true;
}
}
}
?>
I used a function to overcome the duplication. Please comment if it does not work.

PHP MYSQL Associate array and table

Here is a code.
This loads all the header part (i.e the header for the table) dynamically from the database.
The below code works fine. But the column is mismatched.
i.e. the first row first column of the header is blank and there is a dislocation in the table.
Code
<table border="1">
<?php
$book_query = mysql_query("select * from book_master");
$i = 0;
while($row = mysql_fetch_assoc($book_query))
{
$columns = array_keys($row);
?>
<th>
<?php
foreach($columns as $column)
{
?>
<td><?php echo $column; ?> </td>
</th>
<?php
}
?>
<tr>
<?php
foreach($row as $key=>$value)
{
?>
<td><?php echo $value; ?></td>
<?php
}
?>
</tr>
<?php
$i++;
}
?>
</table>
EDIT:
Here is my print_r($columns) value:
Array ( [0] => Author Name [1] => Book Name [2] => Rating [3] => Location )
I know the problem is with the loop. Could someone help me out?
You can try to change
<th>
<?php
foreach($columns as $column)
{ ?>
<td><?php echo $column; ?> </td>
<?php
}
?>
</th>
to
<tr>
<?php
foreach($columns as $column)
{ ?>
<th><?php echo $column; ?> </th>
<?php
}
?>
</tr>
Just remove TH tag because its create one extra cell, so your layout messed up
<table border="1">
<?php
$book_query = mysql_query("select * from book_master");
$i = 0;
while($row = mysql_fetch_assoc($book_query))
{
if($i == 0){
$columns = array_keys($row);
?>
<?php
foreach($columns as $column){ ?>
<td><?php echo $column; ?> </td>
<?php } ?>
<?php } ?>
<tr>
<?php
foreach($row as $key=>$value){ ?>
<td><?php echo $value; ?></td>
<?php } ?>
</tr>
<?php
$i++;
}
?>
</table>
Hope this will help someone.
Just I have replaced the TH tag with TR and the output is perfect.
<table border="1">
<?php
$book_query = mysql_query("select * from book_master");
while($row = mysql_fetch_assoc($book_query))
{
$columns = array_keys($row);
?>
<tr>
<?php
foreach($columns as $column){ ?>
<td><?php echo $column; ?> </td>
<?php } ?>
</tr>
<tr>
<?php
foreach($row as $key=>$value){ ?>
<td><?php echo $value; ?></td>
<?php } ?>
</tr>
</table>

php get the same index values from two arrays (outside foreach)

Have array named for example $data_debit_turnover
Array
(
[0] => Array
(
[VatReturnRowNumberForDebitTurnover] => 63
[Total] => 0.00
)
[1] => Array
(
[VatReturnRowNumberForDebitTurnover] => 64
[Total] => 44.28
)
)
Have HTML that need to look like this
<table><tr>
<td><strong>63</strong></td>
<td>0.00</td>
</tr><tr>
<td><strong>64</strong></td>
<td>44.28</td>
</tr></table>
At first tried with php foreach, but in such case instead of one table get multiple tables [0], [1] etc.
Then tried
<td><strong>64</strong></td>
<td><?php
if( $data_debit_turnover[1][VatReturnRowNumberForDebitTurnover] == '64'){
echo $data_debit_turnover[1][Total]. ' Total<br>';
}?>
</td>
but problem is with [1] etc. I do not know number of []; may be [1] and may be [30].
Tried something like this
$resultVatReturnRowNumberForDebitTurnover = array();
$resultTotal = array();
foreach($data_debit_turnover as $i => $result){
$resultVatReturnRowNumberForDebitTurnover[] = $result[VatReturnRowNumberForDebitTurnover];
$resultTotal[] = $result[Total];
}
<td><strong>64</strong></td>
<td><?php
if (in_array('64', $resultVatReturnRowNumberForDebitTurnover)) {
echo $resultTotal[1];
}?>
</td>
The same problem [1]. How to echo corresponding (index) value from the another array.
For example if 64 is the second value in array $resultVatReturnRowNumberForDebitTurnover then need to echo the second value from array $resultTotal.
Possibly there is some other way.
Please advice.
Showing what I did with foreach
<?php
foreach($data_debit_turnover as $i => $result){
?>
<table>
<tr>
<td><strong>63</strong></td>
<td>
<?php if($result[VatReturnRowNumberForDebitTurnover] = '63') {
echo $result[Total];
} ?>
</td>
</tr>
<tr>
<td><strong>64</strong></td>
<td>
<?php if($result[VatReturnRowNumberForDebitTurnover] = '64') {
echo $result[Total];
} ?>
</td>
</tr>
</table>
<?php
}
?>
Update Thanks to #user2340218 advice get some solution. For each <td> must use foreach. Possibly there is some better solution.
<table><tr>
<td><strong>64</strong></td>
<td><?php foreach($data_debit_turnover as $vatReturn){if($vatReturn['VatReturnRowNumberForDebitTurnover'] == '64') {echo $vatReturn['Total'];}}?></td>
</tr><tr>
<td><strong>67</strong></td>
<td><?php foreach($data_debit_turnover as $vatReturn){if($vatReturn['VatReturnRowNumberForDebitTurnover'] == '67') {echo $vatReturn['Total'];}}?></td>
</tr></table>
Solution Finally used solution #Daniel P advised.
$resultVatReturnRowNumberForDebitTurnover = array();
$resultTotal = array();
foreach($data_debit_turnover as $i => $result){
$resultVatReturnRowNumberForDebitTurnover[] = $result[VatReturnRowNumberForDebitTurnover];
$resultTotal[] = $result[Total];
}
$VatReturnRowNumberForDebitTurnoverModified = array_combine($resultVatReturnRowNumberForDebitTurnover, $resultTotal);
<table>
<tr>
<td><strong>62</strong></td>
<td>
<?php echo $VatReturnRowNumberForDebitTurnoverModified[62]; ?>
</td>
</tr>
<tr>
<td><strong>63</strong></td>
<td>
<?php echo $VatReturnRowNumberForDebitTurnoverModified[63]; ?>
</td>
</tr>
</table>
Actually have to accept #Daniel P answer:) But as understand can not accept 2 answers
I am guessing that VatReturnRowNumberForDebitTurnover numbers are unique so your array should look like this :
Array
(
[63] => 0.00
[64] => 44.28
)
The array index is your VatReturnRowNumberForDebitTurnover and the value is your total.
To test if a VatReturnRowNumberForDebitTurnover has a value your simply use isset()
if (isset($array[63])) {
echo $array[63]
}
Building the table :
<table>
<?php foreach($data_debit_turnover as $i => $total){ ?>
<tr>
<td><strong><?php echo $i; ?></strong></td>
<td><?php echo $total; ?></td>
</tr>
<?php } ?>
</table>
Do you mean this?
<table>
<?php foreach($data_debit_turnover as $vatReturn){ ?>
<tr>
<td><strong><?php print $vatReturn['VatReturnRowNumberForDebitTurnover'] ?></strong></td>
<td><?php print $vatReturn['total'] ?></td>
</tr>
<?php } ?>
</table>
Use foreach like this:
<table>
<?php
foreach ($Array as $item)
{
echo '<tr>';
echo '<td><strong>' . $item['VatReturnRowNumberForDebitTurnover'] . '</strong></td>';
echo '<td>' . $item['Total'] . '</td>';
echo '</tr>'
}
?>
</table>

Categories