Echoing users by group name in PHP - php

What I want to do is click a name of a group(every group what I create other than poweruser and admin groups) and that will echo all of the users in that group from the database. I have figured out the code so far but now my problem is how will I print it all out when clicking the name of the group?
My code so far is:
<h3>Groups</h3>
<?php
include('db.php');
if (isset($_GET["groupID"])) {
$sql="SELECT `group`.*, `user`.* FROM `user` inner join `group` on group.groupID=user.groupID where group.groupID= " . mysql_real_escape_string($_GET["groupID"]) ;
} else {
$sql="SELECT * FROM `group` WHERE groupName <> 'admin' AND groupName <> 'poweruser'" ;
}
$result=mysql_query($sql,$connection);
while($line=mysql_fetch_array($result)){
echo "<a href='index.php?page=groups&group=".$line['groupID']."'>".$line['groupName'].'</a><br />';
}
mysql_free_result($result);
mysql_close($connection);
?>

On top of the $_GET["group"] issue, it doesn't look like you're printing out the users anywhere.
You should move the output into the if block, and create new output for the first condition.
<?php
include('db.php');
if (isset($_GET["groupID"])) {
$sql="SELECT `group`.*, `user`.* FROM `user` inner join `group` on group.groupID=user.groupID where group.groupID= " . mysql_real_escape_string($_GET["groupID"]) ;
$result=mysql_query($sql,$connection);
//Output users after Group Selected
//Could be placed outside the if block to allow user to select a different group
echo("<h3>Users</h3>");
while($line=mysql_fetch_array($result)){
//echo out the user data here
}
} else {
$sql="SELECT * FROM `group` WHERE groupName <> 'admin' AND groupName <> 'poweruser'" ;
$result=mysql_query($sql,$connection);
//Output to let user select group.
echo("<h3>Groups</h3>");
while($line=mysql_fetch_array($result)){
echo "<a href='index.php?page=groups&group=".$line['groupID']."'>".$line['groupName'].'</a><br />';
}
}
mysql_free_result($result);
mysql_close($connection);
?>

