How can i detect category page on Joomla? - php

I want to detect virtuemart category page in mod_breadcrumbs.
Breadcrumbs is loading all page and i just wanted write a message in category page.
My breadcrumbs code:
<div class="breadcrumbs<?php echo $moduleclass_sfx; ?>">
<?php
echo '<ul>';
for ($i = 0; $i < $count; $i ++) {
// If not the last item in the breadcrumbs add the separator
if ($i < $count -1) {
if (!empty($list[$i]->link)) echo '<li>'.$list[$i]->name.'</li>';
else echo '<li class="pathway">' . $list[$i]->name . '</li>';
if($i < $count -2) echo ' <li class="pathway separator">></li> ';
} else if ($params->get('showLast', 1)) { // when $i == $count -1 and 'showLast' is true
if($i > 0) echo ' <li class="pathway separator">></li> ';
echo '<li class="pathway">' . $list[$i]->name . '</li>';
}
}
echo '</ul>';
?>
</div>

Here's how you can check if you're on a category view:
$appInput = Jfactory::getApplication()->input;
if($appInput->getCmd('option')=='com_content' && $appInput->getCmd('view')=='category' ){
//add your code here
}

Related

Additional <a> tag in final print in foreach php

I want print wordpress nav menu in custom html .
This function print my favorite out put , but the problem is printing the additional tags in output
I don't want use wp_nav_menu function
my favorite output is like :
<div class="menu">
<ul>
<li>
<a href="" ></a>
<div class="menu">
<ul>
.....
</ul>
</div>
</li>
</ul>
</div>
and my function is :
function sh_mobileMenuRender($menu_id)
{
$menu_items = wp_get_nav_menu_items($menu_id);
$html = '<div class="position-fixed mobileMenu-continer"
id="mobileMenu-continer" data-open="' . $opening_from . '"><div id="mobileMenu" class="menu"><ul>';
$is_frist_menu = true;
//این آرایه مقدار تورفتگی ها را محاسبه میکند
$y = array();
for ($x = 0; $x < $c; $x++) {
if ($menu_items[$x]->menu_item_parent == 0) {
//تو رفتگی رو صفر کن
$y[$x] = 0;
if ($is_frist_menu) {
$html .= '<li>بستن<i aria-hidden="true" class="fas fa-arrow-right"></i></li>';
$html .= '<li><a href="#">' . $menu_items[$x]->title;
$is_frist_menu = false;
} else {
$html .= '</a>';
if ($y[$x] < $y[$x - 1]) {
$dif = $y[$x - 1] - $y[$x];
for ($v = 1; $v <= $dif; $v++) {
$html .= '</li></ul></div>';
}
}
$html .= '</li><li><a href="#">' . $menu_items[$x]->title;
}
continue;
}
//find parent menu form privous menu
for ($b = $x - 1; $b >= 0; $b--) {
if ($menu_items[$x]->menu_item_parent == $menu_items[$b]->ID) {
//محاسبه تورفتگی
$y[$x] = $y[$b] + 1;
if ($b == $x - 1) {
$html .= '<i aria-hidden="true" class="fas fa-arrow-left"></i></a>';
$html .= '<div class="menu"><ul class="submenu">';
$html .= '<li>بازگشت<i aria-hidden="true" class="fas fa-arrow-right"></i></li>';
$html .= '<li><a href="#">' . $menu_items[$x]->title;
} else {
$html .= '</a>';
if ($y[$x] < $y[$x - 1]) {
$dif = $y[$x - 1] - $y[$x];
for ($v = 1; $v <= $dif; $v++) {
$html .= '</li></ul></div>';
}
}
$html .= '</li><li><a href="#">' . $menu_items[$x]->title;
}
break;
}
}
}
$html .= '</ul></div></div>';
echo $html;
}
I want print wordpress nav menu in custom html .
This function print my favorate out put , but the problem is printing the additional tags in output
I don't want use wp_nav_menu function

Unable to display the exact HTML code with PHP for loop

