Selective SELECT?

I want to select some records from a table and display them(let’s illustrate with this example) -:

$sql=mysql_query("select headline,body from theTable where id='$id' order by id DESC"); 
while($row=mysql_fetch_array($sql))
{
echo "<strong>".$row['headline']."</strong><p/>";
echo $row['body']."<p/>";
}

The code above makes ALL the headlines to be bold,but what i want is to make ONLY the first displayed headline to be bold - not all the results.The question is - is there a way to selectively format a particular row from a result set, how do i twist the code above to allow for this?

What seems to be a solution, is to use 2 SELECT queries - one for the row of interest, and the other for the other rows, apart from being inefficient, there’s an obvious flaw with
this…[obvious]

<p/> is only valid in real XHTML and IE doesn’t support real XHTML at all so there isn’t a conflict between the two sonce they can’t be used together.

<p/> while valid is also semantically meaningless which means that it should never be used even though it is valid in real XHTML.

IE seems to have a lot of trouble with some self-closing tags. Not sure if this applies to P tags, but I’ve had <div/> tags causing the browser to crash big time (which was pretty awesome, of course :slight_smile: )

an empty paragraph? you shouldn’t really use those. enclose your text in paragraphs and then use CSS to adjust the margins between them.

here’s how I’d do it:

$highlighted = array(1);

while($row=mysql_fetch_array($sql))
{
	$i++;
    if(in_array($i,$highlighted)) {
	  echo "<p><strong>", htmlspecialchars($row['headline']), "</strong></p>";
    }
    else echo "<p>", htmlspecialchars($row['headline']), "</p>";
    
    echo "<p>", htmlspecialchars($row['body']), "</p>";
} 

now you can replace $highlighted = array(1); with $highlighted = array(3,7,9); to emphasise those ones. With a slight variation you could also turn this into highlighting every 5th/10th/etc. headline - I don’t have the time to write code for this though, let me know if you need this one and I’ll have a look later.

then what about my other suggestion? cranial-bore’s solution is good if you only want to catch the first headline, my suggestion of using a dummy variable and increase it through the loop would enable you to address any row you want.


$first = true;
while($row=mysql_fetch_array($sql))
{
	//First record
	if($first) {
		echo "<p><strong>", htmlspecialchars($row['headline']), "</strong></p>";
		$first = false;
	}
	else echo "<p>", htmlspecialchars($row['headline']), "</p>";
	
	echo "<p>", htmlspecialchars($row['body']), "</p>";
}

You need to open paragraphs, not just close them. And you close a paragraph with </p> not <p/>

I don’t know the value of $id, it changes…what i meant to write was:

$sql=mysql_query(“select headline,body from theTable order by id DESC”);

why not use an if statement in there? in simple terms an if id = xx then echo strong end if, … and another if for /strong, or a combined if/else. Or create a dummy variable and count it up in the while loop and use that in your if statement instead of the id.

Loved to hear opinions on this and alternative suggestions.

replicaBAGA, please go jump off a tall bridge.
I’m not buying your crap from you!

Yeah, IE tends to show weird ‘Navigation cancelled’ error dialogs when it encounters self-closed tags that it isn’t expecting. In all fairness, as you said, it’s not really IE’s fault. It just sucks that IE doesn’t do application/xml+xhtml, pretty much causing the failure of proper XHTML and imploding the space-time continuum.

Damn IE!

Thanks for all the suggestions, i will adapt them to the project im working on. The “<p/>” is supposed to be a standalone paragraph tag.

@c2uk, Your other suggestion is great - it is more general, will appreciate if you can help with the actual code.