Displaying HTML based on the selection - php

I'm doing this PHP tutorial on OOP and I'm trying to incorporate the procedural code from the essential training into it, to see if I actually learned anything beyond the basics, and I got stuck, big time.
So, I have these two functions inside the class I named Blog:
public function navigation($selected_subject, $selected_page) {
$output = "<ul class=\"subjects\" id=\"accordion\">";
$subject_set = $this->get_all_subjects($public);
while ($subject = mysqli_fetch_array($subject_set)) {
$output .= "<li";
if ($subject["id"] == $selected_subject['id']) { $output .= " class=\"blogselected\""; }
$output .= ">{$subject["menu_name"]}</li>";
$page_set = $this->get_pages_for_subject($subject["id"], $public);
$output .= "<ul class=\"pages\">";
while ($page = mysqli_fetch_array($page_set)) {
$output .= "<li";
if ($page['id'] == $selected_page['id'] ) { $output .= " class=\"blogselected\""; }
$output .= "><a href=\"../public/blog_index.php?page=" . urlencode($page["id"]) .
"\">{$page["menu_name"]}</a></li>";
}
$output .= "</ul>";
}
$output .= "</ul>";
return $output;
}
public function blog_page($selected_page){
if ($selected_page) {
echo "<h2>" . $selected_page['menu_name'] . "</h2>";
echo "<div class=\"page-content\">";
echo $selected_page['content'];
echo "</div>";
} else {
echo "<h2>Welcome to Widget Corp</h2>";
}
}
and this piece of code on the index page:
<?php
$blog = new Blog;
global $selected_page;
global $selected_subject;
?>
<div class="blog">
<div class="blogpost">
<?php echo $blog->blog_page($selected_page); ?>
</div>
<div class="blognav">
<?php echo $blog->navigation($selected_subject, $selected_page); ?>
</div>
</div>
The code partially works. The navigation with subjects and sub-pages gets displayed fine, when the links are clicked the id "?page=1" is passed to the url, but the problem is that the code is not adding a class "blogselected" to the active links, and it also does not display the content when the links are clicked. The only thing that gets displayed in the content area is the h2 "Welcome to Widget Corp" from else condition in the second function, so I believe the variables $selected_subject and $selected_page might be causing the problem, but I just can't figure it out on my own. I spent countless hours trying to solve this and I'm on the brink of giving up completely. What exactly am I missing here?

Related

Mega dropdown menu css for mysql database data

I want to make mega dropdown menu that comes from mysql database. Below is my php code. The code is working well. But the problem is I am unable to make mega dropdown menu for the code below.
I need the mega menu like the example here : https://bootsnipp.com/snippets/featured/bootstrap-mega-menu
My problem is how will I make more div with the below php code .
Plz help me the css for the mega drop down menu as shown above.
<?php
$sql = "SELECT id, product, parent_id, category_link FROM category ORDER BY parent_id, id";
$results = mysqli_query($conn,$sql) or die(mysqli_error()) ;
if($results)
{
while($result = mysqli_fetch_array($results))
{
$category['categories'][$result['id']] = $result;
$category['parent_cats'][$result['parent_id']][] = $result['id'];
}
}
function getCategories($parent, $category)
{
$html = "";
if (isset($category['parent_cats'][$parent]))
{
$html .= "<div id='wrapper'>";
$html .= "<ul class='mega-menu'>\n";
foreach ($category['parent_cats'][$parent] as $cat_id)
{
if (!isset($category['parent_cats'][$cat_id]))
{
$html .= "<li class='mega-menu-drop'>\n <a class='mega-menu-content' href='" . $category['categories'][$cat_id]['category_link'] . "'>" . $category['categories'][$cat_id]['product'] . "</a>\n</li> \n";
}
if (isset($category['parent_cats'][$cat_id]))
{
$html .= "<li class='mega-menu-drop'>\n <a class='mega-menu-content' href='" . $category['categories'][$cat_id]['category_link'] . "'>" . $category['categories'][$cat_id]['product'] . "</a> \n";
$html .= getCategories($cat_id, $category);
$html .= "</li> \n";
}
}
$html .= "</ul> \n";
$html .= "</div>";
}
return $html;
}
?>
<?php echo $data['category'] = getCategories(0, $category);?>
as of your comment. i guess you need some CSS.
this will work on mouse hover. but not on click like in your example:
.mega-menu-drop {
display:none
}
.mega-menu:hover .mega-menu-drop {
display:block
}

