How can i insert if statement to variable in php [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to make a dynamic tab with PHP. please can someone help me out just need to filter where the category parent = 0.
$parent_category_html.= '<span class="'.$current_tab.'"><a id="tab-'
. $category['id'].'-link" href="#tab-'.$category['id']
. '" role="tab" aria-selected="false" aria-controls="tab-1" class="tab-link">'
. **$category['title'] is it possible to put an if statement here to filter the title where parent = 0**
. '</a>

From comments on the question, it sounds like what you're asking is:
How can I conditionally include a value in my string?
For that you'd use the ternary conditional operator. The operation itself would look like this:
$parent == 0 ? $category['title'] : ''
It essentially means:
If $parent equals 0, $category['title'], else empty string
The whole operation resolves to the resulting value, so you can wrap the whole operation in parentheses and drop it anywhere you would a variable. So something like this:
$parent_category_html.= 'your various HTML code...'
. ($parent == 0 ? $category['title'] : '')
. 'the rest of your HTML code...';

You can add if statement before:
if (statement)
$parent_category_html .= '...';
or try to use ternary operator
$parent_category_html .= "<div>" . ($condition ? 'output if true' : 'output if false') . "</div>";
$parent_category_html.= '<span class="'.$current_tab.'"><a id="tab-' . $category['id'].'-link" href="#tab-'.$category['id'] . '" role="tab" aria-selected="false" aria-controls="tab-1" class="tab-link">';
if ($pareent_id ==0) {
$parent_category_html.= '...';
}
$parent_category_html.= '</a>';

Related

How to structure php if statement to check for smartquote [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
In a db returned record, the var $bookTitle either starts with a smartquote or does not. If the first character is a smartquote, I want to display the record as-is. If the first character is anything else (letter or number), I want the concatenated string to be italicized. I've tried a couple different ways to structure the == '"' without success. The current version is italicized, no matter what is returned (NOTE: the code pasted here won't correctly depict smartquotes).
EDIT: The only part of the statement not working is the ($titleFormat == '"').
EDIT: I discovered that running echo $titleFormat returns a black diamond with a question mark.
<?php
$titleFormat = $bookTitle[0];
if ($titleFormat == '"') {
echo $Parsedown->text($bookTitle . ' ' . $bookSubtitle);
}
else { ?>
<em><?php echo $Parsedown->text($bookTitle . ' ' . $bookSubtitle); ?></em>
<?php
} ?>
Have you tried checking with regex...
if ( preg_match( '/^"|^“/', $bookTitle ) ) {
echo $Parsedown->text( $bookTitle . ' ' . $bookSubtitle );
} else {
printf( '<em>%s</em>', $Parsedown->text( $bookTitle . ' ' . $bookSubtitle ) );
}
Have you also considered that the db result might be encoded, for example: "?
Could it be that the character isn't actually " but “ instead? I've updated the regex above.

How will make variable to do_shortcode value? i want to change shortcode value like (category="others") how it possible? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to make variables of these red block parts
(screenshot).
PHP code:
echo do_shortcode('[product_category category="others" per_page="12" columns="4"]');
You can directly insert variable values in a "" delimited string in PHP.
$cat = "some_category";
$per = 20; // some number
$col = 10; // some number
echo do_shortcode("[product_category category=\"$cat\" per_page=\"$per\" columns=\"$col\"]");
If you still want to use a '' delimited string, you need to append values together.
echo do_shortcode('[product_category category="' . $cat . '" per_page="' . $per . '" columns="' . $col . '"]');

Using foreach loop to create links from an array [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 7 years ago.
Improve this question
Please help me create links using a php foreach loop that iterates over an array which contains the names of navbar links for a webpage.
Currently my loop creates links but when you click on them an error 404 page and displays in the url (for example when clicking on "blog"):
...homebrew-actual/blog.php>Blog <a></li><li><a href="
I would like the url to go to:
... homebrew-actual/blog.php
without the html tags.
Here is my current PHP loop:
<nav>
<ul>
<?php
$navOptions = array('index', 'showcase','about','blog','contact','forums');
foreach($navOptions AS $navOption) {
if ($navOption == $currentPage) {
print '<li>' . '' . ucfirst($navOption) . '</li>';
} else {
echo '<li>' . '<a href="/homebrew-actual/' . $navOption . '.php>' . ucfirst($navOption) . '</a></li>';
}
}
?>
<li class="special">Shop</li>
</ul>
</nav>
Please help me identify a solution to create a links for a navbar using an array with the link names and use a for loop to link to those pages.
Thank you for checking out this question.
You forgot a ":
print '<li>' . '' . ucfirst($navOption) . '</li>';
^--start href ^---end of href, missing "
Since you never close the href string, you end up with broken HTML.

single quotes vs double quotes, how to arrange? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Please let me know how to writte the below code so as to work because as it is it doesn't work
echo "<a href='$row['url']'>$row['link_text']</a>";
Write so you can read it next time. Also syntax highlight is better this way:
echo '' . $row['link_text'] . '';
You are using ' twice, so you need to escape them or just remove them in this case:
echo "<a href='$row[url]'>$row[link_text]</a>";
When you have to insert complex variables like array values inside strings, usually printf or sprintf is more clear and less error-prone.:
printf("<a href='%s'>%s</a>", $row['url'], $row['link_text']);
This will work:
echo "<a href='".$row['url']."'>".$row['link_text']."</a>";
Also this:
echo "<a href='{$row['url']}'>{$row['link_text']}</a>";
It's personal preference.
It's because you've put a ' inside another '.
echo "<a href='{$row['url']}'>{$row['link_text']}</a>";
or
echo "<a href='" . $row['url'] . "'>" . $row['link_text'] . "</a>";
Choose the one more to your liking.
You can try with.
echo "<a href='".$row['url']."'>".$row['link_text']."</a>";
Or
echo "<a href='{$row['url']}'>{$row['link_text']}</a>";
Or
echo ''.$row["link_text"].'';

joomla 2.5 navigation menu subtitle [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Hi I am web designer and I would like to know how to set menu subtitle? It is easily possible with yoo themes but I need to know how it is done without using YooTheme templates. I think there is need of little modification in mod_menu but I don't know what exactly. I googled all day and can't find a solution.
There are certainly better solutions but i've done it this way:
Insert a charakter in the name of your menu-item. For example a "|".
It should look like this: Title | Subtitle. At this position you can divide the name.
Now you have to override the file default_component.php in modules/mod_menu/tmpl.
Add this lines:
$parts = explode("|", $linktype);
// the "|" is the divider
if(isset($parts[1])){
$linktype = $parts[0].'<span>'.$parts[1].'</span>';
}else{
$linktype = $parts[0];
};
after:
$class = $item->anchor_css ? 'class="'.$item->anchor_css.'" ' : '';
$title = $item->anchor_title ? 'title="'.$item->anchor_title.'" ' : '';
if ($item->menu_image) {
$item->params->get('menu_text', 1 ) ?
$linktype = '<img src="'.$item->menu_image.'" alt="'.$item->title.'" /><span class="image-title">'.$item->title.'</span> ' :
$linktype = '<img src="'.$item->menu_image.'" alt="'.$item->title.'" />';
}
else { $linktype = $item->title;
}
Now you have a span around the subtitle and it's possible to style it.

Categories