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;
Related
I have these cards with data that comes from a database. This is the reason it is in a while loop.
Now my problem is that I don't have a clue how to add pagination to this. If some of you guys have any suggestions or answers please let me know.
$start_from = ($page - 1)*$record_per_page;
$query = "SELECT organisation_name, organisation_logo, organisation_url FROM `organisations`";
$result = mysqli_query($conn, $query);
if ($result ->num_rows > 0) {
echo '<div id="myItems">';
echo '<div class="row">';
while ($row = $result-> fetch_assoc()) {
echo '
<div class="col-md-6">
<div class="card rounded-lg mt-3">
<div class="card-body p-0 shadow">
<div class="row">
<div class="col-md-4 p-0">
<img src="../img/';
if ($row['organisation_logo'] == '') {
echo 'stock/no-image-icon.png"';
} else {
echo 'uploads/'.$row['organisation_logo'].'"';
}
echo 'class="border p-3" style=" width: 150px; height: 150px;">
</div>
<div class="col-md-8">
<h3 class="card-title mt-3">'.$row["organisation_name"].' <a class="text-dark" href="http://'.$row["organisation_url"].'" target="_blank"><i class="fas fa-external-link-alt" style="font-size: 18px;"></i></a></h3>
<p>Current active surveys: <b>0</b></p>
<b>View <i class="fas fa-arrow-circle-right"></i></b>
</div>
</div>
</div>
</div>
</div>';
}
echo '</div>';
}
else {
echo 'nothing here';
}
You need to use LIMIT and OFFSET functionality in your SQL query. Limit determines how many elements you want to show on one site. Offset is point from where you want to get your items. So, for example – if You want 5 items, and you are on page 3, you have to add LIMIT 5 OFFSET 10. You calculate offset with (page_number-1) * limit equation. If you want to count how many pages you have, you must get total pages number, and use something like $pages = ceil($total / $limit). There are a lot of examples on stackoverflow.
I think this one can be very helpful – Simple PHP Pagination script
You also don't need to do all the calculation on your own, there are plany of pagination library for PHP, check look for them.
Hope it helps you a little.
What I'm trying to do: Where category (projects) matches id (markets) echo category (markets).
Table 1 Sample (projects table)
Table 2 Sample (markets table)
Sample of PHP Code
$num_rec_per_page=5;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
function GET($key) {
return isset($_GET[$key]) ? $_GET[$key] : null;
}
$category= GET('category');
if ($category !== null) {
$sql = "SELECT * FROM projects WHERE category='$category' LIMIT $start_from, $num_rec_per_page";
} else {
$sql = "SELECT * FROM projects LIMIT $start_from, $num_rec_per_page";
}
$sql_query = mysql_query($sql);
$post = $sql_query;
$sql1 = "SELECT * FROM markets";
$sql_query1 = mysql_query($sql1);
$marketsinfo = mysql_fetch_array($sql_query1);
In my code below, I've tried putting a while loop within the main while loop since we have to find out what category its in then display it for each blog post.
It only worked for the first result, and then I did some research online and found that it is very poor design to do this.
Where I'm currently at with displaying the result (see code in between hyphens):
<!-- Blog - Start -->
<?php while ($post = mysql_fetch_array($sql_query)) {?>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 blog blog_altered blog_left">
<div class="row">
<!-- Blog Image - Start -->
<div class=" col-lg-6 col-md-6 col-sm-10 col-xs-12 pic inviewport animated delay1" data-effect="fadeIn">
<img alt="blog-image" class="img-responsive" src="<?php echo $post['imageoutside'] ?>">
</div>
<!-- Blog Image - End -->
<!-- Blog Info - Start -->
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 inviewport animated delay1" data-effect="fadeIn">
<div class="info">
----------------------------------------------------------------------
<span class="date">
<?php
if($post['category'] === $marketsinfo['id']){
echo $marketsinfo['category'];
}
?>
</span>
----------------------------------------------------------------------
<h4 class="title"><?php echo $post['title'] ?></h4>
<p><?php echo $post['summary'] ?></p>
<a class="btn btn-primary text-on-primary" href="projects/readmore.php?job=<?php echo $post['job'] ?>">Read More</a>
</div>
</div>
<!-- Blog Info - End -->
</div>
</div>
<?php } ?>
<!-- Blog - End -->
Hope I've been thorough enough without being confusing. How can a noob accomplish this? I'll take any pointers you have!!
If you use a join, you can get the category text in one query and avoid all the looping in PHP.
A LEFT Join will return all records from Projects and only those records which match from markets. So if a projects category doesn't exist in markets, a NULL value will be returned.
If you used a INNER JOIN, only records which have corresponding values in BOTH tables would be returned.
Rule of thumb: get the data you need from the database in 1 trip when you need it. Format in PHP, Datagrab in SQL. Don't get more than you need, and don't get less.
SELECT P.Job, M.ID as Markets_ID, M.Category, P.Title
FROM projects P
LEFT JOIN Markets M
on P.Category =M.ID
WHERE P.category='$category'
LIMIT $start_from, $num_rec_per_page"
Note: you will need to put a table alias on category in the where clause. I'm assuming your passing in the ID so P.Category was used.
do you want join table projects with tables market by category?
may be u can do this
SELECT p.id as id
, m.category as category
from projects as p
left
join markets as m
on p.category = m.id
WHERE m.category='$category'
LIMIT $start_from
, $num_rec_per_page
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.
I have an issue with bootstrap and creating a 4 column responsive grid from a mysql response.
The problem is that if the second mysql query has a variable number of results, it brakes the grid.
Here is my code (where the first query has 9 results and the second query has a variable number of results):
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result)) {?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $row['username'];
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
which gives me this:
enter image description here
instead of this (obviously with many 'Hello world'):
enter image description here
Any help greatly appreciated!
Bootstrap doesn't claim to do any kind of elegant bin-packing on panels with different sizes. You could do some programming or css work to make all your panels the same size.
If that doesn't work for your application, you're going to need a layout library that does bin-packing so these panels of different sizes align properly.
There are a few such libraries available as jQuery plugins.
In this, $row[username] is wrong as it should be $row['username'].
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
BTW, I changed your code bit. Please Try this.
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result))
{
$username=$row['username'];
?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $username;
$b = "SELECT * FROM $table_presents WHERE bought_for='$username' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
[NOTE: Users can inject your SQL commands. Use prepared statements and parameterized queries. For more info, click Prevent SQL Injections
I want to display images in from server. In the database you can find the description and name of the image. the images should be displayed in a div where always the newest is on top. As the number of images increase so does the number of div's. so, the oldest will be at the bottom. I have read a number of posts and forums but I was unable to get the logic and the code on how to do it. I hope you can help me with my problem. Thanks a lot for your answer.
the only code i have is the formatting of the dive's:
<div class="gallery">
<div class= "group">
<div class="images">
</div>
<div class="details">
</div>
</div>
<div class= "group">
<div class="images">
</div>
<div class="details">
</div>
</div>
<div class= "group">
<div class="images">
</div>
<div class="details">
</div>
</div>
</div>
it's something like this:
newest image | some details here
newer image | some datails here
new image | some details here
old image | some details here
EDIT 1
I combined Lauri Elias' and iamde_coder's answer. I come up with this code which works almost similar to what is wanted. the only problem is that it displays the item (image&details in a div) 4 times. How can I eliminate the three? thanks!
$image_query = mysql_query ("SELECT filename, story FROM tbl_contest ORDER BY time DESC");
while($image_data = mysql_fetch_array($image_query)){
$imageName = stripslashes(mysql_real_escape_string($image_data['filename']));
$imageDetails = stripslashes(mysql_real_escape_string($image_data['story']));
$count = 0;
foreach($image_data as $imageName) {
echo '<div class="group">';
echo '<div class="images"><img src="/Mainfolder/image_entry/'.$imageName.'"></img></div>';
echo '<div class="details">'.$imageDetails.'</div></div>';
$count ++;
}
}
Now that you've given a little more code to help try using this:
$count = 0;
$image_query = mysql_query ("SELECT filename, story FROM tbl_contest ORDER BY time DESC") or die(mysql_error());
while($image_data = mysql_fetch_array($image_query)){
$imageName = stripslashes(mysql_real_escape_string($image_data['filename']));
$imageDetails = stripslashes(mysql_real_escape_string($image_data['story']));
$count++;
echo '<div class="group">';
echo '<div class="images"><img src="/Mainfolder/image_entry/'.$imageName.'" alt="Image '.$count.'" /></div>';
echo '<div class="details">'.$imageDetails.'</div></div>';
}
You could, for example, use a MySQL select somewhat like this:
SELECT name, description FROM my_database.images ORDER BY created_at DESC;
Then bind the result set to a variable in PHP like $images and then iterate on it and generate HTML like this:
foreach($images as $image) {
echo '<div class="group">';
echo '<div class="images"><img src="/images_folder/'.$image['name'].'"></img></div>';
echo '<div class="details">'.$image['description'].'</div></div>';
}