Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm trying to develope an application with codeigniter and I'm trying to find a mistake here which I don't find. The code is the next one:

<?php foreach($projects as $key => $project){ ?>
        <li <?php if ($current_project == $project['id']): ?>class="active"<? endif; ?>><a href="<?php echo $base_url . config_item('language_abbr') ?>/<?= $project['id'] ?>/admin/galerias"><?php echo $project['nombre']?></a></li>
<?php } ?>

Before of this I had:

<?php foreach($projects as $key => $project): ?>
        <li <?php if ($current_project == $project['id']): ?>class="active"<? endif; ?>><a href="<?php echo $base_url . config_item('language_abbr') ?>/<?= $project['id'] ?>/admin/galerias"><?php echo $project['nombre']?></a></li>
<?php endforeach; ?>

And instead of moaning about the }, it moans about the endforeach; , so what happen?.

And this is the code of the zone where is located the mistake because when I remove this part of code, it works:

<?php if (!empty($user_id) && $isAdmin): ?>
    <nav>
     <ul class="nav">
       <li 
          <?php if ($current_project == 0): ?>
            class="active"
            <? endif; ?>>
               <a href="<?php echo $base_url . config_item('language_abbr') ?>/0/admin/proyectos"><?= t('h_all'); ?></a>
        </li>

        <?php 
            foreach($projects as $key => $project){ ?>
                 <li 
                  <?php if ($current_project == $project['id']): ?>
                  class="active"<? endif; ?>>
                     <a href="<?php echo $base_url . config_item('language_abbr') ?>/<?= $project['id'] ?>/admin/galerias"><?php echo $project['nombre']?></a>
                 </li>
            <?php } ?>
      </ul>
    </nav>
  <?php endif; ?>

Thanks so much

share|improve this question
    
There is a lot of different php opening tags here - some are obviously to echo out content and do not require the php but there are some that are used in flow control (if/then/else) and these do not echo out content yet do not have the php either. I realise these may be valid but things like <? endif; ?>> ought to remain consistent with the other tag styles – RamRaider 2 days ago
up vote 1 down vote accepted

Perhaps not exactly the style of solution you were looking for but it is quite easy to read to find potential mistakes.

<?php
    foreach( $projects as $key => $project ){
        $class=( $current_project==$project['id'] ) ? " class='active'" : '';

        echo "
        <li{$class}>
            <a href='".$base_url . config_item('language_abbr')."/".$project['id']."/admin/galerias'>".$project['nombre']."</a>
        </li>";
    }
?>

The offending portion of code where you believe the error might lie in a similar style to my original answer - to my mind it is much easier to read and therefore much easier to spot where mistakes are - though it is a personal preference I guess and the style used in the original code seems to be more popular these days.

<?php
    if ( !empty( $user_id ) && $isAdmin ){

        $class=( $current_project == 0 ) ? " class='active'" : '';
        $lang=config_item('language_abbr');
        $linktext=t('h_all');

        echo "
        <nav>
            <ul class='nav'>
                <li{$class}><a href='{$base_url}{$lang}/0/admin/proyectos'>{$linktext}</a></li>";

        foreach( $projects as $key => $project ){
            $class=( $current_project == $project['id'] ) ? " class='active'" : '';
            echo "<li{$class}><a href='{$base_url}/{$lang}/{$project['id']}/admin/galerias'>{$project['nombre']}</a></li>";
        }

        echo "
            </ul>
        </nav>";
    }
?>
share|improve this answer
    
After change these lines by what you wrote I have no unexpected end of file. – Charly66 Jan 24 at 7:34
    
Do you mean that you now have a different error ? – RamRaider Jan 24 at 7:41
    
Yes, different error. So I don't know why – Charly66 Jan 24 at 7:55
    
make sure error reporting is on ( error_reporting( E_ALL ); ) to debug and check the logs for a particular line - the code above doesn't appear to have any errors ~ or maybe I'm just not awake enough yet?! – RamRaider Jan 24 at 7:57
    
I have add the whole file, because I don't understand why I have this mistake – Charly66 Jan 24 at 8:12
<?php foreach($projects as $key => $project){ ?>
 <li <?php if ($current_project == $project['id']): ?>class="active"<? endif; ?>>
<a href="<?php echo $base_url . config_item('language_abbr') ?>/<?= $project['id'] ?>/admin/galerias">
 <?php echo $project['nombre'];?></a></li> <?php } ?>

You were missing a semi colon after $project['nombre']

share|improve this answer

try :

<?php
foreach($projects as $key => $project){
echo "<li ".($current_project == $project['id'])?"class=active":""." ><a href='".$base_url . config_item('language_abbr').$project['id']."/admin/galerias' >$project[nombre]</a></li>";
}

?>
share|improve this answer
  <?php foreach($projects as $key => $project){ 
         $class = $current_project == $project['id']) ? 'active' : '';
   ?>
         <li class="<?=$class;?>"><a href="<?php echo $base_url . config_item('language_abbr') ?>/<?= $project['id'] ?>/admin/galerias"><?php echo $project['nombre']?></a></li>
    <?php } ?>
share|improve this answer
    
you have an unbalanced brace $current_project == $project['id']) – RamRaider Jan 24 at 8:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.