0

I am trying to apply if else condition in bootstrap class with php variable but its not working. My variable is getting result. Below is my tbody where i am applying php code:

<div class="<?php '#count_rows#' <= '2')
        { 
            echo 'middle_menu';
        } 
    else 
        { 
            echo 'middle_menu1';
        } ?>">
        <table class="table_4">
            <thead>
                <tr>
                    <th class="th_11">Quantity</th>
                </tr>
            </thead>
            <tbody>
                #TABLE_BODY#
            </tbody>
        </table>
</div>

Below is my 2 css classes:

.middle_menu
    {
        margin-top: 40px;       
        padding-bottom: 200px;
    }
.middle_menu1
    {
        margin-top: 40px;
    }

I am fetching my variable from another page where i set this type of opening and closing variable with #. Below is my code for your reference but i dont think that is an issue for me because i check this #count_rows# variable with print_r in my current page and it showing me correct result.

foreach ($form_field as $key => $value){
    $htmlContent = str_replace('#'.$key.'#', $value, $htmlContent);
}
5
  • 3
    Maybe there's some sort of shorthand I've never used, but I don't see an if there at all, nor an opening parentheses Commented May 10, 2018 at 15:17
  • that is an typo mistake i added if as you can see else condition Commented May 10, 2018 at 15:19
  • 1
    Then edit your question and fix it Commented May 10, 2018 at 15:19
  • 1
    Ehm, missing if ( aside, are you comparing the strings '#count_rows#' and '2'? Neither of those are variables. Commented May 10, 2018 at 15:20
  • 1
    Your code doesn't make any sense, and is invalid php. First off you are missing an 'if'. Next you are missing an opening paren. Then you are comparing a string constant '#count_rows#' with another string constant '2'. That is always going to evaluate to the same thing. Last and certainly not least, you can't evaluate a javascript variable that is changed or runs in the browser in a serverside php script. Commented May 10, 2018 at 15:20

2 Answers 2

2
<?php $className = $count_rows <= 2 ? 'middle_menu' : 'middle_menu1'; ?>
<div class="<?php echo $className; ?>" />

You can create a test.php then run php test.php and you should see the result

<?php

// $count_rows = 1;
$count_rows = 3;
$className = $count_rows <= 2 ? 'middle_menu' : 'middle_menu1';
echo "<div class=\"$className\"/>\n";
Sign up to request clarification or add additional context in comments.

Comments

0
 <div class="<?php '#count_rows#' <= '2')

You are missing a keyword here and also some brackets (which should cause a fatal syntax error), Also you are comparing two different literal strings - surely one of the terms should be a PHP variable?

You can't read back stuff you've previously written to the output stream - you are confusing what is happening in HTML, CSS and PHP.

I think you mean...

 <div class="<?php if ($form_fields['count_rows'] <= '2')

Comparing a number by casting it to a string is rather dangerous.

Personally I would do this:

 <div class="<?php echo (2 <= $form_fields['count_rows'])
        ? 'middle_menu' : 'middle_menu1'; ?>">

Since you didn't mention that the script blew up in your face, I suspect there may be a lot of other issues.

 foreach ($form_field as $key => $value){
   $htmlContent = str_replace('#'.$key.'#', $value, $htmlContent);
 }

This is very innefficient. Consider:

 $find=array_keys($form_field);
 foreach ($find as $index=>$term) {
     $find[$index]='#' . $term . '#';
 }
 $htmlContent = str_replace($find, $form_feld, $htmlContent);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.