$class="special";
foreach($foo as $bar) {
echo("<p class='$class'>$bar</p>);
$class="";
}
That is a very interesting short circuit. Thanks for that one ear1grey.
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???
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.
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?
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" }
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…
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
.
Can you give me a code example, cause i keep getting the error
<?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>';
}
?>
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
Well this fixes the error but the class isn’t filled in:
Forums
hmmm going to try something else, thx anyway
Got it fixed, this topic can be deleted
Huh? Thou speaketh in jest surely!