PDA

Arată versiune īntreagă : Tutorial Forum



glummyro
23.09.2009, 13:26
Salut,

Un tutorial cum sa-ti faci propriul tau forum :)

1. SQL:


Table forum_question
CREATE TABLE `forum_question` (
`id` int(4) NOT NULL auto_increment,
`topic` varchar(255) NOT NULL default '',
`detail` longtext NOT NULL,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`datetime` varchar(25) NOT NULL default '',
`view` int(4) NOT NULL default '0',
`reply` int(4) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

Table forum_answer
CREATE TABLE `forum_answer` (
`question_id` int(4) NOT NULL default '0',
`a_id` int(4) NOT NULL default '0',
`a_name` varchar(65) NOT NULL default '',
`a_email` varchar(65) NOT NULL default '',
`a_answer` longtext NOT NULL,
`a_datetime` varchar(25) NOT NULL default '',
KEY `a_id` (`a_id`)
) TYPE=MyISAM;


2. Creaza fisierul create_topic.php


<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="add_topic.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>
</tr>
<tr>
<td width="14%"><strong>Topic</strong></td>
<td width="2%">:</td>
<td width="84%"><input name="topic" type="text" id="topic" size="50" /></td>
</tr>
<tr>
<td valign="top"><strong>Detail</strong></td>
<td valign="top">:</td>
<td><textarea name="detail" cols="50" rows="3" id="detail"></textarea></td>
</tr>
<tr>
<td><strong>Name</strong></td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50" /></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name="email" type="text" id="email" size="50" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>


3. Creaza fisierul add_topic. php


<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="forum_question"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// get data that sent from form
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];

$datetime=date("d/m/y h:i:s"); //create date time

$sql="INSERT INTO $tbl_name(topic, detail, name, email, datetime)VALUES('$topic', '$detail', '$name', '$email', '$datetime')";
$result=mysql_query($sql);

if($result){
echo "Successful<BR>";
echo "<a href=main_forum.php>View your topic</a>";
}
else {
echo "ERROR";
}
mysql_close();
?>


4. Creaza fisierul main_forum.php


<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="forum_question"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
$result=mysql_query($sql);
?>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
// Exit looping and close connection
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#E6E6E6"><a href="create_topic.php"><strong>Create New Topic</strong> </a></td>
</tr>
</table>


5. Creaza fisierul view_topic.php


<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="forum_question"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];

$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF">
<tr>
<td bgcolor="#F8F7F1"><strong><? echo $rows['topic']; ?></strong></td>
</tr>

<tr>
<td bgcolor="#F8F7F1"><? echo $rows['detail']; ?></td>
</tr>

<tr>
<td bgcolor="#F8F7F1"><strong>By :</strong> <? echo $rows['name']; ?> <strong>Email : </strong><? echo $rows['email'];?></td>
</tr>

<tr>
<td bgcolor="#F8F7F1"><strong>Date/time : </strong><? echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>
<?php
$tbl_name2="forum_answer"; // Switch to table "forum_answer"

$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'";
$result2=mysql_query($sql2);

while($rows=mysql_fetch_array($result2)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td bgcolor="#F8F7F1"><strong>ID</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><? echo $rows['a_id']; ?></td>
</tr>
<tr>
<td width="18%" bgcolor="#F8F7F1"><strong>Name</strong></td>
<td width="5%" bgcolor="#F8F7F1">:</td>
<td width="77%" bgcolor="#F8F7F1"><? echo $rows['a_name']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Email</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><? echo $rows['a_email']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Answer</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><? echo $rows['a_answer']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Date/Time</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><? echo $rows['a_datetime']; ?></td>
</tr>
</table></td>
</tr>
</table><br>

<?
}

$sql3="SELECT view FROM $tbl_name WHERE id='$id'";
$result3=mysql_query($sql3);

$rows=mysql_fetch_array($result3);
$view=$rows['view'];

// if have no counter value set counter = 1
if(empty($view)){
$view=1;
$sql4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'";
$result4=mysql_query($sql4);
}

// count more value
$addview=$view+1;
$sql5="update $tbl_name set view='$addview' WHERE id='$id'";
$result5=mysql_query($sql5);

