Echo within an Echo Codeigniter - php

so I am new to codeigniter and PHP and I have a problem. I can't get the proper syntax to execute an echo within an echo. In my code, the else won't work since I'm using another php code to get the site_url() in codeigniter. Here's the code.
<td><?php if($row->homeowner_feedback == 0) {echo "Finished"; } else { echo '<button type="button" class="btn btn-custom-3" data-href="<?php echo site_url() . "user_tracking/set_finished_recent/" . $row->ticketid; ?>" data-toggle="modal" data-target="#delete-modal">Set as Finished</button>'?></td>

Try like this.load url helper first using $this->load->helper('url'); in controller.use dot(.) for concatenation rateher than nested echo.
<td><?php if($row->homeowner_feedback == 0)
{
echo "Finished";
} else {
echo "<button type='button' class='btn btn-custom-3' data-href='".base_url()."'/user_tracking/set_finished_recent/".$row->ticketid."' data-toggle='modal' data-target='#delete-modal'>Set as Finished</button>";
} ?></td>

Just for fun, when you have an if/else that is performing the same function, in this case echo'ing something, you could use this ternary operation instead (Tested).
<td>
<?= ($row->homeowner_feedback == 0)
? 'Finished'
: '<button type="button" class="btn btn-custom-3" data-href="' . base_url() . '/user_tracking/set_finished_recent/' . $row->ticketid . ' data-toggle="modal" data-target="#delete-modal">Set as Finished</button>';
?>
</td>

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>

how to disable button when date(d-m-Y) value is less than date(30-m-Y) using if condition PHP

i have workers or employee table .. and i have Resign Date inside the table ..
and there is Edit Button, Replace Button and Delete button in every single data in table ..
Replace BUTTON is for replacing the new worker over the resigned worker.. and i already make the button disable when the resign date is empty and enable when it not empty .
now i don't know how can i make the button enable in next month of resign date.
for example :
if somebody resigns on 17/3/2021, the button should be disabled until 1/4/2021 (the first day of the next month) .. i hope someone can help me
here is my table :
$data = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC);
$kode=trim($data ['id_mcm']);
$kode_mcm=trim($data ['kode_mcm']);
$nama_mcm=trim($data ['nama_mcm']);
$kode_ctr=trim($data ['kode_ctr']);
$nama_ctr=trim($data ['nama_ctr']);
$no_ktp=trim($data ['no_ktp']);
$no_hp=trim($data['no_hp']);
$domisili=trim($data['domisili']);
$tgl_join=date_format($data ['tgl_join'],'d-m-Y');
if ($data ['tgl_resign']!=''){$tgl_resign=date_format($data ['tgl_resign'],'d-m-Y');
<!--i tried to added this code below so i can use if inside Replace button tag with less operation-->
$tgl_resign_baru=date_format($data ['tgl_resign'],'30-m-Y');
}else{$tgl_resign='';}
$alamat=trim($data['alamat']);
?>
<tr <?php if($tgl_resign!=''){echo 'style="background-color:#f2c4cd"';} else {echo'';}?>>
<td><?php echo ++$no_urut;?></td>
<td><?php echo $kode_mcm;?></td>
<td><?php echo $nama_mcm;?></td>
<td><?php echo $kode_ctr;?></td>
<td><?php echo $nama_ctr;;?></td>
<td><?php echo $no_ktp;?></td>
<td><?php echo $no_hp;?></td>
<td><?php echo $domisili;?></td>
<td><?php echo $tgl_join;?></td>
<td><?php echo $tgl_resign;?></td>
<td><?php echo $alamat;?></td>
<td>
and here is the Button tag :
<!-- button Modal replace-->
<button type="button" class="btn btn-success btn-xs" data-toggle="modal" data-target="#myy<?php echo $kode; ?>"
<?php if (($akses=="Leader" || $akses=="IT") && $tgl_resign!="" && $kode_mcm!=""
&& $tgl_resign>$tgl_resign_baru) <-- and i add this too . but it doesn't work
{
echo '';
}else{
echo 'disabled';}?>>
Baru
</button>
You can simplify this logic by setting the resign date to the the end of the month, and then comparing that to the current date. If it's greater than or equal to the current date you can disable the button.
<?php
$resignDate = '2021-03-17';
$resignDateEOM = date('Y-m-t', strtotime($resignDate)); //2021-03-31
$currentDate = date('Y-m-d');
if ($resignDateEOM >= $currentDate) {
echo 'resign date end-of-month is still in the future';
}
<?php
$disabled = '';
if (
($akses=="Leader" || $akses=="IT")
&& $tgl_resign!=""
&& $kode_mcm!=""
&& $resignDateEOM >= $currentDate
) {
$disabled = 'disabled';
}
?>
<button
type="button"
class="btn btn-success btn-xs"
data-toggle="modal"
data-target="#myy<?= $kode; ?>"
<?= $disabled; ?>
></button>

Syntax error on php code