WordPress breadcrumbs in search results

In WordPress i'm currently using Yoast's SEO Plugin to display breadcrumbs for my pages and posts, which is working fine when visiting a specific page.
Here is the function i'm using to display the breadcrumbs inside of my WordPress templates:
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
For example when browsing to Team Members which is a child of About Us I get:
Home > About Us > Team Members
However i'd like to be able to display the same breadcrumbs (for the individual pages and posts) inside the search results loop.
So far what displays when searching for Members is:
Your Search Results:
Team Members
Home > Search > Members
Members Area
Home > Search > Members
But I don't want breadcrumbs for the Search Results page, I want them for the individual pages and posts that are displayed as a result of searching for a keyword.
For example Imagine I searched again for Members I would like displayed the below:
Your Search Results:
Team Members
Home > About Us > Team Members
Members Area
Home > Members Area
I'm not fussed if this is or isn't integrated with the SEO plugin, however thus far it's the best solution I found to display breadcrumbs in WordPress!
Also incase abody requires it, here is my search.php file: http://pastebin.com/0qjb2954
Try this. That's my own working snippet that shows breadcrumbs inside search loop.
/*Begin Loop */
<?php
echo '<div class="b-search_result_list__item_breadcrumbs breadcrumbs">';
$current_type = get_post_type();
if ($current_type == 'page') {
$parents = get_post_ancestors(get_the_ID());
if($parents){
for($i=count($parents)-1;$i>=0;$i--){
echo '<span typeof="v:Breadcrumb">';
echo '<a rel="v:url" property="v:title" title="'.get_the_title($parents[$i]).'" href="'.get_permalink($parents[$i]).'">'.get_the_title($parents[$i]).'</a>';
echo '</span>';
}
}else{
echo '<span typeof="v:Breadcrumb">';
echo '<a rel="v:url" property="v:title" title="'.get_bloginfo('name').'" href="'.get_bloginfo('url').'">'.get_bloginfo('name').'</a>';
echo '</span>';
}
echo '<span typeof="v:Breadcrumb">';
echo '<span property="v:title">'.get_the_title().'</span>';
echo '</span>';
}else{
$current_obj = get_post_type_object($current_type);
echo '<span typeof="v:Breadcrumb">';
echo '<a rel="v:url" property="v:title" title="'.get_bloginfo('name').'" href="'.get_bloginfo('url').'">'.get_bloginfo('name').'</a>';
echo '</span>';
echo '<span typeof="v:Breadcrumb">';
echo '<a rel="v:url" property="v:title" title="'.$current_obj->labels->name.'" href="'.get_post_type_archive_link( $current_type ).'">'.$current_obj->labels->name.'</a>';
echo '</span>';
$current_taxonomies = get_object_taxonomies($current_type);
if($current_taxonomies){
$current_terms = get_the_terms(get_the_ID(), $current_taxonomies[0]);
if($current_terms){
$current_term = array_shift($current_terms);
echo '<span typeof="v:Breadcrumb">';
echo '<a rel="v:url" property="v:title" title="'.$current_term->name.'" href="'.get_term_link($current_term).'">'.$current_term->name.'</a>';
echo '</span>';
/*
var_dump($current_obj->labels->name); // Archive name
var_dump(get_post_type_archive_link( $current_type )); // Archive link
var_dump($current_term->name); // Term name
var_dump(get_term_link($current_term)); // Term link
var_dump(get_permalink()); // Post link
*/
}
}
echo '<span typeof="v:Breadcrumb">';
echo '<span property="v:title">'.get_the_title().'</span>';
echo '</span>';
}
echo '</div>';
?>
/*End Loop*/
try adding this line of code above the yoast breadcrumb function in your search.php file:
WPSEO_Breadcrumbs::$instance = NULL;
This would be line 22 I believe, and also make sure to use the Yoast breadcrumb function from your question, not the new breadcrumb() function that's there now.
Please let me know if this works!
Full explanation:
The Yoast plugin breadcrumbs functionality is build on the page load, based on the current page as the child. To make it load the right child and parents, you'd need to reset it before you run the function. There is no built-in reset function, however setting the static $instance to NULL should cause the plugin to re-generate its data based on the current global post object which is set while you're looping.
Building upon Yavor's answer I found a way. Been banging my head about it for hours. You can place the backup and restore otuside of the loop though. Here it is:
global $wp_query;
//backup
$old_singular_value = $wp_query->is_singular;
//change
$wp_query->is_singular = true;
//reset
WPSEO_Breadcrumbs::$instance = NULL;
//breadcrumbs
if (function_exists('yoast_breadcrumb')){
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
}
//restore
$wp_query->is_singular = $old_singular_value;
It fakes the query to make it singular so the newly-refreshed breadcrumbs thinks that this is not the search page but a single post or page or whatever you are displaying as your search results.
Using a plugin to generate breadcrumbs is not really necessary. Here's a simple PHP function you can add to your functions.php file:
function breadcrumbs() {
global $post;
echo "<ul id='breadcrumbs'>";
if (!is_home()) {
echo '<li>Home</li>';
if (is_category() || is_single()) {
echo "<li>" . the_category(' </li><li> ');
if (is_single()) {
echo "</li><li>" . the_title() . "</li>";
}
} elseif (is_page()) {
if($post->post_parent){
foreach ( get_post_ancestors( $post->ID ) as $ancestor ) {
echo '<li>' . get_the_title($ancestor) . '</li>' . get_the_title();
}
} else {
echo "<li>" . get_the_title() . "</li>";
}
}
} elseif (is_tag()) {
single_tag_title();
} elseif (is_day()) {
echo "<li>Archive for " . the_time('F jS, Y') . "</li>";
} elseif (is_month()) {
echo "<li>Archive for " . the_time('F, Y') . "</li>";
} elseif (is_year()) {
echo "<li>Archive for " . the_time('Y') . "</li>";
} elseif (is_author()) {
echo "<li>Author Archive</li>";
} elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {
echo "<li>Blog Archives</li>";
} elseif (is_search()) {
echo "<li>Search Results for" . the_search_query() . "</li>";
}
echo "</ul>";
}
along with some CSS to style it, customize as you desire
#breadcrumbs {
list-style:none;
margin:5px 0;
overflow:hidden;
}
#breadcrumbs li{
float:left;
}
#breadcrumbs li+li:before {
content: '| ';
padding:0 4px;
}
You can then implement those breadcrumbs on any page you like, including your searchpage.php file or whichever file you use to display search results with this call
<?php breadcrumbs(); ?>
The search pages have a conditional function that can be used. You could always apply that to loading the breadcrumbs. Here is an example:
if ( ! is_search() ) {
if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
}
}
It depends where you are loading the breadcrumbs as well, but this should typically work unless your theme is very unique.

