So I have this feature on my site that people can go to a top users tab and it will show the person with the most profit on my site.
Using this code
<?php
$rs1 = mysql_query("SELECT profit,steamid,name,avatar FROM `users` GROUP BY profit DESC LIMIT 1");
$row = mysql_fetch_row($rs1);
$profit = round($row[0],2);
$steamid = $row[1];
$name = $row[2];
$avatar = $row[3];
echo'
<div class="col-md-4 col-lg-4">
<div class="widget-bg-color-icon card-box">
<a href="profile.php?action=view&id='.$steamid.'" target="_BLANK"><img src="https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/e3/'.$avatar.'" width="100px" alt="user-img" class="img-circle hoverZoomLink">
<h2>'.$name.'</h2></a>
<p><font color="#01DF01">$'.$profit.'</font></p>
<div><font color="white">Most Profit</font></div>
</div>
</div>
';
But I can't figure out how to retrieve the information from the user with the second most profit.
Website: CSGOLog.com/topusers1.php
Limit can take two arguments
SELECT profit,steamid,name,avatar FROM `users` GROUP BY profit DESC LIMIT 1, 1
It the same query you used but with an additional parameter for limit.
According to my understanding you should use ORDER BY rather than GROUP BY here.
Related
i am writing a php script that retrieves images from a database and shows them on page inside an album , each album contains several images...
here is an example for the code below :
$sql = "SELECT * FROM tbl_album where status='process' ORDER BY albumid DESC LIMIT $start_from, 12";
<?php
$rs_result = mysql_query ($sql,$con);
while ($row = mysql_fetch_assoc($rs_result))
{
$aid=$row['albumid'];
$aimage=$row['image'];
$aname=$row['name'];
$astatus=$row['status'];
echo '<div class="col-lg-4 col-md-6 col-sm-6 agile_gallery_grid">';
echo '<div class="hover ehover14">';
?>
<?php
echo"<a href='#details?d=$aid' onclick='showfunction()'>";
echo "<img src='admin/acatch/$aimage' class='img-responsive' alt='$aname' width='200' height='200' data-picture_id='$aid'>";
echo'</a>';
}
?>
this code shows all the albums contained in the database ,
but what i want to do is that when i click on the album cover , i want to display the images inside it on the same page , but i am not able to pass the album id to another php script on the same page to do this,
please if anyone can help
thank u in advance
Have a look at the following code:
<?php
$sql = "SELECT * FROM tbl_album where status='process' ORDER BY albumid DESC LIMIT $start_from, 12";
$rs_result = mysql_query($sql, $con);
while($row = mysql_fetch_assoc($rs_result)):
$aid=$row['albumid'];
$aimage=$row['image'];
$aname=$row['name'];
$astatus=$row['status'];
?>
<div class="col-lg-4 col-md-6 col-sm-6 agile_gallery_grid">
<div class="hover ehover14">
<a href='#details?d=$aid' onclick='showfunction()'>
<img src="admin/acatch/<?=$aimage?>" class="img-responsive" alt="<?=$aname?>" width="200" height="200" data-picture_id="<?=$aid?>">
</a>
</div>
</div>
<div class="gallery_album" id="gallery_album_<?=$row['albumid']?>">
<?php
/**
* now collect all images associated with each album by using $aid
*/
$sql_images = "SELECT * FROM tbl_album_images WHERE fk_album_id = $aid";
$images_result = mysql_query($sql_images, $con);
while($image = mysql_fetch_assoc($images_result)):
?>
<div class="gallery_album_image"><img src="<?=$image['dir_and_filename']?>"></div>
<?php
endwhile;
?>
</div>
<?php endwhile; ?>
What we are doing here is gettign all the albums as you allready does. But we introduce a subquery inside the first while-loop to fetch all associated images for each album.
We put these images inside new div-container with a unique album id you can use to show or hide the actual album with your showfunction()
Use CSS and javascript to show or hide accordingly.
OBS! WARNING!! Your mysql queries, and the one I provided, is very very BAD!
Have a look into prepared statement using php PDO or mysqli...
Hello please someone can help me with this code? I think there i can remove * and GET id.
<div class="col-md-4">
<h1>Sidebar</h1>
<?php
require_once 'connessione.php';
$id=$_GET['id'];
$query = "SELECT * FROM campi_name where id='$id'";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>
<div class="media">
<div class="media-left">
<a href="#">
<img class="media-object" src="..." alt="...">
</a>
</div>
</div>
<?php
}
?>
</div>
How can i show only item (from db) related to the Brand row from table campi_name?
and with code like that how i can show 5 random item from campi_name?
Thank you!
For the first question you can use
$query = "SELECT Brand FROM campi_name WHERE id='" . $id . "'";
I'm not sure because I haven't understand what you are asking. Do you want to select only Brand column or what? Do you want to select just the row that content some letters?
For the second question use
$query = "SELECT * FROM campi_name ORDER BY rand() LIMIT 5"
I have a donation site and a little bit stuck on finding what percent of the Donation Goal that the Current Donations are at?
In my MySQLi database I have two columns - donation_goal (numerical eg. 500) and current_raised (numerical eg. 20.00).
What I need to do is find and display how much in percentage has been raised currently out of the donation goal. So if £10 was donated and the goal was £100 it would display 10%. But it would do this each time the page was loaded.
I display each page with the code below and need to find a way to add this feature to this code below:
<?php
$sql = "SELECT * FROM pages ORDER BY RAND() LIMIT 3";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
$page_name = $row['page_name'];
$count = strlen($page_name);
if($count < 27){
$space = '<br> <br/>';
} else {
$space = '';
}
$page_dp = $row['page_dp'];
$donation_goal = $row['donation_goal'];
$current_raised = $row['current_raised'];
?>
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="pageimg/<? echo $page_dp; ?>" style="width:348px;height:233px;" alt="Page Image">
<div class="caption">
<h3><? echo $page_name; ?> <? echo $space; ?></h3>
<div class="progress">
<div class="progress-bar progress-bar-success progress-bar-striped" role="progressbar" aria-valuenow="10" aria-valuemin="0" aria-valuemax="100" style="width: 10%">10%
<span class="sr-only">10% Complete (success)</span>
</div>
</div>
<p><b>£<? echo $donation_goal; ?>.00</b> Target | <b>£<? echo $current_raised; ?></b> Raised</p>
Here is a little info about this current topic, please help us raise more than we can! We need your help!
<p>Donate Now Details</p>
</div>
</div>
</div>
<?
}
?>
I have tried adding this code to the mix, but receiving errors so am pretty sure I have no idea how to add it correctly:
$sql = "SELECT * FROM pages, COUNT( current_raised ) AS test, concat(round(( current_raised/donation_goal * 100 ),2),'%') AS percentage ORDER BY id DESC LIMIT 3";
This just gave out errors. Any ideas of how to do this efficiently with short code?
If you would like to calculate the percentage of the dontation from the goal you could do this directly in your query:
SELECT page_dp,
donation_goal,
current_raised,
ROUND(current_raised/donation_goal*100) percentage
FROM pages
ORDER BY RAND()
LIMIT 3
Then you only need to output the percentage within your loop:
<span class="sr-only"><?php print $row['percentage']; ?>% Complete (success)</span>
Is this what you want?
SELECT p.*, pc.cr / donation_goal
FROM pages p CROSS JOIN
(SELECT SUM(current_raised) as cr FROM pages) pc
ORDER BY RAND()
LIMIT 3;
how can I use the ASC AND DESC in php with mysql ? here's my code below.
<?php
$gallery_query = mysql_query("SELECT * FROM `gallery` WHERE `control_id` = '{$row['control_id']}'");
if(mysql_num_rows($gallery_query) == 0){
?>
<td align="center">
<p>No Photos Available</p>
</td>
<?php
} else{
while($photo = mysql_fetch_assoc($gallery_query)){
?>
<td align="center">
<a href="#" data-toggle="modal" data-target="#photo<?php echo $photo['id']?>">
<div class="popover_img">
<img src="<?php echo $photo['photo']?>">
</div>
</a>
</td>
and I want to do is Ascending the data from the latest photo to old photo
then add ORDER BY to query
mysql_query("SELECT * FROM `gallery` WHERE `control_id` = '{$row['control_id']}' ORDER BY date DESC");
use date (created date) column to order rather id or both
ORDER BY DESC :- means last to first(new to old) records
for more :- http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html
its very simple
SELECT * FROM gallery WHERE control_id = 'YOUR ID' ORDER BY YOUR PHOTO ID COLUMN DESC
1)if you want to display your latest photo to old photo than you have to use in query "DESC" i.e descending order for e.g "select * from gallery where control_id='2' order by my_photo_id DESC
2)and if you want to display old photo to latest photo than you can use in query "ASC" i.e Ascending order and by default data will be display in ascending order. for e.g "select * from gallery where control_id='2' order by my_photo_id ASC
For more information you can learn from this link: http://www.w3schools.com/sql/sql_orderby.asp
And Yes I forgot I have Column in the table named post_date and here's the correct answer to my question
here's the code
<table style="width:100%;padding:0px;">
<tr>
<?php
$gallery_query = mysql_query("SELECT * FROM `gallery` WHERE `control_id` = '{$row['control_id']}' ORDER by `post_date` DESC");
if(mysql_num_rows($gallery_query) == 0){
?>
<td align="center">
<p>No Photos Available</p>
</td>
<?php
} else{
while($photo = mysql_fetch_assoc($gallery_query)){
?>
<td align="center">
<a href="#" data-toggle="modal" data-target="#photo<?php echo $photo['id']?>">
<div class="popover_img">
<img src="<?php echo $photo['photo']?>">
</div>
</a>
</td>
<?php
}
}
?>
</tr>
</table>
you can change your sql query in php like this
SELECT * FROM `gallery` WHERE `control_id` = '{$row['control_id']} ORDER BY 'ColumnID' DESC
I have a php code which also includes pagination
<?php
//to make pagination
$statement = "`blog_posts`";
//show users
$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint} , {$limit}");
while ($row = mysql_fetch_assoc($query)) {
echo '
<div class="box">
<a class="title" href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a>
<hr/>
<p>'.$row['postDesc'].'</p>
<div class="clear"></div>
<hr/>
<div class="desc">
Posted on '.date("jS M Y H:i:s", strtotime($row["postDate"])).'
<div class="underside">Read More</div>
</div>
</div>
';
}
echo pagination($statement,$limit,$page);
If I was to have three posts like so...
ID TITLE
-------------
1 Apples
2 Post 2
3 Grapes
My blog would spit them out like so...
Apples
Post 2
Grapes
This is bad because the newest posts are at the very bottom of the page (or at the last page)
I want it so that the newest articles appear first.
How would I do this. Please help explain why my current code isn't working correctly.
SELECT * FROM {$statement} ORDER BY `date` DESC LIMIT {$startpoint} , {$limit}
Where date should be the date, blog was saved
change your query like below and add order by
$query = mysql_query("SELECT * FROM {$statement} ORDER BY id desc LIMIT {$startpoint} , {$limit}");
Please, use order by column for sorting as per desc or asc order :
<?php
//to make pagination
$statement = "`blog_posts`";
//show users
$query = mysql_query("SELECT * FROM {$statement} ORDER BY date desc LIMIT {$startpoint} , {$limit}");
while ($row = mysql_fetch_assoc($query)) {
echo '
<div class="box">
<a class="title" href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a>
<hr/>
<p>'.$row['postDesc'].'</p>
<div class="clear"></div>
<hr/>
<div class="desc">
Posted on '.date("jS M Y H:i:s", strtotime($row["postDate"])).'
<div class="underside">Read More</div>
</div>
</div>
';
}
echo pagination($statement,$limit,$page);