PHP Xpath Select elements after - php

I'm trying to get the subcategories for each category with xpath from this html code :
<ul class="simple_list">
<li class="category"><span class="category_span">ILUMINAT UTILITAR</span></li>
<li class="subcat"><span class="subcat_span">Indus70</span></li>
<li class="subcat"><span class="subcat_span">ApliLED48</span></li>
<li class="subcat"><span class="subcat_span">Minoris</span></li>
<li class="subcat"><span class="subcat_span">Canopus 230V</span></li>
<li class="category"><span class="category_span">ILUMINAT OFFICE</span></li>
<li class="subcat"><span class="subcat_span">Pavo 1 modul</span></li>
<li class="subcat"><span class="subcat_span">Laminar</span></li>
<li class="subcat"><span class="subcat_span">Alcor T</span></li>
<li class="subcat"><span class="subcat_span">Cassiopeia</span></li>
<li class="subcat"><span class="subcat_span">Sirius 8 module</span></li>
<li class="subcat"><span class="subcat_span">Cygnus</span></li>
<li class="subcat"><span class="subcat_span">Aries</span></li>
<li class="category"><span class="category_span">ILUMINAT INDUSTRIAL</span></li>
<li class="subcat"><span class="subcat_span">Castor 6 module</span></li>
<li class="subcat"><span class="subcat_span">Corona</span></li>
<li class="subcat"><span class="subcat_span">Octans</span></li>
<li class="subcat"><span class="subcat_span">Lacerta 3 module</span></li>
<li class="subcat"><span class="subcat_span">Fornax</span></li>
<li class="subcat"><span class="subcat_span">Cetus</span></li>
<li class="category"><span class="category_span">ILUMINAT DE EXTERIOR</span></li>
<li class="subcat"><span class="subcat_span">ELMA80</span></li>
<li class="subcat"><span class="subcat_span">Solaris 2 module</span></li>
<li class="category"><span class="category_span">BECURI CU LED</span></li>
<li class="subcat"><span class="subcat_span">LEC</span></li>
<li class="category"><span class="category_span">ACCESORII</span></li>
<li class="subcat"><span class="subcat_span">Banda</span></li>
<li class="subcat"><span class="subcat_span">Drosel</span></li>
For the subcat items I tried this :
$xpath->query('//li[#class="subcat" and not(preceding-sibling::li[#class="category"]['.$i.'])])
I want to extract all subcat items under a category item ... any xpath idea would be great .
At this point I'm doing this :
for ($i = 0; $i<=$nodecount; $i++) {
foreach( $xpath->query('//li[#class="category"]['.$i.']') as $category ) {
echo "<br/>" . "$i - Main Categ Name : " . $category->nodeValue . "<br/>";
//Now get the subcateg names and links
$subnodecount = 0;
foreach( $xpath->query('//li[#class="subcat"]') as $sub_categ_links_on_page ) {
$subnodecount++;
}
for ($y = 0; $y<=$subnodecount; $y++) {
foreach( $xpath->query('//li[#class="subcat" and preceding-sibling::li[#class!="category"]]['.$i.']['.$y.']') as $sub_category ) {
echo "--- $y - Sub Categ Name : " . $sub_category->nodeValue . "<br/>";
}//end foreach
}// end FOR $y
///////////////////////////////////////////////////////////
}//end foreach
}// end FOR $i
But I am stuck at the subcategs part..
I'm looking to get this output :
1 - Main Categ Name : ILUMINAT UTILITAR
--- 1 - Sub Categ Name : ApliLED48
--- 2 - Sub Categ Name : Minoris
--- 3 - Sub Categ Name : Canopus 230V
--- 4 - Sub Categ Name : Pavo 1 modul
[and so on]
Live code can be found and fiddled with, here :
http://codepad.viper-7.com/i8hweN

To get all those list below the category you could also use != in this case:
$xpath->query('//li[#class="subcat" and preceding-sibling::li[#class!="category"]]');
Sample Output
As an alternative, you could gather all those results first. Then you could just use CSS or a simple formatting for presentation:
$elements = $xpath->query('//li[#class="subcat" and preceding-sibling::li[#class!="category"]]');
$list = array();
if($elements->length > 0) {
foreach($elements as $cat) {
$category = $xpath->evaluate('string(./preceding-sibling::li[#class="category"][1])', $cat);
$list[$category][] = $cat->nodeValue;
}
}
// presentation
$i = $j = 1;
foreach($list as $category => $subcategory) {
echo $i . '. ' . $category . '<br/>'; $i++;
foreach($subcategory as $sub) {
echo str_repeat(' ', 5);
echo $j .'. ' . $sub . '<br/>'; $j++;
}
$j = 1;
}
Sample Output

Related

Selecting every 3rd item starting after the 2nd

I have the following code:
$loop = 0;
$columns = 3;
<?php while ( have_posts() ) :
the_post();
$loop++;
?>
<li class="<?php if ( $loop % $columns == 0) echo 'last'; if ( ( $loop - 1 ) % $columns == 0 ) echo 'first'; ?>">
</li>
What I want to achieve is:
<li class="first"></li>
<li class="last"></li>
<li class="first"></li>
<li class=""></li>
<li class="last"></li>
<li class="first"></li>
<li class=""></li>
<li class="last"></li>
What I am getting is (which makes sense..):
<li class="first"></li>
<li class="=""></li>
<li class="last"></li>
<li class="first"></li>
<li class=""></li>
<li class="last"></li>
<li class="first"></li>
<li class=""></li>
<li class="last"></li>
meaning the first 2 are given the class first and last respectively and after that it is every 3.
If i was able to use css I would say nth-child(3n+2) but I can't because I want to assign a new class, and I can only assign a style with css.
How can I achieve it with php?
The following will generate the code that you gave as an example:
<?php
$loop = 0;
$offset = 2;
while ($loop < 100) {
echo '<li class="';
if ($loop < $offset) {
if ($loop % 2 == 0) {
echo 'first';
} else {
echo 'last';
}
} else {
if (($loop - $offset) % 3 == 0) {
echo 'first';
} elseif (($loop - $offset + 2 ) % 3 == 0) {
echo '';
} else {
echo 'last';
}
}
echo '"></li>';
++$loop;
}
If the $loop variable is under the offset it print first and last alternately otherwise it will check divisibility with 3, giving the dividend an extra offset for each condition so they will be true sequentially.

Got all the same ID's from api, what can i do to make them count itself?

I'm getting all the data from GT api (xml) from link: http://api.gametracker.rs/demo/xml/server_info/217.26.212.10:20021/
Especially for players list, as you can see, all the PID's are 0, i'm displaying them in table with following code:
<?php
foreach( $players as $player ) {
echo "
<ul>
<li id=\"number\">$player->pid.</li>
<li id=\"nickname\">$player->name</li>
<li id=\"score\">$player->score</li>
<li id=\"time\">$player->time</li>
</ul>";
}
?>
What I can do to make pid count itself for as many as found players in xml file?
$i = 0; //Counter variable
foreach( $players as $player ) {
$i++; //Increment the counter for each iteration
echo "
<ul>
<li id=\"number\">$player->pid.</li>
<li id=\"nickname\">$player->name</li>
<li id=\"score\">$player->score</li>
<li id=\"time\">$player->time</li>
</ul>";
}
echo 'There were ' . $i . ' players in total';
All you need to do is have a variable for that:
$i = 1;
foreach ($players as $player)
{
echo "<ul>
<li id=\"number-$i\">$i.</li>
<li id=\"nickname-$i\">$player->name</li>
<li id=\"score-$i\">$player->score</li>
<li id=\"time-$i\">$player->time</li>
</ul>";
$i++;
}

Magento retrieve children from specific category

I have a problem with closing my li's and ul's at the correct moment.
With the code we have we retrieve all childeren of a specific categorie in the magento shop.
Now the problem is that i want to divide all children in lists. So we can have them sorted by category -> sub-category -> sub-sub category. I want my structure to be;
<ul>
<li>
<a>Head Child</a>
<ul>
<li>
<a>Sub child</a>
<ul>
<li><a>Sub sub child</a></li>
<li><a>Sub sub child</a></li>
</ul>
</li>
</ul>
</li>
<li>
<a>Head Child</a>
<ul>
<li>
<a>Sub child</a>
<ul>
<li><a>Sub sub child</a></li>
<li><a>Sub sub child</a></li>
</ul>
</li>
</ul>
</li>
</ul>
The output im getting now is
<ul>
<a title="View the products for the " href="#">Head child</a>
<li class="sub_cat">
<a title="View the products for the " href="#">Sub child</a>
</li>
<li class="sub_cat">
<a title="View the products for the " href="#">Sub sub child</a>
</li>
</ul>
This is our php code;
<?php
$cat = Mage::getModel('catalog/category')->load(9);
$subcats = $cat->getChildren();
foreach(explode(',',$subcats) as $subCatid)
{
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
echo '<ul>'.$_category->getName().'';
$sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
$sub_subcats = $sub_cat->getChildren();
foreach(explode(',',$sub_subcats) as $sub_subCatid)
{
$_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
if($_sub_category->getIsActive()) {
echo '<li class="sub_cat">'.$_sub_category->getName().'';
$sub_sub_cat = Mage::getModel('catalog/category')->load($sub_subCatid);
$sub_sub_subcats = $sub_sub_cat->getChildren();
foreach(explode(',',$sub_sub_subcats) as $sub_sub_subCatid)
{
$_sub_sub_category = Mage::getModel('catalog/category')->load($sub_sub_subCatid);
if($_sub_sub_category->getIsActive()) {
echo '<li class="sub_cat">'.$_sub_sub_category->getName().'';
}
}
}
}
echo '</ul>';
}
}
?>
You should consider using recursive function here
$cat = Mage::getModel('catalog/category')->load(9);
$html = '';
$html = getSubCategoriesHTML($cat, $html);
function getSubCategoriesHTML($cat, $html) {
$html .= ''.$cat->getName().'';
$html .= '<ul>';
$subcats = $cat->getChildren();
foreach(explode(',',$subcats) as $subCatid) {
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
$html .= '<li>';
$html .= getSubCategoriesHTML($_category, $html);
$html .= '</li>;
}
}
$html .= '</ul>';
return $html;
}
You can add this function in a helper or in your category model.
I haven't tested it at all so it may not work correctly. But I hope it can help you.

