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>
Related
I'm working OpenCart version 2.0.3.1 and I'm trying to print a click to call for price and hide to "Add to cart button" if initial price is set to '0.00' I cant seem to figure out the issue here. Code is in product.tpl around line 320 is where I'm working here is my code that I'm trying to add to get working.
<?php if ($price > '0.00') {
Call us at 888-555-6666
<?php } else { ?>
<button type="button" id="button-cart" data-loading-text="<?php echo $text_loading; ?>" class="btn btn-primary btn-lg btn-block"><?php echo $button_cart; ?></button>
<?php } ?>
?>
The logic you use if ($price > '0.00') is fine. But, it seems you're confused about using HTML in PHP code.
It should be like below:
<?php if ($price > '0.00') { ?>
Call us at 888-555-6666
<?php } else { ?>
<button type="button" id="button-cart" data-loading-text="<?php echo $text_loading; ?>" class="btn btn-primary btn-lg btn-block"><?php echo $button_cart; ?></button>
<?php } ?>
I am new to php and javscript.I am working on some project of question and answer system.I want to send question id to javascript function.
here is my php code
<?php
if ($result->num_rows > 0)
{
$i = 1;
while($row = $result->fetch_assoc())
{
$p = $row["description"] ;
echo '<div onclick="myOverFunction(<?= $p ?>)" align="left" class="j" role="group" aria-label="..." > <button id = "btnl<?=$i?>" type="button" class="btn btn-default btn-xs btn-round" style="border-radius:10px;">';
echo $i;
echo '</button>';
echo '<p class="question" style ="display:inline; ">';
echo $p;
echo '</p>';
echo '</div>';
}
}
?>
My javacript function is:
<script>
function myOverFunction(x) {
document.getElementById("demo").innerHTML = x;
document.getElementById("btnl"+x).style.backgroundColor = "#d9534f";
}
</script>
my code does not working ..please help me ...
Your problem is:
echo '<div onclick="myOverFunction(<?= $p ?>)"
Change it to:
echo '<div onclick="myOverFunction('. $p .')".
Same goes for the other occurrences.
You can't echo from inside an echo
Your final echo should look like this:
echo '<div onclick="myOverFunction(\''. $p .'\')" align="left" class="j" role="group" aria-label="..." > <button id = "btnl'.$i.'" type="button" class="btn btn-default btn-xs btn-round" style="border-radius:10px;">';
Note: If $p is a string, it needs quotes. And quotes inside quotes need escaping
You are breaking this line by trying to re-open php tags within the echo:
echo '<div onclick="myOverFunction(<?= $p ?>)" align="left" class="j" role="group" aria-label="..." > <button id = "btnl<?=$i?>" type="button" class="btn btn-default btn-xs btn-round" style="border-radius:10px;">';
Try this:
echo '<div onclick="myOverFunction('.$p.')" align="left" class="j" role="group" aria-label="..." > <button id = "btnl'.$i.'" type="button" class="btn btn-default btn-xs btn-round" style="border-radius:10px;">';
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 } ?>
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
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; ?>