I'm trying to get my next images in a loop so they go back to the start of the the image list. I am able to get the images to go through the list right up to the last image then my last image shows no PID on the link.
I have
<div id="photolistholder">
<?php if(isset($_GET['pid'])){?>
<?php
//Now we'll get the list of the specified users photos
$var =$_GET['pid'];
$sql = "SELECT * FROM coverphotos WHERE coverphoto_id='$var'";
$query = mysqli_query($mysqli,$sql)or die(mysqli_error($mysqli));
while($photo = mysqli_fetch_array($query)){
$user=rawfeeds_user_core::getuser($photo['coverphoto_userid']);
?>
<p class="frontpage_description"><a href="profile.php?username=<?php echo $user['username']; ?>">
<?php
$id=$_SESSION['id'];
if($user['id']){
echo "<img border=\"0\" src=\"userimages/cropped".$user['id'].".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" ><br/>".$user['fullname']."'s</a> Album: </p>";
}else{
echo "<img border=\"0\" src=\"userimages/cropped".$id.".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" ><br/>".$user['fullname']." </a></p>";
}
$id=$_SESSION['id'];
$var = $_GET['pid'] ;
$photo_sql = "SELECT coverphoto_id FROM coverphotos WHERE (coverphoto_id < ".$var." OR coverphoto_id > ".$var.") AND coverphoto_userid = ".$user['id']." ORDER BY coverphoto_id DESC LIMIT 1";
$photo_query = mysqli_query($mysqli,$photo_sql)or die(mysqli_error($mysqli));
$photo_prev=mysqli_fetch_array($photo_query);
if($photo_prev>1){
echo"<a href='coverimagephoto.php?pid=".$photo_prev['coverphoto_id']."'>Previous</a> | ";
}else{
echo " ";
}
$var = $_GET['pid'] ;
$photo_sqls = "SELECT coverphoto_id FROM coverphotos WHERE (coverphoto_id > ".$var." OR coverphoto_id < ".$var.") AND coverphoto_userid = ".$user['id']." ORDER BY coverphoto_id ASC LIMIT 1";
$photo_querys = mysqli_query($mysqli,$photo_sqls)or die(mysqli_error($mysqli));
$photo_next=mysqli_fetch_array($photo_querys);
if($photo_next>1){
echo "<a href='coverimagephoto.php?pid=".$photo_next['coverphoto_id']."'>Next</a>";
}else{
echo "";
}
$user1_id=#$_SESSION['id'];
$user2_id=$user['id'];
if($user1_id==#$_SESSION['id']){
echo " | <a href='include/photo_delete.php?pid=".$photo['coverphoto_id']."'>Delete</a>";
}else{
echo"";
}
?>
<center>
<?php
//echo "<div class='photo_captionholder'><b>Name</b> : ".$photo['photo_name']." | <b>Caption</b> : ".$photo['photo_caption']." |
//<b>Uploaded</b> : ".$photo['photo_datetime']."</div>";
echo "<img id=\"coverimage\" border=\"0\" src='coverimages/".$photo['coverphoto']."?".time()."' onerror='this.src=\"coverimages/nocover.png\"'>";
}}
?>
</body>
I tried using
$var = $_GET['pid'] ;
$photo_sql = "SELECT coverphoto_id FROM coverphotos WHERE (coverphoto_id > ".$var." OR coverphoto_id < ".$var.") AND coverphoto_userid = ".$user['id']."";
$photo_query = mysqli_query($mysqli,$photo_sql)or die(mysqli_error($mysqli));
$photo_next=mysqli_fetch_array($photo_query);
if($photo_next>1){
echo"<a href='coverimagephoto.php?pid=".$photo_prev['coverphoto_id']."'>Next</a> | ";
}else{
echo " ";
}
But the link just jumps between two images. The images are not in 1,2,3,4,5 they can be 1,2,4,5 if a user deletes an image and it can start on image PID 3 or 5.
Would it not be much simpler to code and understand and also reduce your queries (round trips to the server and back) from N+1 queries to 1 query where N=(number of photos) if you just used the first query and then fetched all the results into an array, and then process up and down that array
Quick mockup:
<div id="photolistholder">
<?php
if( isset($_GET['pid']) ) {
// validate the pid
$_GET['pid'] = some_validation_please($_GET['pid']);
$id=$_SESSION['id'];
$sql = "SELECT * FROM coverphotos WHERE coverphoto_id='$_GET['pid']'";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
$photos = array();
while($row = mysqli_fetch_array($result)){
$photos[] = $row;
}
foreach( $photos as $idx => $photo ) {
// add all your image html with the prev and next links
// using $idx-1 for prev image
// using $idx+1 for next image
// and checking if $idx = 0 for first image
// so you can use count($photos)-1 to know you are on last image
// so you use photos[0] as the next image to get continuous loop
// and if $idx == 0 next image is count($photos)-1
}
?>
Sorry I have not got the time to pick the bones out of what you are putting out to the browser, as I am bound to make a silly mistake and get hassled for it.
I have figured out a better way that works perfectly. Just use a UNION
$var = mysqli_escape_string($_GET['pid']) ;
$photo_sql = "(SELECT coverphoto_id FROM coverphotos WHERE coverphoto_id < ".$var." AND coverphoto_userid = ".$user['id']." AND album=1 ORDER BY coverphoto_id DESC LIMIT 1)";
$photo_sql.= " UNION (SELECT coverphoto_id FROM coverphotos WHERE coverphoto_id > ".$var." AND coverphoto_userid = ".$user['id']." AND album=1 ORDER BY coverphoto_id DESC LIMIT 1)";
$photo_query = mysqli_query($mysqli,$photo_sql)or die(mysqli_error($mysqli));
$photo_prev=mysqli_fetch_array($photo_query);
if($photo_prev>1){
echo"<a href='coverimagephoto.php?pid=".$photo_prev['coverphoto_id']."'>Previous</a> | ";
}else{
echo " ";
}
$var = $_GET['pid'] ;
$photo_sqls = "(SELECT coverphoto_id FROM coverphotos WHERE coverphoto_id > ".$var." AND coverphoto_userid = ".$user['id']." AND album=1 ORDER BY coverphoto_id ASC LIMIT 1)";
$photo_sqls.= " UNION (SELECT coverphoto_id FROM coverphotos WHERE coverphoto_id < ".$var." AND coverphoto_userid = ".$user['id']." AND album=1 ORDER BY coverphoto_id ASC LIMIT 1)";
$photo_querys = mysqli_query($mysqli,$photo_sqls)or die(mysqli_error($mysqli));
$photo_next=mysqli_fetch_array($photo_querys);
if($photo_next>1){
echo "<a href='coverimagephoto.php?pid=".$photo_next['coverphoto_id']."'>Next</a>";
}else{
echo " ";
}
Related
I need to save the count(activity_type) group by user_posted_to into a variable from mysql query
$query = "
SELECT user_posted_to, COUNT(*)
FROM activity_log_table
WHERE
post_type = 'discussion'
AND activity_type = 'Like' ";
$query .= "AND activity_timestamp >= CURRENT_DATE - INTERVAL 7 DAY AND activity_type <= CURRENT_DATE ";
$query .= "GROUP BY user_posted_to ORDER BY activity_timestamp DESC LIMIT 25";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
$user_posted_to = (int)$row['user_posted_to'];
$timestamp = time();
// I need to insert the count into this table for number_assert
$query2 = "INSERT INTO top_weekly (user_id, number_assert, timestamp) VALUES ($user_posted_to, $timestamp)";
$result2 = mysqli_query($connection, $query2);
$confirm_query2 = confirm_query($result2);
if($confirm_query2) {
echo "success query 2";
} else {
echo "failed query 2";
}
}
i expect to save the count() group by into a php variable and to be able to use it later on the page
You want to alias the « COUNT(*) » to something else, like « cnt ». Then you can access the column by name after fetching, as demonstrated below :
$query = "
SELECT
user_posted_to,
COUNT(*) as cnt
FROM activity_log_table
WHERE
post_type = 'discussion'
AND activity_type = 'Like'
AND activity_timestamp >= CURRENT_DATE - INTERVAL 7 DAY
AND activity_type <= CURRENT_DATE
GROUP BY user_posted_to
ORDER BY activity_timestamp DESC
LIMIT 25";
$result = mysqli_query($connection, $query);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "user_posted_to: " . $row["user_posted_to"]. " - Count: " . $row["cnt"] . "<br>";
}
} else {
echo "0 results";
}
Add and AS label to the COUNT(*) then you will be able to readout that label in the row you get.
$query = "SELECT user_posted_to, COUNT(*) AS varCount FROM activity_log_table WHERE post_type = 'discussion' AND activity_type = 'Like' ";
$nummerOfCount = $row['varCount']
i have made this code to get the top 5 songs views:
$mysqli_query ="SELECT page, count( * ) AS 'Cnt'
FROM pageview
GROUP BY page
ORDER BY `Cnt` DESC
LIMIT 5 ";
$result = mysqli_query($conn, $mysqli_query) or die (mysqli_error($conn));
$num_rows = mysqli_num_rows($result);
echo "<br>Top 5 Song views:<br>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo " " .$row["page"]."<br />";
}
} else {
echo "0 results";
}
//fim
mysqli_close($conn);
?>
And it is working, but the $row["id"] is missing, in my mind i have to make another query, my question is can i join this in the existing query?
maybe you should do this
$mysqli_query ="SELECT id,page, count( * ) AS 'Cnt'
FROM pageview
GROUP BY page
ORDER BY `Cnt` DESC
LIMIT 5 ";
$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die ("Fail".mysqli_error($conn));
$record2 = mysqli_fetch_array($rslt2);
$nRows = mysqli_num_rows($rslt2);
for ($i=0; $i<$nRows; $i++) {
$record2 = mysqli_fetch_array($rslt2);
echo $record2["title"];
}
the echo result is skipping the first row of the query, what am I doing wrong?
You are loosing the first row of your result set because you read the first row and throw it ways/ignore it
$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die ("Fail".mysqli_error($conn));
// this line gets the first row of your result set
// and you just ignore it therefore throw it away
$record2 = mysqli_fetch_array($rslt2);
$nRows = mysqli_num_rows($rslt2);
for ($i=0; $i<$nRows; $i++) {
$record2 = mysqli_fetch_array($rslt2);
echo $record2["title"];
}
As #Anant says, you are better advised to use a while as if the result set is empty, it just does nothing. Admittedly not always an advantage.
$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die ("Fail".mysqli_error($conn));
while ( $record2 = mysqli_fetch_array($rslt2) ) {
echo $record2["title"];
}
remove the line $record2 = mysqli_fetch_array($rslt2);, which is placed before the for loop.
new code would be:
$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die("Fail".mysqli_error($conn));
$nRows = mysqli_num_rows($rslt2);
for ($i=0; $i<$nRows; $i++) {
$record2 = mysqli_fetch_array($rslt2);
echo $record2["title"];
}
I'm trying to order a list of items based on the amount of comments for each topic as shown below:
$page = $_GET['page'];
$query = mysql_query("SELECT * FROM topic WHERE cat_id='$page' LIMIT $start, $per_page");
if (mysql_num_rows($query)>=1)
{
while($rows = mysql_fetch_array($query))
{
$number = $rows['topic_id'];
$title = $rows['topic_title'];
$description = $rows['topic_description'];
//get topic total
$sqlcomment = mysql_query("SELECT * FROM comments WHERE topic_id='$number'");
$commentnumber = mysql_num_rows($sqlcomment);
// TRYING TO ORDER OUTPUT ECHO BY TOPIC TOTAL ASC OR DESC
echo "
<ul>
<li><h4>$number. $title</h4>
<p>$description</p>
<p>$topictime</p>
<p>$commentnumber</p>
</li>
</ul>
";
}
}
else
{
echo "<p>no records available.</p><br>";
}
What would be the best way to order each echo by $num_rows (ASC/DESC values)? NOTE: I've updated with the full code - I am trying to order the output by $commentnumber
The first query should be:
SELECT t.*, COUNT(c.topic_id) AS count
FROM topic AS t
LEFT JOIN comments AS c ON c.topic_id = t.topic_id
WHERE t.cat_id = '$page'
GROUP BY t.topic_id
ORDER BY count
LIMIT $start, $per_page
You can get $commentnumber with:
$commentnumber = $rows['count'];
You don't need the second query at all.
First of all you have error here
echo "divs in order from least to greatest "number = $num_rows"";
It should be
echo "divs in order from least to greatest number = " . $num_rows . "";
And about the most commented try with
$sql = "SELECT * FROM `table` WHERE `id` = '$id' ORDER BY column DESC/ASC";
Or if there is not count column try with
$sql = "SELECT * FROM `table` WHERE `id` = '$id' ORDER BY COUNT(column) DESC/ASC";
Hello I am facing a problem with my pagination system, where if I list results from a mysql table it is working fine, but in case If I add some conditions inside the SQL Query like "AND" this column "AND" other column the script shows the results properly on the first page, when I chooce the second page instead of showing the second portion of results from 26 forward it is starting a new pagination and it is showing everything from the begining without the contions added inside the query. Here is the code of the pagination with the query:
//This gets all the other information from the form
$ciudad=$_POST['ciudad'];
$tipo=$_POST['tipo'];
$con=mysqli_connect();
// Check connection
$sql = "SELECT * FROM cursos WHERE 1";
if (!empty($ciudad)) {
$sql .= " AND ciudad = '$ciudad' ";
}
if (!empty($tipo)) {
$sql .= " AND tipo= '$tipo' ";
}
if (!$result = mysqli_query($con,$sql))
{
die("Error: " . mysqli_error($con));
}
$per_page =25;//define how many games for a page
$count = mysqli_num_rows($result);
$pages = ceil($count/$per_page);
if(!isset($_GET['page']) || $_GET['page']=="") {
$page="1";
} else {
$page=$_GET['page'];
}
$start = ($page - 1) * $per_page;
$sql = "SELECT * FROM cursos WHERE 1 LIMIT $start,$per_page";
?>
This is the code of the generated pages links:
<?php
//Show page links
for ($i = 1; $i <= $pages; $i++)
{?>
<li id="<?php echo $i;?>"><?php echo $i;?></li>
<?php
}
?>
The 2 problems where:
additional filter are not anymore selected in the next page ($_POST will be empty)
instructions related to pagination where calculated AFTER the query (which, obviously, couldn't use theses parameters)
You can either store your extra queries conditions in session, or add it as parameter in the "next page link", or transform your link to a submit form (which is probably the best option)
<li id="<?php echo $i;?>"><?php echo $i;?></li>
If you choose the link solution, don't forget to change your _POST in _GET (or check the second if the first is empty, or use $_REQUEST)
I have to mention your code is not sql injection free and using mysqli_prepare() may worth the time (for security and performances)
EDIT: so, here we go:
sidenotes: using $_REQUEST is not always recommended
And I noticed also you execute your query BEFORE using the pagination system...
//This gets all the other information from the form
$ciudad=$_REQUEST['ciudad'];
$tipo=$_REQUEST['tipo'];
$con=mysqli_connect();
// Check connection
$sql = "SELECT * FROM cursos WHERE 1";
if (!empty($ciudad)) {
$sql .= " AND ciudad = '$ciudad' ";
}
if (!empty($tipo)) {
$sql .= " AND tipo= '$tipo' ";
}
// PAGINATION MOVED UP
$per_page =25;//define how many games for a page
$count = mysqli_num_rows($result);
$pages = ceil($count/$per_page);
if(empty($_GET['page'])) {
$page="1";
} else {
$page=$_GET['page'];
}
$start = ($page - 1) * $per_page;
$sql .= ' LIMIT '.$start.','.$per_page;
if (!$result = mysqli_query($con,$sql))
{
die("Error: " . mysqli_error($con));
}
//Show page links
for ($i = 1; $i <= $pages; $i++)
{?>
<li id="<?php echo $i;?>"><?php echo $i;?></li>
<?php
}
?>
If $ciudad and $tipo both are not empty your query on execution will look like this:
SELECT * FROM cursos WHERE 1 AND ciudad = '$ciudad' ORDER BY id DESC AND tipo= '$tipo' ORDER BY id DESC
It should be like this if i am not mistaken:
SELECT * FROM cursos WHERE 1 AND ciudad = '$ciudad' AND tipo= '$tipo' ORDER BY id DESC
What I would do is change this:
$sql = "SELECT * FROM cursos WHERE 1";
if (!empty($ciudad)) {
$sql .= " AND ciudad = '$ciudad' ORDER BY id DESC ";
}
if (!empty($tipo)) {
$sql .= " AND tipo= '$tipo' ORDER BY id DESC ";
}
too this:
$sql = "SELECT * FROM cursos WHERE 1 ";
if (!empty($ciudad)) {
$sql .= "AND ciudad= '$ciudad' ";
if (!empty($tipo)) {
$sql .= "AND tipo= '$tipo' ";
}
$sql .= "ORDER BY id DESC ";
}
I've also got a link which might help you out with the pagination.
http://www.phpjabbers.com/php--mysql-select-data-and-split-on-pages-php25.html
If city and type are set then your SQL will have two instances of order by... You should add order by after the if statements.