0

Does anyone know how I could limit the number of profile images that are fetched from MySql array to say 6 profile images?

Thanks

Code:

   $newest_set = get_newest_profile() ;

    while ($newest = mysql_fetch_array($newest_set )){
        echo" 
        <div class=\"mod_newest_image\">
        <a href=\"profile.php?id={$newest['id']}\"><img width=95px src=\"data/photos/{$newest['id']}/_default.jpg\" /></a>
        <div>
        <strong> {$newest['display_name']}</strong>
        <br />
        </div>
        </div>";


    }
?>

3 Answers 3

2

Add LIMIT clause to your sql

ORDER BY `date_column` DESC LIMIT 6
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, and this sounds really daft but where abouts in the code does it need to go?
0

You could use an limit to your query

 SELECT * FROM `your_table` LIMIT 0, 6 // takes the rows from the results 0 - 6

Or you can use an counter

$fetch_counter = 0;
while ($newest = mysql_fetch_array($newest_set ) && $fetch_counter < 6){
        echo" 
        <div class=\"mod_newest_image\">
        <a href=\"profile.php?id={$newest['id']}\"><img width=95px src=\"data/photos/{$newest['id']}/_default.jpg\" /></a>
        <div>
        <strong> {$newest['display_name']}</strong>
        <br />
        </div>
        </div>";
        $fetch_counter++;
    }

Note that fetch_counter starts at 0, so if it is 6, the fetch is runned 7 times already.

3 Comments

TWCrap thank you for that it seems to be working however the images are not displaying now? any ideas?
@MarkTanner, what is not working? does it runs the while loop? or does it says that it can't load the image. What you can do is an simple echo at the begining of the while loop (in the while loop) and add this line there: echo "Where running this now for the " . ($fetch_counter + 1) . " time. <br />";
Or are you using the limit solution?
0

Set a limit on your sql query. Like,

$sql=mysql_query("select fields from tablename where conditions limit 0,6");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.