for ($i = 0; $i < count($item); $i++) {
echo '<ul class="list1">';
if ($i <= 1) {
echo '<li>'.$item[$i].'</li>';
}
echo '</ul>';
echo '<ul class="list2">';
if ($i >= 2 && $i <= 7) {
echo '<li>'.$item[$i].'</li>';
}
echo '</ul>';
}
I tried to display the following HTML with only php(above code) however, it seems like it's not working as expected. How can I display the exact same thing with php?
<ul class="list1">
<li>
<? echo $item[0];?>
</li>
<li>
<? echo $item[1];?>
</li>
</ul>
<ul class="list2">
<li>
<? echo $item[2];?>
</li>
<li>
<? echo $item[3];?>
</li>
<li>
<? echo $item[4];?>
</li>
<li>
<? echo $item[5];?>
</li>
<li>
<? echo $item[6];?>
</li>
<li>
<? echo $item[7];?>
</li>
</ul>
do something like this,
<?php
$item = ['1','2','3','4','5','6','7','8'];
for ($i = 0; $i < count($item); $i++) {
if ($i < 1) {
echo '<ul class="list1">';
}
if ($i <= 1) {
echo '<li>'.$item[$i].'</li>';
}
if ($i == 1) {
echo '</ul>';
}
if ($i == 2) {
echo '<ul class="list2">';
}
if ($i >= 2 && $i <= 7) {
echo '<li>'.$item[$i].'</li>';
}
if ($i == 7) {
echo '</ul>';
}
}
?>
this is the simplified syntax for the expected output
echo '<ul class="list1">';
for ($i = 0; $i < count($item); $i++) {
echo '<li>'.$item[$i].'</li>';
if ($i == 1) {
echo '</ul><ul class="list2">'';
}
}
echo '</ul>';
Try use two for loop, first loop items from 0 to 1 in "list1", second loop items form 2 to 7 in "list 2"
echo '<ul class="list1">'
for ($i = 0; $i <= 1; $i++) {
echo '<li>'.$item[$i].'</li>'
}
echo '</ul>';
echo '<ul class="list2">'
for ($i = 2; $i <= 7; $i++) {
echo '<li>'.$item[$i].'</li>'
}
echo '</ul>';

I am trying to call questions from my mysql and show them in ajax pagination in php

