I am trying to take the following rows
username | id | role
SELECT u.username, u.id, r.role FROM ".TBL_USERS." u
INNER JOIN ".TBL_ADMIN_ROLES." r ON r.userid = u.id
WHERE u.userlevel > 3
At the moment, I use the following code to get the results.
<?php
$q = $database->getAllAdmins();
while($row=mysql_fetch_assoc($q))
{
?>
<a class="main" href="profile.php?id=<? echo $row['id']; ?>"><? echo $row['username']; ?></a>
<ul>
<li><? echo $row['role']; ?></li>
</ul>
<?
}
?>
Obviously, this is looping through and showing the username over and over.
What I want it to do is show the username once and then show every role below.
Any ideas? Thanks :)
$q = $database->getAllAdmins();
$user = "";
while($row=mysql_fetch_assoc($q)) {
if ($user != $row['username']) {
if ($user != "") { echo "</ul>"; }
echo "<a class='main' href='profile.php?id={$row['id']}'>{$row['username']}</a>";
echo "<ul>";
}
echo "<li>{$row['role']}</li>";
$user = $row['username'];
}
My answer is the same concept as the others, just cleaner.
If you add a ORDER BY i.id to your query, you can then use a variable to keep track of the last user.
<?php
$q = $database->getAllAdmins();
$lastUserID = 0;
while($row=mysql_fetch_assoc($q))
{
$newUser = $lastUserID != $row['id'];
if($newUser) {
?>
<a class="main" href="profile.php?id=<? echo $row['id']; ?>"><? echo $row['username']; ?></a>
<ul>
<? }
?>
<li><? echo $row['role']; ?></li>
<? if($newUser) {
?>
</ul>
<? $lastUserID = $row['id'];
}
}
?>
Related
I went through multiple processes of trying to accomplish this, but couldn't quite figure it out. I went on Stack Overflow to find duplicate answers. I found some and try to adjust them into my code. Still no use. I'm trying to say if I'm logged in go to the profile page and if a username is in the database, I want you to display their name. If not in the database, then just don't show anything. Here is what I came up with:
if (isset($_SESSION['user_id'])){
$username = mysqli_real_escape_string($con, $_POST['username']);
$sql = "SELECT * FROM users WHERE username = '".$username."'";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result)>=1){
echo "User was found in the database";
}
else{
echo "User was not found in the database.";
}
}
So the if isset user_id basically says if I'm logged in then do this. The code after that is trying to find if a username is found in that database. If so then say it's found. If not, then don't. I hope this was clear! Thank you!
EDIT: Here is the HTML Code:
<?php
session_start();
ob_start();
include_once('dbconnect.php');
?>
<div class="banner_container">
<div class="jumbotron text-center">
<?php
if (isset($_SESSION['user_id']) && isset($_POST['username'])){
if(($_SESSION['user_id'] != "") && ($_POST['username'] != "")){
$user_id = $_SESSION['user_id'];
$username = mysqli_real_escape_string($con, $_POST['username']);
$sql = "SELECT * FROM users WHERE username = '".$username."' AND user_id !=".$user_id;
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0){
echo "User was found in the database";
}
else{
echo "User was not found in the database.";
}
}
else{
echo "Username or user Id is empty";
}
}
?>
<?php
echo "<h1>";
echo $_SESSION['first_name'];
echo " ";
echo $_SESSION['last_name'];
echo "</h1>";
echo "<p>";
echo '"';
echo $_SESSION['quote'];
echo '"';
echo "<br>";
echo $_SESSION['who'];
echo "</p>";
?>
</div>
</div>
<li>Home</li>
<li> About</li>
<li class="qotd"> Quote of the Day</li>
<li class="all_categories">All Categories</li>
<li> Authors</li>
<?php
if(isset($_SESSION['user_id'])){
echo $_SESSION['user_id'];
echo '<li id="active" class="dropdown">';
echo '<a id="act_color" href="#" class="dropdown-toggle" data-toggle="dropdown">';
echo $_SESSION['first_name'];
echo "'s";
echo ' ';
echo "Profile";
echo '<b class ="caret"></b></a>';
echo '<ul class="dropdown-menu">';
echo '<li> Profile</li>';
echo '<li>Log out</li>';
echo '</ul>';
echo '</li>';
echo '</a>';
echo '</li>';
} else {
}
?>
This is the profile page that I'm on.
Use this code:
if (isset($_SESSION['user_id']) && isset($_POST['username'])){
if(($_SESSION['user_id'] != "") && ($_POST['username'] != "")){
$user_id = $_SESSION['user_id'];
$username = mysqli_real_escape_string($con, $_POST['username']);
$sql = "SELECT * FROM users WHERE username = '".$username."' AND user_id !=".$user_id;
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0){
echo "User was found in the database";
}
else{
echo "User was not found in the database.";
}
}
else{
echo "Username or user Id is empty";
}
}
I am trying to have make it so if a user looks at their page they can see the assignments that they have either been working on or have completed. Then I want to make it so that if they look at another users page they can see the projects that they have created.
$my_id = $_SESSION['user_id'];
$frnd_query = mysql_query("SELECT user_one, user_two FROM frnds WHERE user_one = '$my_id' OR user_two = '$my_id'");
while($run_frnd = mysql_fetch_array($frnd_query)) {
$user_one = $run_frnd['user_one'];
$user_two = $run_frnd['user_two'];
if($user_one == $my_id) {
$user = $user_one;
} else {
$user = $user_two;
}
$username = getuser($user, 'username');
echo "<a href = 'my_ideas.php?user=$user' class = 'list' style=display:block>Ideas</a>";
?>
<ul>
<li><a href = '#my_links'>Linked Ideas</a></li>
<li><a href = '#idea_chemistry'>My Idea Chemistry</a></li>
<li><a href = 'settings.php'>Profile Settings</a></li>>
</ul>
</div>
<div id = 'my_projects'>
<?php
if($user != $_SESSION['user_id']) {
$ideas_sql = "SELECT * FROM projects WHERE user_id = $username";
$query = mysql_query($ideas_sql) or die(mysql_error());
$rsIdeas = mysql_fetch_assoc($query);
do {
?>
<h2><a href = 'edit_post.php?id=<?php echo $rsIdeas['id']; ?>'><?php echo $rsIdeas['name']; ?></a></h2>
<?php echo $rsIdeas['keywords']; ?></p>
<p><?php echo $rsIdeas['description']; ?></p>
<?php } while ($rsIdeas = mysql_fetch_assoc($query));
} else {
$ideas_sql = "SELECT * FROM projects WHERE user_id = $my_id";
$query = mysql_query($ideas_sql) or die(mysql_error());
$rsIdeas = mysql_fetch_assoc($query);
do {
?>
<h2><a href = 'edit_post.php?id=<?php echo $rsIdeas['id']; ?>'><?php echo $rsIdeas['name']; ?></a></h2>
<p><?php echo $rsIdeas['keywords']; ?></p>
<p><?php echo $rsIdeas['description']; ?></p>
<?php } while ($rsIdeas = mysql_fetch_assoc($query));
}
?>
<?php
}
?>
I have googled this and searched for related questions on here but I cannot seem to find anything that helps with my problem.
This is basically a code review question. There is also another site on SO network where you could place this type of question. Although you should show that you have some knowledge of coding.
I am not sure why there isn't a starting PHP tag, but here is the first part.
<?php
$my_id = $_SESSION['user_id'];
$frnd_query = mysql_query("SELECT user_one, user_two FROM frnds WHERE user_one = '".$my_id."' OR user_two = '".$my_id."'");
while($run_frnd = mysql_fetch_array($frnd_query)) {
$user_one = $run_frnd['user_one'];
$user_two = $run_frnd['user_two'];
if($user_one == $my_id) {
$user = $user_one;
} else {
$user = $user_two;
}
$username = getuser($user, 'username');
echo "<a href = 'my_ideas.php?user='".$user."' class = 'list' style=display:block>Ideas</a>";
?>
<ul>
<li><a href = '#my_links'>Linked Ideas</a></li>
<li><a href = '#idea_chemistry'>My Idea Chemistry</a></li>
<li><a href = 'settings.php'>Profile Settings</a></li>>
</ul>
<div id = 'my_projects'>
<?php
if($user != $_SESSION['user_id']) {
$ideas_sql = "SELECT * FROM projects WHERE user_id = '".$username."'";
$query = mysql_query($ideas_sql) or die(mysql_error());
$rsIdeas = mysql_fetch_assoc($query);
do {
?>
<h2><a href = 'edit_post.php?id=<?php echo $rsIdeas['id']; ?>'><?php echo $rsIdeas['name']; ?></a></h2>
<?php echo $rsIdeas['keywords']; ?></p>
<p><?php echo $rsIdeas['description']; ?></p>
<?php } while ($rsIdeas = mysql_fetch_assoc($query));
} else {
$ideas_sql = "SELECT * FROM projects WHERE user_id ='".$my_id."'";
$query = mysql_query($ideas_sql) or die(mysql_error());
$rsIdeas = mysql_fetch_assoc($query);
do {
?>
<h2><a href = 'edit_post.php?id=<?php echo $rsIdeas['id']; ?>'><?php echo $rsIdeas['name']; ?></a></h2>
<p><?php echo $rsIdeas['keywords']; ?></p>
<p><?php echo $rsIdeas['description']; ?></p>
<?php } while ($rsIdeas = mysql_fetch_assoc($query));
}
?>
<?php
}
?>
Question: Is this a custom function getuser().
I've got a table called tickets. The Tickets table has 4 columns: user_id, ticket_id, subject, message. I want to loop the ticket_id and subject of the logged in user. I want to echo it with foreach, but I cant get it to work. This is what I've got:
Query:
$showTheTickets = array();
$user_id = $_SESSION['user_id'];
$getTickets = mysqli_query($mysqli,"SELECT * FROM tickets WHERE user_id = '$user_id'") OR die (mysqli_error($mysqli));
while($row = mysqli_fetch_assoc($getTickets)){
$showTicketID = $row['ticket_id'];
$showTicketSubject = $row['subject'];
}
Code in the page:
<?php foreach ($showTheTickets as $showTheTickets): ?>
<div class="preview">
<?php echo $showTicketID ?>
<?php echo $showTicketSubject ?>
</div>
<?php endforeach; ?>
Anyones willing to help me? Thanks.
Try this:
$showTheTickets = array();
$user_id = $_SESSION['user_id'];
$getTickets = mysqli_query($mysqli,"SELECT * FROM tickets WHERE user_id = $user_id") OR die (mysqli_error($mysqli));
while($row = mysqli_fetch_array($getTickets)){
$row = array( 'ticket_id' => $row['ticket_id'], 'subject' => $row['subject'] );
$showTheTickets[] = $row;
}
And then:
<?php foreach ($showTheTickets as $stt): ?>
<div class="preview">
<?php echo $stt['ticket_id'] ?>
<?php echo $stt['subject'] ?>
</div>
<?php endforeach; ?>
Hope it helps...
You are overwriting the values you get from the query and your $showTheTickets array remains empty.
You probably want something like:
$showTheTickets = array();
while($row = mysqli_fetch_assoc($getTickets)){
$showTheTickets[$row['ticket_id']] = $row['subject'];
}
and:
<?php foreach ($showTheTickets as $id => $subject): ?>
<div class="preview">
<?php echo $id; ?>
<?php echo $subject; ?>
</div>
<?php endforeach; ?>
Why not simply
while($row = mysqli_fetch_assoc($getTickets)){
?><div class="preview"><?
echo $row['ticket_id'];
echo $row['subject'];
?></div><?
}
you have to save $showTicketID and $showTicketSubject in a array (could be the same array or 2 arrays) and then in the view show the arrays, like this:
while($row = mysqli_fetch_assoc($getTickets)){
$array[$row['ticket_id']] = $row['subject'];
}
and the view:
<?php foreach ($showTheTickets as $ticketid => $ticketsubject): ?>
<div class="preview">
<?php echo $ticketid ?>
<?php echo $ticketsubject ?>
</div>
<?php endforeach; ?>
I am having a problem with trying to show different menu options based on UserLevel. I have a mysql database with a users table. The users table contains a UserLevel which will either be set to 0 or 1. But for some reason my php just isn't working. In fact, when I add the php to the menu, it then does not display ANYTHING on the site below the menu. Any advice would be much appreciated.
Code that starts session
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
?>
<?php include "mainNav.php"; ?>
<center>
<h2> Campaign Updates</h2>
</center>
<div id="campaignPostWrap">
<div id="campaignScrollBox">
<?php
$con=mysqli_connect("localhost","dorians","ds2953!b67P$","aldentec");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM campaigns ORDER BY postDate desc");
while($row = mysqli_fetch_array($result))
{
echo "<div id='campaignPostContainer'>";
echo "<ul class='campaignPostBox'>";
echo "<p class='postInfo'>";
echo "Posted on:";
echo "<li>" . $row['postDate'] . "</li>";
echo "</p>";
echo "<p class='postInfo'>";
echo "Posted by:";
echo "<li>" . $row['postName'] . "</li>";
echo "</p>";
echo "<li class='postEntry'>" . $row['postEntry'] . "</li>";
echo "</ul>";
echo "</div>";
echo "<hr>";
}
mysqli_close($con);
?>
</div>
<?php include "campaignPost.php"; ?>
</div>
<?php include "chat.php"; ?>
<?php
}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = md5(mysql_real_escape_string($_POST['password']));
$checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");
if(mysql_num_rows($checklogin) == 1)
{
$row = mysql_fetch_array($checklogin);
$email = $row['EmailAddress'];
$userlevel = $row['UserLevel'];
$_SESSION['Username'] = $username;
$_SESSION['EmailAddress'] = $email;
$_SESSION['LoggedIn'] = 1;
$_SESSION['UserLevel'] = $userlevel;
echo "<h1>Success</h1>";
echo "<p>We are now redirecting you to the member area. If you are not automatically redirected <a href='index.php'>Click here</a></p>";
header( "refresh:10;url=index.php" );
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your account could not be found. Please click here to try again.</p>";
}
}
else
{
?>
Menu code that isn't working
<?php session_start(); ?>
<?php
$userlevel = $_SESSION['UserLevel'];
if($userlevel == 0) {
echo "<ul class="mainNav">
<li> Create Character</li>
<li> Create Quest</li>
<li> View Characters</li>
<li> View Quests</li>
<li> Book List</li>
</ul>";
} elseif($userlevel == 1) {
echo "<li> DM Tools</li>";
}
?>
<?php include "greeter.php"; ?>
Your quotes are undoubtedly the problem here:
if($userlevel == 0) {
echo "<ul class="mainNav">
<li> Create Character</li>
<li> Create Quest</li>
<li> View Characters</li>
<li> View Quests</li>
<li> Book List</li>
</ul>";
} elseif($userlevel == 1) {
echo "<li> DM Tools</li>";
}
Notice the syntax highlighting above shows the issue in your string. See how it turns black when it gets to mainNav? That's because mainNav is no longer part of the string. That's a bad thing here.
Look at the first line of your echo:
echo "<ul class="mainNav">
You open a quote and then close it at class=". Now, it's trying to evaluate mainNav as a constant or some other language construct. On top of that, it doesn't know what to do with mainNav as you haven't provided any kind of operators.
Instead, you should do something like:
if($userlevel == 0) {
echo '<ul class="mainNav">
<li> Create Character</li>
<li> Create Quest</li>
<li> View Characters</li>
<li> View Quests</li>
<li> Book List</li>
</ul>';
} elseif($userlevel == 1) {
echo '<li> DM Tools</li>';
}
Alternatively, you could escape every location where there is a non-string-terminating quote like \".
Another option would be to use Heredoc syntax.
I need the first class to be active and then empty from then on.
<?
$slidesinfo = mysql_query("SELECT * FROM slides ORDER BY id ASC LIMIT 4");
while ($slidesingo = mysql_fetch_array($slidesinfo))
{
?>
<li class="active"><span><? echo $slidesingo['title'] ?></span></li>
<?
}
?>
So the result should be:
<li class="active"><span>Title 1</span></li>
<li><span>Title 2</span></li>
<li><span>Title 3</span></li>
<li><span>Title 4</span></li>
try this code.
$i = 1;
while ($slidesingo = mysql_fetch_array($slidesinfo))
{
$cls = "";
if($i==1)
{
$cls = "active";
$i++;
}
?>
<li class="<?php echo $cls;?>"><span><? echo $slidesingo['title'] ?></span></li>
<?
}
?>