How to query ONLY 2nd level categories? - php

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

Related

undefined index by selecting data from two tables with a common column

I have two tables - home and posts;
home.artid is equal to posts.id.
Want select id, artid, inde from home PLUS title from posts
where home.pos is slider:
$items = "";
$st = $db->query("select home.id, home.artid, home.inde
from home
join posts on home.artid = posts.id
where home.pos = 'slider'
order by home.inde asc");
while ($row = $st->fetch()){
$items .= "<div class='slidertitle'>" . $row['posts.title'] . "</div>\n";
}
echo $items;
Error:
Undefined index posts.title...
Any help?
If you want to select posts.title then select it
$st = $db->query("select home.id, home.artid, home.inde,
posts.title
from home
join posts on home.artid = posts.id
where home.pos = 'slider'
order by home.inde asc");
// and it would be called just `title`
while ($row = $st->fetch()){
$items .= "<div class='slidertitle'>" . $row['title'] . "</div>\n";
}
echo $items;

SUM on each line in a SELECT statement

I have a problem with a join statement with a SUM on a field.
I have these two Tables:
Table 1 (parent): Trips
ID - int( 11 ) - Auto increment
Maxspots - int( 11 )
Table 2 (child): Bookings
ID - int( 11 ) - Auto increment
FK_ID - int( 11 )
Spots - int( 11 )
I have the following code:
$SQL = "SELECT ";
$SQL .= "SUM(Bookings.Spots) as SUMSPOT, Trips.ID, Bookings.FK_ID, Bookings.ID ";
$SQL .= "FROM Trips ";
$SQL .= "INNER JOIN Bookings on Bookings.FK_ID = Trips.ID";
$SQL_Query = mysql_query($SQL);
$tdOdd = 'style = "background-color: #FFFFFF;"';
$tdEven = 'style = "background-color: #ced4ee;"';
echo "<table>";
echo "<tr>";
echo "<th>Trip nr</th>";
echo "<th>Sum of spots</th>";
echo "</tr>";
$indx = 1;
while ($row = mysql_fetch_array($SQL_Query, MYSQL_ASSOC)) {
$Tripnr = $row['FK_ID'];
$Sumspots = $row['SUMSPOT'];
if (($indx % 2) == 1) {$rowClass = $tdOdd; } else { $rowClass = $tdEven; }
echo "<tr ".$rowClass.">";
echo "<td>".$Tripnr."</td>";
echo "<td>".$Sumspots."</td>";
echo "</tr>";
$indx++;
}
echo "</table>";
mysql_free_result($SQL_Query);
Its returning only one Booking-line with the total sum of all spots (within the join). I need it to give me the sum of the Booking.Spots of each line. Help anyone?
Add the GROUP BY for your non-aggregate fields.
$SQL = "SELECT ";
$SQL .= "SUM(Bookings.Spots) as SUMSPOT, Trips.ID, Bookings.FK_ID, Bookings.ID ";
$SQL .= "FROM Trips ";
$SQL .= "INNER JOIN Bookings on Bookings.FK_ID = Trips.ID";
$SQL .= "GROUP BY Trips.ID, Bookings.FK_ID, Bookings.ID";

articles tag cloud php mysql

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

SQL statement updates only one user's data

<?php
function get_user_id($username) {
return mysql_result(mysql_query("Select id From users Where username = '" . mysql_real_escape_string($username) . "'"), 0);
}
$sql = "select * from rating
WHERE user_id=" . get_user_id($myusername) . "
ORDER BY punkte ASC";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
$catid = $row['rating_id'];
$catname = $row['song_id'];
echo "<li id='item_$catid' class='ui-state-default'><span class='ui-icon ui-icon-arrowthick-2-n-s'></span>$catname</li>";
}
?>
UPDATE:
Sorry, I found the mistake, it was quite stupid:
$catid = $row['rating_id'];
$catname = $row['song_id'];
It should be:
$catid = $row['song_id'];
$catname = $row['song_name'];
So, thanks to all! As always: You can't figure it out before you post a question to Stackoverflow :)
For your new question, you can solve it with a inner join:
select s.song_name from rating r
inner join songs s on s.song_name_id = r.song_id
Inner join
http://www.w3schools.com/sql/sql_join_inner.asp
Yes,
$catid = $row['rating_id']; //rating id is not related to song
TO
$catid = $row['song_id'];
$catname = $row['song_name'];

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

Categories