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
https://forums.phpfreaks.com/topic/72998-while-help/
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
https://forums.phpfreaks.com/topic/72998-while-help/#findComment-368108
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
https://forums.phpfreaks.com/topic/72998-while-help/#findComment-368111
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
https://forums.phpfreaks.com/topic/72998-while-help/#findComment-368124
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.