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
Related
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 write a php code to select form tables:
books
images
Some books does not have an image, so I want to skip it and select another book.
I have wrote this code but it does not work with me perfectly.
Now I'm getting only 5 records! it must be 6 as I limited in the book select query.
$slider_sql = "select * from books limit 6";
$slider_result = $conn->query($slider_sql);
while($slider_row = $slider_result->fetch_assoc()) {
extract($slider_row);
$img_sql = "SELECT big_img FROM images WHERE book_id = '$id'";
$img_rs = $conn->query($img_sql);
$img_row = $img_rs->fetch_assoc();
if ($img_rs->num_rows == 0)
continue; //--> here I want to start while again to select another book.
echo $book_name.'<br>';
echo $img_row['big_img'].'<br>';
}
Thanks for your help and time!
Instead of having a sub-query in a loop (which is nearly ALWAYS a bad idea!), use a JOIN instead, which simplifies it to one query instead of two. Then set a condition that big_img should not be empty. This guarantees that you will only find rows where there's an image matching the book. LIMIT will still only ensure the return of 6 rows. <> in MySQL is the same as !=.
$slider_sql = "SELECT b.book_name, i.big_img
FROM books b
JOIN images i
ON i.book_id=b.id
WHERE i.big_img <> ''
LIMIT 6";
$result = $conn->query($slider_sql);
while ($row = $result->fetch_assoc()) {
echo $row['book_name'].'<br>';
echo $row['big_img'].'<br>';
}
MySQL JOIN
Having some issues, basically I have a random image gallery.
This is the code for the random thumbnails:
<?php
include('../../connect.php');
$result = mysql_query("SELECT *
FROM picture AS r1
JOIN
(
SELECT ROUND(RAND() * (SELECT MAX(id)
FROM picture) ) AS id
) AS r2
WHERE r1.id >= r2.id
AND public_approved=1
ORDER BY r1.id ASC
LIMIT 4;")
or die(mysql_error());
while($row = mysql_fetch_array( $result ))
{
echo "<div style='float:left; margin:2px;' >";
echo '<a href="pictures.php?id=' . $row['id'] .'"><img src="../../files/small/thumb0_'. $row['file_name'] . '.' . $row['file_extension'] . '" border="0"></br>';
echo "</div>";
}
?>
When one of the thumbnails is clicked the page reloads and uses the following command to load the main content:
<?php
include('../../connect.php');
$passed_id = $_GET['id'];
$result1 = mysql_query("SELECT * FROM picture where id='$passed_id' ")
or die(mysql_error());
while($row = mysql_fetch_array( $result1 ))
{?>
This continues on through a table displaying the larger image and title and so on.
I'm experiencing two problems:
1 - Even though I specify 4 in the limit, sometimes only 1 2 or 3 thumbs are displayed.
2 - Sometimes when I click on a thumbnail the wrong id is used, i.e. thumbnail id 2 but firing id 8.
The reason why you are getting 1,2 or 3 thumbs is because r1.id >= r2.id where r1.public_aprove 1 only has a result set 1 or 2 or 3. The reason is your RAND().
For problem 2 which row[id] are you using r1.id or r2.id - I suspect its r2.id.
Suppose X is the number of images you want to display, you should really take [MAX(id) - X] as the RAND() multiplier. This way the result set will have enough results.
Second problem seems simple, someone will probably post a good answer, but keep in mind it's usually a column/parameter confusion in your php/html code.
Good luck!
I was looking for a simple pagination script and found one here, this seems to be working just fine.
However, when i click on "2", as in page 2, it just shows the records of page 2 underneath those that are already there. So basically if I would click on page 214 it still shows all of the records on one page.
I am not very experienced with PHP so i couldn't figure out what was wrong with the paginator.class.php, hopefully someone here can.
This is the code on the page where it should do the pagination:
else {
$query = "SELECT COUNT(*) FROM products";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_fetch_row($result);
$pages = new Paginator;
$pages->items_total = $num_rows[0];
$pages->mid_range = 9;
$pages->paginate();
$query1 = "SELECT serial, name, description, price, picture FROM products WHERE serial != '' ORDER BY serial ASC $pages->limit";
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo '<div style="margin-bottom:10px;display:inline-block;background-color:#E3E3E3;width:190px;height:200px;"><img style="padding-top:10px;padding-left:25px;width:150px;height:150px;" src="'.htmlspecialchars($row['picture']).'"><br><div align="center"><b>'.htmlspecialchars($row['name']).'</b><br><h6>€'.htmlspecialchars($row['price']).'</h6></div></div> ';
};
echo ' ';
echo '<br><br><div style="margin-left:330px;">';
echo $pages->display_pages();
echo '</div>';
}
The paginator.class.php can be found on the website I just mentioned.
The issue lies in your query:
"SELECT serial, name, description, price, picture FROM products WHERE serial != '' ORDER BY serial ASC $pages->limit"
You need to determine what the value of $pages->limit is. It seems to me that instead of calculating how many records should be displayed on each page (let's say 10 for argument's sake) and then determining what page you're on and setting the LIMIT condition.
What it should be set to is something like this:
LIMIT 30, 10
That's for page 4 - it displays records 30-40, rather than what I suspect it's doing, which is
LIMIT 40
That line will simply show up to 40 records and not close the lower bound of the window.
FYI take a look at the MySQL SELECT syntax in the manual.
I'm getting terribly slow results from the following code. I've been trying to troubleshoot it for hours but with no results. I've included only the relevant code.
Edit:
I am trying to select an id from a database and then use that id to get all the images associated with it. I am then narrowing that group of images down to one. Once I have that image I am resizing it through an external file.
I've tried removing various parts of the code to identify the problem and it seems as if the slow down is being caused by the second query but I am not sure why. Thanks for your help.
$getworks = mysql_query ("SELECT a_id from artists where display_works = '1' and active = '1' order by project_year desc, fullname desc");
while ($getworksrow = mysql_fetch_assoc($getworks)){
$totalimages=1;
$addstyle = "";
$id = $getworksrow["a_id"];
$getimages = mysql_query ("SELECT a_id, image_id from images where a_id = '". $id ."' order by position asc LIMIT 1");
$getimagesrow = mysql_fetch_assoc($getimages);
foreach ($getimagesrow as $getimagesrows){
extract($getimagesrow);
if($totalimages > 1){ $addstyle = 'style="display:none;"'; }
else {
$myimagename = "http://artist.com/$a_id/images/$image_id" . "_large.jpg";
list($width, $height, $type, $attr) = getimagesize("$myimagename");
$myimagename = "http://artist.com/artists/resize.php/$a_id/images/$image_id" . "_large.jpg?resize(157x2000)";
if($getworksrows["layout"] == "vert"){$pl = "_vertical";}else if($getworksrows["layout"] == "website"){$pl = "-s";}else if($getworksrows["layout"] == "video"){$pl = "_video";}else{$pl = "_horizontal";}
echo "<li class='thumbnail_container' $addstyle> <a class='thumbnail' href=\"../works$pl.php?a_id=" . $getworksrows["a_id"] . "\"><span><img src=\"$myimagename\" /></span>\n</a></li>\n";
}
$totalimages++;
}
}
It's a a big performance overhead to execute queries like this specially when parent query have large no. of records.
You should use join artists table with images table and get all data by single query.
Later on make 2D array of per artists and images. and loop according to 2D array to display data
Below is join query you should use:
SELECT * from artists as art
left join images as img on art.a_id=img.a_id
where display_works = '1' and active = '1'
order by project_year desc, fullname desc
In While make data array:
while ($getworksrow = mysql_fetch_object($getworks)){
$data['a_id']['img_id']=$getworksrow->image; //Make 2D array
........
........
}
looping and display data :
foreach($data as $id=>$images)
{
foreach($images as $val){
// Do your stuff for displaying data
}
}
So please do required changes.