div is not on the code - php

I'm generating html code with a string this way
foreach ($busquedas as $busqueda) {
$checked = $busqueda->porDefecto ? "checked" : ' ';
$radio_html = "<input type='radio' radio-id='".$busqueda->id."' name='default' class='radio-default' value='Por defecto' checked =".$checked." > Por defecto";
$html .= "<div class='col-md-12 search-div'>";
$html .= "<div class='col-md-12'>";
$html .= "<div class='col-md-6'>".$busqueda->nom."</div>";
$html .= "<div class='col-md-6'>".$_SESSION['user_rol'] == 0?$radio_html:''."</div>";//if $radio_html is shwon the paren div col-md-6 is not shown
$html .="</div>";
$html .= "<div class='col-md-12'>";
$html .= "<div class='col-md-6'><button class='btn btn-default load_search_btn' search_id='".$busqueda->id."'>Cargar</button></div>";
$html .= "<div class='col-md-6'><button class='btn btn-default delete_search_btn' search_id='".$busqueda->id."'>Eliminar</button></div>";
$html .="</div>";
$html .="</div>";
}
When $radio_tml is shown the parent div with class col-md-6 is not on the code but if $radio_html is shown the div is shown too,I thought some tag is not closed but I can't see it

For me your code implies that when you have
$_SESSION['user_rol'] == 0
the div section after that is not closed
You should add brackets as IncredibleHat was saying or add the </div> in the if statement (and not only in the else)

Related

How to concatenate increment operator to PHP

