I'm trying to pull data from my database like their First name and Last name and make it display on my webpage. This is my php inside of my html markup.
<?php
if (isset($_SESSION['userId'])) {
require './includes/dbh.inc.php';
$result = mysqli_query($conn,"SELECT * FROM users");
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<li class='login current2'><a href='login.php'>". $row['fnidusers'] . $row['lnidusers'] ."</a></li>";
}
}
}
else {
echo "<li class='login current2'><a href='login.php'>Login / Sign up</a></li>";
}
?>
I'm attempting to make a drop down with their name as the label for it. For example:
Amazon has similar drop down that I'm looking for, It says the name of the user.
Thanks for the help.
If you want to display only the current logged in user, you can do it with your request like this :
"SELECT * FROM users WHERE user_id = '" . $_SESSION['userId'] . "'"
Here you are searching in all rows, the one with id the current logged in user id.
Now, just reuse the var in your while but without the while :
$row = $result->fetch_assoc();
Now your code will look something like this :
<?php
require './includes/dbh.inc.php';
if (isset($_SESSION['userId'])) {
$resut = mysqli_query($conn, "SELECT * FROM users WHERE user_id = '" . $_SESSION['userId'] . "'");
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<li class='login current2'><a href='login.php'>". $row['fnidusers'] . $row['lnidusers'] ."</a></li>";
}
} else
echo "<li class='login current2'><a href='login.php'>Login / Sign up</a></li>";
?>
Please excuse my bad english ^^
Related
How it looks:
https://jsfiddle.net/jef2L8m6/
How it should look:
https://jsfiddle.net/jef2L8m6/1/
I know it looks really bad, this is just for testing purposes only.
Some of the Backend Code:
<?php //Selects all of the logged in users messages.
$name = $_SESSION["name"];
$con = mysqli_connect('localhost','root','','chat');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM `chat` ORDER BY date";
$result = mysqli_query($con,$sql);
$numrows = mysqli_num_rows($result);
if( $numrows == "0" or !isset($_SESSION["name"])){
echo "<div class='msg'>You are not registered (Or there are no messages to display)</div>";
exit();
}else{
echo "";
}
echo "<div class='msg_container'>";
while($row = mysqli_fetch_array($result)) {
echo "<div class='msg_user'>";
echo "<div class='username_user'><span>" . $row['username'] . "</span></div>";
echo "<div class='message_user'><span>" . $row['message'] . "</span></div>";
echo "</div>";
}
echo "";
mysqli_close($con);
?>
Thank you so much for taking your time to read this.
I am trying to figure out how I would change the div tags of each separate user depending on their name?
Is there any way to do this using PHP, I have tried doing 2 separate query's of one that selects just the users messages and another that selects everyones (excluding the users)
But none of them worked due to it not ordering them correctly.
Could I somehow change the div's using PHP if the username that comes out is not equal to the username in the session?
Thank's so much, if you don't think I explained this very well please give me some feedback and I will change/add what you need, THANK YOU!
Thank you so much "u_mulder", you have been very helpful in making me think of a simple way to solve this problem.
I was thinking way too complex for something so simple!
Here is the final code for anyone who this may help:
while($row = mysqli_fetch_array($result)) {
$class_msg = "msg";
$class_username = "username";
$class_message = "message";
if ($row['username'] == $_SESSION['name']) {
$class_msg = "msg_user";
$class_username = "username_user";
$class_message = "message_user";
}
echo "<div class='$class_msg'>";
echo "<div class='$class_username'><span>" . $row['username'] . "</span></div>";
echo "<div class='$class_message'><span>" . $row['message'] . "</span></div>";
echo "</div>";
}
while($row = mysqli_fetch_array($result)) {
$class = 'msg';
if ($row['username'] == $_SESSION['name']) {
$class = 'msg_user';
}
echo "<div class='" . $class . "'>";
// other codes here
}
I have trouble to select a set of specific data using ID from the database. For example, employee one has a unique id of e000000001, when I click the view button in the index will lead to employee detail page which shows the detail of that particular employee instead of all the employees' detail. Thank you.
//from index.php page
<?php
require_once 'db/dbEmpList.php';
$sqlStr = "SELECT * FROM employees;";
$result = $connection->query($sqlStr);
if ($result->num_rows > 0) {
echo "<table class='table table-sm'><thread><tr><th>Full Name</th><th>Employee ID</th><th>Position</th><th>View Employee's Details</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>"
. $row["empName"]. "</td><td>"
. $row["empID"]. "</td><td>"
. $row["position"]. "</td>"
. "<td> <a href='employeedetail.php?id={$row["empID"]}'>View</a>"
. "</td></tr>";
}
}
// from employee page
require_once 'db/dbEmpDetail.php';
$sql = "SELECT * FROM employees where empID = '{$row["empID"]}' ";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>' .'<td>' .$row["empName"].'</td>'.'<td>'. $row["position"].'</td>' .'<td>'.$row["empNRIC"].'</td>' .'<td>'.$row["empID"].'</td>' .'<td>'.$row["empEmail"].'</td>' .'<td>'.$row["empPwd"].'</td>' . "</tr>";
}
} else {
echo "0 results";
}
mysqli_close($connection);
?>
// FROM EMPLOYEE PAGE
The way you retrieve URL query string is wrong. You should be using $_GET to get the query string from URL. In your case it should be $_GET['id']. See the code below:
require_once 'db/dbEmpDetail.php';
$employeeid = trim(mysqli_real_escape_string($_GET['id']));
$sql = "SELECT * FROM employees where empID = '".$employeeid."' ";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>' .'<td>' .$row["empName"].'</td>'.'<td>'. $row["position"].'</td>' .'<td>'.$row["empNRIC"].'</td>' .'<td>'.$row["empID"].'</td>' .'<td>'.$row["empEmail"].'</td>' .'<td>'.$row["empPwd"].'</td>' . "</tr>";
}
}
else {
echo "0 results";
}
mysqli_close($connection);
?>
I am having a huge problem. I have displayed data on a single events page the events being displayed are the rows of data in the database.
Here's the code.
<?php
$sql = "SELECT * FROM events ORDER BY `startdate` DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div class='event col-sm-3'>
<h1>". $row['eventname'] . "</h1>
<p><b>Regular:</b> " . $row['regular'] . "Ksh</p>
<p><b>VIP:</b> " . $row['vip'] . "Ksh</p><br>
<p><b>" . $row['startdate'] . "</b></p>
<p><b>Tickets remaining:<b>" . $row['tickets'] . "</p><br><br>
<button class='button'>book</button>
</div>";
}
}
?>
Here are the events being displayed
Now I would want when I click the button that says book the event the fields(items) in the specific row that I have clicked(using the book button) be displayed in a different page and get stored in variables.
If I understand what you are trying to do, you might want to use an 'a' tag and not a button.
You should pass the 'id' value of the ticket to the page where you want to have an independent query (individual_ticket.php)
Your code should look like this
<?php
$sql = "SELECT * FROM events ORDER BY `startdate` DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div class='event col-sm-3'>
<h1>". $row['eventname'] . "</h1>
<p><b>Regular:</b> " . $row['regular'] . "Ksh</p>
<p><b>VIP:</b> " . $row['vip'] . "Ksh</p><br>
<p><b>" . $row['startdate'] . "</b></p>
<p><b>Tickets remaining:<b>" . $row['tickets'] . "</p><br><br>
Book
</div>";
}
}
?>
And inside the 'individual_ticket.php'.
Do something like.
<?php
if(isset($_GET['id']))
{
$id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
$sql = "SELECT * FROM events WHERE id = '$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
//you can start assigning variables here
}
}
}
?>
$sql = "SELECT * FROM today WHERE heading='$heading' and day='$day'";
$sql1 = "SELECT * FROM today WHERE day='$day'";
$result = $conn->query($sql);
$result1 = $conn->query($sql1);
if ($result->num_rows > 0) {
echo "<div id='post'><h1>".$row["heading"]."</h1>
<aside class='related-post'>".while($row = $result1->fetch_assoc())
{echo'<img src='".$row["image"]."'>;}
.</aside>}";
I have been using while loops for fetching data from table. My connection is working is perfect but I need another loop in the first that is not working. Isn't it the good way?
Update: I tried to finish echo and again started as follow but still an error
while($row = $result->fetch_assoc()) {
echo "<div id='post'><h1>"
.$row["heading"].
"</h1><div class='post-side'><img class='post-image' src='"
.$row["image"].
"'><div class='post-data'><p><strong>Age: </strong><span>$age</span></p><p><strong>Date of birth: </strong><span>"
.$row["day"].
"-"
.$row["month"].
"-"
.$row["year"].
"</span></p></div></div></div><div class='description'><p>"
.$row["description"].
"</p></div><div class='bottom-related'><aside class='related-post'>";
while($row = $result1->fetch_assoc())
{echo"<img src='"
.$row["image"].
"'>/";}.echo"</aside><aside class='ad2'>".$includead."</aside></div>";
}
echo "</div>";
} else {
echo "No table found";
}
$conn->close();
You're trying to concatenate to a string a WHILE loop; this is wrong.
You should echo your first part, end with it and then do your while loop, and echo the end afterwards:
Your quotes are a bit messed up as well
if ($result->num_rows > 0)
{
echo "<div id='post'><h1>".$row["heading"]."</h1>
<aside class='related-post'>";
while($row = $result1->fetch_assoc())
{
echo'<img src="'.$row["image"].'">';
}
echo '</aside>';
}
You can't concatene while with String, it's a syntaxic error
Also, you have a probleme when trying to echo a String, you can use this syntax:
echo "PHP"; // will evaluate PHP variables and whitespace inside a string
echo 'PHP'; // will evaluate nothing;
But you can not start flushing a string ' and finish by " or vice-versa.
Here the correct code :
<?php
$sql = "SELECT * FROM today WHERE heading='$heading' and day='$day'";
$sql1 = "SELECT * FROM today WHERE day='$day'";
$result = $conn->query($sql);
$result1 = $conn->query($sql1);
if ($result->num_rows > 0) {
echo "<div id='post'><h1>" . $row["heading"] . "</h1><aside class='related-post'>";
while($row = $result1->fetch_assoc()) {
echo'<img src="' . $row["image"] .'">';
}
echo "</aside>";
}
A Solution has been found! Thank you to #northkildonan and #Doug Leary.
Solution Code;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<li class='list-group-item'>" . $row["user_name"];
if ($row["user_name"] == "TacoLover22") {
echo "<span class='badge'>Dev</span>";
}
echo "</li>";
}
##
I have a list of usernames and I have a code that checks if a username exists, if it does exist, I want a div to be appended to it. Here's an image explaining this
However, the code I have works, but it doesn't assign that div to the selected username.
Here's the PHP Code;
<?php
if (
$row["user_name"] === "TacoLover22");{
echo "<span class='badge'>Dev</span>";
}
?>
here's the other php code that grabs all the usernames;
<?php
$servername = "####";
$username = "####";
$password = "########";
$dbname = "########";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT user_name FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<li class='list-group-item'>" . $row["user_name"];
}
} else {
echo "0 results";
}
$conn->close();
?>
The result of this code can be found here.
EDIT: Here's the full code from suggestions, it still doesn't seem to work. The span doesn't appear now.
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<li class='list-group-item'>" . $row["user_name"];
if ($row["user_name"] === "TacoLover22") {
echo "<span class='badge'>Dev</span>";
}
echo "</li>";
}
Thanks for all and any help.
I think this will do it:
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<li class='list-group-item'>" . $row["user_name"];
if ($row["user_name"] === "TacoLover22") {
echo "<span class='badge'>Dev</span>";
}
echo "</li>";
}
you have to append the <span> inside your mysql-fetch-loop (note: you are talking about divs, but you are using a span, which is something different in html).
while($row = $result->fetch_assoc()) {
echo "<li class='list-group-item'>" . $row["user_name"];
if ($row["user_name"] == "TacoLover22")
{
echo "<span class='badge'>Dev</span>";
}
}
explanation: you are iterating the resultset of your mysql-query right in your while-loop. the variable $row is your pointer which points to the respective row of your result.
after your while-loop is finished, your "pointer" will always show to the last result row of your mysql-query. so $row["user_name"] will always be set to the last user name found in your database. that's why you have to access it inside the while-loop.
how to handle multiple users (as requested):
I decided to use switch instead of if elseif statements here since it's better to read and less to write imho:
while($row = $result->fetch_assoc()) {
echo "<li class='list-group-item'>" . $row["user_name"];
switch($row["user_name"])
{
case "TacoLover22":
echo "<span class='badge'>Dev</span>";
break;
case "AnotherUser":
echo "<span class='badge'>User</span>";
break;
}
}
assuming u are using sqli extention
the column user_username is list of special users, otherwise you will need 2 tables, 1 table to store users, and 1 auxiliar table to store special users
//query
$sql = "SELECT user_name FROM users";
//send to mysql, get result
$sendquery = $conn->query($sql);
//fetch results as array
while ($result=$sendquery->fetch_array) {
echo 'username:' . $result['user_name'] . '<span class='badge'>Dev</span>';
}
but your question is confusing, you should use 2 tables if you are having different type of users
TABLE USERS user_id | name | dev_id | email | password | ...
TABLE DEVS dev_id | name | level | access | ....
SQL query, using LEFT JOIN
SELECT
usern.name as username, dev.name as devname
FROM USERS as user
LEFT JOIN DEVS as dev
USING dev_id
php output use $result['devname']
$result['']
if you only neeed "TacoLover22" to display, in case thats the only person you need to add something, add a IF in the while cycle
//query
$sql = "SELECT user_name FROM users";
//send to mysql, get result
$sendquery = $conn->query($sql);
//fetch results as array
while ($result=$sendquery->fetch_array) {
if ($result['user_name'] == "TacoLover22") {
echo 'username:' . $result['user_name'] . '<span class='badge'>Dev</span>';
}
else {
echo $result['user_name'];
}
}