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.
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 have been working with web crawler. it worked for few sites,
now when i tried it with this particular site, it came nothing. no error nothing.
i wonder what went wrong..
the code goes as:
<?php
require_once('dom/simple_html_dom.php');
$html = file_get_html('http://www.studentdoc.com/phpBB2/viewforum.php?f=18&sid=2a150b97528c8ec47600692cc77daaf3');
$elementCount=0;
foreach($html->find('dl.icon a') as $elemen) {
foreach($elemen->find('dt a') as $element) {
$elementCount++;
$element->href = "http://www.usmleforum.com" . $element->href;
echo '<li target="_blank" class="itemtitle">';
if($elementCount < 5 && $elementCount > 2 && rand(0,1) == 1) {
echo '<span class="item_new">new</span>';
}
echo $element;
echo '</li>';
if($elementCount==12){
break;
}
}
}
?>
please go through the below given link for HTML structure..
http://www.studentdoc.com/phpBB2/viewforum.php?f=18&sid=2a150b97528c8ec47600692cc77daaf3
Any help is appreciated..
There is no DOM element like dl.icon a dt a. You probably want to fetch dl.icon dt a. Remove a from first argument in find method.
Always try to debug your code before asking questions. Simple echo "A"; die(); echo "B"; die(); after every statement will be very helpfull :)
In this case second foreach have 0 elements all the time.
The following is my code:
<?php
include('simple_html_dom.php');
$rowdate;
$html = new simple_html_dom();
$html->load_file('http://www.forexfactory.com/calendar.php');
foreach($html->find('.calendar_row') as $e)
{
$date=$e->find('span.date');
if ($date[0] != "")
{
$rowdate=$date[0];
}
$time=$e->find('.time');
$currency=$e->find('.currency');
$impact=$e->find('.impact');
$event=$e->find('.event');
echo $rowdate;echo ",";
echo $time[0];echo ",";
echo $currency[0];echo ",";
echo $impact[0];echo ",";
echo $event[0];
echo "<br>";
}
The above code works fine however $impact is not displayed at all while if you open the url in your browser directly and see the source code , we can see that the impact class is present within each calendar_row
Can anyone please guide me as to what I am doing wrong ?
Instead of:
$impact = $e->find('.impact');
echo $impact[0];
You want:
$impact = $e->find('.impact', 0);
echo $impact;
And you probably really want:
$impact = $e->find('.impact span', 0)->class;
Read the simple html dom documentation if you don't understand why.
I need to find links in a part of some html code and replace all the links with two different absolute or base domains followed by the link on the page...
I have found a lot of ideas and tried a lot different solutions.. Luck aint on my side on this one.. Please help me out!!
Thank you!!
This is my code:
<?php
$url = "http://www.oxfordreference.com/views/SEARCH_RESULTS.html?&q=android";
$raw = file_get_contents($url);
$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
$content = str_replace($newlines, "", html_entity_decode($raw));
$start = strpos($content,'<table class="short_results_summary_table">');
$end = strpos($content,'</table>',$start) + 8;
$table = substr($content,$start,$end-$start);
echo "{$table}";
$dom = new DOMDocument();
$dom->loadHTML($table);
$dom->strictErrorChecking = FALSE;
// Get all the links
$links = $dom->getElementsByTagName("a");
foreach($links as $link) {
$href = $link->getAttribute("href");
echo "{$href}";
if (strpos("http://oxfordreference.com", $href) == -1) {
if (strpos("/views/", $href) == -1) {
$ref = "http://oxfordreference.com/views/"+$href;
}
else
$ref = "http://oxfordreference.com"+$href;
$link->setAttribute("href", $ref);
echo "{$link->getAttribute("href")}";
}
}
$table12 = $dom->saveHTML;
preg_match_all("|<tr(.*)</tr>|U",$table12,$rows);
echo "{$rows[0]}";
foreach ($rows[0] as $row){
if ((strpos($row,'<th')===false)){
preg_match_all("|<td(.*)</td>|U",$row,$cells);
echo "{$cells}";
}
}
?>
When i run this code i get htmlParseEntityRef: expecting ';' warning for the line where i load the html
var links = document.getElementsByTagName("a"); will get you all the links.
And this will loop through them:
for(var i = 0; i < links.length; i++)
{
links[i].href = "newURLHERE";
}
You should use jQuery - it is excellent for link replacement. Rather than explaining it here. Please look at this answer.
How to change the href for a hyperlink using jQuery
I recommend scrappedcola's answer, but if you dont want to do it on client side you can use regex to replace:
ob_start();
//your HTML
//end of the page
$body=ob_get_clean();
preg_replace("/<a[^>]*href=(\"[^\"]*\")/", "NewURL", $body);
echo $body;
You can use referencing (\$1) or callback version to modify output as you like.
I'm working on a wordpress theme and I'm trying to call the parent category's name to pull the appropriate page template.
I can get the call function to echo the correct name, but when I try to nest it the function doesn't run. I saw that I needed to use { } since I was already inside php but it still isn't working right. Can someone straighten me out?
This gives the correct output:
<?php $category = get_the_category();
$parent = get_cat_name($category[0]->category_parent);
if (!empty($parent)) {
echo '' . $parent;
} else {
echo '' . $category[0]->cat_name;
}
?>
. . . so I created a category_parent.php file with that in it.
This is what I'm trying to nest it in:
<?php get_template_part( ' ' ); ?>
Like this:
1.
<?php get_template_part( '<?php get_template_part( 'category_parent' ); ?>' ); ?>
or this
2.
<?php get_template_part( '{get_template_part( 'category_parent' ); }' ); ?>
Neither one works.
I really don't know if this is what you want as I did not try to make sense of what you are doing. However, generally speaking, you do this:
<?php get_template_part( get_template_part( 'category_parent' ) ); ?>
Edit:
I looked up what get_template_part() does in WP, and I think Felix Kling's answer is what you need. There is a big difference between sending something to the screen and assigning it to a variable.
<?php
echo 'filename';
?>
If you include that file, you will see filename in the browser. PHP knows nothing about it. (Okay, it could if you made use of output buffering functions, but that's besides the point...)
However if you do something like:
<?php
$x = 'filename';
?>
You can now use it in your function:
<?php
get_template_part($x);
?>
So what Felix is telling you to do is to put the logic that you currently have into a function. In this example:
<?php
function foo()
{
return 'filename';
}
get_template_part(foo());
?>
Now whatever value foo() returns will be sent to your get_template_part().
Taking your code:
$category = get_the_category();
$parent = get_cat_name($category[0]->category_parent);
if (!empty($parent)) {
$name = $parent;
} else {
$name = $category[0]->cat_name;
}
get_template_part($name);
You could take Felix's answer and put it into a file called category_parent.php, and then use it like:
require_once 'category_parent.php'
get_template_part(getName());
Honestly I am not so familiar with Wordpress, but it seems to me, you could do:
function getName() {
$category = get_the_category();
$parent = get_cat_name($category[0]->category_parent);
if (!empty($parent)) {
return '' . $parent;
} else {
return '' . $category[0]->cat_name;
}
}
get_template_part(getName());
konforce is correct about the syntax and, like konforce, I have no idea what you are trying to do. You do not need to use { } because your are not trying to dynamically name a variable and you certainly don't need to escape to php using <?php ?>, as (1) you are already in php and (2) it will stop interpreting PHP and assume html the second it hits the first '?>'.
There is no special syntax for nesting functions. Simply:
get_template_part(get_template_part('category_parent'));
is the syntax, but I have no idea what the function is or does, so I have no idea if that will work.
To debug, why don't you try this:
$parent = get_template_part('category_parent');
echo 'parent: ' . $parent . '<br />';
$result = get_template_part($parent);
echo 'result: ' . $result . '<br />';
When using variables in php strings, you will need to use double quotes ("). I assume option 2 should work then.