bbPress

Simple, Fast, Elegant

bbPress support forums » Troubleshooting

for each question

(16 posts)
  • Started 1 year ago by Null
  • Latest reply from ear1grey
  • This topic is not a support question
  1. Ok, I have searched the net for hours, but I couldn't get an answer for my question.

    If we use the for "each function" in php we can get a list of items like:
    Jack
    John
    Adrian
    Etc

    But the first name a want to have bold so it looks like:
    Jack
    John
    Adrian
    Etc

    Best way for me to do this, is to ad a class to the first name in the php code. But how do I do this? Cause the "for each" generates a list that is the same for eacht row (php/html code wise). So I only want the first name to be bold using a class (from a css) but all others not effected.

    Thx

    Posted 1 year ago #
  2. $class="special";
    foreach($foo as $bar) {
    echo("<p class='$class'>$bar</p>);
    $class="";
    }

    Posted 1 year ago #
  3. That is a very interesting short circuit. Thanks for that one ear1grey.

    Posted 1 year ago #
  4. Euuhh well still can't get it to work...

    I have the function:
    function get_bb_menu() {
    global $bbdb;
    return $bbdb->get_results("SELECT * FROM
    $bbdb->menu WHERE set = 'active' ORDER BY order ASC");
    }

    This will give (tabel sperated with - ):
    set - item - page - is - order
    active - Forums - index.php - is_front() - 0
    active - Statistic - statistics.php - is_bb_stats() - 1
    active - Search - search.php - is_bb_search() - 2

    Now I want this in a for each so it generates (note this is A PART what it should generate):

    <li><a <?php if (is_bb_search())
    {
    echo "id="current"";
    }?> href="<?php option('uri'); ?>search.php">Search</a></li>
    <li><a <?php if (is_bb_stats())
    {
    echo "id="current"";
    }?> href="<?php option('uri'); ?>statistics.php">Statistics</a></li>

    But how to do this???

    Posted 1 year ago #
  5. That's very difficult to decipher, so for maintainability may I suggest you break out the code so you create variables first then use them when creating the echo statement.

    It looks like you need to use call_user_func(tabel["is"]) to run the function names that you're retrieving from the db.

    Posted 1 year ago #
  6. I did that but you can't use php in an echo:

    <?php
    global $bbdb;
    $r = mysql_query("SELECT * FROM
    $bbdb->menu WHERE set = 'active' ORDER BY order ASC");
    while($rw = mysql_fetch_array($r))
    {
    echo '<li><a href="'.$rw['page'].'">'.$rw['item'].'</a></li>';
    }
    ?>

    This works perfectly, but now I want to add some php code into <a!here the php code!>...</a> so it will look like this (i only show the echo now):

    {
    echo<li><a <?php if ('.$rw['is'].')
    {
    echo "id="current"";
    }?> href="<?php option('uri'); ?>'.$rw['page'].'">'.$rw['item'].'</a></li>
    ;
    }`

    But this aint working cause you cant do php in an echo. Any suggestions?

    Posted 1 year ago #
  7. Just compute the computable stuff beforehand and it becomes a lot more maintainable/readable.

    // setup the basic data
    $uri=option("uri").$rw["page"];
    $class="";

    // compute the computable data
    if ($rw['is']) { $class="current" }

    // write the line
    echo "<li><a class='$current' href='$uri'>".$rw['item']."</a>"

    Note that I've used class rather than id for semantic purposes - the ID of an element shouldn't change, but it's class can change and can be multiple, so you could even do:

    $class="item";
    if ($rw['is']) { $class .=" current" }

    Posted 1 year ago #
  8. Hmm got this now:
    <?php
    global $bbdb;
    $r = mysql_query("SELECT * FROM
    $bbdb->menu WHERE set = 'active' ORDER BY order ASC");
    while($rw = mysql_fetch_array($r))
    $class="";
    if ($rw['is']) { $class .=" current" }
    {
    echo '<li><a class="$current" href="'.$rw['page'].'">'.$rw['item'].'</a></li>';
    }
    ?>

    But this gives me an error:
    Parse error: parse error, unexpected '}' on line 71

    Think I have misplaced something here?

    Greetz

    ps I didn't include $uri=option("uri").$rw["page"]; at all cause I wanted to test this first. So I keep it out for now...

    Posted 1 year ago #
  9. the while looks like it needs curly brackets adding to it or you're just going to repeatedly set $class="" then remove the curly bracket above the echo.

    Posted 1 year ago #
  10. Can you give me a code example, cause i keep getting the error :(

    Posted 1 year ago #
  11. <?php
    global $bbdb;
    $r = mysql_query("SELECT * FROM $bbdb->menu WHERE set = 'active' ORDER BY order ASC");
    while($rw = mysql_fetch_array($r)) <strong>{</strong>
    $class="";
    if ($rw['is']) { $class .=" current" }
    echo '<li><a class="$current" href="'.$rw['page'].'">'.$rw['item'].'</a></li>';
    }
    ?>

    Posted 1 year ago #
  12. Still no luck :(
    I have this:
    <?php
    global $bbdb;
    $r = mysql_query("SELECT * FROM
    $bbdb->menu WHERE set = 'active' ORDER BY order ASC");
    while($rw = mysql_fetch_array($r))
    {
    $class="";
    if ($rw['is']) { $class .=" current" }
    echo '<li><a class="$current" href="'.$rw['page'].'">'.$rw['item'].'</a></li>';
    }
    ?>

    Which is the same code as yours, but I still get the error:
    Parse error: parse error, unexpected '}' on header.php on line 84

    And line 84 contains the if:
    if ($rw['is']) { $class .=" current" }

    I am totally lost here :S

    Posted 1 year ago #
  13. ...current"; }
    i.e.
    ...current"SEMICOLON }

    ?

    Posted 1 year ago #
  14. Well this fixes the error but the class isn't filled in:

  15. Forums
  16. hmmm going to try something else, thx anyway

Posted 1 year ago #
  • Null
    Member

    Got it fixed, this topic can be deleted :D

    Posted 1 year ago #
  • ear1grey
    Member

    Huh? Thou speaketh in jest surely!

    Posted 1 year ago #

  • RSS feed for this topic

    Reply

    You must log in to post.

    Code is Poetry.