Ver Mensaje Individual
  #1 (permalink)  
Antiguo 06/03/2010, 11:31
bitgia
 
Fecha de Ingreso: enero-2010
Mensajes: 14
Antigüedad: 14 años, 3 meses
Puntos: 0
Exclamación Creación de un muro para una red social

Hola,

Mirad, tengo un problema, estoy haciendo una red social, y en este mismo momento estoy en el paso de la creación del muro de mensajes. Tengo un problema, tengo todo el código perfectamente, CSS + PHP + AJAX + jQuery + MySQL, lo tengo todo hecho. Vale, por el momento se envían las notas y se publican perfectamente, pero ahora viene el problema, no hay manera de que el sistema recoja el nombre de la persona (nombre real, no nombre de usuario) que a enviado la nota, y lo inserte en la base de datos, y por lo tanto, no se visualiza quien lo publicó. Por ejemplo queda así:

Cita:
: Hola! Esto es una nota :)
Cuando tendría que quedar por ejemplo así:

Cita:
Juan Francisco: Hola! Esto es una nota :)
Les dejo el código de los archivos para que lo revisen a ver que es lo que puede estar fallando.

Archivo "sendpost.php":

Código:
<?php

require('connect.php');

// 'username' => The username in DB
// 'realname' => The real name
// This get the data of the DB	
if (defined('SEEING_PROFILE')) {
	global $_PROFILE;
	$userInfo = $_PROFILE;
	$user = SEEING_PROFILE;
	$id = $_PROFILE['ID'];
	$profile = true;
	$external = true;
} elseif ($_USER) {
	$profile = true;
	if (MODULE != 'tag' || (MODULE == 'tag' && (!$params))) {
		$userInfo = $_USER;
		$user = $_USER['username'];
		$realname = $_USER['realname'];
		$id = $_USER['ID'];
	}
	else $tags = true;
}
else {
	if (MODULE == 'tag') {
		if (strlen($params) > 0) {
			$profile = true;
			$tags = true;
		}
		else $profile = true;
	}
	else $profile = false;
}

if(empty($_POST['comment'])) die("0");
// If there isn't a comment text, exit

$comment = mysql_real_escape_string(nl2br(strip_tags($_POST['comment'])));
$user= $_USER[$realname];
// This would be a nice place to start customizing - the default user
// You can integrate it to any site and show a different username.

$addon='';
if($_POST['parent']) $addon=',parent='.(int)$_POST['parent'];

mysql_query("INSERT INTO wallpost SET usr='".$user."', comment='".$comment."', dt=NOW()".$addon);

if(mysql_affected_rows($link)==1)
	echo mysql_insert_id($link);
	// If the insert was successful, echo the newly assigned ID
else
	echo '0';
?>

Archivo "notsscrpt.js":

Código:
$(document).ready(function(){
// Executed once all the page elements are loaded

	lastVal = totHistory;
	
	// Create the slider:
	$("#slider").slider({
			value:totHistory,
			min: 1,
			max: totHistory,
			animate: true,
			slide: function(event, ui) {

				if(lastVal>ui.value)
					$(buildQ(lastVal,ui.value)).hide('fast').find('.addComment').remove();
				// Using buildQ to build the jQuery selector
				// If we are moving the slider backward, hide the previous comment

				
				else if(lastVal<ui.value)
					$(buildQ(lastVal,ui.value)).show('fast');
				// Otherwise show it
				
				lastVal = ui.value;
			}
		});
});

var totHistory=0;
// Holds the number of comments

var positions = new Array();
var lastVal;

function addHistory(obj)
{
	/* Gets called on page load for each comment, and on comment submit */
	totHistory++;
	positions.push(obj.id);
}

function buildQ(from,to)
{
	/* Building a jQuery selector from the begin
		and end point of the slide */
	
	if(from>to)
	{
		var tmp=to;
		to=from;
		from=tmp;
	}
	
	from++;
	to++;
	
	var query='';
	for(var i=from;i<to;i++)
	{
		if(i!=from) query+=',';
		query+='.com-'+positions[i-1];
	}

	/* Each comment has an unique com-(Comment ID) class
		that we are using to address it */

	return query;
}

