Hello i'm a (beginning) php backend dev and i'm working on a dj panel but it doesnt work the right way i tried as many things as i could but i cant get it to work..
$active_ids = '1, 3, 4';
$query = "SELECT * FROM users WHERE id IN ({$active_ids})";
$result = $mysqli->query($query);
$query2 = "SELECT dj, count(*) AS n FROM timetable WHERE dj IN ({$active_ids}) GROUP BY dj";
$result2 = $mysqli->query($query2);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td>", $row['username'] ,"</td>";
}
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()){
echo "<td>", $row2['n'] ,"</td>";
echo "</tr>";
}
}
}
this is what it shows
ZOMBOY
Hater
ZOMBOY2 3
1
1
and this is how it needs to become but i cant find a way to do it
ZOMBOY 3
Hater 1
ZOMBOY2 1
You can use join instead to querying two table
$active_ids = '1, 3, 4';
$query = "SELECT u.username, count(*) AS n FROM users u, timetable tt WHERE u.id=tt.dj and u.id IN ({$active_ids}) GROUP BY tt.dj";
$result = $mysqli->query($query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td>", $row['username'] ,"</td>";
echo "<td>", $row['n'] ,"</td>";
echo "</tr>";
}
}
You can do it like this, but must be look Joins
$active_ids = '1, 3, 4';
$query = "SELECT * FROM users WHERE id IN ({$active_ids})";
$result = $mysqli->query($query);
$query2 = "SELECT dj, count(*) AS n FROM timetable WHERE dj IN ({$active_ids}) GROUP BY dj";
$result2 = $mysqli->query($query2);
$columnOne = Array();
$columnTwo = Array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
$columnOne[]= $row['username'];
}
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()){
$columnTwo[] = row2['n'];
}
}
}
echo '<table>';
for($i=0;$i<count($columnOne);$i++){
echo '<tr><td>' . $columnOne[$i] . '</td><td>' . $columnTwo[$i] . '</td></tr>';
}
echo '</table>';
You could try something such as this (Not quite as elegant as others):
# Escape your characters
$active_ids = "'1', '3', '4'";
# Tidy up the querys to reduce the change of reserved words being used
$query = "SELECT * FROM `users` WHERE `id` IN ({$active_ids});";
$result = $mysqli->query($query);
$query2 = "SELECT `dj`, COUNT(*) AS n FROM `timetable` WHERE `dj` IN ({$active_ids}) GROUP BY `dj`";
$result2 = $mysqli->query($query2);
# Count your results
$c1 = count($result);
$c2 = count($result2);
#Set the counter to be the larger of the 2
$counter = (($c1 > $c2) ? $c1 : $c2);
if ($result->num_rows > 0 && $result2->num_rows > 0)
{
# Print the table opener
print '<table class="your_class">';
# Loop through your results
for ($i = 0; $i < $counter; $i++)
{
# Print the data needed
print '<tr><td>' . $result[$i]['username'] . '</td><td>' . $result2[$i]['n'] . '</td></tr>';
}
# End the table
print '</table>';
}
Related
Hi am retreiving the distinct dates then am trying to retreive the rows with that dates but am getting them as zero..
Here is code
$sql = "SELECT DISTINCT date FROM video_data ORDER BY id DESC";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$date = $row['date'];
echo "<tr>";
echo "<td>$date</td>";
$query_order = "SELECT * FROM video_data WHERE date=$date";
$query_order_total = mysqli_query($connection,$query_order);
var_dump($query_order);
$total = mysqli_num_rows($query_order_total);
echo "<td>$total</td>";
echo"<tr>";
But when am selecting "SELECT * FROM video_data" its showing correct count
you are missing ' ' around $date
$query_order = "SELECT * FROM video_data WHERE date='$date'";
That said: when you have, let's say, 60 dates found in your first query, you will than fire 60 new queries to find the actual data.
how about:
$sql = "SELECT COUNT(*) total, date
FROM video_data
GROUP BY date
ORDER BY date DESC";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>". $row['date'] ."</td>";
echo "<td>". $row['total']. "</td>";
echo"<tr>";
}
I'm trying to loop a mysql query and display every result on new row. I know i'm missing some pices, do i need to SET variable in mysql instead?
$value = array(B1,B2,B3,B4);
$query = "SELECT SUM($value[0]) AS ".$value[0]."_SUM, "
."SUM(answer_value) AS ".$value[0]."_ANSWER_SUM "
."FROM questions q JOIN answers a ON q.question_id = a.question_id "
."WHERE $value[0]=1";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result)) {
echo "<tr><td>".$value[0]."</td>";
echo "<td>".$row['B1_ANSWER_SUM']."</td>";
echo "<td>".$row['B1_SUM'] ."</td>";
echo '</tr>' ;
}
I would like to get this result:
B1 B1_ANSWER_SUM B1_SUM
B2 B2_ANSWER_SUM B2_SUM
B3 B2_ANSWER_SUM B2_SUM
You need to loop through your array and query each item in the array. See below:
$value = array('B1', 'B2', 'B3', 'B4');
foreach ($value as $v) {
$query = "SELECT SUM($v) AS ".$v."_SUM, SUM(answer_value) AS ".$v."_ANSWER_SUM FROM questions q JOIN answers a ON q.question_id = a.question_id WHERE $v=1";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result)) {
echo "<tr><td>".$v."</td>";
echo "<td>".$row[$v.'_ANSWER_SUM']."</td>";
echo "<td>".$row[$v.'_SUM'] ."</td>";
echo '</tr>' ;
}
}
Yesterday I asked a question about array, with commas, output as column.
Check it here
Today I have come this far:
<?php require_once("dbconnection.php"); ?>
<?php
$sql = "SELECT amount, ingredients FROM opskriftreg LIMIT 1;";
$ingredients = explode(',', $row['ingredients']);
$amount = explode(',', $row['amount']);
echo '<table>';
for ($i = 0; $i < count($ingredients); $i++) {
echo '<tr>';
echo '<td>', $ingredients[i], '</td>';
echo '<td>', $amount[i], '</td>';
echo '</tr>';
}
echo '</table>';
?>
The site shows an empty table now, but no errors, no nothing.
Anybody got any ideas??
You didn't executed the query..
$sql = "SELECT amount, ingredients FROM opskriftreg LIMIT 1;";
// $result = mysqli_query($mysqli_link, $sql);
// $row = mysqli_fetch_array($result);
$ingredients = explode(',', $row['ingredients']);
$amount = explode(',', $row['amount']);
Go to mysqli_query for more help.
i have tags on my articles and i wanted to make a tag cloud for it but i can't figure it out how to do that
any help would be nice
$result = mysql_query("SELECT *, COUNT(login_news.tag) FROM login_tags
LEFT JOIN login_news ON login_tags.tag_id = login_news.tag GROUP BY tag_id
");
while($row = mysql_fetch_array($result)){
echo $row['name'];
echo "<br>";
echo $row['COUNT(login_news.tag)'];
echo "<br>";
}
this is as much as i could guess
Try this, add mysql alias to count(login_news.tag) by adding AS and called it in php as $result['AliasName']
$result = mysql_query("SELECT *, COUNT(login_news.tag) AS tag_count FROM login_tags
LEFT JOIN login_news ON login_tags.tag_id = login_news.tag GROUP BY tag_id
");
while($row = mysql_fetch_array($result)){
echo $row['name'];
echo "<br>";
echo $row['tag_count'];
echo "<br>";
}
this is how i did it -_-
$result = mysql_query("SELECT tag_id, tag_name, COUNT(login_news.tag) AS tag_count FROM login_tags
INNER JOIN login_news ON login_tags.tag_id = login_news.tag GROUP BY tag_name
");
while($row = mysql_fetch_array($result)){
if($row['tag_count'] > 5){
$fontsize = "11";
}
if ($row['tag_count'] > 15){
$fontsize = "13";
}
if ($row['tag_count'] > 30){
$fontsize = "15";
}
?>
<li style="font-size:<?php echo $fontsize?>!important;"><? echo $row['tag_name']; ?> </li>
<?
}
?>
I have this query for counting the number of items in a category:
SELECT category, COUNT(*) AS category_count FROM users GROUP BY category
Which creates results looking like:
category category_count
========== ================
X 3
Y 2
Now, In PHP I want to display the counts of the categories. For example, I might want to echo the count from category X, how would I do it?
Thanks in advance
Assuming $result holds the result of your query:
while ($row = mysql_fetch_array($result))
{
echo 'Category: ' . $row['category'];
if ($row['category'] == 'X')
{
echo ' Count: ' . $row['category_count'];
}
echo '<br/>';
}
$res = mysql_query("SELECT category, COUNT(*) AS category_count FROM users GROUP BY category");
while($row = mysql_fetch_assoc($res)){
echo $row['category'].": ".$row['category_count']."<br/>";
}
$result = mysql_query("SELECT category, COUNT(*) AS category_count FROM users GROUP BY category");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
{
if ( $row['category'] == 'x' )
{
echo $row['category_count'];
}
}
while ($row = mysql_fetch_array($result))
{
echo 'Category: ' . $row['category'] . ' Count:' . $row['category_count'];
echo "<br>";
}
It would be better if you use the where clause in your query.
SELECT COUNT(*) AS category_count FROM users WHERE category = 'x'
$conn = mysql_connect("address","login","pas s");
mysql_select_db("database", $conn);
$Var = mysql_query("query");
While ($row = mysql_fetch_assoc($var) { echo $var["column"] }.