Bold a Select Result From an Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bjwillis
Forum Newbie
Posts: 1
Joined: Mon Jun 03, 2002 4:15 pm

Bold a Select Result From an Array

Post by bjwillis »

I have an array of info, which is generated into html through a while loop. I want to bold a certain element if it corresponding variable is passed through the page....any help?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Here is a quick and simple solution, I hope it's what you wanted:

Code: Select all

<?php

/* Assuming the variable from somewhere else is called $variable */

// Transpose the keys and values
$arrayK = array_flip($array);

// Well, we make sure the variable passed exists in the array
if ( isset($arrayK&#1111;$variable]) )
&#123;
	// If so, we make it bold...
	$array&#1111;$arrayK&#1111;$variable]] = '<b>'.$array&#1111;$arrayK&#1111;$variable]].'</b>';
&#125;

unset($arrayK);

// And viola! print the array out! =)
foreach ( $array as $key => $value )
&#123;
	echo "$value<br />";
&#125;

?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Not sure this is exactly what you are looking for but you could have an if loop within the while loop:

Code: Select all

if ($var == 'this') &#123;
    echo '<b>'.$var.'</b>';
&#125; else &#123;
    echo $var;
&#125;
Mac
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Just to point out the fundamental difference between twigletmac's code and mine. twigletmac would check on each loop of the array, whereas mine wouldn't.

That is assuming I understood all correctly.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Well it was just a simple solution to a question without much info.
'spose it would depend on the size of the array whether it would make much difference.

Mac
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Very true.

I just need to keep my post count higher than everyone elses. :D
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I onced supposed a word-chain. It's amazing how it raises one's post-counter ;)
(ok,ok this was spam)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I think you're still fending off the competition fairly well :D. (Although volka is trying really hard).

Now I guess we just have to wait for bjwillis to get back to see if we got anywhere close to answering his question :roll: .

Mac
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

*hrm....should I manually adjust volka's count in the DB? Muhahahah! The power of DB access *

++$post_count;
Post Reply