CodeIgniter Forums
how to mix html and php? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: how to mix html and php? (/showthread.php?tid=81170)

Pages: 1 2


how to mix html and php? - richb201 - 01-30-2022

<
I am trying to determine which of these buttons to display based on the num_projects. But the else doesn't seem to work.   
<php if ($this->dataStore("cost_center")->get(0,"num_projects")>0 ;?>
            <button type="button" class="btn btn-success"><i class="fa fa-star"></i>  1. Company</button>
        else
            <button type="button" class="btn btn-danger"><i class="fa fa-star"></i>  1. Company</button>


RE: how to mix html and php? - InsiteFX - 01-31-2022

PHP Code:
<?php if ($this->dataStore("cost_center")->get(0,"num_projects")>0): ?>
    <button type="button" class="btn btn-success"><i class="fa fa-star"></i>  1. Company</button>
<?php else: ?>
    <button type="button" class="btn btn-danger"><i class="fa fa-star"></i>  1. Company</button>
<?php endif ?>



RE: how to mix html and php? - richb201 - 01-31-2022

Thanks. It seems phpStorm doesn't like <?php endif ?>  So I tried

    <php if ($this->dataStore("cost_center")->get(0,"num_projects")>0 ;?>
            <button type="button" class="btn btn-success"><i class="fa fa-star"></i>  1. Company</button>
    <php else: ?>
            <button type="button" class="btn btn-danger"> <i class="fa fa-star"> 1. Company</button>
    <php endif ?>

The actual code is
<div class="small-section">
<php if ($this->dataStore("cost_center")->get(0,"num_projects")>0) ;?>
<button type="button" class="btn btn-success"><i class="fa fa-star"></i> 1. Company</button>
<php else: ?>
<button type="button" class="btn btn-danger"> <i class="fa fa-star"></i> 1. Company</button>
<php endif; ?>
<button type="button" class="btn btn-danger"><i class="fa fa-lightbulb-o"></i> 2. Activities</button>
<button type="button" class="btn btn-success"><i class="fa fa-magic"></i> 3. Business Components</button>
<button type="button" class="btn btn-warning"><i class="fa fa-map-marker"></i> 4. Cost Centers</button>
<button type="button" class="btn btn-danger"><i class="fa fa-rss"></i> 5. Titles</button>
<button type="button" class="btn btn-link"><i class="fa fa-link"></i> 6. Projects</button>
</div>

This is displaying the 1. Compnay button twice, once in green and once in red.


RE: how to mix html and php? - InsiteFX - 02-01-2022

I fixed the code above it was missing some of the question marks try it now.


RE: how to mix html and php? - richb201 - 02-03-2022

How about this one? Why is this not working?
<?php
$szValNow="5";
?>
<div class="small-section">
<div class="progress mb-3">
<div class="progress-bar progress-bar-striped" role="progressbar" style="width: 10%" aria-valuenow=<php $szValNow ?> aria-valuemin="0" aria-valuemax="100"></div>
</div>
</div>


RE: how to mix html and php? - Acuru - 02-03-2022

You need to:

Code:
<?php echo $szValNow?>
or


Code:
<?=$szValNow?>


I am suggesting using second one in views, I feel like it makes code more readable and clean. It works greatly for me.


Slso i would change from:


Code:
$szValNow="5";
(...)
aria-valuenow=<php $szValNow ?>

to:


Code:
$szValNow=5;
(...)
aria-valuenow="<php $szValNow ?>"

Better to keep variables as int than string if possible, in that case it can not make a diffrence, but it is good practice, and with it you can have mathematical operations on variable when it would be needed.


RE: how to mix html and php? - richb201 - 02-03-2022

I modified it to this but it still doesn't work.
<?php
$szValNow=80;
?>
<div class="small-section">
<div class="progress mb-3">
<div class="progress-bar progress-bar-striped" role="progressbar" style="width: 10%" aria-valuenow=<php echo $szValNow ?> aria-valuemin="0" aria-valuemax="100"></div>
</div>
</div>


RE: how to mix html and php? - Acuru - 02-03-2022

Cause you didn't payed enough attention to what i wroted Wink It should look


Code:
<?php
$szValNow=80;
?>
<div class="small-section">
<div class="progress mb-3">
<div class="progress-bar progress-bar-striped" role="progressbar" style="width: 10%" aria-valuenow="<?php echo $szValNow ?>" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</div>


PS pls use forum code markdown it is way more rediable


RE: how to mix html and php? - John_Betong - 02-03-2022

@richb201 ,

https://www.php.net/manual/en/language.types.string.php

I started using PHP HereDoc strings and it greatly simplifies mixing PHP and HTML. PHP also has NowDoc strings but I very seldom use that syntax.

Plain variables are expanded within the HereDoc syntax but arrays and expressions must be enclosed in curly braces without spaces. For example
Code:
//========================
// usage:
// fred($string_array_object);
//========================
function debug($val)
{
$val= print_r($val,true);
$sty = ‘width:88%; margin:2rem auto: background-color: snow; color:#000;’:

// NOTHING FOLLOWS ____BLOCK
echo $tmp = <<< ____BLOCK
  <div style=‘$sty’>
$val
</div>
____BLOCK;
// NOTHING AFTER ;
}// endfunc



RE: how to mix html and php? - InsiteFX - 02-04-2022

Because your missing the ? mark in the <php should be <?php