if (isset($_GET["groupID"])) {
should that not be
if (isset($_GET["group"])) {
also change the $_GET["groupID"] to $_GET["group"] in ur $sql query

The following code is an example on how you could it. Just put it below the code you already have:
if ($_GET['page'] == 'groups'):
$groupID = $_GET['groupID'];
$sql = "SELECT * FROM users WHERE groupID = '$groupID'";
$res = mysql_query($sql); //MySQL query
$count = mysql_num_rows($res); //This counts our results
if($count != 0): //This makes sure we have at least 1 result
while($user = mysql_fetch_assoc($res)):
echo $user['name'];
endwhile;
endif;
endif;

Add Peter Stuart's code in your file and change
<a href='index.php?page ... > to <a href='groups.php?page ... >

Related

Check if there is a value in db with if

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>";

Show records in the table with if - PHP

I have a plant table and a customer campaign table, I want to show only plants that are not associated with the customer campaign table.
through this if cycle, I would like to get a solution like if it is true not to show the plants that are associated if and it is false shows the plants that are not associated
it's possible?
<?php
session_start();
include 'connessione.php';
$var = true;
$var = 1;
$query = mysqli_query($connessione, "
SELECT *
FROM store_locator
INNER JOIN campagne_cliente
ON store_locator.id = campagne_cliente.impianto_id_campagna");
if (!$query)
{
die('Error: ' . mysqli_error($connessione));
}
if($var === true){
echo "email already exists";
}else{
echo "ok";
}
?>
but if i have only plants associated and nothing plants free i have always ok.
Why?
Correct:
<?php
session_start();
include 'connessione.php';
$id = $_SESSION['id'];
$query_string = "SELECT * FROM store_locator WHERE store_locator.id NOT IN (SELECT impianto_id_campagna FROM campagne_cliente);
";
$query = mysqli_query($connessione, $query_string);
?>
<?php
while($row = mysqli_fetch_assoc($query)){ ?>
<?php echo $row['id'] ;?>
<?php } ?>
USE NOT IN QUERY LIKE THIS....
AM Just Try to understand you,you can manage it according to your table structure.
SELECT * FROM `plant_table` not in (select customer_campaign.plant_id from customer_campaign);
Hope This will Help You.
Thanks
I got this result:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'not in (select campagne_cliente.impianto_id_campagna from campagne_cliente)' at line 2
this is a query:
SELECT * FROM `store_locator`
not in
(select campagne_cliente.impianto_id_campagna
from campagne_cliente);

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

Echo rows via JOIN in MySQL/PHP (?)

I wanna be able to echo out if Groupname and Username are connected correctly, where the current userid (saved in a session) is $uid.
I've been sitting for hours trying all kinds of JOINs and the closest I've gotten is having it output 1/? members for each team, but not all of them.
EDIT:
$uid = $_SESSION['uid'];
$sql = "SELECT * FROM group
INNER JOIN usergroup ON group.groupid=usergroup.groupid
WHERE usergroup.userid=$uid";
$result=$mysqli->query($sql);
if(mysqli_num_rows($result)>0) {
while($row = mysqli_fetch_array($result)) {
$gid = $row['groupid'];
$sql2 = "SELECT * FROM user
INNER JOIN usergroup ON user.userid=usergroup.userid
WHERE usergroup.groupid=$gid";
$result2=$mysqli->query($sql2);
$row2 = mysqli_fetch_array($result2);
echo "<td>".$row['groupname']."</td>";
echo "<td>".$row2['username']."</td>";
echo "<td>".$row['groupid']."</td>";
}
}
Thing is, that it kinda works well, except that it doesn't print all the groupmembers names out, it prints out just one. Which one seems to depend on the order in the table.
You did not have a loop on the second query's resultset. However, it is not needed to have a second SQL query. Just do it in one go; SQL was designed for that.
Also, you'll have much simpler code:
$uid = $_SESSION['uid'];
// Select everything you need in one go (join user table as well)
$sql = "SELECT group.group_id, group.groupname, user.username
FROM group
INNER JOIN usergroup ON group.groupid=usergroup.groupid
INNER JOIN user ON user.userid=usergroup.userid
WHERE usergroup.userid=$uid";
$result=$mysqli->query($sql);
// Don't need to call mysqli_num_rows if you continue like this:
while($row = mysqli_fetch_array($result)) {
echo "<td>".$row['groupname']."</td>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['groupid']."</td>";
}
Maybe you want to echo some <tr> and </tr> tags, or you"ll have everything in one row, like:
echo "<tr><td>".$row['groupname']."</td>"
."<td>".$row['username']."</td>"
."<td>".$row['groupid']."</td></tr>";
There you go: (you were missing nested while loop)
if(mysqli_num_rows($result)>0) {
while($row = mysqli_fetch_array($result)) {
$gid = $row['groupid'];
$sql2 = "SELECT * FROM user INNER JOIN usergroup ON user.userid=usergroup.userid WHERE usergroup.groupid=$gid";
$result2=$mysqli->query($sql2);
if(mysqli_num_rows($result2)>0) {
while($row2 = mysqli_fetch_array($result2)) {
echo "<td>".$row['groupname']."</td>";
echo "<td>".$row2['username']."</td>";
echo "<td>".$row['groupid']."</td>";
}
}
}
}
Side note: You could achieve the same results with just one SQL query, something like:
SELECT
*
FROM
usergroup ug
INNER JOIN
user u ON ug.userid = u.userid
GROUP BY
ug.id
and then in PHP (pseudo code just, do not copy-n-paste)
while($row => mysqli_fetch_array($result)) {
if (!isset($groupsWithUsers[$row['groupid']['users'])) {
$groupsWithUsers[$row['groupid']['users'] = array()
}
$groupsWithUsers[$row['groupid']['users'][$row['userid']] = $row;
}

loop problem within a statement

i hope someone can help about to scream!
basically I am trying to do a few things with the statement below;
First i want to check if the user id exists in member_categories_position.
If it Does i want then to exclude all entries from the second statement where member_id equals all results from the first statement
the third statement is the else statement that displays if the member_id is not present in the member_categories position.
PROBLEM - the result from the first system loops fine, however when i try and insert into the second statement (!='$memid') is produces no results and has no effect. I think the problem is that $memid is a looped result.
How do i get the second statement to say that any member_id that is in member_categories_position will not show in that statement?
$sql2 = "
SELECT *
FROM member_categories_position a
JOIN member_users b
ON b.id = a.member_id";
$rs2 = mysql_query($sql2);
while ($row = mysql_fetch_array($rs2))
{
$memid = "".$row['member_id']."";
}
if(mysql_num_rows($rs2) != 0)
{
$new= "
SELECT *
FROM member_categories
JOIN member_users
ON member_categories.member_id=member_users.id
JOIN member_config
ON member_categories.member_id=member_config.member_id
WHERE
member_categories.categories='$category'
AND member_categories.member_id !='$field'
GROUP BY member_config.member_id
ORDER BY RAND() limit 0,42";
$rs = mysql_query($new);
while ($row = mysql_fetch_assoc($rs))
{
echo "result excluding member ids from the first statement";
}
echo "<div class=\"clear\"></div>";
}
else
{
$new= "
SELECT *
FROM member_categories
JOIN member_users
ON member_categories.member_id=member_users.id
JOIN member_config
ON member_categories.member_id=member_config.member_id
WHERE
member_categories.categories='$category'
GROUP BY member_config.member_id
ORDER BY RAND() limit 0,42";
$rs = mysql_query($new);
while ($row = mysql_fetch_assoc($rs))
{
echo "Result with all member ids";
}
echo "<div class=\"clear\"></div>";
} } <-- (second is a stray from original post)
$memid is not in scope since it appears to be defined inside the loop. Try defining $memid = ''; at the top of your script.. like this.
$memid = '';
$sql2 = "
SELECT *
That way it will be defined when you use it below..

Categories