PHP Condition Setting for Button [closed] - php

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

Related

Running PHP inside of PHP?

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>

Post the id on delete in codeigniter

I have two tables users and posts where is the primary key is id. When i click on delete button, i want to post the id.
Here is my view:
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Title</th><th scope="col">Hit</th>
<th scope="col">Edit</th><th scope="col">Delete</th><th scope="col">Read More</th>
</tr>
</thead>
<tbody>
<?php foreach($posts as $post) : ?>
<?php if($this->session->userdata('username') == $_SESSION["username"]): ?>
<tr>
<td><?php echo $post['title']; ?> </td>
<td><?php echo $post['post_views']; ?></td>
<td><a class="btn btn-default" href="<?php echo base_url(); ?>posts/edit/<?php echo $post['slug']; ?>">Edit</a></td>
<td>
<?php echo form_open('/posts/delete/'.$post['id']); ?>
<input type="submit" value="Delete" class="btn btn-danger">
<input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
</form>
</td>
<td><p><a class="btn btn-default" href="<?php echo site_url('/posts/'.$post['slug']); ?>">Read More</a></p></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
There are so many ways to do this, the best most common one is to get it from the url like this:
$id = #end($this->uri->segment_array());
Or this:
$id = $this->uri->segment(3);
Or you can do this by passing it in a hidden input like this:
<?php echo form_open('/posts/delete/'.$post['id']); ?>
<input type="submit" value="Delete" class="btn btn-danger">
<input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
</form>
Or using ajax.
delete link with confimation
Delete
call delete method in js and then show confimation msg.
Why you are using form tag to call a function? You can do this with anchor tag easily and can pass id. This one line code will work perfectly
<td><a class="btn btn-danger" href="<?= site_url('posts/delete/'.$post['id']) ?>">Delete</a></td>
In controller delete function will be like this
public function delete($id){
echo $id;
}

php - Data does not insert into table using checkbox

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.

how display and hide the button with the value from database

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 } ?>

Multi-dimensional arrays, but I cannot retrieve data PHP

More errors from my most recent project (posted about another thing earlier).
I've been struggling with this error for a few days now. PHP is not my main programming language, nor am I that great at it.
I'm aware that mysql_* should not be used, also.
Anyways. Here's the code snip.
<?php
$query = "SELECT * FROM events";
$result = mysql_query($query);
$content = array();
$num = mysql_num_rows($result);
if ($num > 0) {
while($row = mysql_fetch_assoc($result)) {
$content[$row['ID']] = $row;
}
}
if ($num > 0) {
?>
<thead>
<tr>
<th><?php echo implode('</th><th>', array_keys(current($content)));?></th><th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($content as $tablerow): ?>
<tr>
<td><?php echo implode('</td><td>', $tablerow);?>
</td><td><div class="btn-group" role="group" aria-label="Actions"><a class="btn btn-primary" role="button" href="#">New Session</a><a class="btn btn-success" role="button" href="#">Continue Latest</a><a class="btn btn-warning" role="button" href="#">Settings</a><?php echo "<a class='btn btn-danger' role='button' href='/manager/delete?id=".$row."'>Delete</a>"; ?>
</tr>
<?php endforeach; ?>
</tbody>
Sorry for derp formatting. Anyways.
I'm trying to get that delete button to link to ../manager/delete?id=x, where x is the ID for the row. My database is formatted in the way where the ID is in a column labeled "ID". I'm trying to reference each of these rows by this ID... why is this not working?
I've also tried (in the place of row), content[row['ID']], content['ID'], 'row['ID']`, with different case on the ID (uppercase/lowercase).
Fred -ii- was correct in his above comment.
I had tried everything other than $tablerow['ID'].... now you know I guess.
Thank you for the help everyone. One of the dumbest things I've ever missed while debugging....
he was partially correct, however i think your id is the key, so you might just reference the key so its a bit cleaner
<?php foreach ($content as $idKey => $tablerow): ?>
<tr>
<td><?php echo implode('</td><td>', $tablerow);?>
</td><td><div class="btn-group" role="group" aria-label="Actions"><a class="btn btn-primary" role="button" href="#">New Session</a><a class="btn btn-success" role="button" href="#">Continue Latest</a><a class="btn btn-warning" role="button" href="#">Settings</a><?php echo "<a class='btn btn-danger' role='button' href='/manager/delete?id=".$idKey."'>Delete</a>"; ?>
</tr>
<?php endforeach; ?>

Categories