I have table with variables in it. I want to hide whole table if the first variable is empty. I want to hide id="con_industry (whole table) if $list_primary_industry variable is empty. Here is my code:
<table class="admintable" id="con_industry">
<tr id="con_id_industry1">
<td class="key"><?php echo JText::_('PRIMARY_INDUSTRY');
?></td<td>echo $list_primary_industry; ?></td></tr>
</table>
Set a check before your table:
<?php if(isset($list_primary_industry) && !empty($list_primary_industry)): ?>
<table class="admintable" id="con_industry">
<tr id="con_id_industry1">
<td class="key"><?php echo JText::_('PRIMARY_INDUSTRY'); ?></td>
<td>echo $list_primary_industry; ?></td>
</tr>
</table>
<?php endif; ?>
Like this: (fixed your code a bit)
<? if($list_primary_industry) {?>
<table class="admintable" id="con_industry">
<tr id="con_id_industry1">
<td class="key"><?php echo JText::_('PRIMARY_INDUSTRY');
?></td><td><? echo $list_primary_industry; ?></td></tr>
</table>
<?}?>
Related
This question already has answers here:
How do I highlight table row when fetching all information with array?
(2 answers)
Closed 3 months ago.
Given the following code, I want to highlight the row of a table wherein the $listing->Full == '1'.
<table id="datatable-responsive" cellspacing="0" width="100%">
<thead>
<tr>
<th align="center">Qty</th>
<th align="center">Posted Date</th>
<th align="center">Expiration</th>
<th align="center">Full Pkg</th>
</tr>
</thead>
<tbody>
<?php foreach($listings as $listing):
?>
<tr>
<td align="center"><?php echo $listing->quantity; ?></td>
<td align="center"><?php
$pdate = new DateTime($listing->posted_at);
echo $pdate->format('m/d/y'); ?></td>
<td align="center"><?php
$date = new DateTime($listing->expdate);
echo $date->format('m/d/y');
?>
</td>
<td align="center"><?php
if($listing->full == '1'):
?>
<?php echo "Yes"; ?>
<?php else:
?>
<?php echo "No"; ?>
<?php endif; ?>
</td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
So ideally, the highlight color would be FFFFE6. Any help is greatly appreciated. This is an MVC site, so changing the CSS isn't conveniently an option.
Test before you set the style. Here is an inline example:
<?php
if($listing->full == '1'):
?>
<td align="center" style="background:#FFFFE6">
<?php echo "Yes"; ?>
<?php else: ?>
<td align="center">
<?php echo "No"; ?>
<?php endif; ?>
</td>
<?php endif; ?>
Here is an example using a class in your CSS:
CSS
.highlight {
background: #FFFFE6;
}
PHP/HTML
<?php
if($listing->full == '1'):
?>
<td align="center" class="highlight">
<?php echo "Yes"; ?>
<?php else: ?>
<td align="center">
<?php echo "No"; ?>
<?php endif; ?>
</td>
<?php endif; ?>
Need help...
I have 2 php file which are screen1.php and screen2.php
screen1.php consist of main data and screen2.php consist of history of main data.
Both main data and history store in 1 table.
How do I work if, i click main data in screen1.php it load history data from screen2.php into screen1.php
Thank you
If all the data is in the same table then you just need to include the data in your query? If you wanted to keep your query simple you could use Ajax
This looks like a job for Ajax.
You will need to include jquery library for this.
Page: main.php?user_id=4
<!----include Jquery library----->
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<!----include Jquery library----->
<title>Main Data</title>
</head>
<body>
<?php $sql="SELECT username, date, account_number, status
FROM table WHERE user_id=" . $_GET['user_id'] ;
//Do a prepared Statement to avoid SQL injection. Here I will just keep things simple
$user=$db->query($sql)->fetch();
?>
<table width="100%" border="1" id="main">
<tbody>
<th colspan="4">Main Data</th>
<tr>
<td align="center"><strong>Username</strong></td>
<td align="center"><strong>Date</strong></td>
<td align="center"><strong>Account Number</strong></td>
<td align="center"><strong>Status</strong></td>
</tr>
<tr>
<td><?php echo $user['username']//James Bond ?></td>
<td><?php echo $user['date']//18 Jan 2015 ?> </td>
<td><?php echo $user['account_number']//58241687929876 ?> </td>
<td><?php echo $user['status']//Active ?> </td>
</tr>
</tbody>
</table>
<button id="view_more">View more</button><!---give button a id--->
<div id="history">
<!------New data will be added here------>
</div>
<!------- THIS IS THE MAGIC PART ----------->
<script>
$(document).on('click','#view_more',function(){
//Event, button ID
$.ajax({
url:"view_history.php?user_id=<?php echo $_GET['user_id']; ?>"
//Link to history.php. Pass user_id to url
success:function(data){
$('#history').empty();
//empty history div if there is anything
$('#history').append(data).hide().fadeIn(500);
//And then append new data
}
});
});
</script>
<!------- THIS IS THE MAGIC PART ----------->
</body>
Page: history.php
<?php
$sql="SELECT type,date FROM table WHERE id=" . $_GET[user_id];
$history=$db->query($sql)->fetch();
?>
<table width="100%" border="1">
<tbody>
<th colspan="4">History of James Bond</th>
<tr>
<td align="center" width="50%"><strong>Type</strong></td>
<td align="center"><strong>Date</strong></td>
</tr>
<tr>
<td><?php echo $history['type'];//Fund ?></td>
<td><?php echo $history['date'];//18 Jan 2015 ?></td>
</tr>
<tr>
<td><?php echo $history['type'];//Refund ?></td>
<td><?php echo $history['date'];//21 Jan 2014 ?></td>
</tr>
<tr>
<td><?php echo $history['type'];//Refund ?></td>
<td><?php echo $history['date'];//22 Jan 2014 ?> </td>
</tr>
</tbody>
</table>
Please be careful take care of security measures.
I've stucked a bit when it comes to a small piece of my PHP code. The role of this script is to display whole table from mysql plus adding to each row a hyperlink to delete such row.
<?php
$connection=mysql_connect('localhost','root','') or die(mysql_error());
error
mysql_select_db('localhost_db',$connection) or die(mysql_error());
$query=mysql_query("SELECT * FROM cms_contest") or die(mysql_error());
if(mysql_num_rows($query)>0):
?>
<table width="100%" border="0">
<tr style="font-weight:bold;">
<td align="center">Id</td>
<td align="center">First Name</td>
<td align="center">Last Name</td>
<td align="center">Email</td>
<td align="center">Phone</td>
<td align="center">Answer</td>
<td align="center">Remove</td>
</tr>
<?php
while($row=mysql_fetch_object($query)):?>
<tr>
<td align="center"><?php echo $row->ID; //row id ?></td>
<td align="center"><?php echo $row->name; // row first name ?></td>
<td align="center"><?php echo $row->surname; //row las tname ?></td>
<td align="center"><?php echo $row->email; //row created time ?></td>
<td align="center"><?php echo $row->phone_number; //row created time ?></td>
<td align="center"><?php echo $row->contest_answer; //row created time ?></td>
<td align="center">REMOVE</td>
</tr>
<?php endwhile;?>
</table>
<?php
else: ?>
<h3>No Results found.</h3>
<?php endif; ?>
It would be perfect if there appear a short javascript popup with question like: are you sure you want to remove this entry? OK / Cancel.
I have no clue how to do it.. thanks for any tips!
Simple/stupid method:
<td>nuke me</td>
But if a web spider or a browser link pre-fetch tool gets loose on this page, you'll be nuking ALL of your records.
Somewhat better:
<td>
<form method="post" action="deleteme.php">
<input type="hidden" name="id" value="<?php echo $row->ID ?>" />
<input type="submit" value="Nuke me" />
</form></td>
And then there's various options involving radio buttons/checkboxes, or JS to trap the click-on-the-link etc...
But, in the end, they ALL boil down to "you have to sent the ID of the row back to the server". How you go about that is up to you... just don't use the plain "click here" version.
Indeed there are several different ways to do this. One way I like is to wrap the entire table in a form that submits to the delete script, and use a button for each row with the row ID as its value.
<form method="post" action="delete.php">
<table width="100%" border="0">
<tr style="font-weight:bold;">
<td align="center">Id</td>
<td align="center">First Name</td>
<td align="center">Last Name</td>
<td align="center">Email</td>
<td align="center">Phone</td>
<td align="center">Answer</td>
<td align="center">Remove</td>
</tr>
<?php while($row=mysql_fetch_object($query)): ?>
<tr>
<td align="center"><?php echo $row->ID; //row id ?></td>
<td align="center"><?php echo $row->name; // row first name ?></td>
<td align="center"><?php echo $row->surname; //row las tname ?></td>
<td align="center"><?php echo $row->email; //row created time ?></td>
<td align="center"><?php echo $row->phone_number; //row created time ?></td>
<td align="center"><?php echo $row->contest_answer; //row created time ?></td>
<td align="center">
<button name="delete-id" type="submit" value="<?php echo $row->ID; ?>">
REMOVE
</button>
</td>
</tr>
<?php endwhile;?>
</table>
</form>
There are also many different ways to confirm the deletion. The simplest I know of on the client side is to add onclick="return confirm('Are you sure?');" to the delete button.
click event:
DELETE
<script type="text(javascript">
function confirmDelete(id){
if(confirm('sure u want to delete entry with id: ' + id + '?')){
window.location.href = "ursite.php?id="+id+"&delete=true";
}
}
</script>
PHP
if(isset($_GET['delete'])){
$stmt = "DELETE FROM cms_contest WHERE ID = ".$_GET['id'];
mysql_query($stmt);
}
it's the worst way to do it.
I recommend you to look at:
jQuery $.post()jQuery $.ajax()
I hope it will help you.
I am looping through an array and building tables. The HTML is then sent to DOMPDF. However, DOMPDF will not create the PDF if the HTML is ill formatted. I assume that is what is happening in my case. Here is my loop:
<?php foreach($credits as $credit) : ?>
<?php if($credit['credit_type'] == "short") : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;">
<tr>
<td><strong><?php echo $credit['category_title']; ?></strong></td>
</tr>
<tr>
<td><?php echo $credit['credit_heading']; ?></td>
</tr>
</table>
<?php endif; ?>
<?php if($credit['credit_type'] == "long") : ?>
<?php if($credit['category_title'] != $oldvalue) : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;">
<tbody>
<?php endif; ?>
<tr>
<?php if($credit['category_title'] != $oldvalue) : ?>
<td width="25%"><strong><?php echo trim($credit['category_title']); ?></strong></td>
<td width="25%"><strong>Title</strong></td>
<td width="25%"><strong>Role</strong></td>
<td width="25%"><strong>Director</strong></td>
<?php endif; ?>
</tr>
<tr>
<td width="25%"><?php echo $credit['credit_heading'];?></td>
<td width="25%"><?php echo $credit['credit_title']; ?></td>
<td width="25%"><?php echo $credit['credit_role']; ?></td>
<td width="25%"><?php echo $credit['credit_director']; ?></td>
</tr>
<?php if($credit['category_title'] != $oldvalue) : ?>
</tbody>
</table>
<?php endif; ?>
<?php $oldvalue = $credit['category_title']; ?>
<?php endif; ?>
<?php endforeach; ?>
I cannot for the life of me work out which tag I am not closing. If anyone could give some insight, that would be fab!
Specifically, the loop is creating rows that show some headings, and then spit out futher rows whenever the category title changes.
This may be a simple solution but perhaps not the best:
I recommend you to use PHP's Tidy class (eventually you'll have to install it first...)
Here is the link for the Tidy class Manual.
At the first line:
ob_start();
This command buffers everything what is outputed by your follwing script.
The code below should be added at the end of your file, or there where you want to show the output.
It first gets the buffer with ob_get_contents() and than it cleans the code up.
Note that you'll eventually have to change the configuration parameters for your needs, there are really very much.
$raw_output = ob_get_clean();
$config = array('indent' => true, 'output-xhtml' => true, 'wrap' => 0);
$tidy = new Tidy;
$tidy->parseString($raw_output, $config, 'utf8');
$tidy->cleanRepair();
echo $tidy;
This Example Code was modified by the original version of the example on php.net.
Hope that helps you.
It's a bit difficult to parse without known more about your data. For example, why is a table for "short" credit open and closed with the record, but the table for "long" credit is conditional on the previous record? Is it because you have a flat data structure so related data shows up as a series of consecutive rows? If that's the case things would be easier if the data were a bit more normalized. I.e. you could iterate through each credit record then through the details separately. Any possibility of fixing your data structure?
Analyzing the code you have, your problem appears to be in the logic for the second section of the code. You are setting the value of the variable $oldvalue at the end of the loop. This is after the logic that closes the table. So if you parse two records that have the same category title the second record will output it's table rows completely outside a table (never mind that it will also have a completely empty row). Additionally, if you have a short credit type following a long the table will never be closed.
That being said, working with what you have I'm guessing you may need something like the following:
// build a dummy "previous" record for the first iteration so the conditionals don't break.
<?php $previous_credit = array('credit_type'=>null,'category'=>null); ?>
<?php foreach($credits as $credit) : ?>
<?php if($credit['credit_type'] == "short" || ($previous_credit['credit_type'] == "long" && $previous_credit['category'] != $credit['category'])) : ?>
</tbody>
</table>
<?php endif; ?>
<?php if($credit['credit_type'] == "short") : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;">
<tr>
<td><strong><?php echo $credit['category_title']; ?></strong></td>
</tr>
<tr>
<td><?php echo $credit['credit_heading']; ?></td>
</tr>
</table>
<?php endif; ?>
<?php if($credit['credit_type'] == "long") : ?>
<?php if($credit['category_title'] != $previous_credit['category_title']) : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;">
<tbody>
<tr>
<td width="25%"><strong><?php echo trim($credit['category_title']); ?></strong></td>
<td width="25%"><strong>Title</strong></td>
<td width="25%"><strong>Role</strong></td>
<td width="25%"><strong>Director</strong></td>
</tr>
<?php endif; ?>
<tr>
<td width="25%"><?php echo $credit['credit_heading'];?></td>
<td width="25%"><?php echo $credit['credit_title']; ?></td>
<td width="25%"><?php echo $credit['credit_role']; ?></td>
<td width="25%"><?php echo $credit['credit_director']; ?></td>
</tr>
<?php endif; ?>
<?php $previous_credit = $credit; ?>
<?php endforeach; ?>
<!-- one last table close for the last record -->
</tbody></table>
(That's some ugly code and I don't have time to keep revising it, so ... community wiki in case anyone else wants to clean it up.)
$result is actually an array which looks like this:
Array ( [book_title] => Bioethics in the 21st Century [id] => 1424
[isbn] => 978-953-307-270-8 [unix_name] =>
bioethics-in-the-21st-century [visible_online] => 1 )
This is my view(better said...poor attempt of a view ). I'm trying to get an alignment based on the index of the array. Like so:
http://pastebin.com/z13PZWe8
<table class="datagrid grid_collapsible" width="100%" cellpadding="2" cellspacing="0" id="webbooks_table">
<thead>
<tr class="datagrid_header"
<td>Book title</td>
<td>ID</td>
<td>ISBN</td>
<td>Is it visible online?</td>
</tr>
</thead>
<tbody>
<?php foreach($this->basicBwDetails as $result): ?>
<tr>
<td><?=$result;?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Thank you for your help!
Are you trying to do this?
<table class="datagrid grid_collapsible" width="100%" cellpadding="2" cellspacing="0" id="webbooks_table">
<thead>
<tr class="datagrid_header">
<td>Book title</td>
<td>ID</td>
<td>ISBN</td>
<td>Is it visible online?</td>
</tr>
</thead>
<tbody>
<?php foreach($this->basicBwDetails as $result): ?>
<tr>
<td><?php echo $result['book_title']; ?></td>
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['isbn']; ?></td>
<td><?php echo ($result['visible_online']) ? 'Yes' : 'No'; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As a side note, <?=$var;?> syntax should be avoided since short_open_tag is disabled in many PHP installations, and this was required to use that syntax until PHP 5.4.0
Depends on how You got the result form database it will be something like this:
<td><?=$result['book_title']?> </td>
<td><?=$result['id']?> </td>
<td><?=$result['isbn']?> </td>
<td><?=$result['visible_online']?> </td>
Or if You're using doctrine:
<td><?=$result->book_title?> </td>
<td><?=$result->id?> </td>
<td><?=$result->isbn?> </td>
<td><?=$result->visible_online?> </td>
You should read tutorial, things like that are in it :)
http://framework.zend.com/manual/en/zend.db.statement.html
...
<tbody>
<?php foreach($this->basicBwDetails as $result): ?>
<tr>
<?php foreach($result as $cell)?>
<td><?=$cell;?> </td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
...
Like this ?