how to create a button to hide the value of the database.
for example :
<a href="#alatrusak" class="btn pull-right btn-danger btn3d" id="alatrusakshow" value="<?php echo $detail['status'] ?> ">
value status = 1 and 2
if button1 value status = 1 results show
and
if button1 value status = 2 results hidden
Underneath your button you would just check the value of $detail['status']
<?php
if ($detail['status'] == 1) {
// echo results
} else {
// echo no result message
}
?>
get the value from db $status=$detail['status'] ; and check if present and show it . very simple :)
<?php
$status=$detail['status'] ;
if($status==1){
?>
<a href="#alatrusak" class="btn pull-right btn-danger btn3d" id="alatrusakshow" value="<?php echo $status;?> ">
<?php
}
elseif($status==2){
?>
<a href="#alatrusak" class="btn pull-right btn-danger btn3d" id="alatrusakshow" value="<?php echo $status;?> ">
<?php
}
That's it!! For more detail on conditional statement check this
<?php if($detail['status']=='1') { ?>
<!-- Show anything you want to display when status = 1 -->
<a href="#alatrusak" class="btn pull-right btn-danger btn3d" id="alatrusakshow" value="<?php echo $detail['status'] ?>">
<?php } ?>
Related
I am still learning and I found that I can't run PHP within PHP, after reading into it makes sense why you cannot do it (D'oh!), what I am struggling to find is the correct way to do this.
I have two buttons which link to different modals, if I do not use them within a IF Statement they work as expected as soon as I add to the IF Statement it breaks the page. (obviously because I am trying to run php within php)
What I'm trying to get working is to show a different button depending on the result of a column called "status" in the MySQL table, if it equals 1 it will show the edit button if anything else it will show a check-out button. I need to pass the <?php echo $fetch['id']?> to the data-target I'm not sure how to go about that.
<?php
$status_code = $fetch["status"];
if("$status_code" == "1")
{ echo '<button type="button" class="button-7" data-toggle="modal" data-target="#update_modal<?
php echo $fetch['id']?>"><span class="glyphicon glyphicon-plus"></span>edit</button>';
} else
{ echo '<button type="button" class="button-7" data-toggle="modal" data-target="#checkout_modal<?
php echo $fetch['id']?>"></span>Check-Out</button>';
} ?>
Any help is much appreciated.
You just need simple concatenation with the . (dot) operator.
E.g.
echo '<button type="button" class="button-7" data-toggle="modal" data-target="#update_modal'.$fetch['id'].'"><span class="glyphicon glyphicon-plus"></span>edit</button>';
...etc.
This is used to join any two string values (whether hard-coded literals or variables) together. What's happening here is your code is building a string from several components and then echoing it.
Documentation: https://www.php.net/manual/en/language.operators.string.php
You don't need to run PHP inside PHP.
You may use a comma (or a dot):
<?php
if ($status_code == "1") {
echo '<button type="button" class="button-7" data-toggle="modal" data-target="#update_modal<'
, $fetch['id']
, '"><span class="glyphicon glyphicon-plus"></span>edit</button>';
} else {
echo '<button type="button" class="button-7" data-toggle="modal" data-target="#checkout_modal'
, $fetch['id']
, '">Check-Out</button>';
}
Or using short tags:
<?php if ($status_code === '1') : ?>
<button type="button" class="button-7" data-toggle="modal" data-target="#update_modal<?= $fetch['id'] ?>">
<span class="glyphicon glyphicon-plus"></span>edit</button>'
<?php else: ?>
<button type="button" class="button-7" data-toggle="modal" data-target="#checkout_modal<?= $fetch['id'] ?>">Check-Out</button>'
<?php endif; ?>
You can have conditions (and other expressions) in short tags:
<button
type="button"
class="button-7"
data-toggle="modal"
data-target="#<?=
$status_code === '1'
? 'update_modal'
: 'checkout_modal'
?><?= $fetch['id'] ?>">
<?php if ($status_code === '1') : ?>
<span class="glyphicon glyphicon-plus"></span>edit
<?php else: ?>
Check-Out
<?php endif; ?>
</button>
And concatenate strings with dots:
<?php
$dataTargetPrefix =
$status_code === '1'
? 'update_modal'
: 'checkout_modal';
?>
<button
type="button"
class="button-7"
data-toggle="modal"
data-target="#<?= $dataTargetPrefix . $fetch['id'] ?>">
<?php if ($status_code === '1') : ?>
<span class="glyphicon glyphicon-plus"></span>edit
<?php else: ?>
Check-Out
<?php endif; ?>
</button>
This status(active 1 /inactive 0) button makes any individual record active/inactive.
I need to change it so that when clicked it makes the that record active and all others inactive by changing tbladdress_status db variable.
if (isset($_GET['tbladdress_status'])) {
if ($_GET['tbladdress_status']=="ok") {
$statusupdate=$db->prepare("UPDATE tbl_address SET
tbladdress_status=:tbladdress_status
WHERE tbladdress_id={$_GET['tbladdress_id']}
");
$update=$statusupdate->execute(array(
'tbladdress_status' => $_GET['activestatus']
));
if ($update) {
Header("Location:../../front-address-all.php?status=ok");
} else {
Header("Location:../../front-address-all.php?status=no");
}
}
}
<?php
if ($pulladdress['tbladdress_status']==1) { ?>
<a href="nedmin/netting/pdo-process.php?tbladdress_id=<?php echo $pulladdress['tbladdress_id'] ?>&activestatus=0&tbladdress_status=ok"><button class="btn btn-success btn-xs"><?php echo _ACTIVE; ?></button>
<?php } elseif($pulladdress['tbladdress_status']==0) {?>
<a href="nedmin/netting/pdo-process.php?tbladdress_id=<?php echo $pulladdress['tbladdress_id'] ?>&activestatus=1&tbladdress_status=ok"><button class="btn btn-danger btn-xs"><?php echo _PASSIVE; ?></button>
<?php } ?>
I have some working PHP code and I have recently added a button that allows the user to download the image form the root directory, in the database we put the file name e.g. example.png / example.jpeg and when the user clicks download it opens the image in a new tab
what we need is: if the [proof] is= NULL , the download button disables, otherwise it will be enabled and they can click the button
<?php
// output data of each row
while($row=mysqli_fetch_assoc($designresult)) {
?>
<div class="card mb-4 box-shadow"><div class="card-header">
<h4 class="my-0 font-weight-normal">Job Reference: <?php echo $row["jobRef"]; ?></h4>
</div>
<div class="card-body">
<p><b>Company Name:</b><br> <?php echo $row["companyName"]; ?> </p>
<p><b>Requested:</b><br> <?php echo $row["dateReq"]; ?> </p>
<p><b>Request By:</b><br> <?php echo $row["yourName"]; ?> </p>
<p><b>Graphic Type:</b><br> <?php echo $row["graphicType"]; ?> </p>
<p><b>Double Sided:</b><br> <?php echo $row["doubleS"]; ?> </p>
<p><b>Design Info:</b><br> <?php echo $row["info"]; ?> </p>
<p><b>Purpose:</b><br> <?php echo $row["purpose"]; ?> </p>
<p><b>Proof:</b><br> <?php echo $row["approved"]; ?> </p>
<p><b>Proof Date:</b><br> <?php echo $row["appDate"]; ?> </p>
<a class="btn btn-success" target="_blank" href="<?php echo IMAGE_DIR . $row['proof']; ?>">Download</a>
</div>
</div>
<?php
}
?>
To my knowledge the link tag does not have a "disabled" attribute. But you can "disable" the link by removing the href attribute.
Something like this: It checks if $row['proof'] has some value (by negating the empty), then it prints out the href if result is true.
<a class="btn btn-success" target="_blank" <?php if(!empty($row['proof'])): ?> href="<?php echo IMAGE_DIR . $row['proof']; ?>" <?php endif; ?> >Download</a>
Or maybe better: Check if variable is empty and give the user a hint that it's not available. I think this is the better solution, because then your users will know what's going on.
<?php if(empty($row['proof'])): ?>
<span>No proof available</span>
<?php else: ?>
<a class="btn btn-success" target="_blank" href="<?php echo IMAGE_DIR . $row['proof']; ?>">
Download
</a>
<?php endif; ?>
<a
class="btn btn-success"
target="_blank"
<?php if(empty($row['proof'])) echo "disabled"; ?>
href="<?php if(!empty($row['proof'])) echo IMAGE_DIR . $row['proof']; ?>">
Download
</a>
try above code. and add disabled class using this condition
<?php
$state = (empty($row['proof'])) ? "disabled='disabled'" : "";
$class = (empty($row['proof'])) ? "disabled" : "";
?>
<a class="btn btn-success <?php echo $class; ?>" target="_blank" href="<?php echo IMAGE_DIR . $row['proof']; ?>" <?php echo $state; ?>>Download</a>
To disabled the button, you need to use disabled HTML attribute. The code above checks $row['proof'] == NULL. If this statement is true it prints disabled = "disabled" in the button element and it isn't true, it prints nothing.
Assuming that you are using bootstrap, .disabled will grayed out the button.
the checkbox is not working.. I could not insert new data into the table
there is nothing happen when I click the "add to cart" button.
<div class="carousel-inner">
<div class="item">
<ul class="thumbnails">
<?php
$query = mysql_query("SELECT * FROM product where pro_category='3' LIMIT 0,4 ");
while ($data = mysql_fetch_assoc($query)):
?>
<li class="span3">
<div class="product-box">
<span class="sale_tag"></span>
<?php echo '<p><img src="admin/pro_image/'.$data['image'].'" /></p>'; ?>
<?php echo ''.$data['name_nl'].''; ?>
<br/>
<td><input class='minuman' type='checkbox' name='add[]' value='<?php echo '<a class="btn btn-success" href="cart.php?add='.$data['code'].'" class="category">
</a>'; ?>'></td>
<p class="price"><?php echo 'RM '.$data['price']; ?></p>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
</div>
<ul class="thumbnails" align="center">
<div id="single_product" align="center">
<?php echo '<a class="btn btn-success" href="cart.php?add='.$data['code'].'" class="category">Add to Cart</a>'; ?>
</div>
</ul>
this is my code for insert cart:
if(isset($_GET['add'])){
$id = $_GET['add'];
$qt = mysql_query("SELECT code, quantity FROM product WHERE code='$id'");
while($qt_row = mysql_fetch_assoc($qt)){
if($qt_row['quantity'] != $_SESSION['cart_'.$_GET['add']] && $qt_row['quantity'] > 0){
$_SESSION['cart_'.$_GET['add']]+='1';
header("Location: keranjang.php");
} else {
echo '<script language="javascript">alert("Stok produk tidak mencukupi!"); document.location="index.php";</script>';
}
}
}
According to PHP Documentation, mysql_query is deprecated since PHP 5.5.0, and removed in PHP 7.0.0. You may use mysqli or PDO instead.
This row might cause problems, because it's not escaped:
<td><input class='minuman' type='checkbox' name='add[]' value='<?php echo '<a class="btn btn-success" href="cart.php?add='.$data['code'].'" class="category">
</a>'; ?>'></td>
and this is one of the ways to escape:
<td>
<input class="minuman" type="checkbox" name="add[]" value="<?php echo '<a class="btn btn-success" href="cart.php?add=' . $data['code'] . '" class="category"></a>'; ?>" />
</td>
About "add to cart" - You can store the products in the session or in a cookie (I think cookies can be a better option), and then show them in the cart.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
For the first page, I want to set the column 'content control' to be button 'update or delete' because it already got data for the particular row. While for the second page, I want to set the column 'content control' to be button 'add or update or delete' as it don't have data along with the row yet. The problem I faced is how to set the first page to contain button 'update or delete' while for the second page to contain button 'add or update or delete'.
<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db("smart_train",$conn);
$result = mysql_query("SELECT * FROM station");
?>
<?php
$i=0;
while($row = mysql_fetch_array($result)) {
$i%2==0;
switch ($i) {
case "0":
$classname="1Row";
$goesToPage='add_station_update1.php';
break;
case "1":
$classname="2Row";
$goesToPage='add_station_update2.php';
break;
case "2":
$classname="3Row";
$goesToPage='add_station_update3.php';
break;
case "3":
$classname="4Row";
$goesToPage='add_station_update4.php';
break;
case "4":
$classname="5Row";
$goesToPage='add_station_update5.php';
break;
case "6":
$classname="6Row";
$goesToPage='add_station_update6.php';
break;
case "6":
$classname="7Row";
$goesToPage='add_station_update7.php';
break;
case "7":
$classname="8Row";
$goesToPage='add_station_update8.php';
break;
case "8":
$classname="9Row";
$goesToPage='add_station_update9.php';
break;
case "9":
$classname="10Row";
$goesToPage='add_station_update10.php';
break;
}
?>
<tr>
<td> <?php echo $row["id"];?> </td>
<td> <?php echo $row["thailand"];?> </td>
<td> <?php echo $row["malaysia"];?> </td>
<td>
<div class="btn-group" class="<?php if(isset($classname)) echo $classname;?>">
<a href="<?php echo $goesToPage; ?>">
<button onclick="myFunction()" type="button" class="btn btn-warning">Update</button>
</a>
<a><button type="button" class="btn btn-default" id="content_control_or">or</button></a>
<a><button type="button" class="btn btn-warning">Delete</button></a>
<?php if(isset($message)) { echo $message; } ?>
</div>
<div class="btn-group" class="<?php if(isset($addclass)) echo $addclass;?>">
<a href="<?php echo $goesToPage; ?>">
<button onclick="myFunction()" type="button" class="btn btn-warning">Add</button>
</a>
<a><button type="button" class="btn btn-default" id="content_control_or">or</button></a>
<a href="<?php echo $goesToPage; ?>">
<button onclick="myFunction()" type="button" class="btn btn-warning">Update</button>
</a>
<a><button type="button" class="btn btn-default" id="content_control_or">or</button></a>
<a><button type="button" class="btn btn-warning">Delete</button></a>
<?php if(isset($message)) { echo $message; } ?>
</div>
<?php
$i++;
}
?>
</td>
</tr>
Your code is unclear you are missing pagination display part ... but anyway with current placed code i can suggest you this simplified algorithm, with following optimizations
First of all you should create add_station.php and update_station.php files which are expect from GET $_GET['id'] pram and then display your add or edit form fir data based on that id, we are going to use such links in html add_station.php?id=.$ID instead writing many switch and creating many duplicated add_station_update1..10.php files
<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db("smart_train",$conn);
$result = mysql_query("SELECT * FROM station");
?>
<?php
while($row = mysql_fetch_array($result)) {
?>
<tr>
<td> <?php echo $row["id"];?> </td>
<td> <?php echo $row["thailand"];?> </td>
<td> <?php echo $row["malaysia"];?> </td>
<td>
<div class="btn-group" >
<?php if(empty($row["thailand"]) && empty($row["malaysia"]) ){?>
<a href="add_station.php?id=<?php echo $row["id"];?>">
<button onclick="myFunction()" type="button" class="btn btn-warning">Add</button>
</a>
<?php }?>
<a href="update_station.php?id=<?php echo $row["id"];?>">
<button onclick="myFunction()" type="button" class="btn btn-warning">Update</button>
</a>
<a><button type="button" class="btn btn-warning">Delete</button></a>
<?php if(isset($message)) { echo $message; } ?>
</div>
</td>
</tr>
<?php } ?>
also my suggestion to you do not display or word between buttons it is anyway clear for user that he can press or on update or on delete ...
if something from above code unclear just ask