How to limit all columns in a database using mysql - php

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

Related

numbering an output in order

i have made a image slider(or what ever you want to call it) and it displays the 6 latest images. Under the current bigger image you have all 6 of the most recent images , and the process works all fine but when you click the smaller image, it doesn't bring up the big image straight away. Because the very latest image is first its 'active' and has its own <a href="#1> and so the href="#1" is entered as i can manipulate the a tag as it is there. But as i used a foreach to bring up the next 5 images from 2nd to 6th in descending order from date submitted, i cant give each individual result there own href"#number" so they can link up with the bigger images, is there a way to assign each result a number by taking what position it sits within the query then adding 1or2 to the answer which would then give the corresponding number in the href so when clicked, the larger image which is linked to the href number too, it will make the larger image appear straight away.
the code from the tutorial that i have amended looks like this ..
<?php
$latest_headlines = get_latest_headlines();
foreach ($latest_headlines as $latest_headline) {
?>
<img src="img/<?php echo $latest_headline['img_title'].'.'.$latest_headline['img_ext']; ?>" class="nav-thumb" alt="<?php echo $latest_headline['title']; ?>" />
<?php
}
?>
<div id="movers-row">
<?php
$recent_headlines = get_recent_headlines();
foreach ($recent_headlines as $recent_headline) {
?>
<div><img src="img/<?php echo $recent_headline['img_title'].'.'.$recent_headline['img_ext']; ?>" class="nav-thumb" alt="<?php echo $recent_headline['title']; ?>" /></div>
<?php
}
?>
</div>
And here are my two functions to get the results, and before people start picking at all the problems ive done, just want to say this is the way ive learnt it im new, it maybe all wrong but its what stuck in my head to do things this way and its worked(ish) so far....
function get_recent_headlines() {
$sql = "SELECT *
FROM `story`
ORDER BY `submit_date` DESC
LIMIT 1, 5 ";
$result = mysql_query($sql) or die(mysql_error());
$recent_headlines = array();
while (($row = mysql_fetch_array($result)) !== false) {
$recent_headlines[] = array(
'title' => $row['title'],
'img_title' => $row['img_title'],
'img_ext' => $row['image_ext']
);
}
return $recent_headlines;
}
function get_latest_headlines() {
$sql = "SELECT *
FROM `story`
ORDER BY `submit_date` DESC
LIMIT 1 ";
$result = mysql_query($sql) or die(mysql_error());
$lastest_headlines = array();
while (($row = mysql_fetch_array($result)) !== false) {
$latest_headlines[] = array(
'title' => $row['title'],
'img_title' => $row['img_title'],
'img_ext' => $row['image_ext']
);
}
return $latest_headlines;
}
change your query of your function get_recent_headlines()
it uses a variable, and increment it in the SELECT statement:
`$sql = "SELECT *,(#row:=#row+1) AS rowno
FROM `story` inner join (SELECT #row:=0) AS row_count
ORDER BY `submit_date` DESC
LIMIT 1, 5 ";`
You can use a user variable to get a sequence number within MySQL.:-
SELECT Sub1.*,#aCount:=#aCount+1 AS aSequence
FROM (SELECT *
FROM `story`
ORDER BY `submit_date` DESC) Sub1
CROSS JOIN (SELECT #aCount := 0) Sub2
LIMIT 1, 5

PHP Get next result from MySQL

I have a database that stores image urls andother data, I want a gallery to display them but because the site is public I may want to delete some images. When a user uploads an image source and title, it is inserted into the database with an auto increment unique ID. The current script only adds or subtracts 1 from the ID to display the next or previous image. Is there any better way of doing this?
My current script:
function image_control($id){
$next=$id+'1';
$prev=$id-'1';
$query = "SELECT MAX(id), MIN(id) FROM mp_images";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$max = $row['MAX(id)']; $min = $row['MIN(id)'];
}
if($next==$max+"1"){$next=$min;} if($prev==$min-"1"){$prev=$max;}
echo "<a href='".$next."'>Next Image</a> <a href='".$prev."'>Previous Image</a>";
}
An example of this failing would be images where the ID's are 3,4,5,7,8.
some pseudo code for getting the next id:
$nextId = SELECT id FROM mp_images WHERE id > $currentId ORDER BY id ASC LIMIT 1
if ( no results )
// no next image
and for the previous:
$prevId = SELECT id FROM mp_images WHERE id < $currentId ORDER BY id DESC LIMIT 1
if ( no results )
// no previous image
It's up to you on what to do if there is no next image (you could for instance fetch the first image) or no previous image (you are on the first image).

How to build next and previous links with php?

