if condition when a div has content do this else do that - php

I have here a script
<div>
<?php
echo '<strong>Other information</strong><br />';
$myname = get_post_meta($post->ID, 'acidity_gl', true); if ( $myname ) { echo 'Acidity: ' . $myname . '<br />'; }
$myname = get_post_meta($post->ID, 'winePh', true); if ( $myname ) { echo 'Wine PH: ' . $myname . '<br />'; }
$myname = get_post_meta($post->ID, 'residual_sugar_gl', true); if ( $myname ) { echo 'Residual Sugar gl: ' . $myname . '<br />'; }
?>
</div>
I would like to add a condition when if $myname has a value then display the DIV but when there is no value on those 3 $myname then dont display DIV

Try this,
<?php
$myname=array();
$name = get_post_meta($post->ID, 'acidity_gl', true);
if ( !empty($name) ) {
$myname[] = 'Acidity: ' . $name ;
}
$name = get_post_meta($post->ID, 'winePh', true);
if ( !empty($name) ) {
$myname[] = 'Wine PH: ' . $name ;
}
$name = get_post_meta($post->ID, 'residual_sugar_gl', true);
if ( !empty($name) ) {
$myname[] = 'Residual Sugar gl: ' . $name;
}
if(!empty($myname))
{
echo '<div>';
echo '<strong>Other information</strong><br />';
echo implode('<br />',$myname);
echo '</div>';
}
?>

if(!empty ( $myname ))
{
// write your code for DIV here
}

Use this code:
<?php
if(!empty($my_name)){
?>
<div>DIV Content here</div>
<?php
}
?>
Or
<?php
if(!empty($my_name)){
echo '<div>DIV Content here</div>';
}
?>

Related

How to remove a specific data from an API?

I'm working on a site which gives metrics of site, it uses API of gtmetrix. So i want to remove a specific data from the coming result but I just dont know how to do it. Some help would be appreciated!
<?php
require_once('service.inc.php');
$test = new Services_WTF_Test("email", "api_key");
$url_to_test = "https://google.me/";
echo "Testing $url_to_test\n";
$testid = $test->test(array(
'url' => $url_to_test
));
if ($testid) {
echo "Test started with $testid\n";
}
else {
die("Test failed: " . $test->error() . "\n");
}
echo "Waiting for test to finish\n";
$test->get_results();
if ($test->error()) {
die($test->error());
}
$testid = $test->get_test_id();
echo "Test completed succesfully with ID $testid\n'<br>";
$results = $test->results();
if ($results): ?>
<?php
foreach($results as $result => $data): ?>
<strong><?php $ukey = strtoupper($result);
echo $ukey; ?>:</strong>
<?php echo $data; ?><br><br>
<?php endforeach;
endif;
?>
The Output Is:
FIRST_CONTENTFUL_PAINT_TIME: 1495
PAGE_ELEMENTS: 44
REPORT_URL: https://gtmetrix.com/reports/google.me/BFylJNX3
REDIRECT_DURATION: 0
FIRST_PAINT_TIME: 1495
DOM_CONTENT_LOADED_DURATION:
DOM_CONTENT_LOADED_TIME: 1908
I want to remove the 3rd data from the api REPORT_URL:
You can skip REPORT_URL in foreach
$ukey = strtoupper( $result );
if( $ukey != 'REPORT_URL' ) {
echo '<strong>' . $ukey . '</strong>';
echo $data . '<br><br>';
}

Dependency of HTML rendering on function's return value that executes later?