How can I implement the target="_blank" attribute into a PHP link so links open up in a new tab?

I have a link that is basically an 'about' page that has social icons pointing to my personal social media outlets. In PHP, the code is like this:
function wp_about_author_get_socials() {
$socials = array();
$socials['twitter'] = array('title'=>'Twitter', 'link'=>'http://www.twitter.com
/%%username%%', 'icon'=> WPAUTHORURL_URL .'/images/twitter.png');
$socials['facebook'] = array('title'=>'Facebook', 'link'=>'http://www.facebook.com
/%%username%%', 'icon'=> WPAUTHORURL_URL .'/images/facebook.png');
I've fiddled around with things, but just can't seem to implement things so the link opens up in a new tab/page. Currently, the links take the user off the website (not ideal). Any guidance in this regard would be greatly appreciated!
What I have tried was changing the links to this:
$socials['twitter'] = array('title'=>'Twitter', 'link'=>'http://www.twitter.com
/%%username%%', 'target'=>'_blank', 'icon'=> WPAUTHORURL_URL .'/images/twitter.png');
But that didn't work.
This may be the part that prints the links, from what I can tell:
// About Author Social
$wp_about_author_social .= wp_about_author_get_social_links($wp_about_author_settings);
if(isset($wp_about_author_settings['wp_author_social_images']) &&
$wp_about_author_settings['wp_author_social_images']){
$wp_about_author_content .= "<p>" .$wp_about_author_links . "</p>";
if($wp_about_author_social != ""){
$wp_about_author_content .= '<p class="wpa-nomargin">'.apply_filters(
'wp_about_author_follow_me', "Follow Me:").'<br />' . $wp_about_author_social.'</p>';
}
} else {
$wp_about_author_content .= "<p class='wpa-nomargin'>";
$wp_about_author_content .= $wp_about_author_links;
if($wp_about_author_social != ""){
$wp_about_author_content .= apply_filters( 'wp_about_author_separator', " - ") .
$wp_about_author_social;
}
$wp_about_author_content .= "</p>";
}
// Generate social icons
function wp_about_author_get_social_links($wp_about_author_settings){
$content="";
$socials = wp_about_author_get_socials();
foreach($socials as $social_key=>$social){
if (get_the_author_meta($social_key)){
if(isset($wp_about_author_settings['wp_author_social_images']) &&
$wp_about_author_settings['wp_author_social_images']){
$content .= "<a class='wpa-social-icons' href='".str_replace('%%username%%',
get_the_author_meta($social_key), $social['link'])."'><img src='". $social['icon']."'
alt='".$social['title']."'/></a>";
} else {
if($content != "")
$content .= apply_filters( 'wp_about_author_separator', " - ");
$content .= "<a href='".str_replace('%%username%%', get_the_author_meta($social_key),
$social['link'])."'>".$social['title']."</a>";
}
}
}
return $content;
}
Within the code you posted, you can add target="_blank" to the links on lines 28 and 34.
Line 28 becomes:
$content .= "<a class='wpa-social-icons' target='_blank' href='".str_replace('%%username%%',
Line 34 becomes:
$content .= "<a target='_blank' href='".str_replace('%%username%%', get_the_author_meta($social_key),

Creating DOM from echoed form values

I have form, which builds a rather large output setup in various elements, based on how the form is filled out. I can post the whole code, but i think this makes it easier to understand, what I am trying to do.
Let's say I have form, with one input text field called 'notes'.
In this example, we pretend i have entered "Hello World!" in 'notes'.
When the form is posted, the value entered into 'notes' is echoed within some elements:
<?php
echo '<div class="bon" id="bon">';
if (empty($_POST['notes'])) {
// Echo nothing
} else {
echo '<div class="bonHr"></div>';
echo '<div class="bonField">';
echo '<span>' . nl2br($_POST['notes']) . '</span>';
echo '</div>';
}
echo '</div>';
?>
And that is fine. Now I want to store the value in mySQL - I know how to do that.
Problem is I want to store the value including the elements around it, into a mysql textfield.
So what I really want to store is everything the contains, in this case:
<div class="bon" id="bon">
<div class="bonHr"></div>
<div class="bonField">
<span>Hello World!</span>
</div>
</div>
How can I do this??
The reason why I need to do this, is that the output varies alot. So please help me to figure out how I can take a "dump" of the output, and throw it into mySQL...
Just store your output in variable show it and save in database.
<?php
$output = "";
$output .= '<div class="bon" id="bon">';
if (empty($_POST['notes'])) {
// Echo nothing
} else {
$output .= '<div class="bonHr"></div>';
$output .= '<div class="bonField">';
$output .= '<span>' . nl2br($_POST['notes']) . '</span>';
$output .= '</div>';
}
$output .= '</div>';
echo $output;
//Save output to database
?>

(EDITED QUES) PHP - Call function within another function

I have referred to similar questions like this and have done exactly the same way as it should have been but no success. Hence I would appreciate if some one can help me with this
I have a file called view.php which calls a function from a class based on a switch case condition. This function displays the output on the page with few links. When clicked on the link it calls another function which sits in my first function. But obviously when I click my link, nothing happens. That's my code.
view.php
require_once(..'/functions.php');
$functions = new myfile_functions();
<form method="post" action="view.php"><div>
<p><select name="potentialareas" id="potentialareas">
<option style="font-weight:bold;" value="-1">Select an area to view </option>
<?php
foreach($accessareas as $accessarea){
echo "<option value='".$accessarea->id."'>".$accessarea->name. " - " . $accessarea->reporttype. "</option>";
}
?>
</select>
</p>
<p><input name="view" id="view" type="submit" value="View" title="view" /><br /></p>
</div></form>
<div style="border-top:1px dotted #ccc;"></div>
<?php
if(isset($_POST['view']))
{
$hierarchyid = $_POST['potentialareas'];
$reporttype = $DB->get_record_sql("some query");
switch($reporttype->reporttype)
{
case "D Report":
$functions->getDepartmentReport($hierarchyid);
break;
case "S Report":
$functions->getSectionReport($hierarchyid);
break;
case "A Report":
$functions->getAreaReport($hierarchyid);
break;
}
}
functions.php
class myfile_functions(){
function getDepartmentReport($departmentid)
{
global $USER, $CFG, $DB;
$dname = $DB->get_record_sql("some query");
$output = "<div id='actualreport'><p><b>" . $dname->name. " Department Report</b></p>";
$output .= "<p>Activities started: </p>";
$output .= "<p>Activities Completed: </p></div>";
$output .= "<div id='separator' style='border-top:1px dotted #ccc;'></div>";
$output .= "<div id='listofsections'><p><b><i>Select the following for a more detailed report.</i></b></p>";
$snames = $DB->get_records_sql('some query');
foreach($snames as $sname)
{$output .= "<p>" .$sname->name. " <a href='view.php?section=" .$sname->id. "' name='section'><i>view report</i></a></p>";
}
$output .= "</div>";
if(isset($_GET['section']))
{
$this->getSectionReport($_GET['section']);
}
echo $output;
}
function getSectionReport($sectionid)
{
global $USER, $CFG, $DB;
$sname = $DB->get_record_sql("some query");
$output = "<div id='actualreport'><p><b>" . $sname->name. " Report</b></p>";
$output .= "<p>Num Users: </p>";
$output .= "<p>Activities Completed: </p></div>";
$output .= "<div id='separator' style='border-top:1px dotted #ccc;'></div>";
$output .= "<div id='listofareas'><p><b><i>Select the following for a more detailed report.</i></b></p>";
$anames = $DB->get_records_sql('some query');
foreach($anames as $aname)
{$output .= "<p>" .$aname->name. " <a href='view.php?area=" .$aname->id. "' name='view' id='view'><i>view report</i></a></p>";
}
$output .= "</div>";
if(isset($_GET['area']))
{
$areaid = $_GET['area'];
$this->getAreaReport($areaid);
}
echo $output;
}
Similarly another function calling the above function, n so on.
function getAreaReport($id)
{ .. same content but calling another function...}
So when ever I click my view report link, I get the id appended id in my querystring, something like
http://mydomain.com/view.php?section=5
Ideally the contents of getSectionReport() should get printed but its not. Please point out what is it that I am doing wrong.
Thanks in advance.
What's your main method for displaying everything? At the moment you have three functions, all of which link to eachother in various ways, but it doesn't look as if you're instantiating anything first. The PHP is being fed a parameter in your URL, sure, but if a class isn't instantiated and a method declared, how does it know what you want it to do?
For myfile.php, maybe you should do something like:
class MyFile
{
public function other_function()
{
// Various stuff
return 'stuff';
}
public function other_other_function()
{
// Various other stuff
return 'other stuff';
}
public function page_view($file_id)
{
$var = $this->other_function($file_id);
return $this->other_other_function($var);
}
}
$class = new MyFile;
echo $class->page_view($_GET['id']);
If the two functions are part of a class (as your comment above hints), then the call should be written as
$this->getUserReport($id);
If you're accessing a sibling function inside the same class, use:
class ClassName
{
public function sibling_function()
{
return 'Hey bro!';
}
public function my_function()
{
return $this->sibling_function();
}
}
If not, use the standard:
public function my_function()
{
return sibling_function();
}
If you're still having trouble, make sure your class is properly instantiated. There's no point calling another function if you're not even instantiating the class first:
$obj = new ClassName;
echo $obj->my_function();

Categories