I use this code to get the informations about a certain id
$sql = dbquery("SELECT * FROM `videos` WHERE `id` = ".$local_id." ");
while($row = mysql_fetch_array($sql)){
$video_id = $row["id"];
$video_title = $row["title"];
}
Let's say the link of a page would be example.com/video.php?id=34
How can i get the next and previous $video_id and $video_title depending on the current id?
A problem is that i can't increase or decrease the value of the current id by 1 because the 35 or 33 may be deleted in the meanwhile...
How can i achieve this?
//edit
I have a very big problem: the previous link sends me to the right link but the next link always sends me to the last video added in the database.
If i go to the last or first videos added in the database i get an error because there are no more next and previous videos added.
Perhaps two more queries would work ...
select id,title from videos where id < $local_id order by id desc limit 1
select id,title from videos where id > $local_id order by id asc limit 1
You have to use select and limit to get that one row you want, i.e.,
SELECT * FROM `videos` WHERE `id` < " . $local_id . " ORDER BY `id` DESC LIMIT 1
Or use > and ORDER BYidASC for the next video, instead of the previous I showed above.
Here you go through
// entry per page
$rowsPerPage = 3;
// Set page number
if(isset($_GET['page']))
$pageNum = $_GET['page'];
else
$pageNum = 1;
Note: Following code is use to show/set next & previous page number.
Note: If the current page is homepage then default $pageNum is 1 and
if $pageNum is set as 2, it will work as
if($pageNum){
$PreviousPageNumber = $pageNum - 1;
$NextPageNumber = $pageNum + 1;
echo '<a href="?page='. $PreviousPageNumber .'>Previous Page</a>';
echo '<a href="?page='. $NextPageNumber .'>Next Page</a>";
}
Note: Following code is use to show record affected by page numbers
$GetPreviousRecord = ($pageNum - 1) * $rowsPerPage;
Note: The first, optional value of LIMIT is the start position. Note:
And the second required value is the number of rows to retrieve.
$query = "SELECT * FROM post WHERE LIMIT $GetPreviousRecord, $rowsPerPage";
$result = mysql_query($query);
And in last use the WHILE loop to get all records from database by using LIMIT.

MySQL JOIN UnIQUE

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";`

how to check current position in mysql

how can i check current number in mysql where....
my query is
$aid = 16;
$get_prev_photo = mysql_query("SELECT * FROM photos WHERE album_id='$aid' AND pic_id<'$picid' ORDER BY pic_id LIMIT 1");
$get_next_photo = mysql_query("SELECT * FROM photos WHERE album_id='$aid' AND pic_id>'$picid' ORDER BY pic_id LIMIT 1");
while i am getting current photo with following query
$photo = mysql_query("SELECT * FROM photos WHERE pic_id='$picid' LIMIT 1");
and getting total photos in album with following query
$photos = mysql_query("SELECT * FROM photos WHERE album_id='$aid'");
$total_photos = mysql_num_rows($photos);
now i want to check where i am and show it as Showing 1 of 20, showing 6 of 20 and so on...
now i want to check where i am actually...
i think you are referring to pagination, which can be achieved using LIMIT and OFFSET sql
decide the number of results you want per page, then select that many
create links like:
View the next 10
and dynamically change those every time
queries look ~like~
$offset=$_GET['view'];
SELECT * FROM table WHERE `condition`=true LIMIT 5 OFFSET $offset
this translates roughly as
select 5 from the table, starting at the 10th record
This is bad:
$photos = mysql_query("SELECT * FROM photos WHERE album_id='$aid'");
Because it grabs all the fields for the entire album of photos when all you really want is the count. Instead, get the total number of photos in the album like this:
$result = mysql_query("SELECT count(1) as total_photos FROM photos
WHERE album_id='$aid'");
if ($result === false) {
print mysql_error();
exit(1);
}
$row = mysql_fetch_object($result);
$total_photos = $row->total_photos;
mysql_free_result($result);
Now you have the count of the total number of photos in the album so that you can set up paging. Let's say as an example that the limit is set to 20 photos per page. So that means that you can list photos 1 - 20, 21 - 40, etc. Create a $page variable (from user input, default 1) that represents the page number you are on and $limit and $offset variables to plug into your query.
$limit = 20;
$page = $_POST['page'] || 1; // <-- or however you get user input
$offset = $limit * ($page - 1);
I'll leave the part where you code the list of pages up to you. Next query for the photos based on the variables you created.
$result = mysql_query("SELECT * FROM photos WHERE album_id='$aid'
ORDER BY pic_id LIMIT $limit OFFSET $offset");
if ($result === false) {
print mysql_error();
exit(1);
}
$photo_num = $offset;
while ($row = mysql_fetch_object($result)) {
$photo_num++;
$pic_id = $row->pic_id;
// get the rest of the variables and do stuff here
// like print the photo number for example
print "Showing photo $photo_num of $total_photos\n";
}
mysql_free_result($result);
I'll leave better error handing, doing something with the data, and the rest of the details up to you. But that is the basics. Also I did not check my code for errors so there might be some syntax problems above. To make a single photo per page just make $limit = 1.

Categories