Page 1 of 1

Bold a Select Result From an Array

Posted: Mon Jun 03, 2002 4:15 pm
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?

Posted: Mon Jun 03, 2002 4:22 pm
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;

?>

Posted: Mon Jun 03, 2002 4:22 pm
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

Posted: Mon Jun 03, 2002 4:23 pm
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.

Posted: Mon Jun 03, 2002 4:27 pm
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

Posted: Mon Jun 03, 2002 4:31 pm
by jason
Very true.

I just need to keep my post count higher than everyone elses. :D

Posted: Mon Jun 03, 2002 4:35 pm
by volka
I onced supposed a word-chain. It's amazing how it raises one's post-counter ;)
(ok,ok this was spam)

Posted: Mon Jun 03, 2002 4:37 pm
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

Posted: Mon Jun 03, 2002 4:40 pm
by jason
*hrm....should I manually adjust volka's count in the DB? Muhahahah! The power of DB access *

++$post_count;