I am using ajax to call the questions from my mysql table to show them in pagination form but i am having trouble calling them according the subject and category and show them in the pagination form.
Here is my code .
<?php
print_r($_GET);
//echo $_GET['val12'];
$X=$_GET['val12'];
$category=$_GET['catg12'];
//print_r($_GET);
?>
<?php
//session_start();
function __autoload($classname)
{
include "admin_panel/$classname.php";
}
$obj = new connect();
$st=$obj->con();
$user= new questionclass();
$results=$user->getQuestions($_POST);
//print_r($_POST);
extract($_POST);
//$_SESSION['category']=$category;
//$key_id=$_SESSION['key'];
$qry = "select * from categories where category_name='$category'";
$run =mysqli_query($st,$qry);
$row = mysqli_fetch_assoc($run);
$testname=$row['test_name'];
$subject_name=explode(',', $testname);
?>
<div class="loading-div"></div>
<div id="results"><!-- content will be loaded here --></div>
<?php
{ //include config file
//Get page number from Ajax POST
if(isset($_POST["pagenum"])){
$page_number = filter_var($_POST["pagenum"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number
if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
}else{
$page_number = 1; //if there's no page number, set it to 1
}
$item_per_page=3;
//get total number of records from database for pagination
$results = $st->query("SELECT COUNT(*) FROM question");
$get_total_rows = $results->fetch_row(); //hold total records in variable
//break records into pages
$total_pages = ceil($get_total_rows[0]/$item_per_page);
//get starting position to fetch the records
$page_position = (($page_number-1) * $item_per_page);
//echo $page_position;
foreach($subject_name as $values ) {
$values=trim($values);
}
//Limit our results within a specified range.
$sel = $st->prepare("SELECT quesname, answer1,answer2,answer3,answer4,correctanswer FROM question where category_name ='$category' && subject_name ='$X' ORDER BY id ASC LIMIT $page_position, $item_per_page");
$sel->bind_result($quesname,$answer1,$answer2,$answer3,$answer4,$correctanswer); //bind variables to prepared statement
$sel->execute(); //Execute prepared Query
//$qrey=mysqli_query($st,$sel);
//print_r($sel);
$x=1;
while($rowin=mysqli_fetch_assoc($sel)){
//Display records fetched from database.
echo '<ul class="contents">';
while($sel->fetch()){ //fetch values
?>
<div class='cont' id="<?php echo $x; ?>">
<?php
echo "<div id='question_splitter_$x'>";?>
<div id='subject_name<?php echo $x;?>' >
<p class='questions' id="qname<?php echo $x;?>"> <?php echo $x?>.<?php echo $rowin['quesname'];?></p>
<input type="radio" value="1" id='radio1_<?php echo $rowin['id'];?>' name='<?php echo $rowin['id'];?>'/><?php echo $rowin['answer1'];?>
<br/>
<input type="radio" value="2" id='radio1_<?php echo $rowin['id'];?>' name='<?php echo $rowin['id'];?>'/><?php echo $rowin['answer2'];?>
<br/>
<input type="radio" value="3" id='radio1_<?php echo $rowin['id'];?>' name='<?php echo $rowin['id'];?>'/><?php echo $rowin['answer3'];?>
<br/>
<input type="radio" value="4" id='radio1_<?php echo $rowin['id'];?>' name='<?php echo $rowin['id'];?>'/><?php echo $rowin['answer4'];?>
<br/>
<input type="radio" checked='checked' style='display:none' value="smart_quiz" id='radio1_<?php echo $rowin['id'];?>' name='<?php echo $rowin['id'];?>'/>
<br/>
</div>
</div>
<?php
echo '<li>';
echo $quesname. '. <strong>' .$answer1.'</strong> — '. '. <strong>' .$answer2.'</strong> — '. '. <strong>' .$answer3.'</strong> — '. '. <strong>' .$answer4.'</strong> — '.$correctanswer;
echo '</li>';
}
echo '</ul>';
echo '<div align="center">';
/* We call the pagination function here to generate Pagination link for us.
As you can see I have passed several parameters to the function. */
echo paginate_function($item_per_page, $page_number, $get_total_rows[0], $total_pages);
echo '</div>';
exit;
}
}
################ pagination function #########################################
function paginate_function($item_per_page, $current_page, $total_records, $total_pages)
{
$pagination = '';
if($total_pages > 0 && $total_pages != 1 && $current_page <= $total_pages){ //verify total pages and current page number
$pagination .= '<ul class="pagination">';
$next = $current_page + 1; //next link
$right_links = $current_page + 3;
$previous = $current_page - 3; //previous link
$first_link = true; //boolean var to decide our first link
if($current_page > 1){
$previous_link = ($previous==0)? 1: $previous;
$pagination .= '<li class="first">«</li>'; //first link
$pagination .= '<li><</li>'; //previous link
for($i = ($current_page-2); $i < $current_page; $i++){ //Create left-hand side links
if($i > 0){
$pagination .= '<li>'.$i.'</li>';
}
}
$first_link = false; //set first link to false
}
if($first_link){ //if current active page is first link
$pagination .= '<li class="first active">'.$current_page.'</li>';
}elseif($current_page == $total_pages){ //if it's the last active link
$pagination .= '<li class="last active">'.$current_page.'</li>';
}else{ //regular current link
$pagination .= '<li class="active">'.$current_page.'</li>';
}
for($i = $current_page+1; $i < $right_links ; $i++){ //create right-hand side links
if($i<=$total_pages){
$pagination .= '<li>'.$i.'</li>';
}
}
if($current_page < $total_pages){
$next_link = ($i > $total_pages) ? $total_pages : $i;
$pagination .= '<li>></li>'; //next link
$pagination .= '<li class="last">»</li>'; //last link
}
$pagination .= '</ul>';
}
return $pagination; //return pagination links
}
?>
before using pagination i was using this code to call all the question
$sel="select * from question where category_name='$category' && subject_name='$X'";
$qrey=mysqli_query($st,$sel);
print_r($sel);
$x=1;
This is the query for getting the category and the subject from the page.

Remove separator of last menu

I want to remove last separator after Login Menu.
Here is my code and output.
<?php
if (!empty($topmenu) && !empty($menulist)) {
foreach ($topmenu as $mainparent) {
$arry = getmenuvalue($mainparent->id, $menulist, MAINURL);
if (isset($mainparent->children) && !empty($mainparent->children)) {
echo '<li class="dropdown"> ' . $arry['name'] . '<span class="caret"> </span>';
echo '</li>';
echo '<li> | </li>';
} else {
echo '<li>' . $arry['name'] . '</li>';
echo '<li> | </li>';
}
}
}
?>
Result of this code is
Home | Register | Login |
I want to Remove last Separator after Login menu.
I want result like this.
Home | Register | Login
Try This
<?php
if (!empty($topmenu) && !empty($menulist)) {
$count = count($topmenu);
$i = 1;
foreach ($topmenu as $mainparent) {
$arry = getmenuvalue($mainparent->id, $menulist, MAINURL);
if (isset($mainparent->children) && !empty($mainparent->children)) {
echo '<li class="dropdown"> ' . $arry['name'] . '<span class="caret"> </span>';
echo '</li>';
if($count != $i)
echo '<li> | </li>';
} else {
echo '<li>' . $arry['name'] . '</li>';
if($count != $i)
echo '<li> | </li>';
}
$i++;
}
}
?>
You can follow this example:
<?
$array = array('One','Two','Three'); // your array
$count = count($array); // check the array count
$i = 1; // use incremental
foreach ($array as $value) {
$separator = ($i == $count ? '' : '|'); // compare if last index use empty else separator
echo $value. $separator; // print separator with value
$i++; // +1 in every iteration.
}
?>
Result:
One|Two|Three
UPDATE 1:
Example with your code
<?
if (!empty($topmenu) && !empty($menulist)) {
$count = count($topmenu);
$i = 1;
foreach ($topmenu as $mainparent) {
$arry = getmenuvalue($mainparent->id, $menulist, MAINURL);
if (isset($mainparent->children) && !empty($mainparent->children)) {
echo '<li class="dropdown"> ' . $arry['name'] . '<span class="caret"> </span>';
echo '</li>';
} else {
echo '<li>' . $arry['name'] . '</li>';
//echo '<li> | </li>';
}
if($i == $count ? '' : '<li> | </li>');
$i++;
}
}
?>
Side Note:
I am not sure about the $topmenu check count($topmenu); if you get the count it will work. Try it.
You can collect your list items and concat them using implode method:
<?php
if (!empty($topmenu) && !empty($menulist)) {
$listItems = array();
foreach ($topmenu as $mainparent) {
$arry = getmenuvalue($mainparent->id, $menulist, MAINURL);
if (isset($mainparent->children) && !empty($mainparent->children)) {
$listItems[] = '<li class="dropdown"> ' . $arry['name'] . '<span class="caret"> </span></li>';
} else {
$listItems[] = '<li>' . $arry['name'] . '</li>';
}
}
echo implode('<li> | </li>', $listItems);
}
?>
You can do something like this
<?php
if (!empty($topmenu) && !empty($menulist)) {
foreach ($topmenu as $key => $mainparent) {
$arry = getmenuvalue($mainparent->id, $menulist, MAINURL);
if (isset($mainparent->children) && !empty($mainparent->children)) {
echo '<li class="dropdown"> ' . $arry['name'] . '<span class="caret"> </span>';
echo '</li>';
//echo '<li> | </li>';
} else {
echo '<li>' . $arry['name'] . '</li>';
//echo '<li> | </li>';
}
end($topmenu);
if ($key !== key($topmenu)) {
echo '<li> | </li>';
}
}
}
?>

PHP - Code only giving one value from Mysql database, while it should give more

I have a piece of code which executes a calendar. On this calendar certain names have to appear. However, on each date appears only one name, while in the database some dates hold up to 20 names.
This image should hopefully clarify things: http://imgur.com/a31pj0s
In this image you can see each date, which on some of them one name. As stated before, most of these contain many more names.
Now, I understand why this does not work, as it executes only once, but how can I get it to actually work? Please help me out :)
Here is also a link to the same piece of code, except easier to read: http://codepad.org/tVSzHDTx
<?php
$j = 2;
$i = 1;
//query the database
$query = mysql_query("SELECT * FROM months");
//fetch the results / convert results into an array
echo '<div class="accordion" id="calendar">';
while($rows = mysql_fetch_array($query)) {
$month = $rows['month'];
$id = $rows['id'];
echo '
<div class="accordion-group">
<div class="accordion-heading" id="month_'.$id.'">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#calendar" href="#monthsid'.$id.'">
'.$month.'
</a>
</div>
<div id="monthsid'.$id.'" class="accordion-body collapse">
<div class="accordion-inner">';
$n = $rows['num_of_days'];
echo '<ul class="list-group" id="'.$id.'" style="margin-bottom: 0;">';
for ($i = 1; $i <= $n; $i++) {
if ($j == 7) {
echo '<li class="list-group-item" id="'.$i.'" style="margin-bottom: 5px;">';
} else {
echo '<li class="list-group-item" id="'.$i.'">';
}
echo '<a data-toggle="modal" href="#myModal" class="add_member" id="'.$month.' '.$i.'">+</a>
<span class="badge">'.$i.'</span>';
if ($i == date("j")) {
echo '<div class="day_segment current_day">';
} else {
echo '<div class="day_segment">';
}
if ($j == 1) {
echo '<i style="color: #aaa;">'.$i.' |</i> Monday';
$j++;
} else if ($j == 2) {
echo '<i style="color: #aaa;">'.$i.' |</i> Tuesday';
$j++;
} else if ($j == 3) {
echo '<i style="color: #aaa;">'.$i.' |</i> Wednesday';
$j++;
} else if ($j == 4) {
echo '<i style="color: #aaa;">'.$i.' |</i> Thursday';
$j++;
} else if ($j == 5) {
echo '<i style="color: #aaa;">'.$i.' |</i> Friday';
$j++;
} else if ($j == 6) {
echo '<i style="color: #aaa;">'.$i.' |</i> Saturday';
$j++;
} else if ($j == 7) {
echo '<i style="color: #aaa;">'.$i.' |</i> Sunday';
$j = 1;
}
echo '</div>';
$posts_query = mysql_query("SELECT * FROM posts WHERE day=$i ");
while ($rows_items = mysql_fetch_array($posts_query)) {
$entry_player = $rows_items['author'];
$entry_comment = $rows_items['comment'];
$entry_day = $rows_items['day'];
$entry_month = $rows_items['month'];
}
for ($k = 1; $k <= 1; $k++) { /* I tried using another for loop, did not work */
if ($id == $entry_month && $i == $entry_day) {
echo '<span class="label label-success" data-toggle="tooltip" rel="tooltip" title="'.$entry_comment.'">'.$entry_player.'</span>';
} else {
echo '<span class="label"></span>';
}
}
echo '</li>';
}
echo '
</ul>
</div>
</div>
</div>
';
} /* End While Loop */
echo '</div></div>';
?>
As mentioned in the comments, you should not use the mysql_ functions as they are deprecated and not secure. Use MySQLi or PDO instead.
To answer your question, the code block below is the reason why it only displays one entry per day. In the while loop, you overwrite all the four variables each time, thus only the last one will be displayed in your for loop. The for loop is not necessary.
while ($rows_items = mysql_fetch_array($posts_query)) {
$entry_player = $rows_items['author'];
$entry_comment = $rows_items['comment'];
$entry_day = $rows_items['day'];
$entry_month = $rows_items['month'];
}
for ($k = 1; $k <= 1; $k++) { /* I tried using another for loop, did not work */
if ($id == $entry_month && $i == $entry_day) {
echo '<span class="label label-success" data-toggle="tooltip" rel="tooltip" title="'.$entry_comment.'">'.$entry_player.'</span>';
} else {
echo '<span class="label"></span>';
}
}
Try changing your code to something like this:
while ($rows_items = mysql_fetch_array($posts_query)) {
$entry_player = $rows_items['author'];
$entry_comment = $rows_items['comment'];
$entry_day = $rows_items['day'];
$entry_month = $rows_items['month'];
if ($id == $entry_month && $i == $entry_day) {
echo '<span class="label label-success" data-toggle="tooltip" rel="tooltip" title="'.$entry_comment.'">'.$entry_player.'</span>';
} else {
echo '<span class="label"></span>';
}
}

Categories