hello is there a way to create pages of loaded divs from sql?
Id like to limit it to 6 per page
I used:
$result = mysqli_query($conn,"SELECT * FROM products ORDER BY ID DESC LIMIT 6"); to limit and I want to display the other ones in next pages.
if(isset($_POST['filter']))
{
$filter = $_POST['filter'];
$result = mysqli_query($conn,"SELECT * FROM products where Product like '%$filter%' or Description like '%$filter%' or Category like '%$filter%'");
}
else
{
$result = mysqli_query($conn,"SELECT * FROM products ORDER BY ID DESC LIMIT 6");
}
if($result){
while($row=mysqli_fetch_array($result)){
$prodID = $row["ID"];
echo '<ul class="col-sm-4">';
echo '<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<img src="reservation/img/products/'.$row['imgUrl'].'" alt="'.$row['Product'].'" title="'.$row['Product'].'" width="150" height="150" />
</a>
<h2>'.$row['Product'].'</h2>
<h2>₽ '.$row['Price'].'</h2>
<p>Stock: '.$row['Stock'].'</p>
<p>Category: '.$row['Category'].'</p>
<i class="fa fa-search"></i>View Details
</div>';
echo '</ul>';
}
}
?>
Thank you.
Use the LIMIT clause with an offset. For example:
SELECT ... LIMIT 0, 6 -- select the six rows starting at row 0
then
SELECT ... LIMIT 6, 6 -- select the six rows starting at row 6
SELECT ... LIMIT 12, 6 -- select the six rows starting at row 12
...
See the MySQL Manual for more details
Related
I want display the date range for my ID like from ID 4 to 6 (means not using limit) but as shown in the picture All value
In my code all the values store in category table is displaying I just want to display only first three records.
my code is here
$sql = "SELECT * FROM category order by id ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo '
<span id="rcorners2">'.$row["category"].' </span>
In mysql you can use limit 3
SELECT * FROM category order by id ASC LIMIT 3
I want to show row number for every row fetched from database. can someone help me to do this.
My Code to fetch rows is:
$query="
select id
, title
, description
, url,votes
from posts
order
by votes desc
";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$arr = array();
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$arr[] = $row;
}
}
# JSON-encode the response
$json_response = json_encode($arr);
// # Return the response
echo $json_response;
My code displays post, title, post votes and post controls (Vote up or Down).
I want to show row number for every row fetched.
My Code to Display posts is:
<ul class="thumbnails" ng-controller="votingCtrl">
<li ng-repeat="post in posts" class="clearfix">
<div class="col-md-1 voting well">
<div class="votingButton" ng-click="upVote(post);">
<i class="glyphicon glyphicon-chevron-up"></i>
</div>
<div class="badge badge-inverse">
<div>{{post.votes}}</div>
</div>
<div class="votingButton" ng-click="downVote(post);">
<i class="glyphicon glyphicon-chevron-down"></i>
</div>
</div>
<div class="well col-md-11">
<h4>{{post.title}}</h4>
<p>{{post.description}}</p>
</div>
</li>
</ul>
Thanks
try to use ROW_NUMBER() mysql function
select id,title,description,url,votes, ROW_NUMBER() OVER (ORDER BY votes DESC) as row_num from posts order by votes desc
With the current logic, row number can be displayed using {{post.id}} as the SELECT query fetches id along with votes as fields.
In the loop itself, we can display a custom counter like:
$arr = array();
$cntr = 0;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
++$cntr;
$row['cntr'] = $cntr;
$arr[] = $row;
}
}
And in view template, you can use {{post.cntr}}.
SELECT
(#row_number:=#row_number + 1) AS num,
id,
title
FROM
Test,
(SELECT #row_number:=0) AS t
LIMIT 5;
Please try above its tested it will work, only need to replace your query with that.
I am working on an image gallery system, and I want to know how to limit/show all image uploaded into the gallery/database table. I normally don't know how this is done so I usually use a number greater than the number of columns in the database to show all my columns. I will love to know how this is done in other to change my method of doing this.
HERE IS MY CODE BELOW;
<?php
$image_id = $the_image = $image_des = $time_uploaded = "";
$gallery = mysqli_query($connect, "SELECT * FROM gallery ORDER BY time_uploaded DESC LIMIT 9000");
while ($pick = mysqli_fetch_array($gallery, MYSQLI_BOTH)) {
$image_id = $pick['id'];
$the_image = $pick['image'];
$image_des = $pick['description'];
$time_uploaded = $pick['time_uploaded'];
echo '<div class="nicdark_margin100">';
echo '<img alt="" class="nicdark_radius nicdark_opacity" src="../img/gallery/'.$the_image.'" width="100" height="100" /> ';
echo ' </div>';
}
?>
MySQL doesn't require a LIMIT field in the query. So to get all results
SELECT * FROM gallery ORDER BY time_uploaded DESC
However, if you're trying to do pagination, you can combine LIMIT and OFFSET
## First Page of 25 images
SELECT * FROM gallery ORDER BY time_uploaded DESC LIMIT 25 OFFSET 0
## Second Page of 25 images (offset by 25)
SELECT * FROM gallery ORDER BY time_uploaded DESC LIMIT 25 OFFSET 25
For more info, see
https://www.w3schools.com/php/php_mysql_select_limit.asp
I'm trying to get my article to fetch how many comments it has, and display the number. Here's the code:
<?php
$amount_get = mysql_query("SELECT * FROM comment WHERE id='" . mysql_real_escape_string($_GET['articleid']) . "'");
$comments = mysql_num_rows($amount_get);
$grab = mysql_query("SELECT id, news_title, news_content, news_date, news_author FROM articles ORDER BY id DESC LIMIT 5");
while($row = mysql_fetch_array($grab)){
?>
<div class="pane">
<li>
<h2><?php echo $row['news_title'] ?></h2>
<div class="meta">
<span class="color"> • </span>
<?php echo $row['news_date'] ?>
<span class="color"> • </span>
</div>
Comments: <?php echo $comments ?>
but for some reason, it stays on "0". This is what it looks like:
and my columns are id, articleid,name,comment,date & ip.
Your where clause is looking for id = articleid.
What you want is where articleid = articleid.
$amount_get = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['articleid']) . "'");
It looks like the SQL is faulty. Check your articleid GET variable. Also should it not be doing a different query and mysql_num_rows for each article's comments as each article likely contains different number of comments.
Try this:
SELECT
articles.id, news_title, news_content, news_date, news_author
(SELECT COUNT(*) FROM comment WHERE comment.id=articles.id) as comments
FROM articles
ORDER BY id DESC LIMIT 5
This will get you the first 5 articles and their related comment counts.
You could just use a single query.
$grab = mysql_query(
"SELECT a.id, a.news_title, a.news_content, a.news_date, a.news_author, count(*) as comment_count
FROM articles a
LEFT JOIN comment c ON c.articleid = a.id
ORDER BY a.id DESC LIMIT 5 GROUP BY a.id");
Then instead of
Comments: <?php echo $comments ?>
Do:
Comments: <?php echo $row['comment_count']; ?>
Let me know if the query is working, I'm not sure if the Group by is correctly placed there.
I have two tables that I need to pull records from (classifieds_items and attachments). The images for the classified items are stored in a different table (attachments) and there can be multiple images for each classified. I need to pull just one image for each classified item. The problem with the statement below is it will duplicate the results with the items thas have multiple images.
$sql = "SELECT *
FROM classifieds_items
JOIN (attachments) ON (attachments.attach_rel_id = classifieds_items.item_id)
WHERE active = 1
AND open = 1
AND date_expiry > ". time()."
AND attachments.attach_rel_module = 'classifieds'
ORDER BY RAND()
LIMIT 4";
$rs = mysql_query($sql);
if(mysql_num_rows($rs)>0) {
while($row=mysql_fetch_array($rs)) {
$dtl_list .="<div class='fclass'>
<span class='fctitle'><a class='albumlnk' href='#'>".stripslashes($row['name']). '</a></span><br />
<img src="uploads/'.stripslashes($row['attach_thumb_location']).'" /><br />
'.stripslashes($row['price'])."
</div>";
}
}
echo $dtl_list;
In mysql you can just add GROUP BY classifieds_items.item_id :
`$sql = "SELECT *
FROM classifieds_items
JOIN (attachments) ON (attachments.attach_rel_id = classifieds_items.item_id)
WHERE active = 1
AND open = 1
AND date_expiry > ". time()."
AND attachments.attach_rel_module = 'classifieds'
GROUP BY classifieds_items.item_id
ORDER BY RAND()
LIMIT 4";`