PHP show text up to /*show more text*/ - php

I would like to only show the text that is before /*SHOW MORE TEXT*/ However I am unsure how to do such a thing because my text is like this:
<ul>
<li>show this</li>
<li>show this</li>
/*SHOW MORE TEXT*/
<li>don't show this</li>
</ul>
I have tried
$texts = explode("/*SHOW MORE TEXT*/", $text);
But the issue with this is then it won't fix the </ul>
How can I fix this small issue?

Just try with Jquery toggle functions it's more easier and faster. Here you find details: http://api.jquery.com/toggle/

Using php you can do this like
<ul>
<li></li>
<?php
if(condition is true){
echo "<li>text will be displayed if condition is true otherwise it will not displayed</li>";
}
?>
</ul>
using javascript you can use
<li style="display:none;">this will not visible</li>

you can use display:none; property for that and later when you need it you can display it using java script.
<li style="display:none;">don't show this</li>

One possible solution is as follows:
Don't include ul tags in the text string, and then use explode and take the first element of array using array[0] and close </ul> tag.

Related

generating <li> elements in php

This should be relatively straightforward but none of the solutions I've found here or elsewhere work at all.
I'm trying to display a table containing all the elements of a list of brands. I added two manually (Adidas and Nike) just to test the display, and these show correctly. I then tried to generate a list using an array of brands (the array brandList has two items, id and name). The output is garbage.
<ul class="brand-list">
<li>Adidas</li>
<li>Nike</li>
<?php
foreach ($brandList as $item) {
$text = '<li>'.$item[1].'</li>';
echo("$text\n");
}
?>
</ul>
expected output:
Adidas
Nike
Puma
Asics
actual output
Adidas
Nike
'.$item[1].''; echo("$text\n"); } ?>
I've tried formatting a dozen different ways (with single and double-quotes, backslashes etc.) but it always just displays half of the line of code instead of the variable. I know it's something simple, but I've spent too much time messing with it for no result.
One way to do this is to let PHP generate the HTML instead of echoing the string. I think that's more clear than echoing.
<ul class="brand-list">
<li>Adidas</li>
<li>Nike</li>
<?php
foreach ($brandList as $item) {
?>
<li><?=$item[1]?></li>
<?php
}
?>
</ul>
A lot is wrong with your snippet and frankly I am not surprise it doesn't work.
You are using the wrong syntax in your foreach loop.
The <li></li> you are creating has no opening tag just an anchor tag <a></a> and a closing list item tag </li>.
$test is a variable with quotes in it, using quotation marks around it during echo is just wrong as the " in your href will close the first quote around the variable outputting others as a string rather than HTML tags.
Though not so important but ditch the \n character. Your browser will render the list in block format with or without it.
Change your snippet to look like this instead
<ul class="brand-list">
<li>Adidas</li>
<li>Nike</li>
<?php
foreach($brandList as $item) {
?>
<li><?=$item[1]?></li>
<?php
}
?>
</ul>
If you prefer your current syntax then change it to this
<ul class="brand-list">
<li>Adidas</li>
<li>Nike</li>
<?php
foreach($brandList as $item) {
$text = '<li>'.$item[1].'</li>';
echo($text);
}
?>
</ul>
Hi I think you have missed starting li tag in side the loop.
Please check this for your solution.
<ul class="brand-list">
<li>Adidas</li>
<li>Nike</li>
<?php
$text="";
foreach ($brandList as $item){
$text .= '<li>'.$item[1].'</li>';
}
echo($text);
?>

Problem on using $_SESSION inside HTML anchor href

I have a sample code here
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
<li>F</li>
<li>G</li>
<li>H</li>
<li>I</li>
<li>J</li>
as you can see i am trying to save into session the letter to be sent into sample.php. But even if i press 'B' what gets sent over to the sample.php is 'J' (the last letter) . tried clicking the other letters too its always 'J'.
can anybody help on what i am doing wrong?
Thank you very much
Try this
A</li>
A</li>
A</li>
It’s because your PHP code run and return the HTML output to your browser.
Because of that, all of them are ‘J’.
For solve the problem use code like this:
<li><a href=“sample.php?v=A”>A</a></li>
<li><a href=“sample.php?v=B”>B</a></li>
<li><a href=“sample.php?v=C”>C</a></li>
And you can get v value in your sample.php with this code:
<?php
$linkv=$_GET[“v”];
If ($linkv==“A”)
{
//write your code here
}
If ($linkv==“B”)
{
//write your code here
}
If ($linkv==“C”)
{
//write your code here
}
?>

How to append php into an unordered list?