I have a function that prints "Our data" first and then inside a loop checks for all the data in the database and then prints the data.
However, if there is no data, it shouldn't print "Our data", how do I achieve this since "Our data" is already printed?
The layout is as follows :
<h1> Our Data </h1>
<p> <!-- DATA --> </p>
The function is as follows:
function cpt_info($cpt_slug, $field_name, $title) {
echo "<h1>Our" . $title . "</h1>"; //here I want to print our data
$args = array('limit' => -1);
$cpt = pods($cpt_slug, $args);
$page_slug = pods_v('last', 'url');
echo "<h1 class='projects-under-" . $cpt_slug . "'>" . $title . "</h1>";
if ($cpt->total() > 0) :
while ($cpt->fetch()) :
if (!strcasecmp($cpt->field('post_name'), $page_slug)) :
$data = $cpt->field($field_name);
foreach ((array) $data as $key => $value) {
if ($value != null) : //here I check whether data is empty
$url = wp_get_attachment_url(get_post_thumbnail_id($value['ID']));
echo "<div class='col-sm-3 col-xs-6'><div class='border-to-circle'>";
echo "<div class='circle-container' style='background: url(" . $url . ")center center, url(" . get_template_directory_uri() . '/images/rwp-banner.jpg' . ")center center;'><h4><a href='" . get_permalink($value['ID']) . "'>" . $value['post_title'] . "</a></h4></div>";
echo "</div></div>";
endif;
}
endif;
endwhile;
endif;
}
Something like below:
if(count($data)){
echo '<h1> Our Data </h1>';
}

Extract form and input fields from HTML

