how to search using a textfield? php [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I want to search a whole news feed by inserting a word in my text field. I have no idea how to do this. Here is my code, so you know what I mean with it.
<form action="search.php" method="get">
<tr><th>search: </th><td><input type="text" name="search" value="{$word}"></td></tr>
</form>
How can I check if the word I inserted in the searchbar, exists somewhere in the news feed?
My news feed is used like this:
$xml=simplexml_load_file("newsfeed.xml");
foreach($xml->channel->item as $item) {
echo '<h1 class="title">' . $item->title . '</h1>';
echo '<p class="desc">'.$item->description."</p>";
}

I think you can use the function strpos.
Find the numeric position of the first occurrence of needle in the
haystack string.
Example:
$a = 'Long text to look into it.'
if (strpos($a, 'it') !== false)
echo 'true';
In your case, you can use strpos either to find the word in the item title or the item description:
$a = $_GET['search'];
foreach($xml->channel->item as $item) {
print_result = 0; // flag to know if the search is in the feed.
if (strpos($item->description, $a) !== false){
print_result = 1;
} // end if desc
if (strpos($item->title, $a) !== false){
print_result = 1;
} // end if title
if(print_result == 1){
echo '<h1 class="title">' . $item->title . '</h1>';
echo '<p class="desc">'.$item->description."</p>";
} //end if print results.
} // end foreach

I would make a foreach like you do it now:
And make kind of relevance points for each news item. Something like if it's in the title then it's more relevant and if it's at the beginning and so on.
Then you can write it into an array and sort it.
I hope you have something like an id in your newsfeed.xml for this.

Related

PHP Manipulating MySQL Results? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I need some little help on a simple thing!
I have this table in my database, where I have a number of images stored, called "covers".
And then I have a table in HTML that displays the covers of this MySQL table inside one of it's cells.
Like this:
<td><a href="movie.php?id=<?php echo $idt + 1; ?>">
<?php echo '<img src="'.htmlentities($idt + 1['cover'], ENT_QUOTES, 'UTF-8').'" alt="Cover" style="max-width:300px;max-height:300px;" />';; ?></a></td>
And this is the code before
$req = mysql_query("select id, name, year, genre, cover from movies");
$dnn = mysql_fetch_array($req);
$idt = $dnn['id'];
But why doesn't it work when I try to dynamically change id by putting this?
$idt + 1;
In order to output data from multiple rows, I suggest using a loop like this:
while ($row = mysql_fetch_assoc($req)) {
?><td>
<a href="movie.php?id=<?php echo $row['id']; ?>">
<img src="<?php echo htmlentities($row['cover'], ENT_QUOTES, 'UTF-8'); ?>" alt="Cover" />
</a>
</td><?php
}
You need to fetch a whole set of results and not just one id and increment it in the view. You should do a foreach loop on $idt like that:
foreach($dnn as $row){
$id=$row['id'];
$cover=$row['cover'];
$genre=$row['genre'];
//etc...
//now echo html with vars like this:
echo "<img src=\"$cover\"/>";
}

Use echo in a css class [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am using php echo throughout different pages on a project, to update a percentage value that is changing daily. This value can be negative or positive.
Here is my code:
<i class="icon-thumbs-up"><strong> <?php $file = file('include.txt');echo $file[n]; ?></strong></i>
I am using FontAwesome icons with a Bootstrap template. Everything is working fine like this.
Now, if the percentage is negative, I would like to use class="icon-thumbs-down" instead of class="icon-thumbs-up".
I have tried to achieve this using
<i class="<?php $file = file('include.txt');echo $file[n]; ?>"><strong> <?php $file = file('include.txt');echo $file[13]; ?></strong></i>
in order to change it on all pages.
However, this is not working. Thanks for any hints!
To clarify:
<i class="icon-thumbs-up"><strong> <?php $file = file('include.txt');echo $file[0]; ?></strong></i>
Content of include.text: 0.58% on line 1 -> All working fine. I got the thumbs up displayed and next to it the value 0.58%.
Now I tried to change to:
<i class="<?php $file = file('include.txt');echo $file[1]; ?>"><strong> <?php $file = file('include.txt');echo $file[0]; ?></strong></i>
Content of include.text: 0.58% on line 1 and icon-thumbs-up on line 2. (Which I would like to change in the include.txt on a daily basis to icon-thumbs-up or icon-thumbs-down, depending on the value of line 1.)
If value from file('include.txt') is integer then you can use ternary operator to echo correct css class, eg. like this:
<li class="<?php echo (int) $file[0] < 0 ? 'icon-thumbs-down' : 'icon-thumbs-up'; ?>"></li>
This is a bit hard to reply to as your code isnt that clear. I can however hand you the following suggestion via an example:
$someInt = 10;
echo $someInt < 0 ? 'icon-negative' : 'icon-positive'; // will echo positive
$someInt = -3;
echo $someInt < 0 ? 'icon-negative' : 'icon-positive'; // will echo negative
This is a short if/else (ternary). To demonstrate how it works, this would be the same in a full syntax:
$someInt = -3;
if($someInt < 0){
echo 'icon-negative';
}
else{
echo 'icon-positive';
}
I would do the following:
<?php
// your logic here, move in another file if this gets bigger:
function percentage($nb) {
$file = file('include.txt');
return $file[$nb];
}
?>
<i class="<?php echo percentage(5) > 0 ? 'positive' : 'negative' ?>">
<strong>Text</strong>
</i>

How do i pass value in url? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
how do i create the url of the links? I want when users click on action it will redirect users to the action page.But i don't want to create pages for the links i have provided below.what i want to do is to display different genres type content on the same page;
here is my html
<ul class="snav">
<li>Action</li>
<li>Art and Experimental</li>
<li>Comedy</li>
<li>Crime and Mystery</li>
<li>Documentary</li>
<li>Drama</li>
<li>Epic and Historical</li>
<li>Family</li>
<li>Fantasy</li>
<li>Foreign</li>
<li>Horror</li>
<li>Live Performances</li>
<li>Musicals</li>
<li>Romance</li>
<li>Science Fiction</li>
<li>Special Interest</li>
<li>Sports</li>
<li>Thriller</li>
<li>War</li>
<li>Westerns</li>
</ul>
One way to accomplish this is to use query strings to form a url based off your base url.
http://domain.tld/index.php?genre=crime
index.php
if ($_GET['genre'] == 'crime') {
// display crime genre
} else {
// display index
}
You'll also want to include some validation/error checking when you're doing this. You can check whether or not the genre param is set using isset(). It is also important to escape the input to 'genre' so that injection attacks are less likely.
To generate the links and change page content all in one go, you can try:
<?php
$actions = array("Action", "Art and Experimental", "Comedy", "Crime and Mystery", "Documentary", "Drama", "Epic and Historical", "Family", "Fantasy", "Foreign", "Horror", "Live Performances", "Musicals", "Romance", "Science Fiction", "Special Interest", "Sports", "Thriller", "War", "Westerns");
?>
<ul class="snav">
<?php
foreach($actions as $action) {
$url_token = str_replace(' ', '_', strtolower($action));
echo '<li>' . $action . '</li>';
}
?>
</ul>
<?php
$genre = $_GET['genre'];
switch ($genre) {
case 'action':
echo 'Woo Action!';
break;
case 'drama':
echo 'GASP!';
break;
case 'art_and_experimental':
echo 'Weird...';
break;
default:
echo 'There is no action called ' . $genre . ' :(';
}
?>
This will generate html of the form:
<ul class="snav">
<li>Action</li>
<li>Art and Experimental</li>
...
</ul>
You're going to have to use $_GET variables using PHP and have something like:
Links look like
Action
PHP file:
if (isset($_GET['genre'])) {
switch ($_GET['genre']) {
case 'action':
echo 'something here';
break;
case 'horror':
echo 'something here';
break;
//etc
}
}
or you could use jQuery to dynamically load the data into the page.
Hope this helps!

URL variable shown on page [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying my best to describe this. I'd basically like to be able to have something like this in a url:
mydomain.com/?&name=Courtney
The name variable being Courtney. I would like to print it onto the page so when visited with the name=Courtney it'd state "Hello Courtney!" or "Hello Taylor!" Depending on who it is, if neither, don't display their name and just say "Hello!" I'm hoping someone here knows how to use a name in a url variable and is welling to share.
Simple in essence, but, I'm not sure how simple it is in code.
Directly from the PHP website:
echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';
if you want to check if the variable is available:
if (isset($_GET["name"])) {
echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';
} else {
echo 'Hello!';
}
<?php
if($_GET['name']){
echo "Hello " . $_GET['name'] . "!";
}else{
echo "Hello!";
}
?>
use
if (isset($_GET['name'])) {
echo "Hello ".$_GET['name']."!";
}
You could do it all in one line with a Ternary Operator
// Collect name if present //
$name = (isset($_GET['name'])) ? ' ' . ucwords(htmlspecialchars($_GET['name'])) : '';
// Echo output //
echo "Hello{$name}!";
// Output if name present //
Hello Courtney!
// Output if no name present //
Hello!
I'll provide a quick example. Complicated subject, simple code.
<?php
if(isset($_GET['name'])){
$name = $_GET['name'];
echo 'Hello ' . $name;
}
else{
echo 'No name supplied'
}
?>
if you want to use the current page url then after page load you can use
var address= document.URL
then extract the name (as url in your question is like 'mydomain.com/?&name=Courtney')
var name= address.substring(address.lastIndexOf('=') + 1)
if(name.length>0)
{alert("Hello "+name)}
else
alert("Hello")

Load HTML <select> options while loading the page in php/javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to load option dynamically,when we have to load options like eg: 1-500. as the values in select list.
Loding can be either done in php or javascript.
plz can someone provide eg/simple snippet or helpfull links? Thanks!
I tried: Javascript
for(var i=1;i<=500;i++)
{
//create new option
var option = new Option(i, "Value" + i);
//Add the new option it to the select
sel.options[i] = option;
}
Iterate over the values with a foreach, for or while loop, and just ouput whatever you need:
<?php
$html = '<select>';
for ($i=0; $i<500; $i++) {
$html .= '<option value="' . $i . '">Option ' . $i . '</option>';
}
$html .= '</select>';
echo $html;
?>

Categories