leeh14 Posted May 13, 2010 Share Posted May 13, 2010 Hi, Right I have a 'while loop' getting data from the mysql database, but what I want to do is once the loop has retrieved 4 data sets function stops running. How is this done? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 13, 2010 Share Posted May 13, 2010 Please post your current code. Ken Quote Link to comment Share on other sites More sharing options...
leeh14 Posted May 13, 2010 Author Share Posted May 13, 2010 So yeah i want the while to run 4 times then stop? $query = "select * from videos"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ $tit_1 = $row["title"]; $des_1 = $row["des"]; $len_1 = $row["length"]; $url_1 = $row["URL"]; $img_1 = $row["image"]; $sli_1 = $row["slider"]; $del = $row["ID"]; echo "<li><a href='$page?\"?goto=true&id=".$del."'><img src='Tutorials/slider_thumbnails/$sli_1' width = '960px' height='400px' alt='CssTemplate Preview' /></a></li>"; } ?> Quote Link to comment Share on other sites More sharing options...
leeh14 Posted May 13, 2010 Author Share Posted May 13, 2010 Is there an answer out there?? Quote Link to comment Share on other sites More sharing options...
Joshua4550 Posted May 13, 2010 Share Posted May 13, 2010 Like so? $query = "select * from videos LIMIT 4"; Quote Link to comment Share on other sites More sharing options...
leeh14 Posted May 13, 2010 Author Share Posted May 13, 2010 Thank you kindly! Works a charm! Quote Link to comment Share on other sites More sharing options...
Joshua4550 Posted May 13, 2010 Share Posted May 13, 2010 No problemo. Please mark the topic as solved. Quote Link to comment Share on other sites More sharing options...
leeh14 Posted May 13, 2010 Author Share Posted May 13, 2010 One more thing however, if i wanted to get the last 4 data sets from the data (e.g the ones with the highest ID number) how would i do that? Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 13, 2010 Share Posted May 13, 2010 A field for date added would be better than using the ID, but with what you have: //First four $query = "SELECT * FROM videos ORDER BY ID ASC LIMIT 4"; //Last four $query = "SELECT * FROM videos ORDER BY ID DESC LIMIT 4"; Of course the last four will be in reverse order. If you need them in ascending order I think this will do it //Last four in ascending order $query = "SELECT * FROM (SELECT * FROM videos ORDER BY ID DESC LIMIT 4) ORDER BY ID ASC"; Quote Link to comment Share on other sites More sharing options...
Joshua4550 Posted May 13, 2010 Share Posted May 13, 2010 What he said. But also like he said, you may want to make it a date - best in milliseconds, so when you insert into mySQL: microtime() Quote Link to comment Share on other sites More sharing options...
leeh14 Posted May 13, 2010 Author Share Posted May 13, 2010 Another one solved this forum is amazing !! Quote Link to comment 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.