menu with 2 levels sub-menu with php & mysql

I want make menu with 2 levels sub menu. Eventually, the HTML should like the code below:
<ul>
<li>elektronikos prekes
<ul>
<li>dar kaskas
<ul>
<li>ka ka 2</li>
</ul>
</li>
<li>tranzistoriai
<ul>
<li>sub 1</li>
<li>tikri 1</li>
</ul>
</li>
</ul>
</li>
<li>kompiuteriai
<ul>
<li>nesiojami 8</li>
</ul>
</li>
</ul>
Here is my code:
$queryreg=mysql_query("select distinct a.meniu as meniu , b.sub as sub, c.sub2 as sub2,a.meniu_id as m_id, b.sub_id as s_id,
c.sub2_id as s_id2, count(distinct p.id) as pre
from meniu a
LEFT OUTER JOIN sub AS b ON ( a.meniu_id = b.meniu_id and b.rodyti='1' )
LEFT OUTER JOIN preke AS p ON ( b.sub = p.sub and a.meniu=p.meniu)
LEFT OUTER JOIN sub2 AS c ON ( b.sub_id = c.sub_id AND c.rodyti='1')
LEFT OUTER JOIN preke AS pr ON ( pr.sub2 = c.sub2 and pr.sub=b.sub and pr.meniu=a.meniu)
where a.rodyti='1' and a.meniu='elektronikos prekes' or a.meniu='kompiuteriai'
group by a.meniu, a.meniu_id, b.sub, b.sub_id, c.sub2, c.sub_id");
$AnkstesnisSub='';
$ankstesnisMeniu='';
$output = $ankstesnisMeniu.$AnkstesnisSub = '';
echo'<ul>';
while ($row = mysql_fetch_assoc ($queryreg))
{
if ($row['meniu'] != $ankstesnisMeniu )
{
if (!empty ($ankstesnisMeniu) )
{
$output .= " </ul></li></ul></li>";
}
$output .= '<li>' . $row['meniu'] . "<ul>";
$ankstesnisMeniu = $row['meniu'];
}
if ( $row['sub'] != $AnkstesnisSub)
{
if (!empty ($AnkstesnisSub))
{
$output .= "</ul></li>";
}
if($row['sub2']==NULL){$prekiu_kiekis = $row['pre'];}
elseif($row['sub2']!=NULL){$prekiu_kiekis ='';}
$output .= '<li>' . $row['sub'] . " $prekiu_kiekis<ul>";
$AnkstesnisSub = $row['sub'];
$prevID2 = $row['s_id'];
}
if($row['sub2']!=NULL){$prekiu_kiekis = $row['pre'];}
elseif($row['sub2']==NULL){$prekiu_kiekis ='';}
$output .= '<li>' . $row['sub2'] ." $prekiu_kiekis</li>";
$prevID3=$row['sub2'];
}
$output = '' . $output . "</ul></li></ul></li>";
echo"$output";
echo'</ul>';
But I get:
<ul>
<li>elektronikos prekes
<ul>
<li>dar kaskas
<ul>
<li>ka ka 2</li>
</ul>
</li>
<li>tranzistoriai
<ul>
<li>sub 1</li>
<li>tikri 1</li>
</ul>
</li>
</ul>
</li>
<li>kompiuteriai
<ul></ul>
</li>
<li>nesiojami 8
<ul>
<li></li>
</ul>
</li>
</ul>
</li></ul>
To the first menu item everything is fine, but the second wrong. maybe somebody can help me?
i think it's very complicated code, why not to separate this for 2 table?
top_menu => link_id, href, link_text, count_sub
sub_menu => link_id, parent_id, href, link_text, count_sub
the code for getting the menu:
$query = mysql_query("SELECT * FROM top_menu");
while ( $row = mysql_fetch_assoc($queryreg) )
{
echo '<li>' . $row['href'] . '</li>';
if( $row['subcount'] != 0 ) {
{ make query for the sub_menu and do more while... and in the while you can add more }
}
}
and so on.. it's more clean for my opinion.

How do I add different class for my ordered lists

I want add left, center & right class to my ordered lists while loop.
Code
<?php while ($fetch) { ?>
<li>haha</li>
<?php } ?>
Results should be
<ul>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
</ul>
Let me know
<?php $classes = array("left","center","right");
$i = 0;
while ($fetch) {
?>
<li class="<?php echo $classes[$i++ % 3] ?>">haha</li>
<?php } ?>
$cnt=0;
while ($fetch)
{
switch ($cnt%3)
{
case 0 : $class = 'left'; break;
case 1 : $class = 'center'; break;
case 2 : $class = 'right'; break;
}
echo '<li class="', $class, '">haha</li>';
++$cnt;
}
I've just tested the following code and verified that it produces the desired output:
<?php
$items = array('haha', 'haha', 'haha', 'haha', 'haha', 'haha');
$cssClasses = array('left', 'center', 'right');
echo "<ul>\n";
$i=0;
foreach ($items as $item) {
echo "\t<li class=\"" . $cssClasses[$i++ % 3] . '">' . $item . "</li>\n";
}
echo "</ul>\n";
?>
The output is:
<ul>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
</ul>

Categories