Jquery cycle menu with container - php

I am trying to link an anchor to cycle to a specific div. This is what I have:
The menu
<div id="mainmenu">
<ul class="sidenav" id="menu">
<li>
<a href="#page2">Serviciile noastre
</a>
</li>
<li>
<a href="#page8">Credite Nevoi personale fara ipoteca
</a>
</li>
<li>
<a href="">Credite Nevoi personale cu ipoteca
</a>
</li>
<li>
<a href="">Credite ipotecare
</a>
</li>
<li>
<a href="">Carduri de credit
</a>
</li>
<li>
<a href="">Credite "Prima Casa"
</a>
</li>
<li>
<a href="">Refinantari credite/carduri
</a>
</li>
<li>
<a href="">Acte necesare
</a>
</li>
<li>
<a href="">Economisire creditare
</a>
</li>
</ul>
</div>
And the JavaScript
var $jts = jQuery.noConflict();
$jts(function() {
// maincontent cycle
$jts('#maincontent').cycle({
fx: 'blindY', // You can choose effect do you like, for reference : http://www.malsup.com/jquery/cycle/browser.html
speed: 'slow',
timeout: 0,
cleartype: true, // true if clearType corrections should be applied (for IE)
cleartypeNoBg: true,
pager: '#menu',
startingSlide : 0,
after:onAfter,
pagerAnchorBuilder: function(idx, slide) {
// return sel string for existing anchor
return '#menu li:eq(' + (idx) + ') a';
}
});
function onAfter(curr, next, opts, fwd){
//get the height of the current slide
var $ht = $jts(this).height();
I am using jquery.cycle.all.min.js.

So if I understand correctly, you wish to have an <a href=... click event to take it to the corresponding slide.
Simplest way to do this for your example slide id of 7:
The link you want to click to view a specific slide:
<!-- support for those strange users
without Javascript - we will fix this later -->
Sample text
Your JS to control the behaviour
//if the user has JS enabled, we will stop the
//link from taking us away from the page
//and only do the sweet animation
$(".go-to").click(function(gt){
gt.preventDefault();
var slide = $(this).attr("rel");
$("#maincontent").cycle(slide);
});
Main things to note:
Use of the rel attribute. Use it, it's great.
Use of the empty selector class go-to - used only to define that particular link to interact with cycle. This can be any name you like (so long as it follows the conventions outlined below), for example foo, bar, coco-the-clown etc.
Finally, the next is a little off-topic, but a useful piece of information none-the-less;
If you want your pages to validate to W3C standards, ID's must be unique, and start with a letter. Direct from the horse's mouth:
ID tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
http://www.w3.org/TR/html4/types.html#type-name
in response to your comment
So the basic premise of what we want to do:
Assign an identifier to each link that corresponds with the slide number - which means this will have to be a number.
Find out the slide order.
Using your link you posted in the comment you should change it to reflect which ever slide number you want to see when clicked, like so:
<a href="credite.php" class="go-to" rel="1" >Credite Nevoi personale fara ipoteca</a>
Also, remove the rel="opt" and class="go-to" from your div element.
If you go and try this now (assuming you have more than 1 slide...) and it is what you wanted, you probably won't need to come back and read the rest.
So when you click the link mentioned above, this should take you to slide number 2 - provided you are not on slide number 2. "What?!" I hear you cry. Read on.
So, if you were to look at the rendered source code in your browser, you will see that inside of your cycle element, maincontent, each of the div elements that are direct descendants of maincontent will be the slides. These are in order (for a computer); 0 is the starting slide and 1 is the second slide, 2 is the third slide ad infinitum.
So - if you are using PHP & MySQL to show your content, it will be super-easy to assign "IDs" to your anchor's rel attributes. Alternatively, you can hard-code these.
Let me know how you did!

Related

Convert html tags to php

I have a system status page from a partner that I am trying to convert so that I can automate its input into my service page. The status of the service is provided by a <span> class. I know this is far from ideal but this is the only way that they provide updates.
Below is a copy of the HTML:
<ul class="item-3">
<li class="status service1">
<span class="icon"></span>
<h6>
SERVICE 1
</h6>
<p>
Updated:
9:52 AM CST Apr, 24
</p>
<span class="arrow up"></span>
</li>
<li class="status service2">
<span class="icon"></span>
<h6>
SERVICE 2
</h6>
<p>
Updated:
9:52 AM CST Apr, 24
</p>
<span class="arrow up"></span>
</li>
<li class="status service3">
<span class="icon"></span>
<h6>
SERVICE 3
</h6>
<p>
Updated:
9:52 AM CST Apr, 24
</p>
<span class="arrow up"></span>
</li>
</ul>
I need to grab the <span class="arrow up"> values as this is the service status, and the <li class="status service3"> as this tells me what service it is.
Since my status page API uses IDs and not "up/down" etc I will need the statuses and services to be in arrays so I can convert them into my formats - hope that makes sense!
Use loadHTML method
<?php
$str = '<h1>T1</h1>Lorem ipsum.<h1>T2</h1>The quick red fox...<h1>T3</h1>... jumps over the lazy brown FROG';
$DOM = new DOMDocument;
$DOM->loadHTML($str);
//get all H1
$items = $DOM->getElementsByTagName('h1');
//display all H1 text
for ($i = 0; $i < $items->length; $i++)
echo $items->item($i)->nodeValue . "<br/>";
?>
Output:
T1
T2
T3
I'm not completely certain what you are trying to achieve, but I'll make a few assumptions to clarify what I say:
I assume you don't generate the HTML yourself, but that you want to grab certain elements from the HTML you present in your question, thereby making it impossible to assign the full HTML to a variable.
The two possible classes in the span would either be "arrow up" or "arrow down" (as a matter of fact that's two classes, namely "arrow" and "up" (or "down"). So if for intstance you want to grab all instances of these span elements, you could simply do the following:
$ElementsArray = document.getElementsByClassName("arrow");
This will give you an array of all elements of the class "arrow" no matter if they also contain the class "up" or the class "down". If you want to be able to only select all "up" or "down" you could do the same just like this:
$arrayOfUps = document.getElementsByClassName("up");
Assuming there are more elements containing the class "up" (such as "elevator up", "seven up", etc.) you can also use the same function to search for elements containing both class names like this:
$arrayOfUps = document.getElementsByClassName("arrow up");
That way you make sure you don't also get the elements containing:
class="seven up", class="elevator up", etc.
If you only want to get an array of all instances of arrow (either "up" or "down") and iterate through each instance, you can of course do the following:
$searchFor = "arrow";
$results = array();
$arrCounter=0;
var $elementsList= document.getElementsByClassName("arrow");
for ($i=0; i < $elementsList->length; i++) {
$temp = $elementsList->item($i);
if (stripos($temp->getAttribute('class'), $searchFor) !== false) {
$results[$arrCounter] = ( stripos($temp->getAttribute('class'), "up") !== false ) ? "UP" : "DOWN" ;
$arrCounter++;
}
}
The same goes for the list-items.
This i by no means elegant coding, and I would also turn this into a function, but I only wanted to suggest the PHP DOM functions to use for the purpose you suggested.
English is not my first language, so I might have completely missed your point, and in that case I apologize in advance.

Get last li content Xpath php

I have next html:
<ul class="pages">
<li class="page-1 active"><a data-page="1" href="/sold?page=1">1</a></li>
<li class="page-2"><a data-page="2" href="/sold?page=2">2</a></li>
<li class="page-3"><a data-page="3" href="/sold?page=3">3</a></li>
<li>...</li>
<li class="page-975"><a data-page="975" href="/sold?page=975">975</a></li>
</ul>
I am trying to get the last li's text which contains the number of the last page (in my example it is 975) with help of Xpath.
I've tried something like:
$page_count = $xpath->query(".//ul[#class='pages']/li/a[last()]/text()")->item(0)->textContent;
but it doesn't work.
what would be the correct query to get last li's text?
try this one:
$page_count = $xpath->query(".//ul[#class='pages']/li[last()]/a/text()")->item(0)->nodeValue;
first: you need to grab the last li and then the text of its a-element
Your version basically searches all the li-elements and within them searches for the last a-element (they only have one) and then their text attribute. So you basically got a list of all the texts.
second: try nodeValue instead of textContent

Fill a container (a div) until it is full

I have an array of strings.
$x=array('blabla1', 'blabla2', ...);
I want to fill a div block with strings from $x until my div is full. The height of my div is fixed to $h.
For instance, I want to put in my div something like that
<div>
<ul>
<li> 'blabla1' </li>
<li> 'blabla2' </li>
<li> 'blabla3' </li>
...
</ul>
</div>
until it is full.
Any guess how to do so ?
Javascript or php ?
Thank you :)
Why I want to do this : I have a side div on my webpage with suggested links. I want to put as many suggested links as possible in this side div.
Colas
PS : Feel free to edit my post (eg, add tags).
Its going to be problematic to do this by just using php.
My suggestion is to do it using overflow hidden css property in combination with a jquery plugin like:
dotdotdot
You must determine the height of your div in any fixed measurable unit such as px. Then adjust the height of each list item. By dividing the height of div/ the height of list item, you will know the number of list items required say n so inside your div do the following:
<div class="define-height">
<ul>
<?php
for ($i = 0; $i < $n; $i++){
?>
<li><?php echo $x[$i];?></li>
<?php } ?>
<ul>
</div>
Because you already know the fixed height value of your div. You can also add a fixed height value to the li elements and then from php do the following:
I am assuming that you already set the fixed height values for the div and ul in your css.
$div_height = 500px;
$li_height = 21px;
echo '<div id="mydiv"><ul id="myul">';
for($i=0;$i<=$div_height;$i+=$li_height){
echo "<li>"."your content here"."</li>";
}
echo '</ul></div>';
Using jQuery client side:
while( $("#divId").height() > $("#ulId").height() ) {
$("#ulId").append("<li>blabla1</li>");
}
From the comments :
any reason you can't just use overflow: hidden? – Marc B
Then you have to doit through javascript. From javascript is the only way you can have the height of a dom element after created. Maybe this div has to be dynamically populated through an ajax call to php that returns only one li element. And after that in javascript calculate if there is enough space to anoter li element in that case do the next call until there is no more available space. You can also set overflow:hidden in your div but this will only hide the content that is out of the div. – slash28cu

Injecting a custom ul into a menu

So I am using a cool little function to render a search bar in my menu. Check it out here:
function add_search_to_wp_menu ( $items, $args ) {
if( 'main-menu' === $args -> theme_location ) {
$items .= '<li class="menu-item menu-item-search">';
$items .= '<form method="get" class="menu-search-form" action="' . get_bloginfo('home') . '/"><p><input class="text_input" type="text" value="Enter Text & Click to Search" name="s" id="s" onfocus="if (this.value == \'Enter Text & Click to Search\') {this.value = \'\';}" onblur="if (this.value == \'\') {this.value = \'Enter Text & Click to Search\';}" /><input type="submit" class="my-wp-search" id="searchsubmit" value="search" /></p></form>';
$items .= '</li>';
}
return $items;
}
add_filter('wp_nav_menu_items','add_search_to_wp_menu',10,2);
I didn't make it - but its doing the job.
So the only problem is that it is appearing at the top level:
<ul>
<li>Normal Item</li>
<li>OH LOOOK THE SEATCHBAR APPEARS HERE</li>
</ul>
The problem is that I need it to appear a few more steps down in the menu, ie - here:
<ul>
<li>
Normal Item
<ul>
<li>
I want THE SEARCH to appear here
</li>
<ul>
</li>
</ul>
Not only that, I want it to appear after the last <ul><li><ul><li> in the menu...
IE, if I had a list of 3 normal items with their own sub items, it would look like this once injected:
<ul>
<li>
Normal Item1
</li>
<li>
Normal Item2
</li>
<li>
Normal Item3
</li>
<li>
Normal Item
<ul>
<li>
I want THE SEARCH to appear here
</li>
<ul>
</li>
</ul>
Hopefully that makes sense. Thanks for the help!
A quick and dirty hack. It will (maybe, needs tuning) works but you have to fiddle with the code and the codex (reference on the bottom of the answer) or you will not make it another time.
function add_search_to_wp_menu_item ( $items, $args ) {
$search_form = '<ul><li class="menu-item menu-item-search">'
. '<form method="get......./form>' // FILL IT WITH YOUR HTML
.'</li></ul></li>';
if( 'main-menu' === $args -> theme_location ) {
$items = preg_replace('/<\/li>\s*$/',$search_form,trim($items));
}
return $items;
}
add_filter('wp_nav_menu_items','add_search_to_wp_menu_item',10,2);
It SHOULD work as I don't know the content of the $item parameter.
If I guess right, stated the correctness of the legacy function, it is a string containing a list of li (without enclosing ul) and then I inject an ul into the last one (if the filter is processing the main-menu element of the page).
References:
http://codex.wordpress.org/Function_Reference/add_filter
http://www.php.net/manual/en/function.preg-replace.php
Addendum
What '/<\/li>\s*$/' is?
It is a regular expression (see an easy tutorial on them], it is not limited to php as it is present in several languages.
It is a language per se, to be precise.
preg_replace leverage on regular expression to let you perform some advanced string manipulation. That regex tell to the engine to find the last tag in the string the one just before the string's end it doesn't matter if there are spaces, tabs or newlines (but just this set of chars) before the ending of the string.
I will dissect the code hoping it will shed some light on it.
The first and last chars / are the regex enclosure, the real regex is <\/li>\s*$
If you search for a literal / into your string you need to escape it with a \. The engine will not think that it marks the end of the regex.
If you escape chars you give them special meanings \s is one of them, and represent a character class (a set of different characters) a tab, a space or a new line.
* is a quantity modifier, it change the meanings of the previous character stating that it can be repeated a number of times there are several of them. Ours say 0 or more.
`$' is a boundary. It means the end of the string.
When the engine find in your string a chunk of characters that fit the description you give him then there is a match and that part of the string will be changed with the second parameter given to preg_replace.
I've just scratched the surface of this topic, and it is up to you to choose to go deeper or not.
The tutorial I linked (you need to read all the 3 parts) is a good point of start.

Fixing Current Tab Color using 1 php file only

im sorry for the poor title, i dont know how to explain it. You see i have already created my panel tabs with the help of ol, li and css. it is working perfectly but then there is one problem that occured in the program. this tab li class="current"
the purpose of the class=current that is set in a specified link will help change the background image of the active link. the css code for that is already set and working. I will first show you the codes i used here:
<ol id="toc">
<li> </li>
<li class="current"><span>#</span></li>
<li><span>A</span></li>
<li><span>B</span></li>
<li><span>C</span></li>
<li><span>D</span></li>
<li><span>E</span></li>
<li><span>F</span></li>
<li><span>G</span></li>
<li><span>H</span></li>
<li><span>I</span></li>
<li><span>J</span></li>
<li><span>K</span></li>
<li><span>L</span></li>
<li><span>M</span></li>
<li><span>N</span></li>
<li><span>O</span></li>
<li><span>P</span></li>
<li><span>Q</span></li>
<li><span>R</span></li>
<li><span>S</span></li>
<li><span>T</span></li>
<li><span>U</span></li>
<li><span>V</span></li>
<li><span>W</span></li>
<li><span>X</span></li>
<li><span>Y</span></li>
<li><span>Z</span></li>
</ol>
as you can see here is the list of links that i have. its purpose is to search the employee name and information from the database and output in on the next tags where the name of the employee must start with the letter specified in the link list A-Z that were clicked.
now my problem is, the links are called to the same page and does not contain its own php file. from the site that was recommended on me to study, i saw that each linked have its own php file thus the only difference is the location on the class=current example:
if your on A.php
<ol id="toc">
<li class="current"><span>A</span></li>
<li><span>B</span></li>
</ol>
the link in that page contains the class=current but the other li tags does not, else when clicked on a different link like for example on B.php
<ol id="toc">
<li><span>A</span></li>
<li class="current"><span>B</span></li>
</ol>
the class="current" is in link B.
but mine is called in the same page by inserting ?namelist=a in each link. if i put class="current" on every li link, i would be able to get what i want which is making it able to see your current page. does anyone here knows how?? thanks for those who will reply :)
MisaChan
This function will loop through a to z and check if the current page is index.php?namelist=a or b,c ect determining where to place the class="current"
function toc_menu($current){
$return ='<ol id="toc">
<li> </li>'."\n";
$return .= ($current=='') ? '<li class="current"><span>#</span></li>'."\n" : '<li><span>#</span></li>'."\n";
foreach(range('a','z') as $link){
$return .= ($current==$link) ? '<li class="current"><span>'.strtoupper($link).'</span></li>'."\n" : '<li><span>'.strtoupper($link).'</span></li>'."\n";
}
$return .="</ol>\n";
return $return;
}
//echo where you want the menu
echo toc_menu(strtolower($_REQUEST['namelist']));
//or hold it in a variable to display later on
$tocmenu = toc_menu(strtolower($_REQUEST['namelist']));
//outputs this is E was clicked
<ol id="toc">
<li> </li>
<li><span>#</span></li>
<li><span>A</span></li>
<li><span>B</span></li>
<li><span>C</span></li>
<li><span>D</span></li>
<li class="current"><span>E</span></li>
<li><span>F</span></li>
<li><span>G</span></li>
<li><span>H</span></li>
<li><span>I</span></li>
<li><span>J</span></li>
<li><span>K</span></li>
<li><span>L</span></li>
<li><span>M</span></li>
<li><span>N</span></li>
<li><span>O</span></li>
<li><span>P</span></li>
<li><span>Q</span></li>
<li><span>R</span></li>
<li><span>S</span></li>
<li><span>T</span></li>
<li><span>U</span></li>
<li><span>V</span></li>
<li><span>W</span></li>
<li><span>X</span></li>
<li><span>Y</span></li>
<li><span>Z</span></li>
</ol>
From what I understand, you want a different color of the link on the current page. For e.g. if someone goes to index.php?namelist=a, the color for link A should be different than others. If this is the case, then just add a check for $_REQUEST['namelist'] while adding the class and you should be okay.

Categories