mysql_close();
?>
<BR>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="add_answer.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="18%"><strong>Name</strong></td>
<td width="3%">:</td>
<td width="79%"><input name="a_name" type="text" id="a_name" size="45"></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name="a_email" type="text" id="a_email" size="45"></td>
</tr>
<tr>
<td valign="top"><strong>Answer</strong></td>
<td valign="top">:</td>
<td><textarea name="a_answer" cols="45" rows="3" id="a_answer"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="id" type="hidden" value="<? echo $id; ?>"></td>
<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>


6. Creaza fisierul add_answer.php


<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="forum_answer"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get value of id that sent from hidden field
$id=$_POST['id'];

// Find highest answer number.
$sql="SELECT MAX(a_id) AS Maxa_id FROM $tbl_name WHERE question_id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

// add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
if ($rows) {
$Max_id = $rows['Maxa_id']+1;
}
else {
$Max_id = 1;
}

// get values that sent from form
$a_name=$_POST['a_name'];
$a_email=$_POST['a_email'];
$a_answer=$_POST['a_answer'];

$datetime=date("d/m/y H:i:s"); // create date and time

// Insert answer
$sql2="INSERT INTO $tbl_name(question_id, a_id, a_name, a_email, a_answer, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_answer', '$datetime')";
$result2=mysql_query($sql2);

if($result2){
echo "Successful<BR>";
echo "<a href='view_topic.php?id=".$id."'>View your answer</a>";

// If added new answer, add value +1 in reply column
$tbl_name2="forum_question";
$sql3="UPDATE $tbl_name2 SET reply='$Max_id' WHERE id='$id'";
$result3=mysql_query($sql3);

}
else {
echo "ERROR";
}

mysql_close();
?>



Sper sa va fie de folos :)

Xro1
23.09.2009, 13:44
Multumim de tutorial..dar din cate stiu eu foarte asemanator gasim si in forumurile deja facute..sa inteleg ca ce ne oferi tu e complet pt un forum ?P.S si daca imi fac un forum cu ceea ce oferi tu am drepturi de autor ?

glummyro
23.09.2009, 14:11
:) eu am pus un tutorial...pentru cei care vor sa aibe propriul lor forum :) asta este un inceput :)

Xro1
23.09.2009, 14:12
ok multumesc inca o data:)!

MaDaLyN17
01.10.2009, 15:35
am o problema, mie imi da urmatoarea eroare in baza de date:


MySQL said:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Table forum_question
CREATE TABLE `forum_question` (
`id` int(4) NOT NULL aut' at line 1

de ce?

Dennis
01.10.2009, 16:19
lipseste un tabel in baza de date

CREATE TABLE `forum_question` (
`id` int(4) NOT NULL aut' at line 1

MaDaLyN17
01.10.2009, 16:29
sa inteleg ca ar trebui sa arate asa:


Table forum_question
CREATE TABLE `forum_question` (
`id` int(4) NOT NULL aut' at line 1
`id` int(4) NOT NULL auto_increment,
`topic` varchar(255) NOT NULL default '',
`detail` longtext NOT NULL,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`datetime` varchar(25) NOT NULL default '',
`view` int(4) NOT NULL default '0',
`reply` int(4) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

Table forum_answer
CREATE TABLE `forum_answer` (
`question_id` int(4) NOT NULL default '0',
`a_id` int(4) NOT NULL default '0',
`a_name` varchar(65) NOT NULL default '',
`a_email` varchar(65) NOT NULL default '',
`a_answer` longtext NOT NULL,
`a_datetime` varchar(25) NOT NULL default '',
KEY `a_id` (`a_id`)
) TYPE=MyISAM;

eu la baza de date imi prind urechile :|

Dennis
01.10.2009, 16:33
NU, seteaza ca `id` int(4) NOT NULL auto_increment,
...sa fie pe auto_increment - eroarea spune ca `id` NU este setat pe auto_increment, scuze, m-am exprimat gresit

MaDaLyN17
01.10.2009, 16:42
... pai ... daca stau sa ma uit mai bine, observ ca fix asa scrie:
`id` int(4) NOT NULL auto_increment,
sau adica sa sterg NOT NULL din fata o.O

sau cum il fac sa fie pe auto_increment

eu ma uit la codu asta ca la felu 14 :|
nu ma pricep deloc la baza de date.

glummyro
01.10.2009, 17:19
incearca asa !



Table forum_question
CREATE TABLE `forum_question` (
`id` int(4) NOT NULL auto_increment,
`topic` varchar(255) NOT NULL default '',
`detail` longtext NOT NULL,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`datetime` varchar(25) NOT NULL default '',
`view` int(4) NOT NULL default '0',
`reply` int(4) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

--
-- ``
--

Table forum_answer
CREATE TABLE `forum_answer` (
`question_id` int(4) NOT NULL default '0',
`a_id` int(4) NOT NULL default '0',
`a_name` varchar(65) NOT NULL default '',
`a_email` varchar(65) NOT NULL default '',
`a_answer` longtext NOT NULL,
`a_datetime` varchar(25) NOT NULL default '',
KEY `a_id` (`a_id`)
) TYPE=MyISAM;

MaDaLyN17
01.10.2009, 17:45
tot la fel :|
Am atasat o imagina sa vedeti eroarea mai in detaliat :?

glummyro
01.10.2009, 17:47
Cateva greseli :)


CREATE TABLE `forum_question` (
`id` int(4) NOT NULL auto_increment,
`topic` varchar(255) NOT NULL default '',
`detail` longtext NOT NULL,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`datetime` varchar(25) NOT NULL default '',
`view` int(4) NOT NULL default '0',
`reply` int(4) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

--
-- ``
--

CREATE TABLE `forum_answer` (
`question_id` int(4) NOT NULL default '0',
`a_id` int(4) NOT NULL default '0',
`a_name` varchar(65) NOT NULL default '',
`a_email` varchar(65) NOT NULL default '',
`a_answer` longtext NOT NULL,
`a_datetime` varchar(25) NOT NULL default '',
KEY `a_id` (`a_id`)
) TYPE=MyISAM;

MaDaLyN17
01.10.2009, 18:06
A mers :D Mersi.
Si cum ar trebui sa sterg de pe acolo? topicurile? :D

EDIT: Am gasit prin baza de date :D
Mersi mult, chiar aveam nevoie de asa ceva ;)

glummyro
01.10.2009, 18:08
asta e un tutorial cum sa-ti incepi propriul forum...nu e complet :D

MaDaLyN17
01.10.2009, 18:22
nu pentru forum am eu nevoie :-j
Oricum, mersi mult ;)

MaDaLyN17
01.10.2009, 19:57
glummyro: ma mai poti ajuta cu o chestie :D ?
Cum pot face, ca dupa un anumit numar de topicuri, sa treaca pe o a II-a pagina, a III-a si tot asa....

Se poate? Stiu ca trebuia adaugat ceva in baza de date si facute cateva modificari in script php :)
Le ai? :D

MaDaLyN17
02.10.2009, 12:46
mai am o problema :(
am facut rost de un script si imi cere

$coloana_bd = "title"; // numele coloanei care va fi afisata
ce coloana din db trebuie sa scriu?
ca nu inteleg.... am cautat peste tot acolo si nu am gasit nimic in care sa scrie ceva de vro coloana :|

Stie cineva? :(

d-zel
06.10.2009, 14:31
scuze ca am gresit probabil nu am confidurat eu ceva calumea!

MaDaLyN17
06.10.2009, 14:35
Scuze @glummyro ca te critic dar eu consider ca e mai bine sa nu pui nimic decat sa pui ceva ce nu functioneaza:| asa ne chinuim toti sa vedem unde e eroare!

ce tot spui?
mie imi merge perfect :B

d-zel
06.10.2009, 15:17
scuze atunci mie nu imi merge:| poate nu am fost eu atent

glummyro
06.10.2009, 16:55
Eu am pus un inceput...restul faceti voi !

Marius
06.10.2009, 17:03
Daca inveti putin PHP si ai gandire logica poti sa-ti dezvolti propriul script...tutorialul e bun anyway :)

MaDaLyN17
07.10.2009, 20:01
inca o intrebare si gata :D
un script pentru "search" nu este?
pe cwr nu am gasit :(

glummyro
07.10.2009, 20:02
Search unde ?

MaDaLyN17
07.10.2009, 20:14
in baza de date... prin tabele (topicuri)

De ex:
Am topicurile:
Topic 1
Topic 2
Topic 3

Astea sunt bagate si in baza de date.
Ei bine, daca dau un search pe site cu textul.. sa zicem "Topic", sami afiseze o pagina cu topicurile gasite :)
Adica:
Topic 1
Topic 3

sau

Topic 2
Topic 3

ma intelegi? :D

glummyro
07.10.2009, 20:16
faci un extract din db
$extr=mysql_query("select * from ... where topic='...'");

...

MaDaLyN17
07.10.2009, 20:21
:?
si cum il adaug pe site ... adica nu trebuie conectat la baza de date, nu trebuie adaugat tabelul. el de unde stie din ce tabel sa ia datele?
nu inteleg!!

glummyro
07.10.2009, 20:23
Ai zis ca vrei sa-si faca cautare...
faci un formular care il adaugi unde vrei tu, acel formular cand da cauta trebuie sa deschida un script de cautare...

MaDaLyN17
07.10.2009, 20:31
hmm.. deci am codul (formularul) search pe care il adaug pe prima pagina de ex.
Codul este urmatorul:

<form action="search.php" class="search">
<input type="text" class="text" />
<input type="image" src="search.gif" value="search" />
</form>

Unde adaug

$extr=mysql_query("select * from ... where topic='...'");
in search.php ?
Daca da, alaturi de ce?

glummyro
07.10.2009, 20:38
Hai sa-ti explic
Formular.html


<form action="cauta.php" class="search">
<input type="text" class="text" name="cuvant" />
<input type="image" src="search.gif" value="search" />
</form>


Cauta.php


<?
$cauta=$_POST['cuvant'];
$tabel=""; //aici introdu dabelul unde se afla topicurile
$caut=mysql_query("select * from $table where topic LIKE '%$cauta%'")or die(mysql_error());
$numar=mysql_num_rows($caut);
if($numar==0){echo "Nu am gasit nimic in baza de date !";}else{
while($vreau=mysql_fetch_array($caut)){
echo $vreau[''];
}
}
?>


$vreau['']; // aici introduci ce ai tu in baza de date
$vreau['titlu'];
$vreau['title'];

gamez0r
07.10.2009, 20:38
@glummyro - esti autorul tutorialului :) ?! cred ca ar fi frumos sa spui si sursa ...

glummyro
07.10.2009, 20:41
Acest tutorial il am de acum 2 ani ! Mi l-a trimis cineva...ce autor sa pun ?

gamez0r
07.10.2009, 20:46
lasa .... :D oricum gj ! e foarte folositor :)

