In the table "ogitems", I have a column called "itemname", "month", and "price".
There duplicate items, arranged by month.
I want to check "itemname" to see if any rows have the same value for itemname, and if yes, echo the "itemname" along with the last two months and the pricing for these two months.
EXAMPLE:
"itemname" = CHEESE CHEDDAR
CHEESE CHEDDAR is found in months 11 AND 12 (so there is two records/rows for CHEESE CHEDDAR). 11 has "price" of 51 while 12 has "price" of 54
I want to echo that information into an anchor tag...
<a href="pricetrend.php?date=<?php echo "" . $row['month'] . "" ?>&price1=<?php echo $price1; ?>&price2=<?php echo $price2; ?>&item=<?php echo "" . $row['itemname'] . ""?>;">
Basically, I am trying to grab the two most recent prices for one item and echo them into this anchor tag. Thanks in advance for any help!
I am assuming that your month is a date field, this should work
SQL Fiddle link: http://sqlfiddle.com/#!2/d63a0/4
SELECT * FROM (
SELECT
o.itemname,
o.month,
( SELECT price FROM ogitems i WHERE i.itemname = o.itemname AND i.month<o.month ORDER BY MONTH DESC LIMIT 1) AS last_month_price,
o.price AS current_month_price
FROM
ogitems o
ORDER BY
o.month DESC
) AS items
GROUP BY
items.itemname
HAVING
items.last_month_price IS NOT NULL
select * from ogitems where itemname='CHEESE CHEDDAR' order by month desc limit 2
This sql statement should fix your problem
<?php
$result = mysql_query("select * from ogitems where itemname='CHEESE CHEDDAR' order by month desc limit 2")
while ($row = mysql_fetch_assoc($result)) {
echo $row["itemname"];
echo $row["month"];
echo $row["price"];
}
?>
Related
I have 3 different tables
table 1 keep general scores over the year
table 2 and 3 is event specific
only common field in all 3 is a member ID
I need to select 5 top score results for each member from table1 combine them with 1 specific result from table 2 and 3
I managed to get everything together in a temp table, but cnt get the output the way I need it
example what i have and need.
Original code
$prevQuery = "SELECT distinct member_id FROM scores";
$prevResult = $conn->query($prevQuery);
while($row = $prevResult->fetch_assoc()) {
$scores=("SELECT member_id,event_id,event_date,event_score FROM scores where member_id = ".$id." ORDER BY event_score DESC LIMIT 5");
Query:
select * from temp_table order by mem_id asc
PHP:
you can simple do your expected result in your application like this
<?php
$result = array(array('mem_id'=>1,'location'=>'A','date'=>'20/05/2017','score'=>100),array('mem_id'=>1,'location'=>'B','date'=>'21/05/2017','score'=>103),array('mem_id'=>1,'location'=>'C','date'=>'22/05/2017','score'=>106),array('mem_id'=>1,'location'=>'C','date'=>'23/05/2017','score'=>108),
array('mem_id'=>2,'location'=>'A','date'=>'20/05/2017','score'=>105),array('mem_id'=>2,'location'=>'B','date'=>'21/05/2017','score'=>109),array('mem_id'=>2,'location'=>'C','date'=>'22/05/2017','score'=>111),array('mem_id'=>2,'location'=>'C','date'=>'23/05/2017','score'=>110));
$new_result=array();
foreach($result as $key=>$row)
{
$new_result[$row['mem_id']][]=$row;
}
echo "<table border='1px'>";
echo "<thead><tr><th>S.No</th><th>date</th><th>score1</th><th>date</th><th>score2</th><th>date</th><th>score3</th><th>date</th><th>score4</th></tr>";
echo "<tbody>";
foreach($new_result as $key=>$row)
{
echo "<tr><td>".$key."</td>";
foreach($row as $key1=>$row1)
{
echo "<td>".$row1['date']."</td>";
echo "<td>".$row1['score']."</td>";
}
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
OUTPUT :
S.No date score1 date score2 date score3 date score4
1 20/05/2017 100 21/05/2017 103 22/05/2017 106 23/05/2017 108
2 20/05/2017 105 21/05/2017 109 22/05/2017 111 23/05/2017 110
im new on php programming and i've searched the function that i need but didn't found it.
here what exactly i want to do :
i want to select 2 columns from a table
set the order by descending by 1 column that is numeric
and then show in php the first 100 rows that were selected
Here is my code right now php shows all the columns i want it to show the first 100
$result = mysqli_query($con,"SELECT pvpkills,char_name FROM characters ORDER BY pvpkills DESC");
while($row = mysqli_fetch_array($result))
{
echo $row['pvpkills'] . "  " . $row['char_name'];
echo "<br>";
}
SELECT pvpkills,char_name FROM characters ORDER BY pvpkills DESC LIMIT 0,100
We have these two fields in our MySQL database:
in_model , in_color
And we are trying to count the total of model (in_model field), which has the same color (in_color field) in PHP with MySQL as backend database. We tried using the count() function, together with the group by. But it would seem we don't have achieve a desired result
This is our MySQL database:
$query = "SELECT in_model, COUNT(in_color) FROM in_newunit GROUP BY in_color,in_model";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)) {
echo "There are ". $row['COUNT(in_color)'] ."
". $row['in_model'] ." items.";
echo "<br />";
}
This is the output we are receiving
There are 1 C2I items.
There are 2 try items.
There are 2 try items.
There are 4 C2I items.
This is what we are trying to achieve
We are trying to have the color appear in the echo
There are 1 C2I Black items.
There are 2 try White items.
There are 2 try Black items.
There are 4 C2I White items.
I think this is straight enough. Try this.
$query = "SELECT in_model, in_color, count(*) AS counter FROM in_newunit GROUP BY in_model, in_color";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)) {
echo "There are ". $row['counter'] ." ". $row['in_model'] ." ".$row['in_color']." items.";
echo "<br />";
}
The query is actually the other way around:
SELECT in_color, count(*) FROM in_newunit
GROUP BY in_color
And you have actually said it yourself:
we are trying to count the total of model (in_model field), which has the same color (in_color field)
"count the total of model" > count(*)
"which has the same color" > for every color the previous count, which is a group by in_color
Also note that if you do count(in_model) you won't be counting values with in_model as null. If you do count(*) you will be counting the null values too. Up to you.
Update
So you want the amount of elements there are by (model, color) pair. Then this is the query:
SELECT in_model, in_color, count(*) FROM in_newunit
GROUP BY in_model, in_color
Eg:
model1 | black | 2
model1 | white | 1
model2 | black | 5
model3 | white | 4
I have a project on student ranking. The sample school does three terminal exam in a year. I created three different tables for each terminal exam, (i.e. firstermsar for first, secondtermsar for second, thirdtermsar for third term respectively).
Table structure is like this:
id studentid matca1 matca2 matexam engca1 engca2 engexam
1 2 15 14 40 12 10 60
2 1 10 5 56 9 13 35
3 4 11 9 45 14 17 40
4 5 14 1 50 20 0 60
Students with id 4 and 5 are in class1 while students 2 and 1 are in in class2. I have a seperate table for classes and also seperate table for student profile info.
Now, I already output the result slip but without ranks which looks like this
StudentId: 2 Full Name: Fawaz James Class: Pry 2 Overall Position: -
1stAss 2ndAss ExamScore Grade Position Rmks
Mark Obtainable 20 20 60
English 12 10 60 A - Excellent
Mathematics 15 14 40 B - V. Good
Though am a starter, and my code is a mess, its provided underneath. Moreover I need help with calculating the overall position on class basis and subject position on class basis. Please help me as my project depends on this for approval and am in my deadline week before I got introduced to stack Overflow.
$query = "SELECT * FROM firsttermsars
WHERE studentID=$stuID AND year=\"$_SESSION[year]\""; #27
$result = mysql_query($query)
or die ("Couldn't execute query.");
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<h2> First Term Stint Report Sheet for $namet during the SESSION $year</h2>";
echo "<table class='table1'>";
echo "<tr>";
echo "<td></td>";
echo "<td><b>1st Assessment</b></td><td><b>2nd Assessment</b></td>
<td><b>Mid Term<br />Exam</b></td><td><b>Mid Term<br />Total Score</b></td><td><b>Grade<br />Point</b></td></tr>";
echo "<tr> <td><b>Mark Obtainable</b></td><td><b>30</b></td><td><b>20</b></td><td><b>50</b></td><td><b>100</b></td><td><b>||=||</b></td></tr><tr>";
$tetal = $row['thirdengtest']+$row['thirdengexam']+$row['eng1'];
if ($tetal >="1") {
$q2 = "1";
$sq2 = "100";
echo "<td>English Language: </td>";
echo "<td>{$row['eng1']}</td><td>{$row['thirdengtest']}</td><td>{$row['thirdengexam']}</td> <td>";
$total1 = $row['thirdengtest']+$row['thirdengexam']+$row['eng1'];
echo $total1."</td><td>";
if ($total1>="75")
echo "A1";
elseif ($total1>="70")
echo "B2";
elseif ($total1>="65")
echo "B3";
elseif ($total1>="60")
echo "C4";
elseif ($total1>="55")
echo "C5";
elseif ($total1>="50")
echo "C6";
elseif ($total1>="45")
echo "D7";
elseif ($total1>="40")
echo "E8";
elseif ($total1>="1")
echo "F9";
elseif ($total1=="0")
echo "-";
else
echo "F";
echo "</td></tr>";
}
$tetal = $row['thirdmathtest']+$row['thirdmathexam']+$row['math1'];
if ($tetal >="1") {
$q3 = "1";
$sq3 = "100";
echo "<tr>";
echo "<td>Mathematics </td>";
echo "<td>{$row['math1']}</td><td>{$row['thirdmathtest']}</td> <td>{$row['thirdmathexam']}</td> <td>";
$total2 = $row['thirdmathtest']+$row['thirdmathexam']+$row['math1'];
echo $total2."</td><td>";
if ($total2>="75") echo "A1"; elseif ($total2>="70") echo "B2"; elseif ($total2>="65") echo "B3"; elseif ($total2>="60") echo "C4"; elseif ($total2>="55") echo "C5"; elseif ($total2>="50") echo "C6"; elseif ($total2>="45") echo "D7"; elseif ($total2>="40") echo "E8"; elseif ($total2>="1") echo "F9"; elseif ($total2=="0") echo "-"; else echo "F";
echo "</td></tr>";
}
?>
I hope you can help me like this, and hope is not complicated. I wanted to add the picture of the output but its complaining i don't have the required reputation
Firstly, in the database, make a new column for total('Total') and ('Grade'), and when you are submitting the marks details to your database, add the marks and grade and then submit the total to the database, instead of calculating them while retrieving the information.
Second, when you are selecting students from the database, use the SQL command:
SELECT * FROM firsttermsars ORDER BY Total DESC
More info on ORDER BY
This will automatically return the list of students in descending order of their total marks.
Similarly, for subject ranking, you can have a separate table, where ranking is done on subject marks
Then, while retrieving the data,
<table>
<?php
while ($i<$num) //num is number of rows
{
?>
<tr><td><?php echo mysql_result($result,$i,"Name"); ?></td>
//... Similarly for all clumns
<?php
}
?>
</table>
This should make the table with all students ranked according to their total marks. Similarly you can make a table for ranking by subject marks.
This is how i would solve the problem. Hope I helped!
Although i am late, but it may help some one.
First of All you should calculate Obtained marks and store in database table.
For simplicity i am showing just 3 column.
Id Name Obtained
1 A 100
2 B 58
3 L 88
4 F 102
5 C 99
Insert the row in table , then run this query to get Position of student.
SELECT id, name, Obtained, FIND_IN_SET( score, (
SELECT GROUP_CONCAT( score
ORDER BY score DESC )
FROM scores )
) AS position
FROM scores
WHERE id = 5
Detail here https://dba.stackexchange.com/questions/13703/get-the-rank-of-a-user-in-a-score-table
Basically, I have a MySQL database, and in it I have a table called "games", I figured out how to display all the games in the table using PHP, as you can see here:
http://rawgameshop.com/index/all/
Basically this is what it does:
while ($row = mysqli_fetch_array($result))
{
echo $row['Name']."<br />";
echo $row['Price']."<br />";
echo $row['Stock']."<br />";
echo $row['Platform']."<br />";
}
The table in the database is sorted by the "Names" value. What I want to accomplish is that I sort it by the "Price" value, descending from e.g. 9.99, so that I can use the "Games under €9.99" button on my front page.
So what's your SQL Query? You could just add ORDER BY clause to it.
SELECT * from games ORDER BY Price DESC
And for all games under 9.99, you could do
SELECT * from games where Price < 10 ORDER BY Price DESC
You left out the part where you create your query. But your query should be:
SELECT *
FROM games
WHERE price < 10 ORDER BY price DESC
In PHP:
if (result = mysqli->query('SELECT * FROM games WHERE price < 10 ORDER BY price DESC')) {
//print out all games here
result->close();
}