Use echo in a css class [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 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>

Related

Clean HTML and PHP For Loop Not Displaying Anything [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
<?php for($x = 0; $x <= count($slides); $x++):?>
<li data-target="#main-carousel" data-slide-to="<?php echo($x);?>'" class="active"></li>
<?php endforeach; ?>
Not exactly sure what the error is everything seems right.
You start yourself with a for loop, while closing it with a foreach loop. These are two very different things and cannot be matched like that.
You simply need to replace
<?php endforeach; ?>
with
<?php endfor; ?>
Alternatively, you can use curlybrackets { instead, having something like
<?php for($x = 0; $x <= count($slides); $x++) { ?>
<!-- do HTML here -->
<?php } ?>

Css In Php not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
So this is the part of my code that I'm trying to make it work :
while ($row = mysql_fetch_array($satt)){
echo "<li";
if($row["satake"] == $fns ) {
echo "class=\"selected\"";
}
echo "><a href=\"home.php?fn=".urlencode($row["satake"]).
"\">{$row["satake"]}</a></li>";
}
And upper in page I have defined these:
<?php
if (isset($_GET['fn'])){
$fns = $_GET['fn'];
$ys = "";
} elseif (isset($_GET['yosef'])){
$ys = $_GET['yosef'];
$fns = "";
} else
{
$ys = "";
$fns = "";
}
?>
And this is the css :
.selected { font-weight: bold; }
It doesn't give me any error and it runs smoothly but doesn't work.
While it is true that you were missing an space, you might want to take these steps in the future to avoid this confussion:
PHP is a template engine. Use it as such (put PHP presentation INSIDE html, and not the other way around).
Check out the dynamic generated solution with the browser inspector (Chrome or Firefox).
So, your code becomes much more readable and easier to spot errors when you do:
while ($row = mysql_fetch_array($satt)){
// Some light logic
$satake = $row["satake"];
$liclass = $satake == $fns ? "selected" : "";
$cleansatake = urlencode($satake);
// The presentation
?>
<li class="<?= $liclass; ?>" >
<a href="home.php?fn=<?= $cleansatake; ?>" >
<?= $satake; ?>
</a>
</li>
<?php
} ?>

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\"/>";
}

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!

how to search using a textfield? php [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 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.

Categories