I got this simple script , unfortunately It has a issue , its only pulling the last result of the table called site where its supposed to replace badwords/bannedwords/smiles , here's the system That I've made, thanks!
<?php
$select=mysql_query("SELECT * FROM chat ORDER BY id DESC");
while($rows=mysql_fetch_assoc($select)) {
$mid=$rows['id'];
$name=$rows['name'];
$text=$rows['message'];
$date=$rows['date'];
$sitechoose = mysql_query("SELECT * FROM site");
while($change = mysql_fetch_assoc($sitechoose)){
$o = array($change['original'],);
$r = array($change['changed'],);
$messages = str_replace($o, $r, $text);
}
echo "<div class='chat-content'>
<div class='background chat-title'>
<a href='user.php?id=".$name."'>
<span class='user-name user-group-".$power."'>".$name."</span>
</a>
<div class='chat-date float-r'>
<time datetime='2014-12-06T16:56:36+00:00'>".$date."</time>
</div>
</div>
<div class='chat-message'>".$messages."</div>
</div>";
}
?>
It is only pulling out the last result inserted in the site table , I wonder why and how can I fix it?
After looking what the problem was we finally found it :)
<?php
$select = mysql_query("SELECT * FROM chat ORDER BY id DESC");
while($rows=mysql_fetch_assoc($select)) {
$mid=$rows['id'];
$name=$rows['name'];
$text=$rows['message'];
$date=$rows['date'];
$sitechoose = mysql_query("SELECT * FROM site");
while($change = mysql_fetch_assoc($sitechoose)) {
$o = $change['original'];
$r = $change['changed'];
$text = str_replace($o, $r, $text);
}
echo "<div class='chat-content'><div class='background chat-title'><a href='user.php?id=".$name."'><span class='user-name user-group-".$power."'>".$name."</span></a><div class='chat-date float-r'><time datetime='2014-12-06T16:56:36+00:00'>".$date."</time></div></div><div class='chat-message'>".$text."</div></div>";
}
?>
Related
i am making a profile.php page and i would like it to show the user all his projects, this is my first time doing something like this, and i cant find a solution for it
code to show the projects :
$username = $_SESSION['username'];
if ($_SESSION['type'] = "developer"){
$q = "SELECT * FROM `projects` WHERE `developer` = '$username'";
$result = mysqli_query($con,$q);
$row = mysqli_fetch_array($result);
$numrows = mysqli_num_rows($result);
if(empty($numrows)){
echo'
<div class="row">
<div class="col-lg-12 newp">
<p><span class="glyphicon glyphicon-plus plus"></span>Add a new project</p>
</div>
</div>';
}else{
$p_id = $row['project_id'];
$p_name = $row['project_name'];
$p_owner = $row['owner'];
$p_developer = $row['developer'];
$p_price = $row['price'];
$p_date_started = $row['date_started'];
$p_date_end = $row['date_end'];
$p_paid = $row['paid'];
//foreach project the user has do this :
echo"
<div class=\"row\">
<div class=\"col-lg-12\">
<p>$p_name </br>owner : $p_owner, developer : $p_developer, price : $p_price$</br>started : $p_date_started, ends :$p_date_end, paid :$p_paid</p>
</div>
</div>";
}
}
} else {
while($row = mysqli_fetch_array($result)) {
$p_id = $row['project_id'];
...
Besides the other answer given:
You're presently assigning instead of comparing with
if ($_SESSION['type'] = "developer"){...}
^
which the above will fail and everything inside that conditional statement and should read as
if ($_SESSION['type'] == "developer"){...}
^^
with 2 equal signs.
Make sure the session has also been started, it's required when using sessions.
session_start();
You're also open to an SQL injection. Use a prepared statement:
https://en.wikipedia.org/wiki/Prepared_statement
I have the below code and cant make it work to save my life. It just needs to have one of 3 links depending on which department it is from. If department = 1 then URL1.com and so on.
<?php
// Make a MySQL Connection
mysql_connect("localhost","dfasrgasdg","asdgasdgasdg") or die(mysql_error());
mysql_select_db("asdgasdgash") or die(mysql_error());
// Get all the data from the "example" table
$album = mysql_query("SELECT * FROM gallery_albums WHERE draft = 0 ORDER BY gallery_id DESC
LIMIT 0,3")
or die(mysql_error());
while($album1 = mysql_fetch_array( $album )) {
if ($album1['department_id'] == '1'); {
$albumURL = "http://ufire.sabinalcanyon.org/gallery.php?gallery_id=".$album1['gallery_id'];
}
if ($album1['department_id'] == '2'); {
$albumURL = "http://ems.sabinalcanyon.org/gallery.php?gallery_id=".$album1['gallery_id'];
}
if ($album1['department_id'] == '3'); {
$albumURL = "http://vfire.sabinalcanyon.org/gallery.php?gallery_id=".$album1['gallery_id'];
}
echo "
<li class=\"clearfix\">
<a href=\"".$albumURL."\" class=\"thumbnail\">
<img src=\"".$album1['poster_image']."\" width=\"50\" height=\"50\" alt=\"\" />
</a>
".$album1['title']."
<div class=\"entry-excerpt\">".$album1['desc']."</div>
</li>";
}
?>
u do know that you have a single '=' for comparison when you should have 2?
got a problem with my while statement, it will not display the first row & I have no idea why, any help would be appreciated.
<?php
$user = $_SESSION['username'];
$result = mysql_query("SELECT *FROM bookwrite WHERE username = '$user' ", $connection);
if (mysql_fetch_array($result)==0){
$class = "hideMe";
$firstnameResult = mysql_query("SELECT * FROM users WHERE username = '$user' ", $connection);
$row2 = mysql_fetch_array($firstnameResult);
echo "<div class=\"platOptions welcome\"><div class=\"welcomeinfo\"><h1>Welcome!</h1>Welcome to Little Quill, ".$row2["firstName"].". We see you are new to the platform. To get started by adding your first post please click the \"New Entry\" button below <a class=\"newentryBTN\" href=\"newEntry.php\">New Entry</a></div></div>";
} else {
$class = "";
while ($row = mysql_fetch_array($result)) {
echo nl2br("<li class=\"editable\" id=\"".$row["id"]."\">"."<div class=\"entryInfo clearfix\">"."<div class=\"statusWrapper\"><div class=\"status\"></div><div class=\"statusIcon\"></div></div>"."<h1>".$row["post_title"]."</h1>"."<div class=\"timeline-date entryDate\">".$row["post_date"]."</div>"."</div>"."<p contentEditable=\"true\">".$row["post_details"])."</p><a class=\"deleteBTN\"href='delete.php?id=".$row["id"]."'>x</a></li>";
}
}
?>
The first result is consumed by mysql_fetch_array, so when you start your loop you are consuming the second item. Try this code out, I basically assign the next value, at the end of the loop.
Also, I 100% agree with Jason McCreary, and I will make the comment that your current query is subject to SQL Injection. However, this should solve your first problem, helping you take a baby step towards to solution that's ultimately correct.
<?php
$user = $_SESSION['username'];
$result = mysql_query("SELECT * FROM bookwrite WHERE username = '$user' ", $connection);
$bookwrite_row = mysql_fetch_array($result);
if($bookwrite_row) {
while($bookwrite_row) {
echo nl2br("<li class=\"editable\" id=\"".$row["id"]."\">"."<div class=\"entryInfo clearfix\">"."<div class=\"statusWrapper\"><div class=\"status\"></div><div class=\"statusIcon\"></div></div>"."<h1>".$row["post_title"]."</h1>"."<div class=\"timeline-date entryDate\">".$row["post_date"]."</div>"."</div>"."<p contentEditable=\"true\">".$row["post_details"])."</p><a class=\"deleteBTN\"href='delete.php?id=".$row["id"]."'>x</a></li>";
$bookwrite_row = mysql_fetch_array($result);
}
} else {
$class = "hideMe";
$firstnameResult = mysql_query("SELECT * FROM users WHERE username = '$user' ", $connection);
$row2 = mysql_fetch_array($firstnameResult);
echo "<div class=\"platOptions welcome\"><div class=\"welcomeinfo\"><h1>Welcome!</h1>Welcome to Little Quill, ".$row2["firstName"].". We see you are new to the platform. To get started by adding your first post please click the \"New Entry\" button below <a class=\"newentryBTN\" href=\"newEntry.php\">New Entry</a></div></div>";
}
if (mysql_num_rows($result)==0){
$class = "hideMe";
...
here is my problem - i have Div with include "file.php" with content of Database then i Insert new data to database and want to reload .load() file.php to div but content is same until i refresh the page. Someone who know what it is ?
File.php
<?php
include "../lib/dbconnect.php";
$class = $_GET['class'];
$get_posts = mysql_query("SELECT content, date, author, author_id FROM classPosts WHERE class = '$class' ORDER BY id DESC");
while (list($content, $time, $author, $author_id) = mysql_fetch_row($get_posts)){
$get_user_name = mysql_query("SELECT name, lastName FROM users WHERE nick = '$author'");
while (list($name, $lastName) = mysql_fetch_row($get_user_name)){
$time = new Cokidoo_DateTime("#" . $time);
echo "
<div class=\"div\">
<div class=crop-small title=\"$author\">
<a href=/user/user.php?user=$author_id><img src=/user/pics/$author_id.jpg class=img-small></a>
</div>
<span class=small-text><b style=\"color: rgb(100,100,100)\">$name $lastName</b><br>
<span class=\"small-text\">Přezdívka <b style=\"color: rgb(100,100,100)\">$author</b></span><br>
Přidáno $time</span><p><br></p>
<span class=\"small-text\">$content</span>
</div>
";
}
}
?>
there is Javascript
if(data.success)
{
$("#class_posts").fadeOut(function(){
$("#new_post").hide(0);
$("#class_posts").load("../trida/get_posts.php");
$("#class_posts").fadeIn();
$("#new_post_text").html("");
});
}
try appending a random number on the end as a query string - to prevent caching.
var randNum = Math.floor(Math.random() * 999999);
$("#class_posts").load("../trida/get_posts.php?"+randNum);
Basically I have two files, group.php and class.Groups.php
I am trying to print out the 20 last messages posted to the group, however I cannot figure it out as I am pretty new to this! Any help welcomed.
The problem I am having is that the function I am calling is inside a class. Just dont know how to do the while loop and printing.
Would also welcome suggestions and feedback on my code structure & layout.
Thanks.
class.Groups.php
<?php include("config.php");
// Get Group Details
class Group {
var $groupID;
var $groupName;
var $groupDescription;
var $groupMessage;
function group_details($ID) {
$group_details = mysql_query("SELECT * FROM Groups WHERE GroupID = '".$ID."' LIMIT 1");
if (mysql_num_rows($group_details) == 1) {
$group_details_row = mysql_fetch_assoc($group_details);
$this->groupID = $group_details_row ['GroupID'];
$this->groupName = $group_details_row ['Name'];
$this->groupDescription = $group_details_row ['Description'];
}
}
function group_member($ID) {
$group_member = mysql_query("SELECT * FROM GroupMembers WHERE GroupID = '".$ID."' AND UserID = '".$_SESSION['UserID']."' LIMIT 1");
if (mysql_num_rows($group_member) == 1) {
return 1;
}
else {
return null;
}
}
function group_messages($ID) {
$group_messages = mysql_query("SELECT * FROM GroupMessages INNER JOIN Users ON Users.UserID = GroupMessages.UserID WHERE GroupID = '".$ID."' LIMIT 20");
while ($group_messages_row = mysql_fetch_assoc($group_messages)) {
$this->groupMessage = $group_messages_row['Message'];
}
}
}
?>
group.php
<?php include("includes/class.Groups.php");
$group = new Group;
$group->group_details($_GET['ID']);
if ($_SESSION['LoggedIn'] == 1) {
if ($group->group_member($_GET['ID'])) {
include("includes/header.php"); ?>
<div id="container">
<div id="menu"><?php include("includes/sidebar.php"); ?></div>
<div id="main">
<h2><?php echo $group->groupName; ?></h2>
<p><?php echo $group->groupDescription; ?></p>
<h3>Messages</h3>
<?php
while ($group->group_messages($_GET['ID'])) {
echo $group->groupMessage;
}
?>
</div>
</div>
<?php
}
else {
echo "You are not member of this group.";
}
}
?>
You cannot use $group->group_messages() inside a while, because:
It doesn't return anything (return evaluates to false)
It goes through all the rows in one call, while you are expecting it to return one row at a time
You can rewrite your class code to collect all rows in a variable, return it and the iterate on it using foreach loop
// class.Groups.php
function group_messages($ID) {
$group_messages = array();
$group_messages = mysql_query("SELECT * FROM GroupMessages INNER JOIN Users ON Users.UserID = GroupMessages.UserID WHERE GroupID = '".$ID."' LIMIT 20");
while ($group_messages_row = mysql_fetch_assoc($group_messages)) {
$group_messages[] = $group_messages_row['Message'];
}
return $group_messages;
}
// groups.php
<h3>Messages</h3>
<?php
foreach ($group->group_messages($_GET['ID']) as $group_message) {
echo $group_message;
}
?>
You seem to have an extra curly brace in this chuck of code:
<?php
while ($group->group_messages($_GET['ID'])) {
echo $group->groupMessage; }
} ?>