Jump to content

Help displaying username with posts


mjrzasa

Recommended Posts

Hi guys, I have a website I am just about done with (sidebet.mjrwebdesign.net) and I have users able to register, login, and post comments. I just want the username of each person to be attached to their comments. I am able to use the below to show name and email, so I figured if I just change them and put "username" in the place of email for example, that it should work. Because in my database table there are "name, email, username". I have been trying to figure this out for so long and cant find anything to work.

function UserFullName()     {         return isset($_SESSION['name_of_user'])?$_SESSION['name_of_user']:'';     }      function UserEmail()     {         return isset($_SESSION['email_of_user'])?$_SESSION['email_of_user']:'';     }  $row = mysql_fetch_assoc($result);                        $_SESSION['name_of_user']  = $row['name'];        $_SESSION['email_of_user'] = $row['email'];                        return true; $row = mysql_fetch_assoc($result);        $user_rec['name'] = $row['name'];        $user_rec['email']= $row['email'];       

 
Also I tried this and cant get it to work/dont know where to put it/link to it... please help! Thanks!
<?php>$username = "your_name";$password = "your_password";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
 or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with
$selected = mysql_select_db("examples",$dbhandle) 
  or die("Could not select examples");

//execute the SQL query and return records
$result = mysql_query("SELECT id, model,year FROM cars");

//fetch tha data from the database 
while ($row = mysql_fetch_array($result)) {
   echo "ID:".$row{'id'}." Name:".$row{'model'}."Year: ". //display the results
   $row{'year'}."<br>";
}
//close the connection
mysql_close($dbhandle);
?>
Link to comment
Share on other sites


<?php

class Comment
{
private $data = array();

public function __construct($row)
{
/*
/ The constructor
*/

$this->data = $row;
}

public function markup()
{
/*
/ This method outputs the XHTML markup of the comment
*/

// Setting up an alias, so we don't have to write $this->data every time:
$d = &$this->data;

$link_open = '';
$link_close = '';

if($d['url']){

// If the person has entered a URL when adding a comment,
// define opening and closing hyperlink tags

$link_open = '<a href="'.$d['url'].'">';
$link_close = '</a>';
}

// Converting the time to a UNIX timestamp:
$d['dt'] = strtotime($d['dt']);

// Needed for the default gravatar image:
$url = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]).'/img/default_avatar.gif';

return '

<div class="comment">

<div class="name">'.$link_open.$d['name'].$link_close.'</div>
<div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div>
<p>'.$d['body'].'</p>
</div>


';
}

public static function validate(&$arr)
{
/*
/ This method is used to validate the data sent via AJAX.
/
/ It return true/false depending on whether the data is valid, and populates
/ the $arr array passed as a paremter (notice the ampersand above) with
/ either the valid input data, or the error messages.
*/

$errors = array();
$data = array();

// Using the filter_input function introduced in PHP 5.2.0



// Using the filter with a custom callback function:

if(!($data['body'] = filter_input(INPUT_POST,'body',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
{
$errors['body'] = 'Please enter a comment.';
}


if(!empty($errors)){

// If there are errors, copy the $errors array to $arr:

$arr = $errors;
return false;
}

// If the data is valid, sanitize all the data and copy it to $arr:

foreach($data as $k=>$v){
$arr[$k] = mysql_real_escape_string($v);
}

// Ensure that the email is lower case:

$arr['email'] = strtolower(trim($arr['email']));

return true;

}

private static function validate_text($str)
{
/*
/ This method is used internally as a FILTER_CALLBACK
*/

if(mb_strlen($str,'utf8')<1)
return false;

// Encode all html special characters (<, >, ", & .. etc) and convert
// the new line characters to <br> tags:

$str = nl2br(htmlspecialchars($str));

// Remove the new line characters that are left
$str = str_replace(array(chr(10),chr(13)),'',$str);

return $str;
}

}

?>
Link to comment
Share on other sites

I'm so lost.. I have 4 files that I think are being used here.. my login page that has the textarea that you can type in and submit (index.php), my member page that currently DOES display current users' email and name, but not username...(login-home.php), then this huge file I downloaded and edited, which contains a bunch of functions (include/fg_membersite.php), and a file that contains what is inside the comments that are posted.(comment.class.php).

 

LOGIN form on index.php:

<div id='fg_membersite'>
<form class="loginform" id='login' action='<?php echo $fgmembersite->GetSelfScript(); ?>' method='post' accept-charset='UTF-8'>


<input type='hidden' name='submitted' id='submitted' value='1'/>



<div><span class='error'><?php echo $fgmembersite->GetErrorMessage(); ?></span></div>
<div class='container2'>
    <label for='username' >U:</label>
    <input type='text' name='username' id='username' value='<?php echo $fgmembersite->SafeDisplay('username') ?>' maxlength="50" /><br/>
    <span id='login_username_errorloc' class='error'></span>
</div>
<div class='container2'>
    <label for='password' >P:</label>
    <input type='password' name='password' id='password' maxlength="50" /><br/>
    <span id='login_password_errorloc' class='error'></span>
</div>

<div class='container2'>
    <input id="log" type='submit' name='Submit' value='SUBMIT' />
</div>


</form>

The code in fg_membersite.php that the email and name functions work, but the one i made (MyHandle) does not..

 function UserFullName()
    {
        return isset($_SESSION['name_of_user'])?$_SESSION['name_of_user']:'';
    }

    function UserEmail()
    {
        return isset($_SESSION['email_of_user'])?$_SESSION['email_of_user']:'';
    }

    function MyHandle()
    {
        return isset($_SESSION['username_of_user'])?$_SESSION['username_of_user']:'';
    }

The comment.class inside comment.class.php:

// Converting the time to a UNIX timestamp:
		$d['dt'] = strtotime($d['dt']);
		
		return '
		
			<div class="comment">
								
				
				<div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div>
				<p>'.$d['body'].'</p>
			</div>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.