Welcome Guest, Not a member yet? Register   Sign In
how to mix html and php?
#1

<
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>
proof that an old dog can learn new tricks
Reply
#2

(This post was last modified: 02-01-2022, 02:03 AM by InsiteFX. Edit Reason: Missing > marks stupid keyboard. )

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 ?>
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 01-31-2022, 06:49 AM by richb201.)

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.
proof that an old dog can learn new tricks
Reply
#4

I fixed the code above it was missing some of the question marks try it now.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

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>
proof that an old dog can learn new tricks
Reply
#6

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.
Thread about my project that is using CodeIgniter:
Reply
#7

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>
proof that an old dog can learn new tricks
Reply
#8

(This post was last modified: 02-03-2022, 12:50 PM by Acuru.)

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
Thread about my project that is using CodeIgniter:
Reply
#9

(This post was last modified: 02-03-2022, 04:10 PM by John_Betong.)

@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
Reply
#10

Because your missing the ? mark in the <php should be <?php
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB