Info
- 16 posts
- 3 voices
- Started 5 years ago by Null
- Latest reply from ear1grey
- This topic is not a support question
for each question
-
- Posted 5 years ago #
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
EtcBut the first name a want to have bold so it looks like:
Jack
John
Adrian
EtcBest 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 5 years ago #
$class="special";
foreach($foo as $bar) {
echo("<p class='$class'>$bar</p>);
$class="";
} -
- Posted 5 years ago #
That is a very interesting short circuit. Thanks for that one ear1grey.
-
- Posted 5 years ago #
Euuhh well still can't get it to work...
I have the function:
function get_bb_menu() {$bbdb->menu
global $bbdb;
return $bbdb->get_results("SELECT * FROMWHEREset= 'active' ORDER BYorderASC");
}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() - 2Now 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 5 years ago #
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 5 years ago #
I did that but you can't use php in an echo:
<?php$bbdb->menu
global $bbdb;
$r = mysql_query("SELECT * FROMWHEREset= 'active' ORDER BYorderASC");
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 5 years ago #
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
classrather thanidfor 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 5 years ago #
Hmm got this now:
<?php$bbdb->menu
global $bbdb;
$r = mysql_query("SELECT * FROMWHEREset= 'active' ORDER BYorderASC");
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 71Think 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 5 years ago #
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 theecho. -
- Posted 5 years ago #
Can you give me a code example, cause i keep getting the error :(
-
- Posted 5 years ago #
<?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 5 years ago #
Still no luck :(
I have this:
<?php$bbdb->menu
global $bbdb;
$r = mysql_query("SELECT * FROMWHEREset= 'active' ORDER BYorderASC");
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 84And line 84 contains the if:
if ($rw['is']) { $class .=" current" }I am totally lost here :S
-
- Posted 5 years ago #
...current"; }
i.e.
...current"SEMICOLON }?
-
- Posted 5 years ago #
Got it fixed, this topic can be deleted :D
-
- Posted 5 years ago #
Huh? Thou speaketh in jest surely!
-
You must log in to post.