Hello how do you add php into an unordered list like below ?
<?php // Render the Category List
categoriesList(); ?>
<?php
into
<ul class="oi_smalldev_categories_list">
<li class="cat-item cat-item-7">
Coding
</ul>
Thank you.
This should work:
<ul>
<?php
$mycats = categoriesList();
foreach ($mycats as $cat) {
echo '<li>'. $cat. '</li>';
}
?>
</ul>
I'm guessing you want to take the results from the function and add them to your unordered list.
Basically, in your function you can create a string that is the HTML you want to output. In this case multiple tags.
You can then store it in a variable and call it with PHP further down the page.
Not the best way to handle it but I think this can do what you're after.
So, here's some pseudo code to handle that.
<?php // Render the Category List
$results = categoriesList();
?>
function categoriesList(){
return '<li>'.'category1'.'</li>'.'<li>'.'category2'.'</li>'.'<li>'.'category3'.'</li>';
}
<ul class="oi_smalldev_categories_list">
<?php echo $results; ?>
</ul>

See if one item in array is true

I'm trying to put a menu together where it will sense what page is loaded. This site is not done in a CMS and is straight PHP/HTML code.
I currently have the navigation working for the primary links. There is a dropdown where I am having problems. I need to be able to see if the parent or any dropdown children are active. If one of the children are active. In the example below this is "FAQ" and the children are "FAQ1," "FAQ2," and "FAQ3."
For this example I'm using a CSS state called "active."
<style>
a{color:red;}
.active{color:blue;}
</style>
Here is the script used for the menu. The links for Home, Products, and Contact are working as expected.
<ul>
<li><a href="index.php" id="homenav" <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="active"';?>>Home</a></li>
<li><a href="products.php" id ="prodnav" <?php if (strpos($_SERVER['PHP_SELF'], 'products.php')) echo 'class="active"';?>>Products</a></li>
<li><a href="faq.php" id ="faqnav" <?php if (strpos($_SERVER['PHP_SELF'], 'faq.php, faq1.php, faq2.php, faq3.php')) echo 'class="active"';?>>FAQ</a>
<ul>
<li>FAQ1</li>
<li>FAQ2</li>
<li>FAQ3</li>
</ul>
</li>
<li><a href="contact.php" id ="connav" <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="active"';?>>contact us</a></li>
</ul>
Can I please get some help on how I should be writing this line to let it work the way the others are?
<li>
<a href="faq.php" id ="faqnav"
<?php
if (strpos($_SERVER['PHP_SELF'], 'faq.php, faq1.php, faq2.php, faq3.php'))
echo 'class="active"';
?>
>FAQ</a>
</li>
strpos() only accepts one string per parameter, you can not give it a list.
Try this:
<?php if (in_array(basename($_SERVER['PHP_SELF']), array('faq.php', 'faq1.php', 'faq2.php', 'faq3.php'))) echo 'class="active"';?>
basename() strips the path from the filename, so you only have the pure file name
in_array() then checks if this path is in an array
array() generates an array of strings to be handed over to in_array(). Note that there are 4 separate strings, not one long one as in your code.
Use in_array for this purpose:
if (in_array($_SERVER['PHP_SELF'], array('faq.php', 'faq1.php', 'faq2.php', 'faq3.php')) echo 'class="active"';
http://php.net/manual/de/function.in-array.php
If you don't have any other pages that contain faq, you can simply use:
if (strpos($_SERVER['PHP_SELF'], 'faq') !== false)
Note that I am using !== false as strpos can return 0 when faq is found at the beginning of your string. You should probably use that for your other comparisons too.
Otherwise, go with the in_array solution of #MrTweek.

First value in implode returning different?

I am not sure why the first value in my array adds <p> tags, and the rest works well inside li tags.
//user posted variable
$checks = $_POST['personalization_result'];
//handling the form data and outputting it into a list
if(!empty($checks)){
$checkValues = array_values($checks);
$checkString = implode('<li style="list-style: none;">'.get_post_meta($post->ID, '_moon_question', true).'', $checkValues);
}
// I need this filter so it can parse the html in the email
add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
//E-mailing my form data here
$sent = wp_mail($to, $subject, $checkString, $headers);
This works, the email is sent, but for some reason it looks like this.
<p>1</p> //this is the first value it should look like below
<li style="list-style: none;">Suits1</li>
<li style="list-style: none;">Suits1</li>
<li style="list-style: none;">Suits1</li>
<li style="list-style: none;">Suits0</li>
<li style="list-style: none;">Suits0</li>
I wish I could ID the problem, but frankly I am not sure where the issue is? I feel its inside the implode maybe the HTML isnt written in properly?
Implode is adding the given String BETWEEN all occurences of the given Array. That means, that the Output you provided COULT NOT have been generated by the script you have given, because there is no </li> given anywhere.
Beside the fact, that nobody knows, what get_post_meta($post->ID, '_moon_question', true) will return, you are most likely looking to generate a List?
Then the code to go would be something like that:
NOTE: First Openig AND LAST closing tag Needs to be seperated, as implode will only add IN BETWEEN.
Therfore the "in between string" Needs to start with a closing tag, and end with a opening tag.
echo "<li>".implode("</li><li>", $myArray)."</li>";

Categories