With this code:
$query = "select users.id_user, users.full_name, users.rights, users.group, group.id, group.name
from users
inner join group
on users.group=group.id
where users.rights= 2";
// connection code
while ($re = mysql_fetch_array($res))
{
$id_user = $re["id_user"];
$id_group = $re["id"];
$full_name= $re["full_name"];
$group_name= $re["name"];
echo "<div>";
echo "<p><strong>$group_name</strong></p>";
echo "$full_name<br />";
echo "</div>";
}
?>
I get this:
Group1
User1
Group1
User2
Group2
User3
Group2
User4
and I need it like this:
Group1
User1
User2
Group2
User3
User4
How to show group name just once?
Tweak your query that you use the database power to give you the information you need.
select
usergroup.name
, GROUP_CONCAT(user.name SEPARATOR '<br />') 'users'
from
user
inner join
usergroup
on
user.usergroup_id = usergroup.id
where
user.rights = 2
group by
usergroup.id
see demo http://sqlfiddle.com/#!2/0d104/1 in your php you can use $row['users']
You may use associative arrays for this task. For example, make an array $groups, and in your while loop do something like
$groups[$re["name"]]["full_name"][] = $re["full_name"];
Then, when you display your html loop through $groups and inside it loop though full names.
This is what I came up with:
$query = "SELECT users.id_user, users.full_name, users.rights, users.group, group.id, group.name
FROM users
INNER JOIN group
ON users.group=group.id
WHERE users.rights=2
ORDER BY group.name ASC";
// connection code
$group_name = '';
while ($re = mysql_fetch_array($res))
{
if( $group_name === '' )
{
$group_name = $re["name"];
echo "<div>";
echo "<p><strong>$group_name</strong></p>";
}
elseif( $group_name !== $re["name"] )
{
$group_name = $re["name"];
echo "</div>";
echo "<div>";
echo "<p><strong>$group_name</strong></p>";
}
$id_user = $re["id_user"];
$id_group = $re["id"];
$full_name= $re["full_name"];
echo "$full_name<br />";
}
?>
It's basically ordering the result by the group names, and while it iterates, the group name is being checked. If it's the same, only the name is printed, if it differs, the div is closed, a new one is made and the new group title is echoed.
Related
I have to check if a value is present in two tables, through a left join, if there is a relationship write yes, otherwise no
every time I associate an evidence (tev_Evidenze) to the structure (tev_Tipi_accreditamento) the query should tell me for that structure there is evidence, only now at structure n6. no evidence is present and anyway I answer yes
code:
<?php
$CONTROLLA = mysqli_query($riskmanagement,
"SELECT * FROM tev_Tipi_accreditamento LEFT JOIN tev_Evidenze
ON tev_Tipi_accreditamento. ID_tipo_acc = tev_Evidenze.id_tipo_accreditamento
WHERE tev_Tipi_accreditamento.id_struttura = tev_Evidenze.id_struttura GROUP BY tev_Evidenze.id_struttura");
$EVIDENZE=mysqli_num_rows($CONTROLLA);
if($EVIDENZE==0) {
echo "SI";
}else{
echo "no";
}
?>
The cycle is correct I am sure that in the db, there are no values present in both tables but my if does not seem to work
NOTE:
<?php
$query_string = "SELECT * FROM tev_Tipi_accreditamento LEFT JOIN tev_Evidenze
ON tev_Tipi_accreditamento. ID_tipo_acc = tev_Evidenze.id_tipo_accreditamento
WHERE tev_Tipi_accreditamento.id_struttura = tev_Evidenze.id_struttura GROUP BY tev_Evidenze.id_struttura";
$query = mysqli_query($riskmanagement, $query_string);
?>
<?php
while($row = mysqli_fetch_assoc($query)){ ?>
<?php echo $row['id_struttura'] ;
if($query_string==0) {
echo "SI";
}else{
echo "no";
}?>
<?php } ?>
You're counting ALL the matches, not the number of matches for each structure. You can use the following:
SELECT s.id_struttura, IF(COUNT(e.id_struttura) > 0, 'Yes', 'No') AS matches
FROM tev_Tipi_accreditamento AS s
LEFT JOIN tev_Evidenze AS e ON s.id_struttura = e.id_struttura AND s.ID_tipo_acc = e.id_tipo_accreditamento
GROUP BY s.id_struttura
This will return a table like:
1 Yes
2 No
3 No
4 Yes
for each structure ID.
DEMO
You can show the result with:
$CONTROLLA = mysqli_query($riskmanagement, "
SELECT s.id_struttura, IF(COUNT(e.id_struttura) > 0, 'Yes', 'No') AS matches
FROM tev_Tipi_accreditamento AS s
LEFT JOIN tev_Evidenze AS e ON s.id_struttura = e.id_struttura AND s.ID_tipo_acc = e.id_tipo_accreditamento
GROUP BY s.id_struttura") or die(mysqli_error($riskmanagement));
echo "<table>";
while ($row = mysqli_fetch_assoc($CONTROLLA)) {
echo "<tr><td>{$row['id_struttura']}</td><td>{$row['matches']}</td>
}
echo "</table>";
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...
I'm creating a website that COUNTS all the occurences of something.
Table:
Pizza John
Google John
Pizza Harry
Lets say you like google and pizza. It will show it like this:
John, John, Harry.
I would like it to appear like this:
John, Harry.
What am I doing wrong? Here's my code. Does it have anything to do with the while loop inside the while loop? How could I make it appear the way I want it to.. I can't figure it out.
$people_like = mysqli_query ($link,
"SELECT user_liked_by FROM (
SELECT COUNT(user_liked_by) AS total, id, user_liked_by
FROM whatilike WHERE whats_liked LIKE '%$thing_liked%' &&
user_liked_by !='$user'
GROUP BY user_liked_by
ORDER BY COUNT(user_liked_by) DESC ) as names;
");
while ($rpl = mysqli_fetch_assoc ($people_like)) {
$person_like_u = $rpl ['user_liked_by'];
$pluq = mysqli_query ($link, "SELECT profile_pic FROM users WHERE
username='$person_like_u'");
while ($pluqr = mysqli_fetch_assoc ($pluq)) {
$pli = $pluqr ['profile_pic'];
echo "<a href='profile.php?u=$person_like_u' >
<img src='data/user_photos/$pli' height='50px' width='50px'
style='border-radius:400px;' id='ioplu' ></a>";
}
}
$people = [];
while ($rpl = mysqli_fetch_assoc ($people_like)) {
$people[] = $rpl ['user_liked_by'];
}
$people = array_unique(array_map("trim",$people));
foreach($people as $p){
$pluq = mysqli_query ($link, "SELECT profile_pic FROM users WHERE
username='$p'");
while ($pluqr = mysqli_fetch_assoc ($pluq)) {
$pli = $pluqr ['profile_pic'];
echo "<a href='profile.php?u=$person_like_u' >
<img src='data/user_photos/$pli' height='50px' width='50px'
style='border-radius:400px;' id='ioplu' ></a>";
}
}
I have an Excel sheet which is converted to csv and then code imports that into mysql.
In Excel I have in first row A's and B's
I get output, It works but I cannot get into displaying data right.
How I'm trying to display is A B then break and again A B and so on. It is ordered by contact_id but I only get AAAAAA's and 1 B if I try double while.
This is the code with which I have tried to achieve this. Tried double while and I have googled and I have noticed double while is not working, Couldn't get it to work with mysql JOIN either and have never worked with foreach function.
$data = dbquery("SELECT contact_id, contact_first, contact_last, contact_email FROM contact_info WHERE contact_first='A)' ORDER BY contact_id ASC");
$data2 = dbquery("SELECT contact_id, contact_first, contact_last, contact_email FROM contact_info WHERE contact_first='B)' ORDER BY contact_id ASC");
while ($userdata = dbarray($data)) {
echo "<li><ul class='ulcla'>";
echo "<h1>".$userdata['contact_first']."</h1>";
echo "<li>".$userdata['contact_last']."</li>";
echo "</ul><br>";
while ($userdata = dbarray($data2)) {
echo "".$userdata['contact_id']." - ".$userdata2['contact_first']." - ".$userdata2['contact_last']." - ".$userdata2['contact_email']."";
echo "<br>";
}
echo "</ol></div>";
}
How could I loop data like that?
Change while ($userdata = dbarray($data2)) { to while ($userdata2 = dbarray($data2)) {for innner while loop
$data = dbquery("SELECT contact_id, contact_first, contact_last, contact_email FROM contact_info WHERE contact_first='B)' ORDER BY contact_id ASC");
$data2 = dbquery("SELECT contact_id, contact_first, contact_last, contact_email FROM contact_info WHERE contact_first='B)' ORDER BY contact_id ASC");
while ($userdata = dbarray($data)) {
echo "<li><ul class='ulcla'>";
echo "<h1>".$userdata['contact_first']."</h1>";
echo "<li>".$userdata['contact_last']."</li>";
echo "</ul><br>";
while ($userdata2 = dbarray($data2)) {
echo "".$userdata2['contact_id']." - ".$userdata2['contact_first']." - ".$userdata2['contact_last']." - ".$userdata2['contact_email']."";
echo "<br>";
}
echo "</ol></div>";
}
If there are the same amount of each A and B then you can create two arrays from the output and use a simple for loop to go through the keys. So it could echo $userdata[$x] and $userdata2[$x] where $x is the loop iterator.
Try to avoid using multiple loops, especially nested loops.
while ($userdata = dbarray($data)) {
//your stuff
while ($other_userdata = dbarray($data2)) {
//your other stuff
}
}
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/>";
}