I am using the below table for 3 conditions, by passing values through variables.
In one of my conditions I have only 3 colums, i.e $h2 will be empty for one condition. if($h2=="") I want to hide that entire column.
How can I do that?
This is my code:
<table id="usertracking" >
<thead>
<tr>
<?php
echo'<td><h3>'.$h1.'<h3></td>';
echo'<td><h3>'.$h2.'<h3></td>';
echo'<td><h3>'.$h3.'<h3></td>';
echo'<td><h3>'.$h4.'<h3></td>';
?>
</tr>
</thead>
<tbody>
<?php
for($i=$start;$i<$end;$i++)
{
echo'<tr>';
echo'<td>'.$ARRAY[$i]['MpwName'].'</td>';
echo'<td>'.$ARRAY[$i]['PatientName'].'</td>';
echo'<td>'.$ARRAY[$i]['PatientAddress'].'</td>';
echo'<td>'.$ARRAY[$i]['New'].'</td>';
echo'<tr>';
}
?>
</tbody>
</table>
You can do something like :
$display_column1=$display_column2=$display_column3=$display_column4=true;
if($h1=="")$display_column1=false;
if($h2=="")$display_column2=false;
...
echo'<tr>';
if($display_column1)echo'<td>'.$ARRAY[$i]['MpwName'].'</td>';
if($display_column2)echo'<td>'.$ARRAY[$i]['PatientName'].'</td>';
...
And better, make this in loop.
You can check if $h2 is empty. If not, print:
<table id="usertracking" >
<thead>
<tr>
<?php
echo'<td><h3>'.$h1.'<h3></td>';
if (!empty($h2)) // check
echo'<td><h3>'.$h2.'<h3></td>'; // print
echo'<td><h3>'.$h3.'<h3></td>';
echo'<td><h3>'.$h4.'<h3></td>';
?>
</tr>
</thead>
<tbody>
<?php
for($i=$start;$i<$end;$i++)
{
echo'<tr>';
echo'<td>'.$ARRAY[$i]['MpwName'].'</td>';
if (!empty($h2)) // check
echo'<td>'.$ARRAY[$i]['PatientName'].'</td>'; // print
echo'<td>'.$ARRAY[$i]['PatientAddress'].'</td>';
echo'<td>'.$ARRAY[$i]['New'].'</td>';
echo'<tr>';
}
?>
</tbody>
</table>
Just put your if condition before each echo that you need:
//in the Head
if ($h2) echo'<td><h3>'.$h2.'<h3></td>';
And in the body, at the column data:
if ($h2) echo'<td>'.$ARRAY[$i]['PatientName'].'</td>';
Using a combination of CSS and PHP, you might try something like
<td style="display:<?/*Your condition here*/?>;">
Use this
//in the Head
if ($h2 != ""){ echo'<td><h3>'.$h2.'<h3></td>'; }
// in loop
if ($h2!= ""){ echo'<td>'.$ARRAY[$i]['PatientName'].'</td>'; }
Related
Currently I have a table and is currently coded as:
$min_hor = $min_ver = 1;
$max_hor = $max_ver = 3;
?>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<style>
.table{
margin: 0 auto;
}
</style>
<div class="text-center">
<table class="table" style="width:70%">
<thead>
<tr>
<th></th>
<?php for($j=$min_hor; $j<=$max_hor; $j++){ ?>
<th><?php echo str_pad($j, 2, "0", STR_PAD_LEFT); ?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php for($i=$min_ver;$i<=$max_ver;$i++){ ?>
<tr>
<th><?php echo $i; ?></th>
<?php for($j=$min_hor; $j<=$max_hor; $j++){ ?>
<td><?php echo $i.'-'.str_pad($j, 2, "0", STR_PAD_LEFT) ?></td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
</div>
This display table as shown in attached screenshot.
Now, I have one additional array as $letter_array = array('A','B'); This array is could be bigger than this one and could be upto D,E,F,...
Now, I need to include this A,B into the table header something like as shown in screenshot.
This is where I became helpless. I couldn't find how could I loop this. Although, to make things easier, I have included codepen link of html code.
Codepen
How about we keep headers in 2 different Arrays and then perform a loop.following is the pseudocode which i think will work.
string a[16]={'01','02','03',......,'B'};
string b[17]={'1','2','3',....,'B'};
<table class="table" style="width:70%">
<thead>
<tr>
<th></th>
foreach(var item in a)
{
<th>{{item}}</th>
}
</tr>
</thead>
<tbody>
foreach(var item2 in b)
{
<tr>
<td>{{item2}}</td>
foreach(var item in a)
{
<td>{{item2}}_{{item}}</td>
}
</tr>
}
</tbody>
</table>
Now, before generating table we need elements at array A and B. to generate the array i.e a and b we can do following :
int min_hor=1;
int max_hor=3;
var a_output=[];
var a_inputLetter=['A','B'];
//first for number only.here let us create a string and push to a_output.
for(int i=min_hor,i<=max_hor;i++)
{
a_output.Add('0'+i);
}
//second let us use Number Letter Combination
foreach(var item in a_inputLetter)
{
for(int i=min_hor;i<=max_hor;i++)
{
a_output.Add('0'+{{i}}_{{item}});
}
}
//third for letter number combination
foreach(var item in a_inputLetter)
{
for(int i=min_hor;i<=max_hor;i++)
{
a_output.Add({{item}}_{{i}});
}
}
//and finally for letter_only
foreach(var item in a_inputLetter)
{
a_output.Add({{item}});
}
//similarly do for Array B as well to generate the second array.
Hi all I have searching for method but not luck. Help me I want to make numbering navigation. Here is my code:
<ul>
<?php
$kw = file_get_contents('keywords.txt');
$kw = explode("\n", $kw);
asort($kw);
$i = 0;
foreach($kw as $k) {
if($i==30) { break; }
?>
<li>
<?php echo ucwords($k);?>
</li>
<?php $i++; } ?>
</ul>
The above code just displayed 30 result from 1500 keyword I have. I want to make Page Numbering navigation also shorting A-Z. So user can Short Keyword by Alphabetic above it and page numbering after it.
Use Datatables.
Table
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>KeyWord</th>
</tr>
</thead>
<tbody>
<?php foreach($kw as $k) { ?>
<tr>
<td><?php echo ucwords($k);?></td>
</tr>
<?php } ?>
</tbody>
</table>
Javascript
$(document).ready(function() {
$('#example').DataTable();
} );
Do include the proper plugin files for js & css. from - Datatables
I'm trying to use if-statement to change <td>-s color.
Basically, I have a simple query to retrieve information from the database.
Additionaly, I have a column there which keeps the information like if the task is accomplished or not. When I retrieve the information I get all of them, but I need the accomplished tasks to be green, and others without any color.
I've searhed for the answer, but I couldn't find anything that satisfies me.
For example:
$qry = mysql_query("select * from table");
$recs = array();
while($row = mysql_fetch_array($qry))
$recs[]=$row;
mysql_free_result($qry);
I've tried to add while statement to the code above, but I was confused and it didnt work :(
I'm printing the results using heredoc:
How to give them color here?
<?php
$last_id=0;
foreach($recs as $rec)
{
$html=<<<HTML
<tr>
<td><b>Номер</b></td>
<td>$rec[0]</td>
</tr>
<tr>
<td><b>Номер документа</b></td>
<td>$rec[1]</td>
</tr>
<tr>
<td><b>Дата регистрации</b></td>
<td>$rec[8]</td>
</tr>
<tr>
<td><b>От кого</b></td>
<td>$rec[2]</td>
</tr>
<tr>
<td><b>По</b></td>
<td>$rec[4]</td>
</tr>
<tr>
<td><b>Краткое содержание</b></td>
<td>$rec[3]</td>
</tr>
<tr>
<td><b>Исполнитель</b></td>
<td>$rec[5]</td>
</tr>
<tr>
<td><b>Срок исполнения</b></td>
<td>$rec[6]</td>
</tr>
<tr>
<td><b>Срок исполнения продлен до</b></td>
<td><b>$rec[10]</b></td>
</tr>
<tr>
<td><b>Прислан</b></td>
<td>$rec[9]</td>
</tr>
<tr>
<td><b>Примечание</b></td>
<td>$rec[7]</td>
</tr>
<tr>
<td bgcolor="#838B83"> </td>
<td bgcolor="#838B83"> </td>
</tr>
HTML;
print $html;
if($rec[0]>$last_id)
$last_id=$rec[0];
};
$new_id=$last_id+1;
?>
rather than colour use a class, so you can change it in CSS
<td<?php if($row['complete']) echo ' class="complete"'; ?>>data</td>
<table>
<tr>
<td>column heading</td>
</tr>
<?php
$qry=mysql_query("select * from table");
while($row=mysql_fetch_array($qry)) {
if($row['urcolumnn']==1)
{
echo "<tr bgcolor=green>";
}
else
{
echo "<tr>";
}
?>
<td>
<?php echo $row['urcolumn']; ?>
</td>
</tr>
<?php } ?>
</table>
This is an example code. think this will help you. here i give the background color to <tr> like this if u want to give color to <td> use this <td style="background-color:green;">
I would suggest you to use ternary operator, rather than using IF statement, or your could use another workaround to use arrays to define colors, this would help you to define various colors for each status value globally, please find an example below:
$aryColor = array(
'complete' => 'green',
'incomplete' => "red",
'pending' => 'orange'
.....
);
//you can specify values for all of your status, or leave them blank for no color, the keys in the array are the possible values from your status field
foreach($recs as $rec) {
echo '<tr bgcolor="'.$aryColor[$rec['status_field']].'">
<td>'.$rec['title_field'].'</td>
</tr>';
}
I hope this helps you out, and you can easily edit this for HEREDOC.
first you should change your column values change its structure to int and set default value as "0".so when your task is complete the field value should be change to "1".
now come to your code:
$qry = mysql_query("select * from table");
while($row = mysql_fetch_assoc($qry)){
if($row['status']==1){
echo "<td color="green">Data</td>"
}
else{
echo "<td color="white">Data</td>";
}
}
Hence you can get the rows in green which status is==1 means complete and white for incomplete.
I need to generate a table with php, that will display the images - names stored on database. It has to display 3 images in a row. The images are added to the database all the time, so I need that to be automatically generated, instead of hard coding the tables. I am not sure how do I do that? Please help!
You need to cycle the result recordset and print out the new row every 3rd element.
For example:
<table>
<tr>
<?php $i=0; foreach ($images as $image): ?>
<td><?php echo $image['name'] ?> <img src="<?php echo $image['path'] ?>" /></td>
<?php if(++$i%3==0): ?>
</tr><tr>
<?php endif ?>
<?php endforeach ?>
</tr>
</table>
suppose u get the all images name from database in an array
$img_array = array(
1=>'f.jpg',
2=>'s.jpg',
3=>'t.jpg',
4=>'f.jpg',
5=>'e.jpg'
);
// now create dynamic table via php
<table border="1" cellpadding="2" cellspacing="2" width="100%">
<tr>
<?php
$i=0;
foreach($img_array as $k){
if($i%3==0) { ?> </tr><tr> <?php } ?>
<td><img src="<?php echo $k?>" border="0"></td>
<?php $i++; } ?>
</tr>
</table>
Note: please write full path of image in src before <?php echo $k?>
Iterate the image records, using modulo 3 to change to the next table row.
Something like:
echo '<table><tr>';
foreach ($images) {
echo '<td>$image</td>';
if ($i % 3 == 0) {
echo '</tr><tr>';
}
}
echo '</tr></table>';
A simple table like that would be like
<table>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
</table>
To generate this automatically you need to store where you are in the table, first col, 2nd col or 3th col.
<?php
$pos = 1;
print "<table>"
for ($i=0; $i<=10;$i++)
{
if ($pos==1)
{
print "<tr><td>1</td>";
$pos=2;
}
else if ($pos==2)
{
print "<td>2</td>";
$pos=3;
}
else if ($pos==3)
{
print "<td>3</td></tr>";
$pos=1;
}
}
if ($pos==2 || $pos==3)
print "</tr>";
print "</table>"
Keep in mind that if you use the options with $i%3 from the other comments, that your table will start end/or finish with an empty row. This would need additional checks. The $i%3 will give the remainder of the division of $i and 3. So when $i/3 == 0, means it is true on every third row.
I use mysql_fetch_array to fetch data from a mysql results set:
while($row = mysql_fetch_array($qry_result)){
Next I would like to place the data into columns in a table, but maximum of two columns at each row.
So using a table:
<table>
<tr>
<td>Record Here</td>
<td>Record Here</td>
</tr>
<tr>
<td>Record Here</td>
<td>Record Here</td>
</tr>
<tr>
<td colspan="2">Record Here</td>
</tr>
As you see above, I want to loop the results and create table columns in the loop.
This so that the records line up two and two on a results page.
Remember, if there is an odd number of records, then the last table column would need a colspan of 2, or perhaps just use an empty column?
Anybody know how to do this?
If I use a for loop inside the while loop, I would just be lining up the same records x times for each while loop. Confusing...
Any ideas?
Thanks
Implement a (1-indexed) counter within the while loop. Then add (after the loop):
if ($counter%2)
echo '<td></td>';
This will leave an additional blank cell in your table if the last column contains only one row.
Something like this should work...
<table>
<?php
while(true) {
$row1 = mysql_fetch_array($qry_result);
if($row1 === false) break;
$row2 = mysql_fetch_array($qry_result);
if($row2 !== false) {
echo "<tr><td>$row1</td><td>$row2</td></tr>";
} else {
echo "<tr><td coslspan=\"2\">$row1</td></tr>";
break; // output the final row and then stop looping
}
}
?>
</table>
<table>
<tr>
<?
$i=1;
$num = mysql_num_rows($qry_result);
while($row = mysql_fetch_array($qry_result)){
if($i%2==1)
?>
<tr>
<?
?>
<td <? if($i==$num && $i%2==1)echo 'colspan="2"';?>><?=$row['keyName']?></td>
<?
if($i%2==0)
{
<?
</tr>
?>
}
$i++;
}
?>