Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP MySQL search

Status
Not open for further replies.

d3sol4t3

Programmer
Oct 27, 2005
40
GB
Hello can anybody help me out with a search script.

What im trying to do:

Make a simple search script that allows user to enter search term, script searches field 'headline' in table 'article' for headline and displays any matching results which are heperlinked. The hyperlink text leads to the headline ID.

E.g

Go here

You see 'test rows' in the table in the middle ?

Say a user searched 'test', it would bring up a result with the full headline ('test rows') and that full headline will be hyperlinked. When clicked it will lead to:


because the headline id is 27.

Information you will need:

table name: article
fields in ' article ' that you need:

id
headline


If anybody could do this it will be really appreciated.
 
take the output from the form and add it as you like to a where clause. eg
Code:
<?
if (isset($_POST['formfield'])):
 $where = " where `headline` like '%". mysql_escape_string(trim($_POST['formfield'])) . "%'";
endif;

$sql = "Select * from $table $where";
//run the query
==========
just so there's no confusion (and this is typed with the best intentions so i'm really not trying to be rude): in general this forum helps people on specific problems they have: debugging code, giving direction etc. typically sites like rentacoder would be more likely to respond to a request to write a script for a user. so, in your post above, hopefully my response will give you the direction needed to complete your task, or you could post back with your sample code saying what you have done and what's going wrong. sleipnir214 wrote an excellent FAQ (in the FAQ sections) as to what is expected of people before they post. but ... you are unlikely to find a poster in this forum that will just take your request and write up your code.
==========
 
Right ok i made a basic search script which works fine.

Theres a feature i wanted to add whichi just cant figure out how to do: getting the id of the headline.

Search in use:

<===search for test

it will take you to



I wanted to add the URL of the article to the 'url' column, just don't know how.

+ id

^^ the above is how to article is displayed, i need to know how to resolve the headline to it's id then echo aa hyperlinked url which is:

+ resolved id
 
so for each row in a resultset you would show something like

Code:
<tr>
<td>
<a href="[URL unfurl="true"]http://www.illworm.com/path/to/fulltext.php?h=<?=$row[/URL]['id']?>"><?=$row['headline']?></a>
</td>
</tr>

hth
justin
 
sorry - this will not give you the headline and url in separate boxes but in the same box - ie the headline becomes clickable itself. to do it the way that you wanted (as evident from your site) you need to adapt the code thus:
Code:
<tr>
<td>
<?=$row['headline']?>
</td>
<td>
<a href="[URL unfurl="true"]http://www.illworm.com/path/to/fulltext.php?h=<?=$row[/URL]['id']?>">
[URL unfurl="true"]http://www.illworm.com/path/to/fulltext.php?h=<?=$row[/URL]['id']?>
</a>
</td>
</tr>
 
it's difficult to tell because you are not posting your code in between code tags. but there should not be a semicolon after the less than signs.

i am also assuming that you are using a while loop to iterate through your results and that each row is assigned to an array called $row:
Code:
$results = mysql_query("Select *...");
while ($row = mysql_fetch_assoc($results)):

//do stuff
endwhile;
 
d3sol4t3, please use [ code] and [ /code] tags without the spaces to properly display your code when posting to this or other forums.

Don't point people to external links unless it is necessary for something other than code you can paste here.

Look at the code you have posted above:
Code:
<;?=$row['id']?>
is incorrect, the ; in there is what is throwing the error, remove that semi-colon and your script should work.
 
Note in your code in the link provided that the error message is pointing to this line:
Code:
<?=$row['headline']?>
which is wrong you don't need the = sign in there and it is what is producing the error.
 
you typically DO need the "=" sign in this short form notation. the alternative is to use <? echo $row['headline']; ?>. I find the short form more useful for inserting php variables into html.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top