Page 1 of 1

showing mysql results differently?

Posted: Mon May 04, 2009 7:55 am
by tomsace
Hi,
I would like to show the first 2 of my returned mysql results as pictures, then the last 4 results as text.
Is this possible?

Code: Select all

<?php
// Performing SQL query
$query = 'SELECT * FROM games WHERE category = "retro" ORDER BY rand() DESC LIMIT 6';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
 
// Printing results in HTML
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
 
        echo "<img src="images/$line['title'].jpg>";
 
}
?>
This is what I have so far but it just shows 6 images.

Re: showing mysql results differently?

Posted: Mon May 04, 2009 9:45 am
by jazz090
use mysql_result() instead of mysql_fetch, and run a FOR loop, that way you have control over the current result i.e. if $i > 0 && $i <3 (images) and $i >=3 && $i <=6 (text):

Code: Select all

<?php
for ($i=0; $i<mysql_num_rows($result); $i++){
    if ($i < 3){
        echo '<img src="images/'.mysql_result($result, $i).'" >';
    }
    else { ## if you change the limit make sure you add ELSE IF in here ($i >=3 && $i <=6) ##
        //output text
    }
}
?>
more here: http://uk2.php.net/function.mysql-result

Re: showing mysql results differently?

Posted: Mon May 04, 2009 9:58 am
by tomsace
Thanks alot, got it working perfectly!