if else with a link doesn't work [closed] - php

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 8 years ago.
Improve this question
If $item not equal to false then echo first link, I then used onclick, but the problem is it seem it always goes to 2nd link. I inspected the $item, it's true, but how come it escaped the if scope? my markup as below:
<div>
<?php
if($item !== false){ ?>
<a onclick="window.open('<?php echo $item['2nd']; ?>');">
<?php }else{ ?>
<a href="<?php echo $item['url']; ?>">
<?php }?>
<div class="content">
</div>
</a>
</div>

You are using a strict "not equal" comparison, which you probably didn't mean to do.
From your code, it appears that $item is an array, but the line if ($item !== false) specifically checks if $item is a boolean false. If $item is an array, then the if statement will always return false because $item isn't a boolean.

Related

Infinite loop. Why? [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 6 years ago.
Improve this question
i wanna test if my variable is empty or not to display some differents things.
When i don't use the else...if everything work but when i use this code :
<?php
$Amazon = get_post_meta($post->ID, "Lien Amazon", true);
?>
<?php
if( $Amazon != NULL ){
echo '<li><span class="post-meta-key">Acheter sur Amazon</li>' ;}
else {
echo '<li><span class="post-meta-key">Acheter sur Amazon</li>' ;}
?>
What is the problem ? Thank you
Here is an output error. You haven't closed and reopened the string on trying to concat a variable.
echo '<li><span class="post-meta-key">Acheter sur Amazon</li>' ;
Do instead:
echo '<li><span class="post-meta-key">Acheter sur Amazon</li>' ;

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 } ?>

Else status in php [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 8 years ago.
Improve this question
I would need some help, since this code does not work (I have lack of knowledge, but I need to have it in that format)
$user_goals is array()
$user_count is just a number like 15
<?php foreach ($user_goals as $number): ?>
<?php if ($number < $user_count): ?>
<h5 class="text-center"><strike><?php echo htmlspecialchars($number); ?> users</strike></h5>
<?php else: ?>
<h5 class="text-center"><?php echo htmlspecialchars($number); ?> users</h5>
<?php endif; ?>
<?php endforeach; ?>
Any help will be apreciated :)
result that I want:
$user_count = 15;
$user_goals = array(
10,
15,
20,
etc,
);
so I want to get:
<strike>10</strike><br>
<strike>15</strike><br>
20
Solved it myself
<?php
foreach ($user_goals as $number) {
if ($number <= $users_count) {
echo "<h5 class='text-center'><strike>$number users</strike></h5>";
}else{
echo "<h5 class='text-center'>$number users</h5>";
}
}
?>
If you are looking to add a strike through when the goal is less than or equal to, you need to use that comparison operator.
if ($number <= $user_count)

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 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