Jump to content

'while' help.


LemonInflux

Recommended Posts

I can't think of a decent analogy, so I'm just going to say my problem.

 

Say I have a code, like:

 

<?php
$sql = 'SELECT * FROM `table` where some_column = "some value"';
$sqlresult = mysql_query($sql);
while ($row = mysql_fetch_assoc($sqlresult)) {
echo $row['value'] .'<br>';
}
?>

 

Now, here's my question:

 

If I wanted the first value that came up to echo like so:

 

echo '<b>'. $row['value'] .'</b><br />';

 

the last value to echo like so:

 

echo '<u>'. $row['value'] .'</u><br />';

 

and all the others like so:

 

echo '<i>'. $row['value'] .'</i><br />';

 

How is this done?

Link to comment
Share on other sites

So you want the first line to be bold, the last line to be underlined and the rest to be in italics?

 

Here's how I would script this:

<?php
$sql = 'SELECT * FROM `table` where some_column = "some value"';
$sqlresult = mysql_query($sql);
$tmp = array();
while ($row = mysql_fetch_assoc($sqlresult)) {
    $tmp[] = $row['value'];
}
for ($i=0;$i<count($tmp);$i++) 
    switch ($i) {
        case 0:
             echo '<span style="font-weight:bold">' . $tmp[$i] . '</span><br>';
             break;
        case count($tmp)-1:
             echo '<span style="font-style: italic">' . $tmp[$i] . '</span><br>';
             break;
        default:
             echo '<span style="text-decoration: underline">' . $tmp[$i] . '</span><br>';
     }
?>

 

Note: not tested.

 

Ken

 

?>

 

Link to comment
Share on other sites

I'd do it this way:

// setup the counter
$i = 0;
$max_i = mysql_num_rows($sqlresult);

while ($row = mysql_fetch_assoc($sqlresult))
{
    // first
    if($i ==  0)
    {
        echo '<b>' . $row['value'] . "</b><br>\n";
    }
    // not last or the first
    elseif($i < $max)
    {
        echo '<i>' . $row['value'] . "</i><br>\n";
    }
    // last
    else
    {
        echo '<u>' . $row['value'] . "</u><br>\n";
    }

    // increment counter
    $i++;
}

 

EDIT: Nevermind beaten to it. :(

Link to comment
Share on other sites

1 final question; is there a way of setting it so that this only works if there is more than one result? Ok, badly worded, I mean like this:

 

if there is 1 result, just list like so:

 

$var;

 

if there are 2:

 

$var .

', '. $var2

 

if there are 3:

 

$var .

', '. $var2 .

', '. $var3

 

If that makes sense. If needed, I'll give a more useful description.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.