News comments count - php

I'm tired of searching for the solution my news comment system.
What i have.
I have 2 different mysql tables item (id, title, category_id, details) and comments (id, item_id, comment)
If i have single news then counting is fine like here in picture:
Single news
Code:
if (!empty($comments)) {
$comm = count($comments);
} else {
$comm = 0;
}
But if i use same code category view, result is:
Category view
If i use code:
$sqls = mysql_query("SELECT c.item_id,
COUNT(c.comment)
FROM comments c
RIGHT JOIN items i
ON c.item_id = i.id
GROUP BY c.item_id");
$comm = mysql_num_rows($sqls);
$smarty->assign('comm',$comm);
Result is :Some number of comments
How to make possible to see the Category View the correct number of comments?

TRY this example. The result is the same as the screenshot below but with some background colour to distinguish the difference. Of course you should change to your own connection and layout.
<?php
$db = new PDO('sqlite:news.db');//change to your own
$sql = $db->prepare("
SELECT item.id, item.title, item.details, comments.comment, COUNT(comments.id) AS NumberOfComment
FROM item LEFT JOIN comments ON item.id = comments.item_id
GROUP BY item.id ORDER BY item.id");
$sql->execute();
$row = $sql->fetchAll();
//get comment
$comment = $db->prepare("SELECT comment FROM comments WHERE item_id = ?");
foreach ($row as $rows){
echo "<br>";
echo "Title: ".$rows['title'];
echo "<br>";
echo "Details: ".$rows['details'];
echo "<br>";
echo "<i>".$rows['NumberOfComment']." comments </i>";
echo "<br>";
$comment->execute(array($rows['id']));
$comments = $comment->fetchAll();
echo '<div style="background-color:pink;">';
foreach ($comments as $showcomment){
echo $showcomment['comment'];
echo "<br>";
}
echo "</div>";
echo "<br>";
}
?>
example item table
example comment table
and the result is...

Related

Getting the value from ($_GET["Value"])

// PAGE ONE
This is the index page, Here I am printing out rows from a list of
movie´s with some basic info, title , year.
I am also adding edit and delete links to the movies,
these work by passing the row id's of that movie.
$sql = "SELECT c.* , d.* FROM category c , movies d WHERE c.ID=d.ID";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<div class='floatbox collection'><table><tr><th>Titel</th><th>regissör</th><th>År</th><th>Genre</th><th>Ändra</th><th>Ta bort</th>";
while($row = $result->fetch_assoc()) {
echo
"<tr><td>".$row["title"]."</td><td>".$row["director"]."</td><td>"
.$row["year"]."</td><td>".$row["category"]."</td>
<td><a href='edit.php?row=".$row["ID"]."'>justera</a></td>
// delete link to send ID to next page
<td><a href='delete.php?delete=".$row["ID"]."'>stryk</a></td>";
}
echo "</table></div>";
} else {
echo "0 results";
}
// PAGE TWO
Here I have my $row[ID] trough $_get by the link on the previous page,
I have checked the value by "dumping" so I know that the row ID is in that
variable: The thing is, How do I call that $ID in my sql statement?
I'm trying to delete by calling that ID on the table row.
// Variable with correct ID value
if(isset($_GET["ID"]))
if($_GET["ID"]) = $ID; // This doesn't work, Do I need to convert
the $get_ID to a variable with the ID that can be called in the statement?
or is my syntax wrong?
// Delete join
$sql = "SELECT * FROM movies, category
INNER JOIN category ON movies.ID = category.ID
DELETE WHERE movies.ID = '$ID'"; // No syntax works here
// Any help appreciated.
// konfirmering
if ($conn->query($sql) === TRUE) {
echo "Register struket <br>
<a href='panel.php'>Gå tillbaka</a>";
} else {
echo "Fel vid anslutning : " . $conn->error;
}
As per your code your delete.php would be like this:
// Variable with correct ID value
if (isset($_GET["delete"])) {
$ID = $_GET["delete"];
// Delete join
$sql = "DELETE FROM movies, category INNER JOIN category ON movies.ID = category.ID WHERE movies.ID = '$ID'";
// konfirmering
if ($conn->query($sql) === TRUE) {
echo "Register struket <br><a href='panel.php'>Gå tillbaka</a>";
} else {
echo "Fel vid anslutning : " . $conn->error;
}
}

Displaying name from an inner joined table?

I've got two tables, one called category, which has the rows id and name, and another called placecategory, which has the tables id, place_id and category_id. I need to inner join these two to echo out the names of the categories where the placecategory.place_id is equal to a $GET[ID].
I've got this so far, but it echo's out nothing.
<?php
include('includes/connectdb.php');
$id = mysqli_real_escape_string($dbc,$_GET['id']);
$qry = 'SELECT id, name FROM category
INNER JOIN placecategory
ON category.id = placecategory.category_id
WHERE placecategory.place_id = '.$id.'';
$result = mysqli_query($dbc,$qry);
while ($row = mysqli_fetch_array($result))
{
echo ''.$row['name'].'';
};
?>
This wont fix your query, but it will display the error generated by the incorrect query. Its a start.
I cannot solve the query issue without a better understanding of your schema.
<?php
include('includes/connectdb.php');
$id = mysqli_real_escape_string($dbc,$_GET['id']);
$qry = 'SELECT id, name
FROM category
INNER JOIN placecategory ON category.id = placecategory.category_id
WHERE placecategory.place_id = '.$id;
$result = mysqli_query($dbc,$qry);
// test the status before continuing
if ( ! $result ) {
echo mysqli_error($dbc);
exit;
}
while ($row = mysqli_fetch_array($result))
{
echo $row['name'];
}
?>

using for loop on mysql table

I have 2 tables on my db:
cat (id, catname)
link (id, name, url, cat)
I wanted to loop through the table link and output data by cat, here the actual code but id doesn't work anyway :)
for ($i = 1; ; $i++)
{
$list = $mysqli->query('SELECT * FROM links WHERE category='$i'');
while($row = $list->fetch_assoc()) {
print $row["category"];
print $row["name"].' ';
print $row["url"];
print '<br>';
}
}
$list->free();
Is there a way we can get the id from table cat and use that to loop through the data in table link?
Thanks
No need for so many requests inside the loop. There are JOIN statements in SQL for such cases:
$result = $mysqli->query('SELECT category.name AS cat_name,
links.name,
links.url
FROM category
INNER JOIN links ON links.cat=category.id');
while($row = $result->fetch_assoc()) {
print $row["cat_name"];
print $row["name"].' ';
print $row["url"];
print '<br>';
}
$result->close();

Msqli, how group "graphically" a query from 2 different tables

I have 2 tables:
table 1) courses_cat (this one have a "title" field)
table 2) courses (this one have a "parent" field which I want use to "group", this "parent" is equal to "title"
So I need to put "graphically" the content of the table 2 inside the table 1.
Here my code (separated for both tables):
<?php
$query = $db->prepare('SELECT title, body, showeb, datapost FROM courses_cat WHERE showeb="si"');
$query->execute();
$query->store_result();
$query->bind_result($title, $body, $showeb, $datapost);
while ($query->fetch())
echo " <div class='pblock'><h2>$title</h2><pre>$body</pre></div> ";
?>
<?php
$query2 = $db->prepare('SELECT parent, title, body, showeb, datapost FROM courses WHERE showeb="si"');
$query2->execute();
$query2->store_result();
$query2->bind_result($parent, $title, $body, $showeb, $datapost);
while ($query2->fetch())
echo " <div id='courseblock'><a href='#'><span class='top'>$title</span><span class='content'>$shortbody</span></a></div>";
?>
I'm not sure I understand 100% what the end result should be, but I'm sure you can merge your two queries into one; looping over this properly might solve your issue:
SELECT cat.title as CatTitle, cat.body as CatBody, courses.title as CourseTitle, courses.body as CourseBody -- and any other fields you need like that
FROM courses_cat cat, courses
WHERE cat.parent = courses.title
GROUP BY cat.title
I try the code posted by Digital Chris, and it works (in part), for explain better, the follow code (see below the edited one) "group ok the categories and put the items inside" but I need to get more of one Course (div "courseblock") into each Category (div "pblock") and this code print just one…(I think it "break the loop" and maybe need to JOIN the data of the "courses" into their "categories")
At this link can see an graphic example of what I mean:
http://liberartestudio.com/download/aaa.jpg
(the category blocks are those whites, the courses those red)
Here the link to the page:
http://lvdsys.liberartestudio.com/services_courses.php
<?php
$query = $db->prepare('
SELECT courses_cat.title AS CatTitle, courses_cat.body AS CatBody, courses_cat.datapost AS CatData, courses.parent, courses.title AS CourseTitle, courses.shortbody AS CourseShortbody, courses.datapost AS CourseData
FROM courses_cat, courses
WHERE courses_cat.title = courses.parent
GROUP BY parent
ORDER BY CatData AND CourseData
');
$query->execute();
$query->store_result();
$query->bind_result($CatTitle, $CatBody, $CatData, $parent, $CourseTitle, $CourseShortbody, $CourseData);
while ($query->fetch())
echo "
<div class='pblock'>
<h2>$CatTitle</h2><pre>$CatBody</pre>
<div id='courseblock'><a href='#'><span class='top'>$CourseTitle</span><span class='content'>$CourseShortbody</span></a>
</div>
</div>
"; ?>
Here my working code (simple version) for what I needed...
<?php
$cat = mysqli_query($db,"SELECT title AS catTitle, body AS catBody FROM courses_cat");
$result = mysqli_query($db,"SELECT parent, title, shortbody FROM courses");
//array x categories
$category = array(); while ($row = mysqli_fetch_assoc($cat)) {$category[] = $row;}
//array x courses
$course = array(); while ($row = mysqli_fetch_assoc($result)) {$course[] = $row;}
// here my divs
$cat_div="";
foreach ($category as $row) {
$cat_div = $row['catTitle'];
echo "<div class='pblock'>";
echo "<h2>".$row['catTitle']."</h2><pre>".$row['catBody']."</pre>";
foreach ($course as $row) {
if ($cat_div == $row['parent']) {
echo "<div id='courseblock'><a href='#'><span class='top'>".$row['title']."</span><span> class='content'>".$row['shortbody']."</span></a></div>";}}
echo "</div>";}
$cat->free_result();
$result->free_result();
mysqli_close($db);
?>

PHP/MySQL Query Results

Having an issue with a query..my wscategories stores the main categories of an online store, and my wssubcategories table stores the sub categories of the table with the foreign key called "parentcategory". I'm trying to list the main categories, with the subcategories underneath a la:
Tops
T-Shirts
Wovens
Sweaters
Pants
Shorts
Jeans
The query/result I wrote is the following:
$query = "SELECT DISTINCT a.categoryname AS maincategory, b.categoryname AS
smallcategory FROM wscategories a, wssubcategories b WHERE a.SECTION = 'girls' AND
b.parentcategory = a.id";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo $row['maincategory'];
echo "<br/>";
echo $row['smallcategory'];
echo "<br/>";
}
Which returns:
Tops
T-Shirts
Tops
Sweaters
Tops
Wovens
Etc. I want the script to just display the main category once, and then the subcategories underneath vs. displaying it multiple times. Any help?
try something like this
// note we need to order by maincategory for this to work
//
$query = "SELECT DISTINCT a.categoryname AS maincategory, b.categoryname AS
smallcategory FROM wscategories a, wssubcategories b WHERE a.SECTION = 'girls' AND
b.parentcategory = a.id
ORDER BY maincategory ASC,
smallcategory ASC
";
$result = mysql_query($query) or die(mysql_error());
// keep track of previous maincategory
$previous_maincategory = NULL;
while ($row = mysql_fetch_assoc($result))
{
// if maincategory has changed from previouscategory then display it
if ($previous_maincategory != $row['maincategory']) {
echo $row['maincategory'];
}
echo "<br/>";
echo $row['smallcategory'];
echo "<br/>";
// record what the previous category was
$previous_maincategory = $row['maincategory'];
}
Set a flag after you echo the main category, like this:
$echoed_main = false;
while ($row = mysql_fetch_array($result)) {
if (!$echoed_main) {
echo $row['maincategory'];
echo "<br/>";
$echoed_main = true;
}
echo $row['smallcategory'];
echo "<br/>";
}

Categories