Get ID of a DIV element using php - php

-- edit --
I've made the question far more detailed here: Drupal aggregators and tailored output
-- original question --
Is it possible to get the id of an html div element on any given page using php so I can do something like:
if($divid == 'id-of-div') { do this; }
--edit--
To clarify:
I have a page on a website. It could be any page. On that website I have a div element which I want to be able to target using php to populate depending on the id of that div element. For example:
if($divid == 'div1') { $output = 'div 1 output'; }
if($divid == 'div2') { $output = 'div 2 output'; }
return $output;
Hope that helps...
-- re-edit(!) --
I've commented below but think it would probably be more useful in here:
Hi, sorry I'm not being more clear. I'm not a php expert (as you can probably tell!). I am theming a Drupal site at the moment and have a function that generates output to a block. These are the same kinds of block but they are unique by virtue of their div id's. I want to be able to tailor the generated output to these depending on which div area it is. Does that make any more sense? Thanks for your help so far, by the way

I think you are misunderstanding the way PHP works. PHP is usually used to generate the raw HTML code of a page, but you usually don't use it to traverse the DOM of the output like you can in jQuery. (In theory, you can do this using output buffering and a HTML parser class, but it makes no sense at all.)
There shouldn't be the need to find elements by ID because in order to put something into a DIV, you can do the following:
<div id="mydiv">
<?php echo "This is inside mydiv"; ?>
</div>
The resulting HTML will be a DIV that contains whatever you told PHP to output. This is usually enough.
Not sure whether this helps you. If it doesn't, maybe add some more detail about what you want to do.

For this use hidden varriables

Related

PHP | How would I create a "plugin system" to inject content/HTML onto my page?

I am trying to create a function that can inject HTML in between certain div tags using PHP.
For example lets say I have the function with the following parameters. add_html('<div class = "greeting">', 'hello')
How would I make it so that the text hello was added inside of <div class = "greeting"> like so: <div class = "greeting">hello</div> My goal is to use PHP for this. How would I go about implementing this? What would be the most simple way to accomplish this?
A function could be like so:
<?php
function add_div($class, $content) {
return sprintf('<div class="%s">%s</div>', $class, $content);
}
echo add_div('foo', 'Hello Earth!');
Output:
<div class="foo">Hello Earth!</div>
But it doesn't really save you much typing.
You've changed the particulars of it, but the fundamental issue remains unanswerable with any kind of specifics that it seems you're looking for. I am struggling to even express how many variables there are in what you're asking. I'm putting this as an answer, not because it answers your question, but because comments are insufficient to address it.
Your current example is that you want a "greeting" div. Okay. But where do you want it? On every page? On the home page? At the top or bottom or somewhere in between? Let's assume that your CMS has a way of knowing what overall layout is going to be used (headers, footers, menus, sidebars, etc.), and what specific template is going to be used to generate the page-specific content (text and pictures of a blog post, for example), and that you want to give your plugins a way to inject their own content into the layout right at the top of the content (above the blog text and pictures). This is now a nice, specific sort of question that StackOverflow is better at answering, instead of such a general open-ended one that you have.
Okay, let's address that particular question. Ideally, you would show your CMS code for generating a page. Let's pretend it's something like this:
echo $this->layout->headers();
echo $this->layout->menu();
echo $this->template->content();
echo $this->layout->footers();
It's probably actually more complex than that, but this will do for an example. Now, we can say that if you want to use a plugin hook architecture, you would add something like this after the menu call:
foreach ($this->plugins as $plugin) {
if (method_exists($plugin, 'inject_pre_content')) {
$plugin->inject_pre_content();
}
}
and you would add an inject_pre_content function in your plugin which echoes your desired HTML.
If you wanted to use event hooks instead, then in the same place you might instead simply add:
$this->triggerEvent('inject_pre_content');
but this relies on you also incorporating an event handling framework.
Plugin hooks are, IMO, easier to build, but not as powerful.
Hope that this helps you to understand what you're up against a little bit better, and clarifies what the two main options might look like.

Remove empty div in php

