I need your help in creating a search result page for my site. Its a simple php cms site simply fetching content from mysql.
Using a simple query to fetch search
$search = $_GET["search"];
$search = preg_replace('#[^0-9]#i','',$_GET['search']);
$searchresult = mysql_query ("SELECT * FROM pages WHERE pgcontent LIKE '%$search%'");
while ($row = mysql_fetch_array($searchresult))
{
echo '<h3>' . $row["PageTitle"] . '</h1>';
echo '<p>' . $row["PageContent"] . '</p><br /><br />';
}
What i want to do is not to display the whole page content, just the line where any word matches with the search term ... or just the first few lines of that page where the search term was found.
Friends can you help me in doing this please? it will be a big favor ... thank you
You could run post processing on $row["PageContent"]
Something like
while ($row = mysql_fetch_array($searchresult))
{
echo '<h3>' . $row["PageTitle"] . '</h3>';
$position = strpos($row["PageContent"], $search);
$snippet = substr($row["PageContent"], $position - 100, $position + 100);
echo '<p>' . $snippet . '</p>';
echo '<br /><br />';
}
Where you begin your snippet 100 characters from the beginning of the matched term and end it 100 characters after the beginning of the term. Obviously you tune this to whatever you want but this is the basic idea.
If I missed the point let me know and I may update.
Related
I already searched for my answer, but all of the solutions do not fit to my problem. I want to build a forum, where first of, all questions are requested from my MySQL database. Then I want to find out the date of the latest entry for each question, so I created this:
while ($array = mysqli_fetch_assoc($res_page)) {
$get_latest_date = "select * from forum"
. " where id = "
. $array["id"]
. ' order by date DESC'
. ' limit 1';
$latest_date = mysqli_query($con, $get_latest_date);
$date = mysqli_fetch_row($latest_date);
echo '<div class="forum_preview">'
. '<a href=#" class="forum_preview_question">'
. $array["question_short"]
. '</a>'
. '<p class="forum_preview_date">'
. $date["date"]
. '</p>'
. '<p class="forum_preview_comments">'
. $number_of_questions
. '</p>'
. '</div>';
}
My problem is, that showing the latest date is not working, because I cannot get the result out of my mysqli_query.
Can anybody show me where my mistake is or does anybody have another idea?
I think what you are looking for is mysqli_fetch_assoc, mysqli_fetch_row returns an array, mysqli_fetch_assoc returns an associative array so you can get data by keys.
$latest_date = mysqli_query($con, $get_latest_date);
$date = mysqli_fetch_assoc($latest_date);
echo $date["date"];
I have a PHP page that is returning data from a MySQL table. I decided to add a feature that would allow the user to set the number of results they want displayed, in case there are thousands of records for example.
Here is the PHP code that displays the data:
while ($row = mysql_fetch_array($result) && ($row <= $noOfResults)) {
echo '<p class="display-date">' . $row['date'] . '</p><p class="display">' . $row['id'] . ' - ' . base64_decode( $row['packetdata']) . '</p>';
echo '<hr align="center" width="100%" />';
echo $noOfResults;
}
I was hoping that this would only display the data up to the point that the user has selected. So if the user selects 10 results, $noOfResults is set to 10, and it will only fetch the first 10 results from the database. This is currently however only displaying a "-" and the $noOfResults variable (which is desired at this point). Is my syntax wrong or is this not the correct method of going about such a problem?
Thanks, got it working with LIMIT, didn't even think to do that.
Can someone explain why this was downvoted, just trying to learn, so as to write better questions in the future, thanks.
the best way to get limited data from database is LIMIT statement in query .
i assume that your query is
$result= "SELECT * FROM `mytable`";
Just add limit statement in
$result= "SELECT * FROM `mytable` LIMIT '$noOfResults'";
then your while loop will be
while ($row = mysql_fetch_array($result) ) {
echo '<p class="display-date">' . $row['date'] . '</p><p class="display">' . $row['id'] . ' - ' . base64_decode( $row['packetdata']) . '</p>';
echo '<hr align="center" width="100%" />';
echo $noOfResults;
}
You are doing it wrong $row will be an array. So to correct this use mysql_num_rows() function in php to check the number of the rows. Try the code below.
if(mysql_num_rows($result)<=$noOfResults){
while ($row = mysql_fetch_array($result) ) {
echo '<p class="display-date">' . $row['date'] . '</p><p class="display">' . $row['id'] . ' - ' . base64_decode( $row['packetdata']) . '</p>';
echo '<hr align="center" width="100%" />';
echo $noOfResults;
}
}
Hope this helps you
I'm creating a site to upload content and I want the user to be able to delete content on their Edit page. The following code is what I have echoed after the user inputs their content
<div id="content_review">
<?php
$q_getRev = "SELECT * FROM REVIEWS ORDER BY RID DESC";
$r_getRev = mysql_query($q_getRev, $connection);
$n_getRev = mysql_num_rows($r_getRev);
for ($i = 0; $i < $n_getRev; $i++) {
echo '<img class="poster" src="'
. mysql_result($r_getRev, $i, 'PREVIEW_IMG') . ' " />';
echo '<div class="title">'
. mysql_result($r_getRev, $i, 'TITLE') . '</div><br/>';
echo '<div class="reviews">'
. mysql_result($r_getRev, $i, 'REVIEW') . '</div>';
echo '<div><img src=image/x.png id="delete">'
. mysql_result($_delRev, $i, 'REVIEW') . '</div>';
}
?>
I want the user to be able to delete content that was previously printed in PHP. I'm not entirely sure how to proceed to make the following line delete content from my REVIEW column from mysql (the following line appears as a delete button next to the already printed content)
echo '<div><img src=image/x.png id="delete">'
. mysql_result($_delRev, $i, 'REVIEW').'</div>';
That line should be:
echo '<div><img src="image/x.png"></div>';
Then you need to write a delete_content.php script that deletes the content whose RID is equal to $_GET['rid']. The PHP in that script should be something like:
$rid = intval($_GET['rid');
$delRev = "DELETE FROM REVIEWS WHERE RID = $rid";
Also notice that I removed the id="remove" attribute from the <img> tag. IDs have to be unique, but you were using the same ID for each item. I doubt you need an ID on the image tags, so I just removed it.
This question already exists:
Closed 10 years ago.
Possible Duplicate:
how to have ranking based on how many words match user input
How can I have a search engine ranked by amount of words that are typed in and ranked by the amount of types they crop up in the fields title, description, keywords, link.
So the higher results are ordered by having more of the words that the user submitted. Say if the user typed in iPhone, iPhone key word crops up in the link, description and title field, so that would be ranked higher than say Apple which mentions it only in the description and Apple Store which mentions it in the title and the description.
My code is below:
$query = " SELECT * FROM scan WHERE ";
$terms = array_map('mysql_real_escape_string', $terms);
$i = 0;
foreach ($terms as $each) {
if ($i++ !== 0){
$query .= " AND ";
}
$query .= "Match(title, description, keywords, link) Against ('".implode(' ',$terms)." IN BOOLEAN MODE') ";
}
$secs = microtime();
$query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error( $connect ));
echo '<p class="time">Qlick showed your results in ' . number_format($secs,2) . ' seconds.</p>';
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title1 = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
$rank = $row['rank'];
$title = substr($title1,0,60);
echo '<h2><a class="ok" href="' . $link . '">' . $title . '</a></h2>' . PHP_EOL;
echo '<p class="kk">' . $description . '<br><br><span class="keywords">' . PHP_EOL;
echo '<p><a class="okay" href="' . $link . '">' . $link . '<br><br><span class="keywords">' . PHP_EOL;
}
While I'm sure it is possible to implement this within mysql, a full-text search index like solr/lucene is designed to handle this stuff directly. You might find better resources & help going down that road.
Using php, copy the array returned by the mysql query:
while ($row = mysql_fetch_assoc($query))
{
$rows[] = $row;
}
Then you can loop over $rows and assign a score to
foreach ($rows as $row)
$row['score'] = score_row($row, $terms)
score_row is a helper method that returns a bigger score depending on which columns have which terms and your definition of "importance".
Then sort thw rows by score, then print the results as you were doing.
Trying to output flickr images from a certain location, not using any geolocation yet, only the a string of the location, but thats a problem for another topic :).
Problem is that when I call flickr.photos.search for that place, I may get a few results from a single user. Is there any easy way in PHP to limit this, but still maintain the number of images I request?
Rather than having the output populated with a few users, I would like to have 12 images each by different users. I know this is easily possible, but its on the tip of my brain and I cant seem to write it.
Here is what I have far.
`
$users = array();
foreach($rsp->photos->photo as $photo)
{
$photo_owner = $photo["owner"];
$photo_id = $photo['id'];
$photo_title = $photo['title'];
$photo_url = "http://flickr.com/photos/" . $photo_owner . "/" . $photo_id . "/";
$flickr_getSizes = "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=$api_key&photo_id=$photo_id";
// If its not in an array, add it and output the image
if (!in_array($photo_owner, $users)){
array_push($users, $photo_owner);
$flickr_getSizes = "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=$api_key&photo_id=$photo_id";
$sizes_rsp = getXML($flickr_getSizes);
foreach($sizes_rsp->sizes->size as $size){
if ($size['label'] == $preferredSize) {
echo '<li>';
echo '<img src="' . $size['source'] .'"/>';
echo '<h3>' . $p_title . '</h3>';
echo '</li>';
}
}
}
}`
There is more above but its only getting the XML.