LemonInflux Posted October 12, 2007 Share Posted October 12, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/72998-while-help/ Share on other sites More sharing options...
kenrbnsn Posted October 12, 2007 Share Posted October 12, 2007 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/72998-while-help/#findComment-368108 Share on other sites More sharing options...
LemonInflux Posted October 12, 2007 Author Share Posted October 12, 2007 The bold, italic and underline was an example, but I could probably sort something out with that code. Will test it, then get back to you, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/72998-while-help/#findComment-368109 Share on other sites More sharing options...
wildteen88 Posted October 12, 2007 Share Posted October 12, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72998-while-help/#findComment-368111 Share on other sites More sharing options...
LemonInflux Posted October 12, 2007 Author Share Posted October 12, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72998-while-help/#findComment-368124 Share on other sites More sharing options...
kenrbnsn Posted October 12, 2007 Share Posted October 12, 2007 For that use a temporary array and the implode() function: <?php $tmp = array(); while ($rw = mysql_fetch_assoc($rw)) $tmp = $rw['var']; echo impode('<br>,',$tmp); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/72998-while-help/#findComment-368192 Share on other sites More sharing options...
MmmVomit Posted October 12, 2007 Share Posted October 12, 2007 I think you meant this. <?php $tmp = array(); while ($rw = mysql_fetch_assoc($rw)) $tmp[] = $rw['var']; echo impode('<br>,',$tmp); ?> Quote Link to comment https://forums.phpfreaks.com/topic/72998-while-help/#findComment-368228 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.