i made a search box which use $_GET method. I search for category and size.
When the category search submitted my url becomes:
index.php?category=1
through:
<li class=""><?php echo $category['name']; ?></li>
When size submitted my url is:
index.php?category=1&size=1
through:
<li id="">
<?php echo $size['name']; ?>
</li>
When i search for another size i have:
index.php?category=1&size=1&size=2
How can i just replace the size instead of adding it again in my url?
You have to use http_build_query function to build your URL from the associative array of parameters. And try to avoid doing it directly in a template. Move this code at least to the controller level.
// Somewhere at the controller
$category = ...;
$size = ...;
$searchParams = array();
if (!empty($category)) {
$name = $category['name'];
$searchParams['category'] = $category['id'];
}
if (!empty($size)) {
$name = $size['name'];
$searchParams['size'] = $size['id'];
}
$searchQuery = http_build_query($searchParams);
// Somehow pass it to the template
$this->addViewVar('searchUrl', $_SERVER['REQUEST_URI'] . $searchQuery);
$this->addViewVar('searchName', $name);
And in the template just use searchUrl variable:
<li class=""><?php echo $searchName; ?></li>
Another way to accomplish this task, if you don't have a separate controllers level, it's to create special functions build_search_url() and search_name():
function build_search_url($path, $category = null, $size = null) {
$searchParams = array();
if (!empty($category)) {
$searchParams['category'] = $category['id'];
}
if (!empty($size)) {
$searchParams['size'] = $size['id'];
}
$searchQuery = http_build_query($searchParams);
return $path . $searchQuery;
}
And then use it like this:
<li class="">
<a href="<?php echo build_search_url($_SERVER['REQUEST_URI'], $category, $size); ?>">
<?php echo search_name($category, $size); ?>
</a>
</li>
Use
<?php echo $_SERVER['PHP_SELF'];?>
Instead of
<?php echo $_SERVER['REQUEST_URI'];?>
REQUEST_URI is adding query strings to your request.
Where as PHP_SELF gives plain file name e.g. index.php.
Easiest way IMHO:
Set the new value for the index in $_GET (or a copy, if you don’t want to manipulate the original array) – and then use http_build_query to create a new query string from that.
You should call a function in you "href" tag.
Inside, use http_build_query($array) to reconstruct the array.
$data = array('category' => $_GET['category']);
$data['size'] = $_GET['size'] ? $_GET['size'] : NEW_SIZE;
function getQuery($data) {
return http_build_query($data);
}
You can also use $_SERVER['REQUEST_URI'], but I'm not sure that this function will replace your item value instead of adding it to the end of the query.
Related
So this is a zany thing that I'm attempting to do, but what I am trying to achieve is querying out some info in WordPress with categories. If a post comes back with a parent category, then echo that out with the child category with a | seperator. If no parent category then just echo out the category.
Here's what I'm trying so far:
<?php
$categories = get_the_category();
$catName = $categories[0]->name;
$catParent = get_cat_name($categories[0]->category_parent);
if($catParent) {
$fullCat = $catParent '|' $catName;
} else {
$fullCat = $catName;
echo $fullCat;
?>
I know this is incorrect, but basically, I'm trying to wrap my head around how to accomplish this. I'm not sure how to combine two variables to use one and then add the separator within the two.
Was also wondering if maybe a ternary operator might be better here too? That was more of thought than anything and probably not that necessary to make this work correctly.
You're wanting to check if $catParent is empty so you can use the empty() function to do that check for you. Quick edit...you need periods (.) in between things you are concatenating.
Link to the docs.
if(empty($catParent)) {
$fullCat = $catParent . '|' . $catName;
} else {
$fullCat = $catName;
}
You must use '.' to concat.
Example:
$fullCat = $catParent. ' | '. $catName;
You can use . (Concatenation) operator to achieve this. Look at the code below.
<?php
$categories = get_the_category();
$catName = $categories[0]->name;
$catParent = get_cat_name($categories[0]->category_parent);
if($catParent) {
$fullCat = $catParent . '|' . $catName;
} else {
$fullCat = $catName;
echo $fullCat;
?>
I am trying to change languages but stay on the current page:
i.e:
www.myurl.com/english/aboutus.php
www.myurl.com/german/aboutus.php
www.myurl.com/french/aboutus.php
So only the language directory changes.
I have the following but can't get it to work:
<?php
$full_url = $_SERVER['FULL_URL'] ;
$temparray = explode(".com/",$full_url,2);
$englishtemp = $temparray[0] . ".com/english/" . $temparray[1];
$englishlink = "<a href=$englishtemp><img src=../images/english-flag.jpg></a>";
echo $englishlink;
?>
I get the url to change from '/french/aboutus.php' to '/english' but it doesn't remember the page name 'aboutus.php' and returns to the index page.
Could any one please help?
Use basename($_SERVER['PHP_SELF']) for the current page.
$englishtemp = "/english/" . basename($_SERVER['PHP_SELF']);
$englishlink = "<a href=$englishtemp><img src=../images/english-flag.jpg></a>";
echo $englishlink;
See PHP documentation on $_SERVER.
Try this instead. It will generate links for all the languages except the current selected one :)
<?php
$full_url = $_SERVER['REQUEST_URI']; //select full url without the domain name
$languages = array('english', 'french', 'german'); //array of all supported languages
$url_bits = explode('/', $full_url); //divide url into bits by /
$current_language = $url_bits[1]; //current language is held in the second item of the array
//find current language in the array and remove it so we wouldn't have to display it
unset($languages[array_search($current_language, $languages)]);
$links = '';
foreach ($languages as $language) {
//generate links with images for the rest of the languages
$links .= '<img src="../images/' . $language . '-flag.jpg" title="'.ucfirst($language).'" />';
}
echo $links;
?>
I'm am working on a Wordpress custom template and I'm filtering post by their classes.
Since Wordpress displays a LOT of unnecessary classes when I put <?php post_class(); ?> in a page, ( here is what it gives me class="post-54 post type-post status-publish format-standard hentry category-3d category-web" )
I'm trying to simplify that by echoing only the categories that relate to the post.
EDIT: This is what calls the post in my page <?php query_posts( 'showposts=99' ); ?>
Then this piece of code
<li class="<?php if ( in_category('category-3d')) { echo "3d"; }
if ( in_category('category-animation')) { echo "animation"; }
if ( in_category('category-motion')) { echo "motion"; }
if ( in_category('category-shortfilm')) { echo "shortfilm"; }?>"></li>
give me this <li class="motion"></li> if my post is in the "motion" category.
The problem is that if my post is in several categories, only the first is echoed... How can I tell WP to echo ALL the names of the categories my post is affected to, while adding a space between them?
Since I am a beginner in php syntax, I'm still learning how to get such a simple thing working in a clean and effective way (i.e. without writing 16 lines of code !)...
Can someone help me on this one?
SECOND EDIT:
Ok I'm still trying to figure out why, but today, my piece of code (above) is working and echoes all my listed classes, but without space between them... So to make my classes work, I'm adding a space at the end of the echo part like this { echo "3d "; }. But I feel like it's a dirty way to make things work...
How to add a space between each class tag in the proper way? I'm aiming for something like this foreach $categories as $cat { echo $cat . " "; } but where $categories and $cat would refer to each "if" statement.
I'm not familiar with WP but if you get your categories with some function like get_categories() then you can use implode to make a string from all categories.
<?php $categories = get_categories(); ?>
<?php $categories = implode(' ', $categories); ?>
<li class="<?php echo $categories; ?>">...</li>
Could you provide what is returning get_categories()?
EDIT:
function get_class_attr() {
$classes = get_post_class();
$classes = substr($classes, 7, -1);
$arr = array();
$arr = explode(' ', $classes);
$classes = 'class="';
foreach($arr as $class_name) {
if(strpos($class_name, 'category-') !== false) {
$classes .= substr($class_name, 9).' ';
}
}
$classes = substr_replace($classes, '"', -1, 1);
echo $classes;
}
get_class_attr(); // output: class="3d web"
Also note that 3d is not a valid class name, you can use something like movie3d or film3d.
Alternative way is to create your class value a little bit earlier and then echo it:
<?php
$classes = array();
if ( in_category('category-3d')) { $classes[] = "3d"; }
if ( in_category('category-animation')) { $classes[] = "animation"; }
if ( in_category('category-motion')) { $classes[] = "motion"; }
if ( in_category('category-shortfilm')) { $classes[] = "shortfilm";}
if(count($classes)) {
$classes = implode(' ', $classes);
} else {
$classes = '';
}
?>
<li class="<?php $classes ?>"></li>
As of the code you've posted, it should add all the categories if the product is in all the four specified ones. You could add the space between the quotes after each echo, e.g.: echo ' motion'.
I would use another approach, adding really all categories, without the need to check them by name:
<li class="<?php echo implode( ' ', get_categories() ); ?>"> … </li>
See: http://codex.wordpress.org/Function_Reference/get_categories on how to filter the output of the function get_categories().
I would suggest instead you use this function from the Wordpress API - something you should read a lot of.
Something like this should work:
<?php $categories = get_categories(); ?>
<li class="<?php foreach ($categories as $cat) {
echo $cat . " ";
}?>"> -- Your li text --
</li>
I am currently designing a website which uses this: <li class="current">.
I have gotten a script to detect when it's on said page to output the right class, however, when on the gallery.php?dir=thedir page, it fails to output the class!
Here's what I have so far, which is not working...
$ispage = preg_match_all('gallery.php/\[(.+?)\]/s');
I would appreciate any information somebody could give me :).
Full code:
<?php
$ispage = preg_match_all('gallery.php/\[(.+?)\]/s');
$currentpage = $_SERVER['REQUEST_URI'];
if ($ispage==$currentpage) {
echo '<li class="current">';
} else {
echo '<li>';
}
?>Photos</li>
Can't you use $_SERVER['SCRIPT_NAME'] or $_SERVER['PHP_SELF'] or $_SERVER['REQUEST_URI'] along with php function strpos($haystack, $findme)? It should make your life easy.
http://php.net/manual/en/function.strpos.php
It seems like $_SERVER['PHP_SELF'] is the one you need, it return the relative filename, without params.
Have a look here : http://www.php.net/manual/en/reserved.variables.server.php
If I understood your question you are trying to access query string values
Try echo $_GET['dir'];
EDIT
Tried this?
$ispage = '/'.basename($_SERVER['PHP_SELF']).'?'.($_SERVER['QUERY_STRING']);
$curepage = $_SERVER['REQUEST_URI'];
I think you need to define an array of links to solve your problem
so that
$curpage = $_SERVER['REQUEST_URI'];
$links = array('/gallery.php?dir=thedir', '/home.php?dir=anotherdir');
foreach($links as $link) {
$class = '';
if($link == $curpage) {
$class = 'class="current"';
echo '<li '.$class.'>';
.... rest of the code ....
}
}
<?php
$currentpage = $_SERVER['REQUEST_URI'];;
preg_match('/[^.]+\.[^.]+$/', $currentpage, $matches);
if ($matches[0] == "/gallery.php?dir=thedir") {
echo '<li class="current">';
} else {
echo '<li>';
}
?>Photos</li>
if a URL is something like: www.site.com/page.php?q=foo
then this
$currentpage = basename(__FILE__);
will just give you page.php
In addition, if you call it this way:
$currentpage = basename(__FILE__,".php");
will give you page
I'm pretty sure preg_match_all returns an int. If this is full code it won't be working at all. And yes, use something like $_SERVER['PHP_SELF']. Also you should think about using some better url to avoid this.
I have a function that I'd like plug a view file. It's pretty simple when you just need to echo one or two things but I have some complicated html and so would like to take advantage of alternate php syntax for the following foreach loop and if statements:
UPDATE I corrected the CI->load->view to include the 3rd parameter according to tpaksu's suggestion. It's closer to working but still not quite right. See comments below in the code:
<?
function displayComments(array $comments, $parentId = null) {
$CI=& get_instance();
foreach($comments as $comment){
if($comment['replied_to_id'] == $parentId){
echo $CI->load->view('reviews/comment_list', $comments, true); // this doesn't work, it only shows the last array member
// echo $comment['comment']; this works as expected
}
}
}
displayComments($comments, $parentId = null);
?>
Here's what the 'reviews/comment list view file looks like in its simplest form:
<ul>
<? foreach($comments as $comment): $comment=$comment['comment']?>
<li>
<?echo $comment?>
</li>
<?endforeach;>
</ul>
Would anyone know to how embed view files into a function?
I usually use to have a snippet_helper in my projects. There, I have many many functions wich generates chunks of reusable things (also called modules or components).
I do like, also, the WordPress approach wich use to return data in the main function (you may need more treatments before display) and a "sister function" to directly echo the results.
I think it'll works with you. For example:
function get_display_comments(array $comments, $parentId = NULL)
{
$CI =& get_instance();
$return = '';
foreach ($comments AS $comment)
{
if ($comment['replied_to_id'] == $parentId)
{
$return .= $CI->load->view('reviews/comment_list', $comments, TRUE);
}
}
return $return;
}
function display_comments(array $comments, $parentId = NULL)
{
echo get_display_comments($comments, $parentId);
}
Your content on the first file :
<?php
$CI=& get_instance();
echo $CI->load->view('reviews/comment_list', $comments, true);
?>
And the reviews/comment_list view :
<ul>
<?php
foreach($comments as $comment){
$comment=$comment['comment'];
echo "<li>" . $comment . "</li>";
}
?>
</ul>
just write this and try again.