I know that I can remove an empty div using javascript, but is it possible using php?
I have a div that has several if statements that will fill the content of the div. If those terms aren't met I get up with an empty div, doing nothing. The div has a class, if that makes it any simpler.
Is this possible in php? Is there a .hide() or .remove() equivalent in php?
You can however try out by including div inside if condition and closing it wherever it is appropriate,
this displays div if a particular condition is present
else doesn't include particular div in your code at all
For E,g
if(some condn..)
{
echo "<div>";
your code...
echo "</div>";
}
I have shown you the simple one using one if, but you can fix it up for any number of ifs
with little attention of opening and closing of DIV tags
Secondly Jquery is Equivalent to CSS
Jquery
i.e $("#abc").hide();
css
#abc{
display:none;
}
etc i have given you just a sample but you can explore on...
PHP is a server scripting language,So it is not possible.
As the others have mentioned, this is not possible using a server-side language, unless you choose to not render the div at all in the first place. This is not necessarily a bad choice, but depending on what you are planning on doing (if you wanted to provide more of an explanation, please feel free to).
If you would prefer to do this client-side then you could use either vanilla javascript and remove the node from the DOM. Alternatively (probably a better idea) you could remove it using a one-liner using jQuery:
$(".yourDiv").remove();
Up to you - hope that helps!
No, it's not possible to remove a created div with PHP.
What you can do is not create the div unless one of your if statements pass
No, PHP is a SERVER side language that only runs before the page is loaded the only way to hide a div is to give a hidden div in the html. You ill need javascript to hide it dynamicly.
using php, you can do it like this
<?php if(satisfy both $a and $b): ?>
<div>
<?php
if($a)
{
echo $a;
}
else if($b)
{
echo $b;
}
?>
</div>
<?php endif; ?>

how to input a chunk of html with php

I am currently working on a website that uses a login system I made.
I have a div that holds a bar that goes on the top of the website. This bar is supposed to change depending if you're a guest, logged in, or a admin. (Therefor there are three separate divs - guestmode, membermode and adminmode)
I have tried to make php print the html when wanted, and it worked. The only problem is that it's messy and not at all easy to edit.
Is it possible for php to print html without using 'echo' or 'print', but copying from another file?
Basically I want to copy normal html syntaxed-code from a file somewhere and turn it into something that php can print, then of course print it.
Is there something out there that can do that?
Thanks in advance!
For PHP, use something like:
echo file_get_contents('path/to/my_html_snippet.my_ext'); // echo instantly
$stored = file_get_contents('path/to/my_html_snippet.my_ext'); // or save it for later in a string
Create a file at path/to/my_html_snippet.my_ext that contains the HTML you want to print.
Its seems like you need a templates.
See mustache (examples and docs - https://github.com/bobthecow/mustache.php)

Css Html JavaScript - Navigation Highlight current selected Menu

i think this question is very easy but I don't know if i am right.
I am an oldschoool html, php coder.
I want to use this kind of navigation:
http://www.cssportal.com/horizontal-menus/13styles.htm
So far no problem. I got an dynamic php page and i want to use this Menu.
There is no problem without this line in the HTML Part:
<li><span>LINK</span></li>
The Problem is the class. What is the smartest way to detect which link is now current?
I would do it in this way. I would write a php script like this pseudo code:
if acutaldocument == "link1.html" then echo "class='current' ";
But i think this is not the smartest way. Or am I right?
Thanks
There's many options...
You can use session cookies, JavaScript, you can pass an id on the end of the url (eg. ?nav=2) or parse the URL and check against it...
All of them work... all of them have there pros and cons... just depends on how your page is set up...
Give each page's body tag an ID. Say, you give the About page's body tag the id "about". Give IDs for all your navigation <li>s too. Say, you give "about" id to the navigation <li>
In your CSS file, do this:
body#about li#about {
// apply differentiating style here...
}
You can keep doing that for all other pages also. And only when both the body ID and the <li> ID match, the style is applied.

framework questions. How can I reuse the same template?

I'm hoping I can get a definite answer on this. I will try and explain this as best I can.
I have a main template file that contains everything I want. In a small area within this template, I want to echo the view that is associated with the link that the user clicks on. This is not a problem so far however, when I have to use a loop data it is not so easy anymore.
If I have this as my template:
table
tr
td><?php echo $myContent; ?></td
/tr
/table
You can clearly see how I would be able to echo the values of the variable. Now, suppose that I wanted to show the user something that required multiple rows of data. I would then have to use a loop. So, you see, it depends on the content that I want to display that determines what construct to use within the HTML.
Can someone please help me to figure this out? I'm sure this is just me not correctly understanding how this should be done.
Thanks
Have a HTML files with custom tags of your own.
say template.html:
<div><$content$></div>
when processing the output, you take the contents of template.html and parse it:
$replace = array();
$replace['<$content$>'] = $pagecontents;
$template = file_get_contents('template.html');
$output = str_replace(array_keys($replace),$replace,$template);
Adapted from http://code.google.com/p/samstyle-php-framework/.

Categories