function addComment(where,parent)
{
	/*	This functions gets called from both the "Add a comment" button 
		on the bottom of the page, and the add a reply link.
		It shows the comment submition form */
		
	var $el;

	if($('.waveButton').length) return false;
	// If there already is a comment submition form
	// shown on the page, return and exit
	
	if(!where)
		$el = $('#commentArea');
	else
		$el = $(where).closest('.waveComment');

	if(!parent) parent=0;

	// If we are adding a comment, but there are hidden comments by the slider:
	
	$('.waveComment').show('slow');
	lastVal = totHistory;
	$('#slider').slider('option','value',totHistory);

	// Move the slider to the end point and show all comments
	

	var comment = '<div class="waveComment addComment">\
		\
		<div class="comment">\
			<div class="commentAvatar">\
			<img src="img/demo.png" width="30" height="30" />\
			</div>\
			\
			<div class="commentText">\
			\
			<textarea class="textArea" rows="2" cols="70" name="" />\
			<div><input type="button" class="waveButton" value="Add comment" onclick="addSubmit(this,'+parent+')" /> or <a href="" onclick="cancelAdd(this);return false">cancel</a></div>\
			\
			</div>\
		</div>\
	\
	</div>';
	
	$el.append(comment);
	
	// Append the form
}

function cancelAdd(el)
{
	$(el).closest('.waveComment').remove();
}

function addSubmit(el,parent)
{
	/* Executed when clicking the submit button */
	
	var cText = $(el).closest('.commentText');
	var text = cText.find('textarea').val();
	var wC = $(el).closest('.waveComment');
	
	if(text.length<4)
	{
		alert("Your comment is too short!");
		return false;
	}
	
	$(el).parent().html('<img src="img/ajax_load.gif" width="16" height="16" />');
	// Showing the loading gif animation
	
	// Send an AJAX request:
	$.ajax({
		type: "POST",
		url: "sendpost.php",
		data: "comment="+encodeURIComponent(text)+"&parent="+parent,
		/* Sending both the text and the parent of the comment */
		success: function(msg){
			
			/* PHP returns the automatically assigned ID of the new comment */
			var ins_id = parseInt(msg);
			if(ins_id)
			{
				wC.addClass('com-'+ins_id);
				addHistory({id:ins_id});
				$('#slider').slider('option', 'max', totHistory).slider('option','value',totHistory);
				lastVal=totHistory;
			}
			
			transForm(text,cText);
			// Hiding the form and showing the comment
			
		}
	});

}

function transForm(text,cText)
{
	var tmpStr ='<span class="name">Demo:</span> '+text;
	cText.html(tmpStr);
}

Archivo "dashboard.php" (la página de inicio de los usuarios donde se envían y muestran las notas):

Código:
<?php
$comments_result = mysql_query("SELECT * FROM wallpost ORDER BY id ASC");
// Selecting all the comments ordered by id in ascending order

$comments=array();
$js_history='';

while($row=mysql_fetch_assoc($comments_result))
{
	if($row['parent']==0)
		// If the comment is not a reply to a previous comment, put it into $comments directly
		$comments[$row['id']] = $row;
	else
	{
		if(!$comments[$row['parent']]) continue;
		
		$comments[$row['parent']]['replies'][] = $row;
		// If it is a reply, put it in the 'replies' property of its parent
	}
	
	$js_history.='addHistory({id:"'.$row['id'].'"});'.PHP_EOL;
	// Adds JS history for each comment
}

$js_history='<script type="text/javascript">
'.$js_history.'
</script>'; ?>

// Aquí va el header, creo que no hace falta que ponga el código

</header>
<body>

<?php echo $js_history; ?>
	<!-- Outputting the addHistory functions -->
	
	<div id="wave">
	        <div id="sliderContainer">
	        	<div id="slider"></div>
	            <div class="clear"></div>
	        </div>
	        <div id="commentArea">

	<?php


		foreach($comments as $c)
		{
			showComment($c);

			// Showing each comment
		}

	?>



	        </div>

	        <input type="button" class="waveButtonMain" value="Add a comment" onclick="addComment()" />
	    </div>
Este es todo el código que hace que funcione el sistema de notas. Falta más código por en medio, pero creo que no hace falta ponerlo ya que no tiene nada que ver. Tampoco he incluido los archivos de conexión, ya que si se envían las notas y se publican perfectamente no es problema de la conexión.

¡Saludos!

P.D.: Si necesitan algún dato más no duden en pedir. Gracias.
P.D2.: En todos los archivos que he puesto el código antes piden el archivo de conexión para la base de datos, pero para acortar no lo he puesto.