I use this code to hide attributes when they do not have any data:
<?php foreach ($_additional[ 'items'] as $_data): ?>
<?php $_attribute=$ _product->getResource()->getAttribute($_data['code']); if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
<tr>
<th class="label">
<?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data">
<?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
This works great, but not for attributes type dropdown.
How can I also hide dropdown attributes, that do not have any value?
If an attribute with the type dropdown isn't set, it usually displays as N/A on the front-end of your website, so you can simply add && $_data['value'] != 'N/A' to the if-statement
So the code would look something like this
<?php foreach ($_additional[ 'items'] as $_data): ?>
<?php $_attribute = $_product->getResource()->getAttribute($_data['code']); if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '' && $_data['value'] != 'N/A')) { ?>
<tr>
<th class="label">
<?php echo $this->htmlEscape($this->__($_data['label'])) ?>
</th>
<td class="data">
<?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
Related
Find blow the source code please, How can I toggle Bootstrap checkbox in PHP For each Statement using jQuery and Pass it the data as an Ajax Request to the endpoint. When I checked any checkbox it will toggle me the value of that particular I toggle.
<?php
if(isset($_GET['page']))
{
$page=$_GET['page'];
}
else
{
$page = 1;
}
$limit = 25;
$offset=($page-1)*$limit;
$select_stmt=$con->prepare("SELECT * FROM plan ORDER BY id DESC LIMIT ?,? ");
$select_stmt->bindValue(1, $offset, PDO::PARAM_INT);
$select_stmt->bindValue(2, $limit, PDO::PARAM_INT);
$select_stmt->execute();
$rows=$select_stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
?>
<tr>
<td><?php echo $row['title']; ?></td>
<td>
<?php
$query = "SELECT * FROM networks WHERE network_id=:myplan_id";
$statement = $con->prepare($query);
$statement->execute([
':myplan_id' => $row['network_id']
]);
$title = $statement->fetch(PDO::FETCH_ASSOC);
echo $title['title'];
?>
</td>
<td>
<?php
$money = Money::of($row['amount'], 'NGN');
echo $money->formatTo('en_US');
?>
</td>
<td>
<?php
if ($row['status'] == NULL || $row['status'] == 'active') {
?>
<span class="badge bg-success">Active</span>
<?php
}else{
?>
<span class="badge bg-danger">In Active</span>
<?php
}
?>
</td>
<td><?php echo $row['created_at']; ?></td>
<td>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="checkbox<?= $row['id']?>">
</div>enter code here
</td>
<td><i class="fa fa-edit"></i> </td>
</tr>
<?php
}
?>
So I'm trying to use a table to update some records in my database but each time I click on update it won't work and it won't do anything. A part of the code below was found in an another topic but it was incomplete so I added some other things.
Js script
$(function(){
$("#loading").hide();
var message_status = $("#status");
$("td[contenteditable=true]").blur(function(){
var field_userid = $(this).attr("id") ;
var value = $(this).text() ;
$.post('update.php' , field_userid + "=" + value, function(data){
if(data != '')
{
message_status.show();
message_status.text(data);
//hide the message
setTimeout(function(){message_status.hide()},1000);
}
});
});
});
This is the table fetching the rows from the database, however everything works besides updating.
HTML & PHP
<form method="post" action="update.php">
<div class="col-sm-12">
<div class="table-responsive">
<table class="table table-striped table-dark">
<tr bgcolor="#df4662" style="color:#FFFFFF;">
<td>ID</td>
<td>Nickname</td>
<td>Name</td>
<td>Rank</td>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td contenteditable="true" id="id:<?php echo $row["id"]; ?>"><?php echo $row["id"]; ?></td>
<td contenteditable="true" id="username:<?php echo $row["username"]; ?>"><?php echo $row["username"]; ?></td>
<td contenteditable="true" id="name:<?php echo $row["steamid"]; ?>"><?php echo $row["steamid"]; ?></td>
<td contenteditable="true" id="ranks:<?php echo $row["ranks"]; ?>"><?php echo $row["ranks"]; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</form>
After a few errors I've been able to have a clean error_logs, but now I don't get any error even after pressing the update button.
update.php
<?php
include '../database.php'
?>
<?php
if(!empty($_POST))
{
foreach($_POST as $field_name => $val)
{
$field_id = strip_tags(trim($field_name));
$split_data = explode(':', $field_id);
$id = $split_data[1];
$field_name = $split_data[0];
if(!empty($id) && !empty($field_name) && !empty($val))
{
$affected_rows = mysqli_query($mysqli,"UPDATE users SET $field_name = '$val' WHERE id = $id");
echo $affected_rows;
echo "Updated";
} else {
echo "Invalid Request";
}
}
}
else {
echo "Invalid Requests";
}
?>
EDIT: Thanking Sam now the problem is just that the record won't update at all
I have the following code for a dropbox;
<select name="Symptom" id="Symptomid" onchange="LSC()">
On change in the dropbox i want to run the following php code;
<?php
$data = array();
{
$symptoms_name = $_POST['Symptom'];
$sql1 = mysql_query("SELECT name,description , comments
FROM symptoms WHERE symptoms.name ='$symptoms_name'") or
die(mysql_error());
while($row1 = mysql_fetch_assoc($sql1)){
$data[] = $row1;
}
}
?>
<?php
if($data){
?>
<table border="1">
<tr>
<td>name</td>
<td>description</td>
<td>comments</td>
</tr>
<?php
if($data){
foreach($data as $sy){
?>
<tr>
<td><?php echo $sy['name']; ?> </td>
<td><?php echo $sy['description']; ?> </td>
<td><?php echo $sy['comments']; ?> </td>
</tr>
<?php
}
}
?>
<?php
}
?>
</table>
}
and display this table under the dropbox on the original html .
I think we have to run a java script which will call the php function and retun the table to the original html.
Kindly assist, I know we have to use ajax.
Regards
Azhar
sorted,
see this has a similar problem
http://www.w3schools.com/php/php_ajax_database.asp
Regards
Azhar
I have an HTML which displays data from a database table. There'a checkbox field, which stores id of the user. And when I click on a row, that ID has to be passed to another page using a JavaScript function. But I have encountered a few errors. I don't know where I went wrong.
This is my Javascript function. It is used to make the table rows clickable. SO when I click on a row, the corresponding ID is passed to editgroup.php page.
<script>
$('table.table tbody tr').click(function(event)
{
if(event.target.type === 'checkbox')
{
event.preventdefault()
}
else
{
window.location='edituser.php?val=<?php echo $id; ?>';
}
});
</script>
Below given is my table:
<table id="edituser" class="clickable"
<thead>
<th></th>
<th>UserID</th>
<th>Username</th>
</thead>
<tbody>
<?php
for($i=0; $i<$x; $i++)
{
$id= $val_array[$i]['id'];
$userName= $val_array[$i]['userName'];
?>
<tr>
<td class="text-center"><input type="checkbox" value="<?php echo $id; ?>"></td>
<td><?php echo $id; ?></td>
<td><?php echo $userName; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
you have to make some change
First in your tr add id like this
<?php
for($i=0; $i<$x; $i++)
{
$id= $val_array[$i]['id']; //data from database
$userName= $val_array[$i]['userName']; //data from database
?>
<tr id="<?php echo $id;?>"> <!-- add id here -->
<td ><input type="checkbox" value="<?php echo $id; ?>"></td>
<td><?php echo $id; ?></td>
<td><?php echo $userName; ?></td>
</tr>
<?php } ?>
now your JS
<script>
$('table.clickable tbody tr').click(function(event){
if(event.target.type === 'checkbox'){
event.preventdefault()
}else{
var val = $(this).attr('id');
window.location='edituser.php?val='+val;
}
}); </script>
Your Javascript will be
$('table.table tbody tr').click(function(event){
if(event.target.type === 'checkbox'){
event.preventdefault()
}else{
var value = (this).find("input[type='checkbox']").val(); /* Get value from clicked tr Checkbox */
window.location.replace('edituser.php?val='+value); /* Pass the value */
}
});
Create new function with dynamic id for your table row
<tr onclick="edit_user(<?php echo $id;?>)" >
....
</tr>
Then you can find your row of where you have to click
<script>
function edit_user(user_id) {
if(user_id != '') {
window.location='edituser.php?val='+user_id;
}
}
</script>
Or else
This is more even simple your code.
<input type="checkbox" value="<?php echo $id; ?>" onclick="document.location.href='edituser.php?val=<?php echo $id; ?>'">
I have my database table named material
material:
mat_id mat_name status
1 bolts
2 steel approved
How can I make my button or maybe href link disable when status = 'approved'
on my html table?
like:
option mat_id material name
show button 1 bolts
no button 2 steel
my code for html/php:
<?php
$resource=mysql_query("Select * from material",$con);
while($result2=mysql_fetch_array($resource))
{
?>
<tr>
<th>option</th>
<th>mat_id</th>
<th>material name</th>
</tr>
<tr>
<td align="center">
<?php
$id = $rows['reqraw_id'];
if($rows['status']=="")
{
echo '<a href="addstockout.php?id=$id" > Update </a>';
} ?>
</td>
<td><?php echo $result2['mat_id']; ?></td>
<td><?php echo $result2['mat_name']; ?></td>
</tr>
<?php };?>
There is a problem on this line
echo '<a href="addstockout.php?id=$id" > Update </a>';
It doesn't get the id array from the db.
...
<?php if($result2['status'] == 'approved') { ?>
<td><button disabled="true">Button</button></td>
<?php
} else{ ?>
<td><button>Button</button></td>
<?php } ?>
...
if($result2['status']=="" OR $result2['status']===NULL)
{
echo '<button type="button">Click Me!</button>';
}else{
echo '<button type="button" disabled>Click Me!</button>';
}
<td><?php echo ($result2['status']===NULL)?"button here":"disabled"; ?></td>
I think this should do it:
Instead of
<td>button</td>
put:
<?php
echo '<td><button ';
if(!($result2['status']==="approved"))
{
echo 'disabled="true"';
}
echo '>Click</button></td>';
?>