I want to extract and echo Attribute Values from both form and input fields from a given HTML. I found that its possible with DOMDocument.
One specific input needs to be grouped by a <div style="position: absolute; left: -5000px;"></div>
I thought that I can either search for if the parent element of the input has this style attributes or if the name of the input field is similiar to b_7e0d9965bedeea1cc5f7217a9_4685998a30. But I have no idea how to do that.
This is the code:
$html = $theme['html']; // From a text input field
$dom = new DOMDocument();
if (#$dom->loadHTML($html)) {
$forms = $dom->getelementsbytagname('form');
foreach ($forms as $form) {
$form_action = $form->getAttribute('action');
$form_method = $form->getAttribute('method');
$form_id = $form->getAttribute('id');
$form_name = $form->getAttribute('name');
$form_class = $form->getAttribute('class');
$form_target = $form->getAttribute('target');
echo '<form';
if (!empty($form_action)) { echo ' action="'. $form_action .'"'; }
if (!empty($form_method)) { echo ' method="'. $form_method .'"'; }
if (!empty($form_id)) { echo ' id="'. $form_id .'"'; }
if (!empty($form_name)) { echo ' name="'. $form_name .'"'; }
if (!empty($form_class)) { echo ' class="'. $form_class .'"'; }
if (!empty($form_target)) { echo ' target="'. $form_target .'"'; }
echo '>';
$inputs = $dom->getelementsbytagname('input');
foreach ($inputs as $input) {
$input_name = $input->getAttribute('name');
$input_value = $input->getAttribute('value');
$input_id = $input->getAttribute('id');
$input_type = $input->getAttribute('type');
$input_class = $input->getAttribute('class');
echo '<input';
if (!empty($input_name)) { echo ' name="'. $input_name .'"'; }
if (!empty($input_value)) { echo ' value="'. $input_value .'"'; }
if (!empty($input_id)) { echo ' id="'. $input_id .'"'; }
if (!empty($input_type)) { echo ' type="'. $input_type .'"'; }
if (!empty($input_class)) { echo ' class="'. $input_class .'"'; }
echo '>';
}
echo '</form>';
}
}
Some Background Info: I want the user to copy and paste his Email Form Code into a Textbox. Then I want to extract the attributes of the Input Fields to use them inside my Template.
Here are the few things wrong with your syntax
$html = $theme['html']; // Make sure $html is a String
$dom = new DOMDocument();
//if (#$dom->loadHTML($html)) {
if ($dom->loadHTML($html)) { //remove #
//$forms = $dom->getelementsbytagname('form');
$forms = $dom->getElementsByTagName("form");
Make these changes and check again. Hope it should work then
To get the parent node of each input field, you can use
$parentOfInput = $input->parentNode();
$parentAttribute = $parentOfInput->getAttribute('style');
To group each form in a div, try doing this:
//echo '<form';
echo '<div style="position: absolute; left: -5000px;"><form'
and at the end
//echo '</form>';
echo '</form></div>'
In case you want to insert the whole form in an existing div, you cannot do this with PHP. As once the HTML is renedered, you cannot insert HTML using PHP.
However you can use javascript or in your case AJAX. Save the whole HTML that you are echoing, in a variable. Then pass that variable in an AJAX call.
$.ajax({url: "urFile.php"}).done(function( stringOfHTMLYouFormed ) {
$("#divID").append(stringOfHTMLYouFormed );
});
I added $parent = $input->parentNode->getAttribute('style'); into the foreach loop to look for the style of the Parent.
Right after it I use to test if the $parent has the desired style to wrap then the corresponding input field into a div.
$parent = $input->parentNode->getAttribute('style');
if ($parent == 'position: absolute; left: -5000px;') {
echo '<div style="position: absolute; left: -5000px;">';
echo '<input';
if (!empty($input_name)) { echo ' name="'. $input_name .'"'; }
if (!empty($input_value)) { echo ' value="'. $input_value .'"'; }
if (!empty($input_id)) { echo ' id="'. $input_id .'"'; }
if (!empty($input_type)) { echo ' type="'. $input_type .'"'; }
if (!empty($input_class)) { echo ' class="'. $input_class .'"'; }
echo '></div>';
} else {
echo '<input';
if (!empty($input_name)) { echo ' name="'. $input_name .'"'; }
if (!empty($input_value)) { echo ' value="'. $input_value .'"'; }
if (!empty($input_id)) { echo ' id="'. $input_id .'"'; }
if (!empty($input_type)) { echo ' type="'. $input_type .'"'; }
if (!empty($input_class)) { echo ' class="'. $input_class .'"'; }
echo '>';
}

In template page_list get image attribute Concrete5

My following code is based on
1.Get current URL
2.Go through array and check if in url value = to value in array
do this:
$on_this_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
foreach ($city_array as $sandwich) {
if (strpos($on_this_link, $sandwich) == true) {
$sandwich = trim($sandwich, '/');
$city = $sandwich;
if ($city == 'newyork') {
foreach ($category_array as $double_sandwich) {
if (strpos($on_this_link, $double_sandwich) == true) {
$double_sandwich = trim($double_sandwich, '/');
$category_is = $double_sandwich;
Loader::model('page_list');
$nh = Loader::helper('navigation');
$pl = new PageList();
$pl->filterByAttribute('city', '%' . $city . '%', 'like');
$pl->filterByAttribute('category','%'.$category_is.'%','like');
$pl->sortByDisplayOrder();
$pagelist = $pl->get();
foreach ($pagelist as $p) {
echo '<li> ' .htmlspecialchars($p->getCollectionName()) . ' </li>';
?>
}
}
}
}
So It will show me only pages that have the same attribute with URL
Each of this page has image attribute that I want to show.
How can I pass this Image Attribute??
Check out the comments in the page list block's view template:
https://github.com/concrete5/concrete5/blob/master/web/concrete/blocks/page_list/view.php#L33
You can get image attributes by putting some code like this inside your foreach ($pagelist as $p) loop:
$img = $p->getAttribute('example_image_attribute_handle');
if ($img) {
//you could output the original image at its full size like so:
echo '<img src="' . $img->getRelativePath() . '" width="' . $img->getAttribute('width') . '" height="' . $img->getAttribute('height') . '" alt="" />';
//or you could reduce the size of the original and output that like so:
$thumb = Loader::helper('image')->getThumbnail($img, 200, 100, false); //<--200 is width, 100 is height, and false is for cropping (change to true if you want to crop the image instead of resize proportionally)
echo '<img src="' . $thumb->src . '" width="' . $thumb->width . '" height="' . $thumb->height . '" alt="" />';
}
Thx but I did it already just another way!
I didnt used
Loader::model('page_list');
Instead I used:
$blockType = BlockType::getByHandle('page_list');
And hardcoded the block!
$th = Loader::helper('text');
$ih = Loader::helper('image');
$page_current = Page::getCurrentPage();
$page_2 = $page_current->getCollectionHandle();
$img = $page->getAttribute('product'); $thumb =
$ih->getThumbnail($img, 240,150, false);
And after I changed a little bit my Code above:
$on_this_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
foreach ($city_array as $sandwich) {
if (strpos($on_this_link, $sandwich) == true) {
$sandwich = trim($sandwich, '/');
$city = $sandwich;
if ($city == 'newyork') {
foreach ($category_array as $double_sandwich) {
if (strpos($on_this_link, $double_sandwich) == true) {
$double_sandwich = trim($double_sandwich, '/');
$category_is = $double_sandwich;
$city_att = $page->getAttribute('city', '%' . $city . '%', 'like');
$sub_cat_att = $page->getAttribute('category','%'.$category_is.'%','like'); ?>
<?php if($city == $city_att && $category_is == $sub_cat_att){ ?><li><img src="<?php echo $thumb->src ?>" width="<?php echo $thumb->width ?>" height="<?php echo $thumb->height ?>" alt="" />
<h3> <?php echo $title ?></h3>
<div class="product_description">
<?php echo $description ?>
</div>
Read More...
</li> <?php } ?> <?php
}
}
}
So everything it is working But still thx for respond! Apreciate

PHP Loop Question?

I have a php script that will loop until all the skill, exp and rating variables are displayed that the user entered into the database.
What I want is the following code to only display once and only if the variables are holding info and not empty. How can I do this. I hope I explained it okay.
I want this code displayed first.
echo '<div id="con">';
echo '<h2 id="s">Skills</h2>';
echo '<h2 id="exp">Exp</h2>';
echo '<h2 id="r">Rating</h2>';
Here is the full code.
<?php
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM skills WHERE user_id='3'");
if (!$dbc) {
print mysqli_error();
}
echo '<div id="con">';
echo '<h2 id="s">Skills</h2>';
echo '<h2 id="exp">Exp</h2>';
echo '<h2 id="r">Rating</h2>';
while ($row = mysqli_fetch_assoc($dbc)) {
if (!empty($row['skill']) || !empty($row['exp']) || !empty($row['rating'])) {
if (! empty($row['skill'])) {
echo '<div class="s">';
echo '<p>' , htmlspecialchars($row['skill']) , '</p>';
}
if (! empty($row['exp'])) {
echo '<div class="s">';
echo '<p>' , htmlspecialchars($row['exp']) , '</p>';
}
if (! empty($row['rating'])) {
echo '<div class="s">';
echo '<p>' , htmlspecialchars($row['rating']) , '</p>';
}
}
}
echo '</div>';
?>
you can take the following after the loop:
echo '<div id="con">';
echo '<h2 id="s">Skills</h2>';
echo '<h2 id="exp">Exp</h2>';
echo '<h2 id="r">Rating</h2>';
and instead of echo in the loops , use vars to store the text and then check if the var holds something and then echo
update:
would become something like this:
$skills = "";
$exps = "";
$ratings = "";
while ($row = mysqli_fetch_assoc($dbc)) {
if (!empty($row['skill']) || !empty($row['exp']) || !empty($row['rating'])) {
if (! empty($row['skill'])) {
$skills .='<div class="s">';
$skills .= '<p>' , htmlspecialchars($row['skill']) , '</p>';
}
if (! empty($row['exp'])) {
$exps .= '<div class="s">';
$exps .= '<p>' , htmlspecialchars($row['exp']) , '</p>';
}
if (! empty($row['rating'])) {
$ratings .= '<div class="s">';
$ratings .= '<p>' , htmlspecialchars($row['rating']) , '</p>';
}
}
}
echo '<div id="con">';
if($skills){
echo '<h2 id="s">Skills</h2>';
echo $skills;
}
if($exps){
echo '<h2 id="exp">Exp</h2>';
echo $exps;
}
if($ratings){
echo '<h2 id="r">Rating</h2>';
echo $ratings;
}
echo '</div>';

Categories