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
Related
I'm currently doing a private chat (style messenger), and I got a problem..
I have a href a link which sends an ID using GET to another page, the thing is that on the other page I load a jquery script which again sends to another page, suddenly it no longer finds the ID GET, what should I do? I want to actualise the page (the messages) thanks (noted that I'm new, I'm not enough good to use ajax or something..)
message.php
message
<?php
// $allUsers = 'SELECT * FROM members WHERE name LIKE "%cc%" ORDER BY id DESC' / SEARCH MEMBERS
$allUsers = $dbh->query('SELECT * FROM members ORDER BY id DESC LIMIT 0, 5');
if ($allUsers->rowCount() > 0)
{
while ($user = $allUsers->fetch())
{
?>
<div id="s_un_main">
<div class="s_un_main_pun">
<img src="../images/avatar/<?php echo $user['avatar'];?>">
<p><?php echo $user['name']; ?></p>
</div>
<div class="s_un_main_pdeux">
<a class="private" target="_blank" href="private.php?id=<?php echo $user['id']; ?>">Message</a>
</div>
</div>
<?php
}
}
else
{
echo "<p>" . "Aucun utilisateur trouvé. " . "</p>";
}
?>
private.php
private
<div id="get_name">
<?php
// USERINFO
if (isset($_SESSION['id']) AND !empty($_SESSION['id']))
{
$getid = $_GET['id'];
$req = $dbh->prepare('SELECT * FROM members WHERE id = :getid');
$req->bindValue('getid', $getid);
$req->execute();
$userinfo = $req->fetch();
}
?>
<div>
<img id="img_header" width="50" src="../images/avatar/<?php echo $userinfo['avatar'];?>">
</div>
<?php echo "<p>" . $userinfo['name'] . "</p>"; ?>
</div>
<section id="zz">
<div id="show_msg">
<?php
// AFFICHER LES MESSAGES
$getid = $_GET['id'];
$takeMsg = $dbh->prepare('SELECT * FROM private WHERE id_sender = :sender AND id_receipter = :receipter OR id_sender = :senderr AND id_receipter = :receipterr');
$takeMsg->bindValue('sender', $_SESSION['id']);
$takeMsg->bindValue('receipter', $getid);
$takeMsg->bindValue('senderr', $getid);
$takeMsg->bindValue('receipterr', $_SESSION['id']);
$takeMsg->execute();
while ($message = $takeMsg->fetch())
{
if ($message['id_receipter'] == $_SESSION['id'])
{
?>
<p style="color: red"><?php echo $message['message']; ?></p>
<?php
}
elseif ($message['id_receipter'] == $_GET['id'])
{
?>
<p style="color: green "><?php echo $message['message']; ?></p>
<?php
}
}
?>
</div>
</section>
<form id="private_form" method="POST" action="">
<textarea name="message"></textarea>
<input type="submit" name="send"></input>
</form>
<script>
setInterval('load_messages()', 1500);
function load_messages()
{
$('#zz').load('private_message.php');
}
</script>
private_message.php
error
<!-- DB -->
<?php include("../db/db.php"); ?>
<!-- DB -->
<?php
// AFFICHER LES MESSAGES
$getid = $_GET['id'];
var_dump($getid);
$takeMsg = $dbh->prepare('SELECT * FROM private WHERE id_sender = :sender AND id_receipter = :receipter OR id_sender = :senderr AND id_receipter = :receipterr');
$takeMsg->bindValue('sender', $_SESSION['id']);
$takeMsg->bindValue('receipter', $getid);
$takeMsg->bindValue('senderr', $getid);
$takeMsg->bindValue('receipterr', $_SESSION['id']);
$takeMsg->execute();
while ($message = $takeMsg->fetch())
{
if ($message['id_receipter'] == $_SESSION['id'])
{
?>
<p style="color: red"><?php echo $message['message']; ?></p>
<?php
}
elseif ($message['id_receipter'] == $_GET['id'])
{
?>
<p style="color: green "><?php echo $message['message']; ?></p>
<?php
}
}
?>
var_dump($id) = not found
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
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");
How can i get all records which are stored in the mysqli database against certain email id.
Like I want to get all the records of question field which are stored against id#check.com, to get that what i have done so far is mentioned below. but my for loop codes prints only www.google.com. how can i get other two field?
<?php
$email = $_SESSION["email"];
$prevq = "Select * FROM `askadoc` WHERE `email` = '$email'";
$result = mysqli_query($conn, $prevq);
$prevq = "";
$location = "";
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)) {
$prevq = $row["question"];
$date = $row["date"];
}
}
?>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
<?php
for($i=0; $i<=count($email); $i++){
echo $date;
echo "<br>";
echo $prevq;
echo "<br>";
}
?>
</div>
</div>
Try this it will work:
<?php
$email = $_SESSION["email"];
$prevq = "Select * FROM `askadoc` WHERE `email` = '$email'";
$result = mysqli_query($conn, $prevq);
$prevq = "";
$location = "";
if (mysqli_num_rows($result) > 0):
?>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
<?php
while ($row = mysqli_fetch_assoc($result)) :
$prevq = $row["question"];
$date = $row["date"];
echo $date;
echo "<br>";
echo $prevq;
echo "<br>";
endwhile;
?>
</div>
</div>
<?php
endif;
?>
Just write your while loop inside your div
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
<?php
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $prevq = $row["question"];
echo $date = $row["date"];
}
}
?>
</div>
</div>
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().