I have a ranking system on my website and I would like it to display in a column rather than a row.
$result = mysql_query("SELECT *, ROUND(score/(1+(losses/wins))) AS performance FROM images ORDER BY ROUND(score/(1+(losses/wins))) DESC LIMIT 0,10");
while($row = mysql_fetch_object($result)) $top_ratings[] = (object) $row;
Related
I'm trying to create a website where users can vote on different items from a game, like a tierlist. I want the item that a user votes on to be from a random table and a random row in that table.
Right now, I have the following code set up to find the random item:
$ran = mt_rand(1, 10);
switch ($ran) {
case "1":$sql = "SELECT item, rating FROM ditems ORDER BY RAND() LIMIT 1";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo $row["gun"];
};
break;
case "2":$sql = "SELECT item, rating FROM citems ORDER BY RAND() LIMIT 1";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo $row["item"];
};
etc
The problem I run into is that I can't access what the chosen item is outside of the switch{} statement because the $row array is declared locally, but I have to be able to do that so that I can seet up a button to vote on the item.
EDIT: I decided to throw all items in a single table and pick a item using
ORDER BY RAND LIMIT 1
You can SELECT another column with a text constant to denote what entity is selected:
$ran = mt_rand(1, 10);
switch ($ran) {
case "1":$sql = "SELECT 'ditem' as entity, item, rating FROM ditems ORDER BY RAND() LIMIT 1";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo $row["gun"];
};
break;
case "2":$sql = "SELECT 'citem' as entity, item, rating FROM citems ORDER BY RAND() LIMIT 1";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo $row["item"];
};
etc
I have the following code which selects information from one random row.
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 1 ");
while($rows = mysql_fetch_array($query))
{
$question = $rows['question'];
$hint = $rows['hint'];
$level = $rows['level'];
$keyword = $rows['keyword'];
$showme = $rows['showme'];
$picture_path = $rows['picture_path'];
}
This works well for me but I now I need to be able to select two more DIFFERENT pictures from the picture_path column and assign them to variables. Again, all three rows need to be different.
Any tips for a newbie on how to do this?
Just change your query as follows:
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 3");
You are ordering by ORD() so it will give you different records.
No, new modification, just change the limit to 3 (whatever your need).
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 3");
As your are already getting random values with order by clause it will always return different values so you just need to edit your limit value and you are done!
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 3");
Can anyone tell me what am I doing wrong. I would like to display the last 5 rows in desc order.
$pull_activity_logs = mysql_query("SELECT * FROM activity_logs WHERE ac_no = '$logined_acc' order by id desc limit 0,5")
or die(mysql_error());
while($row = mysql_fetch_array( $pull_activity_logs )) {
$activity_time = $row["datetime"];
$activity = $row["activity"];
}
echo "$activity";
Help would be deeply appreciated
Well, you can always select the first five in asc order, that would be last five in desc order, and then you could reverse their order in php if needed (5 values isnt anything what an array couldn't handle)
CODE:
$pull_activity_logs = mysql_query("SELECT * FROM activity_logs WHERE ac_no = '$logined_acc' order by id asc limit 5");
$temp_arr = array();
while($row = mysql_fetch_array( $pull_activity_logs )) {
$temp_arr[] = $row; //add to end
}
$final_arr = array_reverse($temp_arr);
foreach($final_arr as $row) //this doesn't have to be named $row
$activity_time = $row["datetime"];
$activity = $row["activity"];
echo "$activity";
}
EDIT:
now when i look at it maybe whole problem was in wring position of your echo:
$pull_activity_logs = mysql_query("SELECT * FROM activity_logs WHERE ac_no = '$logined_acc' order by id desc limit 0,5")
or die(mysql_error());
while($row = mysql_fetch_array( $pull_activity_logs )) {
$activity_time = $row["datetime"];
$activity = $row["activity"];
echo "$activity"; //this goes INSIDE the loop
}
You can retrieve the first five in ascending order and then order them in SQL by using a subquery:
select al.*
from (SELECT al.*
FROM activity_logs al
WHERE ac_no = '$logined_acc'
order by id asc
limit 0, 5
) al
order by id desc;
I'm trying to get a random value from an array that is populated with data from the database. I'm selecting some products from the DB and display them on the front page, but i need to display different (random) producs everytime the page is reloaded.
$row = mysql_query("SELECT * FROM anunt WHERE lichidareStoc = 0 ORDER BY anuntID DESC") or die(mysql_error());
$row2 = mysql_fetch_assoc($row);
So, i think that $row2 is now an array, and has all the infos that i selected previously from the database. How do i select a random 'row' from that array now?
Thanks
You are looking for array_rand, see: http://php.net/manual/en/function.array-rand.php
Example:
$rand_key = array_rand($row2);
echo $row2[$rand_key]; // your random
You could also directly select a random row from your DB:
SELECT *
FROM anunt
WHERE lichidareStoc = 0
ORDER BY rand()
LIMIT 1
But be aware, this will reduce your performance, especially on bigger tables.
Side Note: mysql_* function are deprecated, use mysqli_* instead.
If you want to select a random entry, you could do something like this:
$myRows=array();
$row = mysql_query("SELECT * FROM anunt WHERE lichidareStoc = 0 ORDER BY anuntID DESC") or die(mysql_error());
while($row2 = mysql_fetch_assoc($row))
$myRows[]=$row2;
$randomEntry=$myRows[array_rand($myRows)];
If you wanted a random field of that entry though, you should use array_rand like Bernhard Poiss pointed out.
$row = mysql_query("SELECT * FROM anunt WHERE lichidareStoc = 0 ORDER BY anuntID DESC") or die(mysql_error());
$row2 = mysql_fetch_assoc($row);
$randomField=$row2[array_rand($row2)];
You will first want to populate an array of your results
$row = mysql_query("SELECT * FROM anunt WHERE lichidareStoc = 0 ORDER BY anuntID DESC") or die(mysql_error());
$rows = array();
while($rec = mysql_fetch_assoc($row)){
$rows[] = $rec;
}
Then you can select a random item using array_rand
$random = $rows[array_rand($rows)];
Can anyone tell me how to display the other values, when a query is limited my 3. In this question I asked how to order and limit values, but now I want to show the others in another query. How would I go about doing this?
Here's the code I used before:
$query = "SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
}
If display all rows use like this :
$query = "SELECT gmd FROM account ORDER BY gmd DESC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
}
If display all rows without that 3 rows use like this :
$query = "SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3,1000000";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
}
if you LIMIT the resultset in the query you only get 3 rows returned.
if you want to show the rest, don't limit inside the query, but check the rowcount in your php loop
SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3,9999999999
or may be you need pagination
SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3,10
Will skip first 3 values and display next 10 ones satisfying the condition. This is MySQL only solution though.