Jump to content
php.lv forumi

Vis gatvas bet vēl biki ar chat ludzu palīdziet


Snaip3Rs

Recommended Posts

visiem kam vajag chatu bet nemāk uztaisīt lūkur viegls un gatavs chats bet kā var uztaisīt lai chatā varētu rakstīt tikai ipb lietotāji?

<?php



	 $shouts = 10; // Number of shouts to be displayed
	 $maxlength_name = "20"; // Maximum length of name
	 $maxlength_text = "400"; // Maximum length of text
	 $break_name = "15"; // Break name after characters
						 // without space
	 $break_text = "15"; // Break text after characters
						 // without space
	 $db_server = "localhost"; // MySQL hostname
	 $db_name = "jzvac"; // MySQL database
	 $db_user = "jzvac"; // MySQL username
	 $db_password = ""; // MySQL password
	 $db_table = "mysql_shoutbox"; // Table name (table
								   // will be created
								   // automatically)




 //connect to database
 @mysql_connect($db_server,$db_user,$db_password) or die("Database server connection failed. Check variables \$db_server, \$db_user and \$db_password");
 @mysql_select_db($db_name) or die("Selecting database failed. Check variable \$db_name");

 //create table
 if(isset($_GET["install"])){
@mysql_query("CREATE TABLE $db_table (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,name VARCHAR(255) NOT NULL,comment VARCHAR(255) NOT NULL);") or die ("Creating table failed: ".@mysql_error());
?>Table has been created successfully. <a href="<?php echo $_SERVER["PHP_SELF"]; ?>">Click here to continue.</a><?php
 //print shoutbox
 }else{
?>
<p>
 <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<input type="text" value="Your name" name="input_name" maxlength="<?php echo $maxlength_name; ?>" onfocus="if(this.value=='Your name'){this.value='';}" onblur="if(this.value==''){this.value='Your name';}" /><br />
<input type="text" value="Your text" name="input_text" maxlength="<?php echo $maxlength_text; ?>" onfocus="if(this.value=='Your text'){this.value='';}" onblur="if(this.value==''){this.value='Your text';}" /><br />
<input type="submit" value="Shout!" />
 </form>
</p>
<hr />
<p>
<?php
//function to break text after x characters
function str_break($str,$maxlen){
  $nobr = 0;
  $len = strlen($str);

  for($i=0;$i<$len;$i++){
	if(($str[$i]!=' ') && ($str[$i]!='-') && ($str[$i]!="\n")){
	  $nobr++;
	}else{
	  $nobr = 0;

	  if($maxlen+$i>$len){
		$str_br .= substr($str,$i);
		break;
	  }
	}

	if($nobr>$maxlen){
	  $str_br .= ' '.$str[$i];
	  $nobr = 1;
	}else{
	  $str_br .= $str[$i];
	}
  }

  return $str_br;
}

//number of shouts to be displayed
$display = (isset($_GET["show"]) ? "all" : $shouts);

//print links to either show all or specific number of shouts
if($display == "all"){
  ?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>">View small shoutbox</a><?php
}else{
  ?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?show=all">View all shouts</a><?php
}
?>
</p><p>
<?php
//insert new shout
$input_name = $_POST["input_name"];
$input_text = $_POST["input_text"];

//check if form has been submitted
if(isset($input_name) && isset($input_text) && $input_name!="Your name" && $input_text!="Your text" && strlen($input_name)>0 && strlen($input_text)>0){
  //get last name and comment
  $result = @mysql_query("SELECT * FROM $db_table ORDER BY id DESC LIMIT 0,1;");
  $row = @mysql_fetch_array($result);

  $input_name = str_break($input_name,$break_name); //break name
  $input_name = str_replace("<","<",$input_name); //prevent html input
  $input_name = str_replace(">",">",$input_name); //prevent html input
  $input_name = stripslashes($input_name); //strip slashes

  $input_text = str_break($input_text,$break_text); //break text
  $input_text = str_replace("<","<",$input_text); //prevent html input
  $input_text = str_replace(">",">",$input_text); //prevent html input
  $input_text = stripslashes($input_text); //strip slashes

  if($row["name"] != $input_name && $row["comment"] != $input_text){
	@mysql_query("INSERT INTO $db_table (name,comment) VALUES ('$input_name','$input_text');"); //insert name and shout
  }
}

//prints shouts
$result = @mysql_query("SELECT * FROM $db_table ORDER BY id DESC;");

if($display == "all"){
  while($row=@mysql_fetch_array($result)){
	?><p><strong><?php echo $row["name"]; ?>:</strong> <?php echo $row["comment"]; ?></p><?php
  }
}else{
  for($i=0;$i<$display && ($row=@mysql_fetch_array($result));$i++){
	?><p><strong><?php echo $row["name"]; ?>:</strong> <?php echo $row["comment"]; ?></p><?php
  }
}
 }

 //close database connection
 @mysql_close();
?>
</p>

Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...