showing mysql results differently?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

showing mysql results differently?

Post 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.
Last edited by Benjamin on Mon May 04, 2009 8:53 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: showing mysql results differently?

Post 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
Last edited by Benjamin on Mon May 04, 2009 9:56 am, edited 1 time in total.
Reason: Changed code type from text to php.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: showing mysql results differently?

Post by tomsace »

Thanks alot, got it working perfectly!
Post Reply