justlukeyou Posted February 12, 2011 Share Posted February 12, 2011 I have a database of 100 lines which have echoed onto the page so each line within the database is printed. I would like to limit it to ten. However, I have tried a whole range of different loops but none of them seek to work. Some of them print the database ten times so I have 1000 lines on the page. Can anyone advise the best method of limiting how many lines within a database are printed on the page? Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/ Share on other sites More sharing options...
harristweed Posted February 12, 2011 Share Posted February 12, 2011 use LIMIT in your query.For 10 add LIMIT 0, 10 as the last item in your sql query Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173225 Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 Add a LIMIT clause to the query string. SELECT `field` FROM `table` LIMIT 10 Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173226 Share on other sites More sharing options...
justlukeyou Posted February 12, 2011 Author Share Posted February 12, 2011 Thanks, I have tried both methods but cant get them to work. At the moment I just echoing all the information in the database, does that count as a query? If I try SELECT `field` FROM `table` LIMIT 10 do I need to replace field and table with my fields and table. I have tried that it wont work either. Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173239 Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 In what way did they not work? Yes, you can't echo anything from a database without first executing a query. The query string I posted was only an example of the syntax to use. Post the code you're having problems with. Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173241 Share on other sites More sharing options...
justlukeyou Posted February 12, 2011 Author Share Posted February 12, 2011 Hi, My code is below, Ive tried around 30 variations of the limit function, I have also tried it before and after the query. while($row = mysql_fetch_array($result)) { $id = $row['ID']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; SELECT '$link' FROM `productfeed` LIMIT 0, 2; ?> <?php { echo "<div class=\"productdisplayshell\"> <div class=\"productdisplayoutline\"> <div class=\"productborder\"><center> <a href=\"$link\" target=\"_blank\" ><img src=\"$image\" /></a> </center> </div></div> <div class=\"productdescriptionoutline\"><div class=\"productdescriptionbox\"> <a href=\"$link\" target=\"_blank\" >$description</a> </div><div class=\"productfulldescriptionbox\"> $fulldescription </div></div> <div class=\"productpriceoutline\"> <div class=\"productpricebox\"><center>£ $price</center></div> <div class=\"productbuybutton\"><center><a href=\"$link\" target=\"_blank\" ><img src=/images/buybutton.png /></a></center></div></div></div>" ; } } Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173243 Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 You're attempting to run a query within a loop, which is a bad idea to begin with due to it being resource intensive and inefficient. You didn't include the outer query string, and the inner query isn't actually being executed. Where are the records that you're trying to limit coming from, the outer or inner query? Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173248 Share on other sites More sharing options...
justlukeyou Posted February 12, 2011 Author Share Posted February 12, 2011 Hi, I'm now sure what you mean, at this stage I am just trying to print everything that is in the database and limit it to 100. I've tried lots of different methods of displaying it but this is the only one which prints anything. Should I remove the while option? Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173252 Share on other sites More sharing options...
jcbones Posted February 12, 2011 Share Posted February 12, 2011 Show us your query. Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173256 Share on other sites More sharing options...
justlukeyou Posted February 12, 2011 Author Share Posted February 12, 2011 Hi, I dont have a query. This is all the code I have: $result = mysql_query("SELECT * FROM productfeed"); while($row = mysql_fetch_array($result)) { $id = $row['ID']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; ?> <?php { echo "<div class=\"productdisplayshell\"> <div class=\"productdisplayoutline\"> <div class=\"productborder\"><center> <a href=\"$link\" target=\"_blank\" ><img src=\"$image\" /></a> </center> </div></div> <div class=\"productdescriptionoutline\"><div class=\"productdescriptionbox\"> <a href=\"$link\" target=\"_blank\" >$description</a> </div><div class=\"productfulldescriptionbox\"> $fulldescription </div></div> <div class=\"productpriceoutline\"> <div class=\"productpricebox\"><center>£ $price</center></div> <div class=\"productbuybutton\"><center><a href=\"$link\" target=\"_blank\" ><img src=/images/buybutton.png /></a></center></div></div></div>" ; } } Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173257 Share on other sites More sharing options...
denno020 Posted February 12, 2011 Share Posted February 12, 2011 $sqlCommand = "SELECT * FROM productfeed LIMIT 10;" $query = mysql_query($sqlCommand, $myConnection) or die(mysql_error()); while($row = mysql_fetch_array($query)){ $id = $row['ID']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; echo "<div class=\"productdisplayshell\"> <div class=\"productdisplayoutline\"> <div class=\"productborder\"><center> <a href=\"$link\" target=\"_blank\" ><img src=\"$image\" /></a> </center> <><> <div class=\"productdescriptionoutline\"><div class=\"productdescriptionbox\"> <a href=\"$link\" target=\"_blank\" >$description</a> <><div class=\"productfulldescriptionbox\"> $fulldescription <><> <div class=\"productpriceoutline\"> <div class=\"productpricebox\"><center>£ $price</center><> <div class=\"productbuybutton\"><center><a href=\"$link\" target=\"_blank\" ><img src=/images/buybutton.png /></a></center><><><>" ; } Try that Denno Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173260 Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 You do have a query; right here ---> $result = mysql_query("SELECT * FROM productfeed"); Make it like this ---> $result = mysql_query("SELECT * FROM productfeed LIMIT 10"); Then you'd also need to echo the values in the while loop. Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173261 Share on other sites More sharing options...
jcbones Posted February 12, 2011 Share Posted February 12, 2011 Wow Denno, your editor placed some font's in there. Replace the query: $result = mysql_query("SELECT * FROM productfeed"); With: $result = mysql_query("SELECT * FROM productfeed LIMIT 100"); //you wanted 100 rows right? Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173262 Share on other sites More sharing options...
denno020 Posted February 12, 2011 Share Posted February 12, 2011 Yeah I know, my editor has been acting weird. When I click on the php code button, it places them the wrong way round too! lol, It closes, then opens :S. Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173263 Share on other sites More sharing options...
justlukeyou Posted February 12, 2011 Author Share Posted February 12, 2011 Brilliant, thanks guys. I tried the following code and it limits it just two so I know it works: I am concerned now that from your comments that this is an inneficient way of echoing date from my database. Can you advise it is iniffient. I take it I am trying to do a loop instead of just publishing the data. $result = mysql_query("SELECT * FROM productfeed LIMIT 2"); while($row = mysql_fetch_array($result)) { $id = $row['ID']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; ?> <?php { echo "<div class=\"productdisplayshell\"> <div class=\"productdisplayoutline\"> <div class=\"productborder\"><center> <a href=\"$link\" target=\"_blank\" ><img src=\"$image\" /></a> </center> </div></div> <div class=\"productdescriptionoutline\"><div class=\"productdescriptionbox\"> <a href=\"$link\" target=\"_blank\" >$description</a> </div><div class=\"productfulldescriptionbox\"> $fulldescription </div></div> <div class=\"productpriceoutline\"> <div class=\"productpricebox\"><center>£ $price</center></div> <div class=\"productbuybutton\"><center><a href=\"$link\" target=\"_blank\" ><img src=/images/buybutton.png /></a></center></div></div></div>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173269 Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 That isn't all of the code, is it? Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173271 Share on other sites More sharing options...
justlukeyou Posted February 12, 2011 Author Share Posted February 12, 2011 Hi, this is: $result = mysql_query("SELECT * FROM productfeed LIMIT 2"); while($row = mysql_fetch_array($result)) { $id = $row['ID']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; ?> <?php { echo "<div class=\"productdisplayshell\"> <div class=\"productdisplayoutline\"> <div class=\"productborder\"><center> <a href=\"$link\" target=\"_blank\" ><img src=\"$image\" /></a> </center> </div></div> <div class=\"productdescriptionoutline\"><div class=\"productdescriptionbox\"> <a href=\"$link\" target=\"_blank\" >$description</a> </div><div class=\"productfulldescriptionbox\"> $fulldescription </div></div> <div class=\"productpriceoutline\"> <div class=\"productpricebox\"><center>£ $price</center></div> <div class=\"productbuybutton\"><center><a href=\"$link\" target=\"_blank\" ><img src=/images/buybutton.png /></a></center></div></div></div>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173272 Share on other sites More sharing options...
justlukeyou Posted February 12, 2011 Author Share Posted February 12, 2011 Hi, Is my code inefficient? Im a bit lost as to how I can make it more effective. If I am just printing everything in a database do I not need to use a loop? Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173340 Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 The code you posted earlier had another query inside the while() loop. If that was the case, it would be very inefficient. Currently, I'd make a few changes for readability, like removing the extra set of curly braces, and formatting and adding \n newlines in the html output, but that's about it. Assigning the values from the fetched array to individual variables is a minor inefficiency, but most people would probably consider that trivial since it helps with the readability of the following code. <?php $result = mysql_query("SELECT * FROM productfeed LIMIT 2"); while( $row = mysql_fetch_array($result) ) { $id = $row['ID']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; echo "<div class=\"productdisplayshell\">\n <div class=\"productdisplayoutline\">\n <div class=\"productborder\"> <center><a href=\"$link\" target=\"_blank\" ><img src=\"$image\" /></a></center>\n </div>\n </div>\n <div class=\"productdescriptionoutline\">\n <div class=\"productdescriptionbox\">\n <a href=\"$link\" target=\"_blank\" >$description</a>\n </div>\n <div class=\"productfulldescriptionbox\">$fulldescription\n </div>\n </div>\n <div class=\"productpriceoutline\">\n <div class=\"productpricebox\">\n <center>£ $price</center>\n </div>\n <div class=\"productbuybutton\">\n <center><a href=\"$link\" target=\"_blank\" ><img src=/images/buybutton.png /></a></center>\n </div>\n </div>\n </div>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173363 Share on other sites More sharing options...
justlukeyou Posted February 13, 2011 Author Share Posted February 13, 2011 Hi, I tried but it comes with an error straight away on the first "<" I put some curly tags in and now it comes up with an error for an unexpected end. With my previous code does it have an error with an error because of the curly tags? <?php ***DATABASE LOG IN SECTION*** $result = mysql_query("SELECT * FROM productfeed LIMIT 2"); { while( $row = mysql_fetch_array($result) ) { $id = $row['ID']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; ?> <?php echo "<div class=\"productdisplayshell\">\n <div class=\"productdisplayoutline\">\n <div class=\"productborder\"> <center><a href=\"$link\" target=\"_blank\" ><img src=\"$image\" /></a></center>\n </div>\n </div>\n <div class=\"productdescriptionoutline\">\n <div class=\"productdescriptionbox\">\n <a href=\"$link\" target=\"_blank\" >$description</a>\n </div>\n <div class=\"productfulldescriptionbox\">$fulldescription\n </div>\n </div>\n <div class=\"productpriceoutline\">\n <div class=\"productpricebox\">\n <center>&#163; $price</center>\n </div>\n <div class=\"productbuybutton\">\n <center><a href=\"$link\" target=\"_blank\" ><img src=/images/buybutton.png /></a></center>\n </div>\n </div>\n </div>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173614 Share on other sites More sharing options...
Pikachu2000 Posted February 13, 2011 Share Posted February 13, 2011 The code I posted had no parse errors. Why is there now an opening curly { brace between $result and while() ? Paste the actual error messages, and indicate which lines they refer to. Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173627 Share on other sites More sharing options...
justlukeyou Posted February 13, 2011 Author Share Posted February 13, 2011 Hi, I added those to get rid of the error it came up with. This is the error "Parse error: syntax error, unexpected $end in". This is all the code on my page which creates the error above: <?php ***PASSWORD LOGIN*** $result = mysql_query("SELECT * FROM productfeed LIMIT 0, 2"); while($row = mysql_fetch_array($result)) { $id = $row['id']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; ?> <?php $result = mysql_query("SELECT * FROM productfeed LIMIT 2"); while( $row = mysql_fetch_array($result) ) { $id = $row['ID']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; echo "<div class=\"productdisplayshell\">\n <div class=\"productdisplayoutline\">\n <div class=\"productborder\"> <center><a href=\"$link\" target=\"_blank\" ><img src=\"$image\" /></a></center>\n </div>\n </div>\n <div class=\"productdescriptionoutline\">\n <div class=\"productdescriptionbox\">\n <a href=\"$link\" target=\"_blank\" >$description</a>\n </div>\n <div class=\"productfulldescriptionbox\">$fulldescription\n </div>\n </div>\n <div class=\"productpriceoutline\">\n <div class=\"productpricebox\">\n <center>&#163; $price</center>\n </div>\n <div class=\"productbuybutton\">\n <center><a href=\"$link\" target=\"_blank\" ><img src=/images/buybutton.png /></a></center>\n </div>\n </div>\n </div>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173658 Share on other sites More sharing options...
Pikachu2000 Posted February 13, 2011 Share Posted February 13, 2011 Why have you duplicated the query and while() loop? Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173664 Share on other sites More sharing options...
justlukeyou Posted February 13, 2011 Author Share Posted February 13, 2011 Thats a good question. I shall take it out. Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173665 Share on other sites More sharing options...
justlukeyou Posted February 13, 2011 Author Share Posted February 13, 2011 Hi, It works a treat now, many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/227460-limiting-string-print/#findComment-1173668 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.