Displaying Specific Data from mysql database based on user - php

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().

Related

Display Related Items PHP/MySQL

I have a page that displays the display of an item with a specific id and I want to display other items that are related based on title, subject, or author. I am having issues with displaying related items. Where I put my query together and execute it, the related results are just the same title of the item whose page I am already on (https://brawlins.com/soarOpen/itemRecord.php?id=65437). How do I displays related results that are not the same as the item whose page I currently am on?
<?php
include("config.php");
if(!isset($_GET["id"])) {
echo "No id passed into the page";
exit();
}
$id = $_GET['id'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php
$item_query = $conn->prepare("SELECT * FROM oer_search WHERE id = :id");
$item_query->bindParam(":id", $id);
$item_query->execute();
while($row = $item_query->fetch(PDO::FETCH_ASSOC)){
$type = $row['type'];
$link = $row['link'];
$title = $row['title'];
$description = $row['description'];
$subject = $row['subject'];
$pub_date = $row["publication_date"];
$source = $row['source'];
$isbn = $row["isbn_number"];
$e_isbn = $row["e_isbn_number"];
$license = $row['license'];
$license_url = $row['license_url'];
$base_url = $row['base_url'];
$author = $row['author'];
$review = $row['review'];
$image = $row["image_url"];
$loc_collection = $row["loc_collection"];
$publisher = $row['publisher'];
$pub_url = $row['publisher_url'];
?>
<title><?php echo $title ?></title>
<div class="container content-container">
<div class="card card-content">
<div class="card-body">
<?php
echo "<strong><a class='itemRecordLink' rel='external' href='$link'><h3>$title</h3></a></strong><br/>";
}
$like_query = $conn->prepare("SELECT title FROM oer_search WHERE title LIKE :title OR subject=:subject OR author=:author LIMIT 4");
$titleLike = $searchTerm = "%". $title . "%";
$like_query->bindParam(":title", $titleLike);
$like_query->bindParam(":subject", $subject);
$like_query->bindParam(":author", $author);
$like_query->execute();
echo "<div class='row'>";
while($row = $like_query->fetch(PDO::FETCH_ASSOC)){
echo "<div class='col-md-3 text-center'>";
if($image != "") {
echo "<img src='$image' class='img-fluid img-thumbnail itemRecordImage' alt='cover image' /><br>";
}
else {
echo "<img src='images/cover-image.png' class='img-fluid itemRecordImage' alt='cover image' /><br>";
}
echo "<span>$title</span>";
echo "</div>";
}
echo "</div>";
?>
</div><!--end of card body-->
</div><!--end of card-->
</div><!--end of container-->
<?php include 'footer.php'?>
</body>
</html>

How to detect link in text by php and send default meta information

How to detect link in some text which is included from chat database and detect default meta information and put it to text like on discord.
ATTACHMENT
CODE
<?
session_start();
include('../../php/connect.php');
if(isset($_GET['uid']) && isset($_GET['cid']) && isset($_SESSION['user'])) {
$uid = $_GET['uid'];
$user = $_SESSION['user'];
$check = mysqli_query($con, "SELECT * FROM users WHERE id = '$uid'");
$ch = mysqli_fetch_array($check);
if($ch['username'] == $user) {
$cid = $_GET['cid'];
$uq = mysqli_query($con, "SELECT * FROM users WHERE id = '$cid'");
$u = mysqli_fetch_array($uq);
$asd = $u['username'];
$photo = $ch['photo'];
$query = mysqli_query($con, "SELECT * FROM users WHERE username = '$asd'");
$q = mysqli_fetch_array($query);
$row = mysqli_query($con, "SELECT * FROM messages WHERE (user1,user2,type) = ('$user','$asd','message') OR (user2,user1,type) = ('$user','$asd','message') ORDER BY id ASC");
while($result = mysqli_fetch_object($row)) { $date = date_create($result->sent); $time = date_format($date, 'H:i'); $date = date_format($date, 'd.m.Y');
?>
<div class="message" id="message">
<div class="hr-text">
<span>
<? echo $date; ?>
</span>
</div>
<div id="avatar" class="avatar-u"><img src="<? if($result->user1 == $asd) { echo '../../img/avatars/'.$q['photo']; } elseif($result->user1 == $user) { echo '../../img/avatars/'.$photo; } ?>">
</div>
<span class="username-u">
<? echo $result->user1; ?>
</span>
<span class="time">
<? echo $time; ?>
</span>
<div class="message-content">
<? echo '<xmp>'.$result->content.'</xmp>'; ?>
</div>
</div>
<?
}
} else {
header('location: ../index.php');
}
} else {
header('location: ../index.php');
}
?>
AJAX imports this site to #content-frame every 750ms.
I just want for example paste detected link to <a> element and then write some meta information like in ATTACHMENT upper

How to get php messages in order

I have made a private message board but when I logged in as the other user it move all the messages to the front which you have just posted. How can I keep messages in the same order?
Please find below the code:
$grab_messages = mysql_query("SELECT * FROM pvt_messages WHERE user_to='$username' && user_from='$user' && opened='no'");
$numrows_read = mysql_numrows($grab_messages);
if ($numrows_read != 0) {
while ($get_msg = mysql_fetch_assoc($grab_messages)) {
$id = $get_msg['id'];
$user_from = $get_msg['user_from'];
$user_to = $get_msg['user_to'];
$msg_body = $get_msg['msg_body'];
$date = $get_msg['date'];
$opened = $get_msg['opened'];
$check_pic = mysql_query("SELECT profile_pic FROM users WHERE username='$user'");
$get_pic_row = mysql_fetch_assoc($check_pic);
$profile_pic_db = $get_pic_row['profile_pic'];
if ($profile_pic_db == "") {
$profile_pic = "../img/QNdefualt.jpg";
}
else
{
$profile_pic = "$profile_pic_db";
}
?>
<li class="self">
<div class="avatar"><img src="<?php echo $profile_pic; ?>" draggable="false"/></div>
<div class="msg">
<p><?php echo $msg_body; ?></p>
<time><?php echo $date; ?></time>
</div>
</li>
Main part of code

Delete first post and show second in PHP

I have a question in PHP.
I am creating a website with posts but I can't make the PHP to show just the second post (without show the first).
My code is like this:
<?php
$result = mysqli_query($dbc, "SELECT * FROM projects");
$x = 1;
while($row = mysqli_fetch_array($result)){
$nome = $row['name'];
$conteudo = $row['description'];
$imagem = $row['image'];
$imagem2 = $row['image2'];
?>
<?php static $count2 = 0; if ($count2 == "1") { break; } else { ?>
<div class="content justify" id="projects-<?php echo $x; ?>" >
<?php echo $conteudo; ?>
<?php if(!empty($imagem2)) { ?>
<img class="hide-for-small" src="images/contebt/project/<?php echo $image2; ?>">
<?php }; ?>
</div>
<?php $count2++; } ?>
<?php $x++;}; ?>
With this code I can show just the first post, but I want to show just the second. Can anybody help me, please? Thanks!
This should work with this code :
PHP
<?php
$result = mysqli_query($dbc, "SELECT * FROM projects");
$x = 0;
while($row = mysqli_fetch_array($result)) {
$nome = $row['name'];
$conteudo = $row['description'];
$imagem = $row['image'];
$imagem2 = $row['image2'];
if (!$x) {
$x = 1;
continue;
}
?>
<div class="content justify" id="projects-<?php echo $x; ?>" >
<?php echo $conteudo; ?>
<?php if(!empty($imagem2)) { ?>
<img class="hide-for-small" src="images/contebt/project/<?php echo $image2; ?>">
<?php } ?>
</div>
<?php
$x++;
}
?>
SQL
But you should directly espace the first row directly in mysql usign either limit :
$result = mysqli_query($dbc, "SELECT * FROM projects LIMIT 1, 1");
or where statement :
$result = mysqli_query($dbc, "SELECT * FROM projects where id > 1");

Display partial rows after the first row

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'];
}
}
?>

Categories