$i = 0;
while ($slider_query->have_posts()) {
$slider_query->the_post();
$html ='<div class= "col-lg-3 order-lg-'.$i.' col-md-6 p-0">';
$html .= '<div class= "ss-pic">';
$html .= '<img src="'.get_the_post_thumbnail_url(get_the_ID(), 'full').'"/>';
$html .='</div>';
$html .='</div>';
$html ='<div class= "col-lg-3 order-lg-"'.++$i.'"col-md-6 p-0">';
$html .= '<div class= "ss-text">';
$html .= '<h3>'.get_the_title().'</h3>';
$html .= get_the_content();
$html .= 'Explore';
$html .='</div>';
$html .='</div>';
I want to pre-increment the $i. Can anyone please help me out? I know its a basic, but I have a bit of confusion over the concatenation
So firstly you can pre-increment an integer variable by prepending ++ to it, e.g.
++$i
. That will work for you, e.g.
'<div class= "col-lg-3 order-lg-'.++$i.' col-md-6 p-0">'
However, you also have another problem which is preventing you from seeing the results as you are expecting. Every time you write $html = - which is twice within your loop, you overwrite the contents of $html. The previous contents of the variable are destroyed. Therefore, at whatever time you come to echo $html to the output, you'll only ever see the last version.
You need to concatenate all the HTML together into a single string without overwriting it.
Put
$html = "";
just before the loop starts, and then change
$html ='<div class= "col-lg-3 order-lg-'.$i.' col-md-6 p-0">';
to
$html .='<div class= "col-lg-3 order-lg-'.++$i.' col-md-6 p-0">';
(note the .= there, and also the ++$i which you forgot)
and lower down, change
$html ='<div class= "col-lg-3 order-lg-"'.++$i.'"col-md-6 p-0">';
to
$html .='<div class= "col-lg-3 order-lg-"'.++$i.'"col-md-6 p-0">';
(again just changing the = to .=.)
Demo: http://sandbox.onlinephpfunctions.com/code/7e1ac7b039867eedd22d90dfcdc03e8990419a8f
$i = 0;
while ($slider_query->have_posts()) {
$slider_query->the_post();
$html ='<div class= "col-lg-3 order-lg-'.$i+1.' col-md-6 p-0">';
$html .= '<div class= "ss-pic">';
$html .= '<img src="'.get_the_post_thumbnail_url(get_the_ID(), 'full').'"/>';
$html .='</div>';
$html .='</div>';
$html ='<div class= "col-lg-3 order-lg-"'.$i+2.'"col-md-6 p-0">';
$html .= '<div class= "ss-text">';
$html .= '<h3>'.get_the_title().'</h3>';
$html .= get_the_content();
$html .= 'Explore';
$html .='</div>';
$html .='</div>';
I dont think you can increase a value in one loop. you need to add it with numeric value since you want a different value for each div in one loop

How to insert HTML using foreach() without echo

I have this working code in my view:
<?php
$task_num = 0;
foreach ($curent_day->getTasksList() as $task){
echo '<div class="task">';
echo '<span class="task_id">'.($task_num+1).'.'.'</span>';
echo '<div class="task_time">';
echo '<span class="task_time_start">'.$task->getStartTime().'</span>';
echo '<span class="task_time_finish">'.$task->getFinishTime().'</span>';
echo '</div>';
echo ''.$task->name.'';
echo 'Start';
echo 'Finish';
echo '<div class="status_round '.$task->status.'"></div>';
echo '</div>';
$task_num++;
}
?>
Is there any way to get rid of 'echo'?
P.S. and is the way to insert HTML with HTML helper more correct even if it takes more space?
You can drop echo completely and use native HTML syntax:
<?php $task_num = 0 ?>
<?php foreach ($curent_day->getTasksList() as $task): ?>
<div class='task'>
<span class='task_id'><?= ++$task_num ?></span>
<div class='task_time'>
<span class='task_time_start'><?= $task->getStartTime() ?></span>
<span class='task_time_finish'><?= $task->getFinishTime() ?></span>
</div>
<a href='/' class='task_name'><?= $task->name ?></a>
<a href='/' class='btn task_start btn_disabled'>Start</a>
<a href='/' class='btn task_finish btn_disabled'>Finish</a>
<div class='status_round <?= $task->status ?>'></div>
</div>
<?php endforeach ?>
This will give you better syntax highlighting and autoformatting support in your IDE/editor.
There are several different ways that you can concatenate the string by using single quotes and double quotes, or by using a variable and the ".=" operator to append text onto the end of a string. Just google php strings & concatenation and it will have more than you need to figure this out.
But using you example here is a method:
$task_num = 0;
foreach ($curent_day->getTasksList() as $task){
echo
'<div class="task">' .
'<span class="task_id">' . ($task_num+1) . '.'.'</span>' .
'<div class="task_time">' .
'<span class="task_time_start">' . $task->getStartTime() . '</span>' .
'<span class="task_time_finish">' . $task->getFinishTime() . '</span>' .
'</div>' .
''.$task->name.'' .
'Start' .
'Finish' .
'<div class="status_round '.$task->status.'"></div>' .
'</div>';
$task_num++;
}
You have to echo to output you data to the browser.
You need not use string concatenation or multiple echo statements.
Alternative
$task_num = 0;
foreach ($curent_day->getTasksList() as $task){
$task_num++;
echo
"<div class='task'>
<span class='task_id'>{$task_num}</span>
<div class='task_time'>
<span class='task_time_start'>{$task->getStartTime()}</span>
<span class='task_time_finish'>{$task->getFinishTime()}</span>
</div>
<a href='/' class='task_name'>{$task->name}</a>
<a href='/' class='btn task_start btn_disabled'>Start</a>
<a href='/' class='btn task_finish btn_disabled'>Finish</a>
<div class='status_round {$task->status}'></div>
</div>";
}
You can concatenate your html in one variable and then you can use it with one time echo statement
<?php
$task_num = 0;
$html = '';
foreach ($curent_day->getTasksList() as $task){
$html .= '<div class="task">';
$html .= '<span class="task_id">'.($task_num+1).'.'.'</span>';
$html .= '<div class="task_time">';
$html .= '<span class="task_time_start">'.$task->getStartTime().'</span>';
$html .= '<span class="task_time_finish">'.$task->getFinishTime().'</span>';
$html .= '</div>';
$html .= ''.$task->name.'';
$html .= 'Start';
$html .= 'Finish';
$html .= '<div class="status_round '.$task->status.'"></div>';
$html .= '</div>';
$task_num++;
}?>

Adding MailChimp Shortcode in Theme with PHP

I have the following website: https://www.ajagrawal.com
Currently in my website, I am trying to edit the hero section and replace the name, email and button fields with a custom wordpress form that was made with the plugin. Right now the emails are stored in Wordpress, but I want them in Mailchimp.
I know which code in my theme that form pertains to which is the following:
if(function_exists('newsletter_form')):
$output .= '<div class="row">';
$output .= '<div class="col-sm-6 col-sm-offset-6 col-md-4 col-md-offset-6">';
$output .= '<form method="post" action="'.home_url('/').'?na=s" onsubmit="return newsletter_check(this)">';
$output .= '<div class="c-input-3-wrapper">';
$output .= '<input class="c-input type-3" type="text" name="nn" required="" placeholder="'.esc_html($name_placehodler).'">';
$output .= '<div class="c-input-3-icon"><span class="lnr lnr-user"></span></div>';
$output .= '</div>';
$output .= '<div class="c-input-3-wrapper">';
$output .= '<input class="c-input type-3" type="email" name="ne" required="" placeholder="'.esc_html($email_placeholder).'">';
$output .= '<div class="c-input-3-icon"><span class="lnr lnr-envelope"></span></div>';
$output .= '</div>';
$output .= '<input class="newsletter-submit c-btn type-1 size-4 full" type="submit" value="'.esc_html($btn_text).'">';
$output .= '</form>';
$output .= '</div>';
$output .= '</div>';
endif;
Here is my mailchimp shortcode that represents the form.
[mc4wp_form id="2198"]
I've tried editing the code and inserting it multiple ways and even ended up crashing the site and still no luck. Considering my goal, is this the right approach (putting the shortcode in the PHP code)?
If so, how can this be done?
You can use the do_shortcode function.
Examples in the documentation:
// Use shortcode in a PHP file (outside the post editor).
echo do_shortcode( '' );
// In case there is opening and closing shortcode.
echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
// Enable the use of shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );
// Use shortcodes in form like Landing Page Template.
echo do_shortcode( '[contact-form-7 id="91" title="quote"]' );
// Store the short code in a variable.
$var = do_shortcode( '' );
echo $var;
So in your case something like:
echo do_shortcode( '[mc4wp_form id="2198"]' );
should do the trick.
EDIT:
Something like this should work:
$output .= '<div class="row">';
$output .= '<div class="col-sm-6 col-sm-offset-6 col-md-4 col-md-offset-6">';
$output .= do_shortcode( '[mc4wp_form id="2198"]' );
$output .= '</div>';
$output .= '</div>';
Then do whatever you want with output - probably will have to echo it...

Toggle excerpt and content onclick - Wordpress - php string

I am altering an wordpress theme to display my blog posts and I've been trying to add a toggle between excerpt and content when clicking on 'Read More'.
I have been unsuccessful on many attempts.
Any help would be greatly appreciated.
$post_data = "<article class='post style-2$sticky_class'>";
$post_data.= "<div class='blog-item'>";
$post_data .= "<header class='col-md-4 col-xs-12 no-padding'>";
if(is_sticky(get_the_ID()) && strlen(anps_header_media(get_the_ID(), $image_class)) > 0 ) {
$post_data .= "<div class='absolute stickymark'><div class='triangle-topleft hovercolor'></div><i class='nav_background_color fa fa-thumb-tack'></i></div>";
}
$post_data .= "<a href='".get_permalink()."'>".anps_header_media(get_the_ID(), $image_class)."<div class='cat_banner'>".$cat_name."</div></a>";
$post_data .= "</header>";
$post_data.= "<div class='archive_blog_info col-md-8'>";
if (is_sticky(get_the_ID()) && strlen(anps_header_media(get_the_ID(), $image_class)) < 1 ) {
$post_data .= "<a href='".get_permalink()."' title='".get_the_title()."'><h1><i class='fa fa-thumb-tack hovercolor'></i> ".get_the_title()."</h1></a>";
$post_data .= "<h1>".get_the_date()."</h1>";
}
else {
$post_data .= "<a href='".get_permalink()."' title='".get_the_title()."'><h1>".get_the_title()."</h1></a>";
}
$post_data .= "<div class='post-excerpt'>".$excerpt_text."</div>";
$post_data .= "<div class='post-content'>".$content_text."</div>";
$post_data .= '<a class="btn btn-sm style-4 read" href="">'.__("Read more", 'accounting').'</a>';
$post_data .= '<a class="btn btn-sm style-4 read-less" href="">'.__("Read less", 'accounting').'</a>';
$post_data .= '<a class="news_link btn btn-sm style-4" style="float:right!important;" href="/category/'.$cat_name.'/">'.__("More ".$cat_name." articles >>", 'accounting').'</a>';
$post_data .= "</div>";
$post_data .='</div>';
$post_data .= "</article>";
echo $post_data;
You need to use javascript to handle the click events of read more or read less. I prefer jquery. I have prepared a fiddle.
[http://jsfiddle.net/at36xwa8/4/]

Issue on output's return value

Currently I am using a file with css and all.I have a problem as i am using php output between many html tags. The menu consist of the hard coded pages which is included with the php require_once. However, i also want to add in some pages from my database.
The Problem: The menu only shows 1 page name from my database when i have 2. It is displaying the 2nd page's name i have made. However clicking on it, it calls up the content from the 1st page.
The page name from my database is added using php output after the <ul id="menu-toc">
The page's content from my database is added at the bottom of the "The Page" section.
update: I've changed the placing of the while loop before the to after the
Pattern of css
<div class="bb-item" id="abscess">
<div class="content">
<div class="scroller">
Content
</div>
</div>
</div>
The Page
function find_condition() {
global $connection;
$query = "SELECT * ";
$query .= "FROM pages ";
$query .= "WHERE visible = 1 ";
$query .= "AND subject_id = 2 ";
$query .= "ORDER BY position ASC";
$page_set = mysqli_query($connection, $query);
confirm_query($page_set);
return $page_set;
}
$two = find_condition();
while($page = mysqli_fetch_assoc($two)) {
$pagearray = $page['menu_name'];
?>
<ul id="menu-toc" class="menu-toc">
<li class="menu-toc-current">
ABSCESS</li>
<li>APTHOUS ULCER</li>
<li>BAD BREATH</li>
<?php
$output = "<li>";
$output .= "<a href=\"";
$output .= $pagearray;
$output .= "\">";
$output .= $pagearray;
$output .= "</a></li>";
}
echo $output;
?>
</ul>
</div>
<div class="bb-custom-wrapper">
<div id="bb-bookblock" class="bb-bookblock">
<?php require_once("abscess.php"); ?>
<?php require_once("apthousulcer.php"); ?>
<?php require_once("bad breath.php"); ?>
<?php
$two= find_condition();
while($page = mysqli_fetch_assoc($two)) {
$pagearray = $page['menu_name'];
$content = $page['content'];
$output .= "<div class=\"bb-item\" id=\"";
$output .= $pagearray;
$output .= "\">";
$output .= "<div class=\"content\">";
$output .= "<div class=\"scroller\">";
$output .= "<h2>";
$output .= $pagearray;
$output .= "</h2>";
$output .= $content;
$output .= "</div>";
$output .= "</div>";
$output .= "</div>";
}
echo $output;
?>
You're starting a new <ul> each time through the while loop. You should just do that once, before the loop. You're also reinitializing $output each time through the loop, but not printing it in the loop. So you're just printing the last assignment of output after the loop is done.
$two = find_condition();
?>
<ul id="menu-toc" class="menu-toc">
<li class="menu-toc-current">
ABSCESS</li>
<li>APTHOUS ULCER</li>
<li>BAD BREATH</li>
<?php
while($page = mysqli_fetch_assoc($two)) {
$pagearray = $page['menu_name'];
?>
<?php
$output = "<li>";
$output .= "<a href=\"";
$output .= $pagearray;
$output .= "\">";
$output .= $pagearray;
$output .= "</a></li>";
echo $output;
}
?>

Categories