articles tag cloud php mysql - php

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>
<?
}
?>

Related

How can i make a table while fetch assoc

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>';
}

JOIN the same column in the same table - MYSQL

I have this tables:
TABLE addonlist_final
TABLE addons
First, I have to JOIN the same addon_id so I can get all the needed details(which I've already done). Then I want to JOIN the column with the same identity(addon_id) in the table then add the quantity to each other.
So far I have this code:
<?php
include("conn.php");
echo "<table border='1' >";
echo "<tr>";
echo "<th>Description</th>";
echo "<th>Price</th>";
echo "<th>Qty</th>";
echo "<th>Total Cost</th>";
echo "</tr>";
$totaldue = 0;
$currentaddons = mysql_query("SELECT a.*, af.*
FROM addons AS a, addonlist_final AS af
WHERE a.addon_id = af.faddon_id and af.ftransac_id='2685'
ORDER BY af.timef DESC
");
while($rows = mysql_fetch_assoc($currentaddons)){
$desc = $rows['description'];
$price = $rows['price'];
$qty = $rows['quantity'];
$totalcost = $price * $qty;
$totaldue += $price * $qty;
echo "<tr>";
echo "<td>$desc</td>";
echo "<td>$price</td>";
echo "<td>$qty</td>";
echo "<td>$totalcost</td>";
echo "</tr>";
}
echo "</table>";
echo "<h3>Total Due: ".number_format($totaldue)."</h3>";
?>
This shows me this:
What I want to show is:
Is this possible with just one query? Thanks in advance :)
You uave to use group by
SELECT a.description, sum(af.quantity) as quantity,price
FROM addons AS a, addonlist_final AS af
WHERE a.addon_id = af.faddon_id and af.ftransac_id='2685'
group by a.description
ORDER BY af.timef DESC

how to repeat entire table in while loop

I have a code which repeats rows while loop, but how can i repeat the entire table in while loop.
The code is
$kandivli = mysql_query("SELECT ps_order_detail.product_name, sum(ps_order_detail.product_quantity) AS product_total FROM ps_order_detail JOIN ps_product ON ps_product.id_product = ps_order_detail.product_id JOIN ps_orders ON ps_orders.id_order = ps_order_detail.id_order WHERE ps_product.id_supplier = '" . $supplier_number . "' && ps_orders.current_state = '" . $status1 ."' && ps_order_detail.id_shop = 1 GROUP BY ps_order_detail.product_name");
$ktotal = mysql_query("SELECT sum(ps_order_detail.product_quantity) AS product_total FROM ps_order_detail JOIN ps_product ON ps_product.id_product = ps_order_detail.product_id JOIN ps_orders ON ps_orders.id_order = ps_order_detail.id_order WHERE ps_product.id_supplier = '" . $supplier_number . "' && ps_orders.current_state = '" . $status1 ."' && ps_order_detail.id_shop = 1");
$krow = mysql_fetch_array($ktotal);
$kftotal = $krow['product_total'];
if(mysql_num_rows($kandivli) > 0)
{
echo '<table border="1" align="center" class="total">';
echo "<th> Product Name </td><th> Total Quantity </th>";
while($kandivlirow = mysql_fetch_array($kandivli))
{
$kandivli_product = $kandivlirow['product_name'];
$kandivlitotal = $kandivlirow['product_total'];
echo '<tr>';
echo "<td align='center'>$kandivli_product</td><tdalign='center'>$kandivlitotal</td>";
echo '</tr>';
}
echo "<td align='center'><b>Total</b></td><td align='center'><b>$kftotal</b></td>";
echo '</table>';
}
else echo "<div align='center'>No Pending orders</div>";
?>
Right now i just have 5 shops but if i add a new shop every time i have to copy paste and change this code to && ps_order_detail.id_shop = 6 then && ps_order_detail.id_shop = 7 and so on
So please if anyone shows how to repeat table. It would be grateful.
Hope my question is clear. (I want to repeat the entire table)
Regards
Amod
Ok I have Edited the code Now i get 6 tables as expected but the rows show the same data of shop one.
<?php
session_start();
include_once "connect.php";
?>
<?php
$supplier = mysql_query("SELECT ps_order_detail.product_name, sum(ps_order_detail.product_quantity) AS product_total, ps_supplier.name FROM ps_order_detail JOIN ps_product ON ps_product.id_product = ps_order_detail.product_id JOIN ps_supplier ON ps_supplier.id_supplier = ps_product.id_supplier JOIN ps_orders ON ps_orders.id_order = ps_order_detail.id_order WHERE ps_orders.current_state = 4 GROUP BY ps_order_detail.id_shop");
$alltotal = mysql_query("SELECT sum(ps_order_detail.product_quantity) AS producttotal FROM ps_order_detail JOIN ps_product ON ps_product.id_product = ps_order_detail.product_id JOIN ps_orders ON ps_orders.id_order = ps_order_detail.id_order WHERE ps_orders.current_state = 4 && ps_order_detail.id_shop = 11");
$ct = mysql_fetch_array($alltotal);
$ct1 = $ct['producttotal'];
while($supplierrow = mysql_fetch_array($supplier))
{
$kandivli = mysql_query("SELECT ps_order_detail.product_name, sum(ps_order_detail.product_quantity) AS product_total,ps_supplier.name FROM ps_order_detail JOIN ps_product ON ps_product.id_product = ps_order_detail.product_id JOIN ps_supplier ON ps_supplier.id_supplier = ps_product.id_supplier JOIN ps_orders ON ps_orders.id_order = ps_order_detail.id_order WHERE ps_orders.current_state = 4 GROUP BY ps_order_detail.product_name");
echo '<table border="1" align="center" class="total">';
while($kandivlirow = mysql_fetch_array($kandivli))
{
$sup = $kandivlirow['name'];
$sp = $kandivlirow['product_name'];
$spt = $kadivlirow['product_total'];
echo '<tr>';
echo "<td align='center'>$sp</td><td align='center'>$spt</td><td align='center'>$sup</td>";
echo '</tr>';
}
echo '</table>';
echo '<br>';
}
echo "<td align='center'><b>Total</b></td><td align='center'><b>$ct1</b></td>";
?>
while($kandivlirow = mysql_fetch_array($kandivli))
{
//Your table code
echo '<table border="1" align="center" class="total">';
//Middle section
echo '</table>';
}

PHP MySQL Display counts

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"] }.

How to query ONLY 2nd level categories?

this is my code, it displays all the categories like
A
-A.1
-A.2
B
-B.1
-B.2
And now i want it displays only
A.1
A.2
B.1
B.2
Any idea?
function current_categories($m,$id){
$cat_array=array();
$sql = "select * from `category`";
$query = DB::Query($sql);
while($row = DB::fetch_array($query)){
$cat_array[] = array($row['id'],$row['name'],$row['classid'],$row['sort']);
}
if($id==""){
$id=0;
}
$n = str_pad('',$m,'-',STR_PAD_LEFT);
$n = str_replace("-"," ",$n);
for($i=0;$i<count($cat_array);$i++){
if($cat_array[$i][2]==$id){
echo "<li>"."".$cat_array[$i][1]."</li>";
current_categories($m+2,$cat_array[$i][0]);
}
}
}
I don't know what value you put in as classid for the toplevel classid values. NULL or -1 perhaps?
function second_level_categories(){
$sql = " SELECT lvl2.* FROM `category` as lvl1 "
." JOIN `category` as lvl2"
." ON lvl1.id = lvl2.parent_id"
." WHERE lvl1.parent_id=VALUE_FOR_TOP_LEVEL"
." ORDER BY lvl1.sort ASC, lvl2.sort ASC"
$result = DB::Query($sql);
while($category = DB::fetch_array($result)){
echo '<li><a href="/team/index.php?gid='.$category['id'].'">"';
echo $category['name'];
echo '</a></li>';
}
}

Categories