Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/227460-limiting-string-print/
Share on other sites

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.

 

 

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.

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>&#163; $price</center></div>  <div class=\"productbuybutton\"><center><a href=\"$link\"  target=\"_blank\" ><img src=/images/buybutton.png /></a></center></div></div></div>" ;


}
}



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?

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?

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>&#163; $price</center></div>  <div class=\"productbuybutton\"><center><a href=\"$link\"  target=\"_blank\" ><img src=/images/buybutton.png /></a></center></div></div></div>" ;


}
}

$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

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.

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>&#163; $price</center></div>  <div class=\"productbuybutton\"><center><a href=\"$link\"  target=\"_blank\" ><img src=/images/buybutton.png /></a></center></div></div></div>";


}
}

 

 

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>&#163; $price</center></div>  <div class=\"productbuybutton\"><center><a href=\"$link\"  target=\"_blank\" ><img src=/images/buybutton.png /></a></center></div></div></div>";


}
}

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>&#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";
}

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>&#38;#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";
}
?>

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>&#38;#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";
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.