MaDaLyN17
07.10.2009, 20:47
deci primele doua le-am inteles, dar nu inteleg
$vreau['']; // aici introduci ce ai tu in baza de date
$vreau['titlu'];
$vreau['title'];

ce "vreau" ???
unde`l bag ?

Eu am facut mai pe simplu asa.. un mic test, si am facut in felul urmator in fisierul cauta.php


<?
$host="localhost"; // Host name
$username="xxx"; // Mysql username
$password="xxx"; // Mysql password
$db_name="xxx"; // Database name
$cauta=$_POST['cuvant'];
$tabel="romaneasca"; //aici introdu dabelul unde se afla topicurile
$caut=mysql_query("select * from $table where topic LIKE '%$cauta%'")or die(mysql_error());
$numar=mysql_num_rows($caut);
if($numar==0){echo "Nu am gasit nimic in baza de date !";}else{
while($vreau=mysql_fetch_array($caut)){
echo $vreau[''];
}
}
?>

si imi da urmatoarea eroare cand vreau sa caut ceva!

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

glummyro
07.10.2009, 20:50
<?
$host = "localhost";
$usr = "";
$pas = "";
$db = "";

mysql_connect($host, $usr, $pas)or die("Nu ma pot conecta la server!");
mysql_select_db($db)or die("Nu gasesc baza de date!");

$cauta=$_POST['cuvant'];
$tabel="romaneasca"; //aici introdu dabelul unde se afla topicurile
$caut=mysql_query("select * from $table where topic LIKE '%$cauta%'")or die(mysql_error());
$numar=mysql_num_rows($caut);
if($numar==0){echo "Nu am gasit nimic in baza de date !";}else{
while($vreau=mysql_fetch_array($caut)){
echo $vreau['title'];
}
}
?>

MaDaLyN17
07.10.2009, 20:57
acum nu mai zice nimic, nu da nici eroare, nu zice nici ca nu a gasit nimic... pagina alba :?
uite si tu:
http://madalyn17.host28.net/test1/

glummyro
07.10.2009, 20:59
Ce ai zice daca ai baga method=POST


<form action="cauta.php" class="search" method="POST">
<input type="text" class="text" name="cuvant" />
<input type="image" src="search.gif" value="search" />
</form>

MaDaLyN17
07.10.2009, 21:01
acum nu cred ca mai merge deloc, nu mai apare prelungirea aia la URL dupa cauta.php?.....
Dar am cate ceva in baza de date, si dau search exact cu titlurile topicurilor si nu gaseste nimic :(
nu ar trebui sa apara ceva, indiferent in ce stil

glummyro
07.10.2009, 21:12
Nu iti trebuie prelungire pentru ca tu trimiti cu POST si dincolo iti ia $cauta=$_POST['cuvant'];

MaDaLyN17
07.10.2009, 21:15
dar dar nu gaseste nimic :((((
si stai ca asta nu e tot.. nu stiu sal pun sa imi arate in tabele, pe urma sa`i pun designul...
pff cred ca o las balta :(

MaDaLyN17
07.10.2009, 22:45
Am cautat pe net si am gasit urmatorul cod:


<html>
<head>
<title>designplace.org search script</title>
<meta name="author" content="Steve R, http://www.designplace.org/">
</head>
<!-- © http://www.designplace.org/ -->
<body>

<form name="form" action="search.php" method="get">
<input type="text" name="q" />
<input type="submit" name="Submit" value="Search" />
</form>

<?php

// Get the search variable from URL

$var = @$_GET['q'] ;
$trimmed = trim($var) //trim whitespace from the stored variable

// rows to return
$limit=10;

// check for an empty string and display a message.
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}

// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","username","password"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("database") or die("Unable to select database"); //select which database we're using

// Build SQL Query
$query = "select * from the_table where 1st_field like \"%$trimmed%\"
order by 1st_field"; // EDIT HERE and specify your table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";

// google
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
}

// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}

// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: &quot;" . $var . "&quot;</p>";

// begin to show results set
echo "Results";
$count = 1 + $s ;

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["1st_field"];

echo "$count.)&nbsp;$title" ;
$count++ ;
}

$currPage = (($s/$limit) + 1);

//break before paging
echo "<br />";

// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt;
Prev 10</a>&nbsp&nbsp;";
}

// calculate number of pages needing links
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

// not last page so give NEXT link
$news=$s+$limit;

echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";

?>

<!-- © http://www.designplace.org/ -->

</body>
</html>

Ce as dori eu ar fi urmatoarele :( :
- sa ma ajutati sa ilplementez, astfel incat sa`mi arate rezultatele cum ar trebui sami arate in codul urmator (cu tot cu paginare):

<?php
$host="sql104.host28.net"; // Host name
$username="host2_4246283"; // Mysql username
$password="madalyn1"; // Mysql password
$db_name="host2_4246283_music"; // Database name
$tbl_name="forum_question"; // Table name

// Connect to servern and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$max = 50; //cate stiri sunt pe pagina
$p = @$_GET['p'];
if(empty($p))
{
$p = 1;
}
$limits = ($p - 1) * $max;
$result = mysql_query("SELECT * FROM $tbl_name ORDER BY id DESC LIMIT $limits, $max");
$totalres = mysql_num_rows(mysql_query("SELECT * FROM $tbl_name"));
$totalpages = ceil($totalres / $max);
// OREDER BY id DESC is order result by descending
?>
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Titluri</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Click-uri</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Comentarii</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row
?>
<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td>
</tr>

<?php
// Exit looping and close connection
}
mysql_close();
?>
</table>
<center><< <?php
for($i = 1; $i <= $totalpages; $i++){
echo "<a href='index.php?p=$i'>$i</a>&nbsp;";
}
?> >></center>
- si atat :(

nu stiu sa le combil, sa le fac ... cum naiba, habar nu am :(

Din cate am probat, asta imi zice ceva:

Results

Sorry, your search: "rec" returned zero results

Click here to try the search on google
Couldn't execute query

Marius
08.10.2009, 15:37
@glummyro tu ai scris in scriptul ala la extract $table nu $tabel, probabil din cauza asta nu ii merge



<?
$cauta=$_POST['cuvant'];
$tabel=""; //aici introdu dabelul unde se afla topicurile
$caut=mysql_query("select * from $table where topic LIKE '%$cauta%'")or die(mysql_error());
$numar=mysql_num_rows($caut);
if($numar==0){echo "Nu am gasit nimic in baza de date !";}else{
while($vreau=mysql_fetch_array($caut)){
echo $vreau[''];
}
}
?>


^ cum ai scris tu, uitate la sintaxa


<?
$tabel=""; //aici introdu dabelul unde se afla topicurile
$caut=mysql_query("select * from $table where topic LIKE '%$cauta%'")or die(mysql_error());
?>

si cred ca aici e greseala, $table in loc de $tabel :\....daca e greseala mea nu dati cu paru' doar mi-am exprimat opinia.

MaDaLyN17
08.10.2009, 16:11
am fost eu atent si am modificat :)
hai k mai stiu si eu cate ceva!
si oricum nu merge :(

MaDaLyN17
11.10.2009, 20:58
Deci revin la problema :D
Am pagina Index.php ce contine urmatoarea sursa:

<?php
$host="sql104.host28.net"; // Host name
$username="host2_4246283"; // Mysql username
$password="madalyn1"; // Mysql password
$db_name="host2_4246283_music"; // Database name
$tbl_name="forum_question"; // Table name

// Connect to servern and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$max = 50; //cate stiri sunt pe pagina
$p = @$_GET['p'];
if(empty($p))
{
$p = 1;
}
$limits = ($p - 1) * $max;
$result = mysql_query("SELECT * FROM $tbl_name ORDER BY id DESC LIMIT $limits, $max");
$totalres = mysql_num_rows(mysql_query("SELECT * FROM $tbl_name"));
$totalpages = ceil($totalres / $max);
// OREDER BY id DESC is order result by descending
?>
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Titluri</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Click-uri</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Comentarii</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row
?>
<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td>
</tr>

<?php
// Exit looping and close connection
}
mysql_close();
?>
</table>
<center><< <?php
for($i = 1; $i <= $totalpages; $i++){
echo "<a href='index.php?p=$i'>$i</a>&nbsp;";
}
?> >></center>

Cum adaug o functie de search care sa`mi caute in baza de date si sa`mi afiseze rezultatele in acelas mod cum sunt afisate pe index.php :|
Am incercat fel de fel de metode, si nimic!

MaDaLyN17
12.10.2009, 10:58
Ma ajuta cineva? :(
Am mai avansat putin!
Deci am scris in index.php

<form action="cauta.php" class="search">
<input type="text" class="text" />
<input type="submit" value="search" />
</form>

iar in cauta.php

<?
$host = "sql104.host28.net";
$usr = "host2_4246283";
$pas = "madalyn1";
$db = "host2_4246283_music";
$table = "romaneasca1";

mysql_connect($host, $usr, $pas)or die("Nu ma pot conecta la server!");
mysql_select_db($db)or die("Nu gasesc baza de date!");

$cauta=$_POST['cuvant'];
$tabel="romaneasca"; //aici introdu dabelul unde se afla topicurile
$caut=mysql_query("select * from $table where topic LIKE '$cauta'")or die(mysql_error());
$numar=mysql_num_rows($caut);
if($numar==0){echo "Nu am gasit nimic in baza de date !";}else{
while($vreau=mysql_fetch_array($caut)){
echo $vreau['title'];
}
}
?>

Problema este ca orice as scrie, imi da raspunsul "Nu am gasit nimic in baza de date !"
Desi am destule in tabelul respectiv :|
uitati, www.madalyn17.host28.net/