PLease help me with this code
<?php
if (!empty($this->product->customfieldsSorted['youtube'])) {
$this->position = 'youtube';
echo '<button class="shop_tablinks tab2" onclick='"openSpecs(event, 'Specs3')"' >';
echo JText::_('Video');
echo '</button>';
} // Product Custom ontop end
?>
It seems I don't wrote ok the
onclick='"openSpecs(event, 'Specs3')"' >'
Replace you code with below:
<?php if (!empty($this->product->customfieldsSorted['youtube'])) {
$this->position = 'youtube';
echo '<button class="shop_tablinks tab2" onclick="openSpecs(event, \'Specs3\')">';
echo JText::_('Video');
echo '</button>';
} // Product Custom ontop end
?>
Need to use escape operator for '' if you want to use in html code while echoing as you are echoing with ''. So in HTml code its taking it as concat operator.
Change your line
echo '<button class="shop_tablinks tab2" onclick='"openSpecs(event, 'Specs3')"' >';
to
echo '<button class="shop_tablinks tab2" onclick="openSpecs(event, \'Specs3\')" >';
final code:
if (!empty($this->product->customfieldsSorted['youtube'])) {
$this->position = 'youtube';
echo '<button class="shop_tablinks tab2" onclick="openSpecs(event, \'Specs3\')" >';
echo JText::_('Video');
echo '</button>';
}

passing php variable in javascript function

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;">';

Search Button in php No Result error message

I'm having trouble where should i put my echo 'No Result Found'; in the code code I tried putting it in the last else statement together in code for debugging but it doesn't work. Please help where should I put my no result found. Thanks in advance.
if(!empty($_POST['search'])){
if($result = $db->query("SELECT * FROM product WHERE setname like '%".$_POST['search']."%' OR category like '%".$_POST['search']."%' ")) {
while($row = $result->fetch_assoc()) {
echo '<div class="col-sm-3">';
echo '<form method="POST" action="buynow.php" enctype="multipart/form-data"> ';
echo '<input type="hidden" name="productid" value="'.$row['id'].'">';
echo '<img class="thumbnail img-responsive" src="data:image;base64,'.$row['image'].' " >';
echo '<p>Name : ',$row['setname'], '</span></p>';
echo '<p>Price : ', $row['price'], '</span></p>';
echo '<p>Bonus : <span class="label label-info" style="font-size:16px;">', $row['status'], '</span></p>';
echo '<p>Price Now : ', $row['pricesale'], '</span></p>';
echo '<p>Product Detail: ', $row['productdesc'] ,'</p>';
echo '<button class="btn btn-danger btn-lg btn-block" type="submit" name="submit">Buy Now</button>';
echo '</form>';
echo '<br></div>';
}
echo '</div>';
echo '</div>';
}else{
//code for debugging query
die($db->error);
}
}
You need to count number of num_rows from your query. If no rows found the show no result found
if (!empty($_POST['search'])) {
if ($result = $db->query("SELECT * FROM product WHERE setname like '%" . $_POST['search'] . "%' OR category like '%" . $_POST['search'] . "%' ")) {
$row = $result->num_rows;
if ($row > 0) {
while ($row = $result->fetch_assoc()) {
echo '<div class="col-sm-3">';
echo '<form method="POST" action="buynow.php" enctype="multipart/form-data"> ';
echo '<input type="hidden" name="productid" value="' . $row['id'] . '">';
echo '<img class="thumbnail img-responsive" src="data:image;base64,' . $row['image'] . ' " >';
echo '<p>Name : ', $row['setname'], '</span></p>';
echo '<p>Price : ', $row['price'], '</span></p>';
echo '<p>Bonus : <span class="label label-info" style="font-size:16px;">', $row['status'], '</span></p>';
echo '<p>Price Now : ', $row['pricesale'], '</span></p>';
echo '<p>Product Detail: ', $row['productdesc'], '</p>';
echo '<button class="btn btn-danger btn-lg btn-block" type="submit" name="submit">Buy Now</button>';
echo '</form>';
echo '<br></div>';
}
} else {
echo "No result Forund";
}
} else {
//code for debugging query
die($db->error);
}
}
echo '</div>';
echo '</div>';
You can either use $result->num_rows or mysqli_num_rows($result)
if ($result->num_rows == 0) {
echo 'No Result Found';
}
else {
// Do your while
}
And please use prepared statements
Use prepared statements.
Use PDO, not mysqli.
Use templates.
So it goes
$sql = "SELECT * FROM product WHERE setname like :search OR category like :search";
$stmt = $pdo->prepare($sql);
$stmt->execute(array('search' => '%'.$_POST['search'].'%'));
$data = $stmt->fetchAll();
?>
<? foreach($data as $row): ?>
<div class="col-sm-3">
<form method="POST" action="buynow.php" enctype="multipart/form-data">
<input type="hidden" name="productid" value="<?=$row['id']?>">
<img class="thumbnail img-responsive" src="data:image;base64,<=$row['image']?>">
<p>Name : <?=$row['setname']?></span></p>
<p>Price : <?=$row['price']?></span></p>
<p>Bonus : <span class="label label-info" style="font-size:16px;">
<?=$row['status']?>
</span></p>
<p>Price Now : <?=$row['pricesale']?></span></p>
<p>Product Detail: <?=$row['productdesc']?></p>
<button class="btn btn-danger btn-lg btn-block" type="submit" name="submit">Buy Now</button>
</form>
<br>
</div>
<? endforeach ?>
<? if (!$data): ?>
No Result Found
<? endif ?>

Categories