PHP content in HTML Code with If conditon - php

I am trying to show text content just with a PHP if condition but I have several errors and I don’t see where is the error could someone help me with this, here is the code:
<?php
if(
isset($_POST['send']) &&
!validateDiscp($_POST['Discp']) || !validateSize($_POST['Size'])
) : ?>
<div id="error">
<ul>
<?php if(!validateDiscp($_POST['Discp'])):?>
<li><strong>Discription:</strong>Discription need to be larger then 10!</li>
<?php endif?>
<?php if(!validateSize($_POST['Size'])):?>
<li><strong>Invalid Size:</strong> Size needs to bi S M L XL</li>
<?php endif ?>
</ul>
</div>
<?php elseif(isset($_POST['send'])):?>
<div id="error" class="valid">
<ul>
<li><strong>Congratulations!</strong> All fields are OK ;)</li>
</ul>
</div>
<?php endif ;?>

I would rewrite the whole outside block. Instead do:
<? if( isset($_POST['send'])&& !validateDiscp($_POST['Discp']) || !validateSize($_POST['Size']) ) { ?>
<div id="error">
<ul>
<?php if(!validateDiscp($_POST['Discp'])) { ?>
<li><strong>Discription:</strong> Discription need to be larger then 10!</li>
<?php } ?>
<?php if(!validateSize($_POST['Size'])) { ?>
<li><strong>Invalid Size:</strong> Size needs to bi S M L XL</li>
<?php } else {
$nothing = true;
?>
</ul>
</div>
<?php if( isset( $nothing ) and isset($_POST['send'] ) ) { ?>
<div id="error" class="valid">
<ul>
<li><strong>Congratulations!</strong> All fields are OK ;)</li>
</ul>
</div>
<? } ?>
I think the flaw in your logic ist hat you have an else statement that is dangling. Instead add a little logic to detect when the else case is hit and add your additional logic and your good to go.

All the endifs need to have ; after them. Also, I'm not sure if this is an actual error or just the code getting wrapped, but make sure your first if is all on one line.

after changing things it is working here is the code
<?php if(isset($_POST['send']) && !validateDiscp($_POST['Discp'])|| !validateSize($_POST['Size']) ):?>
<div id="error">
<ul>
<?php if(!validateDiscp($_POST['Discp'])):?>
<li><strong>Discription:</strong> Discription need to be larger then 10!</li>
<?php endif;?>
<?php if(!validateSize($_POST['Size'])):?>
<li><strong>Invalid Size:</strong> Size needs to bi S M L XL</li>
<?php endif; ?>
</ul>
</div>
<?php elseif(isset($_POST['send']) ):?>

Related

How to Wrap A Condition Inside Another Condition in PHP

Can anyone please help me wrap the condition inside another condition in php.
I have this code #1 that I want to be inside code #2.
Here's code #1
<?php if( get_field('highlights') ): ?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php endif; ?>
Here's code #2
<?php if(strstr($_SERVER['HTTP_REFERER'],'www.example.com'))
{
echo '**CODE #1 should be placed here**';
}
?>
Sorry, I don't haev any knowledge in PHP.
Wrapping code 1 inside code 2
After several trial and error, here's what I have to make it work. Please correct me if there's something wrong or to improve.
<?php if (strstr($_SERVER['HTTP_REFERER'], 'www.google.com')){ ?>
<?php if( get_field('highlights') ):?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php endif; ?>
<?php } ?>
This should work in theory. Though I'm unsure what the highlights field is for.
<?php
if(strstr($_SERVER['HTTP_REFERER'],'www.example.com')){
if( get_field('highlights') ){ ?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php
}
}
?>
You may want to replace <?php the_field('highlights'); ?> with something like <?=highlight ?> and iterate through the highlights in a loop. That depends on the situation though.

Display nav list of content is available with php

I've got this PHP code:
<div class="connect_wrap <?php echo $this->class; ?> block" <?php echo $this->cssID; ?>>
<?php if(!$this->empty): ?>
<?php foreach($this->entries as $entry): ?>
<div class="entry block <?php echo $entry->class; ?>">
<div class="tab">
<ul class="tabs">
<li class="tab-link current" data-tab="Kunden">Kunden</li>
<li class="tab-link" data-tab="Loesungen">Lösungen</li>
</ul>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM kunden WHERE partner=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Kunden" class="tab-content current">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/kunden-detail/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM solutions WHERE solution_provider=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Loesungen" class="tab-content">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/synaptic-commerce-solution/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<?php endif;?>
In that code, I've got two DB queries. My problem is, if there is no data for both queries, the div with the class "connect_wrap" should not be displayed.
How can I do that?
Thanks in advance.
do you want to check if the query has data in it and its not empty ? if so try num_rows it will return the number of rows that the query found/affected for example to check if th query returned one or more rows
if($result->num_rows >= 1) {
//do stuf
} else {
// no result found
}
Move the query execution up or preferably: not within the html but in a different file/class. Then add a check before the content_wrap div: if($kundenResult->num_rows >= 1 || $solutionsResult->num_rows >= 1). If you just keep the individual checks like you already have, it will only show the content_wrap div if either or both of the queries return rows of data.

Merging PHP code if statement. Magento Attribute

I am trying to merge these 2 line of code for an if statement. The purpose is to hide the attribute if Code1 is blank. Each one reports correct but when place together the code give errors. Thanks for the hint :)
Code1
<?php if ($_product->getAttributeText('tempi_consegna') != '') ?>
Code2
<p class="availability in-stock"><?php echo $this->__('Consegna:') ?>
<span>
<?php echo $_product->getAttributeText('tempi_consegna') ?>
</span>
</p>
They should be living friendly in magento product availability.
You can try as following:
<?php if ($_product->getAttributeText('tempi_consegna') != ''): ?>
<p class="availability in-stock"><?php echo $this->__('Consegna:') ?>
<span>
<?php echo $_product->getAttributeText('tempi_consegna') ?>
</span>
</p>
<?php endif; ?>

Place DIV's in a containing DIV based on a numeric value

Ive got the follow PHP:
<div class="slide-background">
<div class="slide">
<?php foreach (array_chunk($items->submenu, $linkCount) as $items): ?>
<?php if (12 / $cols == 1):?>
<div class="col-md-12">
<?php else: ?>
<div class="col-md-<?php echo 12 / $cols; ?>">
<?php endif; ?>
<ul>
<?php foreach($items as $submenu): ?>
<?php echo $submenu; ?>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
</div>
<ul class="pager">
<li>prev</li>
<li>next</li>
</ul>
</div>
</div>
basically it calculates how many links to display and how many columns, but i now need to place the links in <div class="slide"></div>, but based on the columns.. so basically i need to say if $cols = 2 place two div's in a div and close.. so its basically how many every $cols it should place so many div's in that div..
Its Confusing for me to even explain.. I think Ive explained it rather well above.. If not place say so and ill try again..
Any Help Greatly Appreciated..
UPDATE:
thanks to Hans ive now have the following:
<?php $linksPerColumn = ceil($linkCount / $cols); $linkCounter = 0;?>
<div class="slide-background">
<div class="slide">
<div class="col-md-<?php echo 12 / $cols ?>">
<ul>
<?php foreach ($items->submenu as $link): ?>
<?php $linkCounter++;?>
<?php if($linkCounter % $linksPerColumn == 0):?>
</ul>
</div>
<div class="col-md-<?php echo 12 / $cols ?>">
<ul>
<?php endif; ?>
<?php echo $link; ?>
<?php endforeach; ?>
</ul>
</div>
</div>
<ul class="pager">
<li>prev</li>
<li>next</li>
</ul>
</div>
</div>
only problem is when there's only one column and i need 2 links and then for it to close the div and the ul and start new ones.. right now it does that except for everyone and not for every two links...
You could use modulus for this one. You should calculate how many items you need per column. And then create a close and open div for example:
<?
$linksPerColumn = 4;
if ($linkCount > 4){
$linksPerColumn = ceil ($linkCount / $amountOfColums);
}
$linkCounter = 0;
?>
<div class="slide-background">
<div class="slide">
<?
foreach ($links as $link)
{
$linkCounter++;
?>
// Do your HTML Here.
<?
if($linkCounter % $linksPerColumn = 0)
{
?>
</div>
<div class="slide">
<?
}
?>
</div>
</div>
// Rest of the HTML here.
I think this should do the trick for you.

Do list separated by date PHP

I need your help.
I'm creating a reporting system and I need it to list all the reports that groups only in blocks according to the date.
ex:
Today - May 23
a result
result 2
May 22
result 3
4 results
5 results
May 21
6 results
7 results
The listing I can do it quietly, the problem is to distinguish the date to be right within the respective block.
My code:
<?php
$data_anterior = "0000-00-00";
$data_hoje = date("Y-m-d");
//Se houverem registros
foreach($notificacoes AS $notif) :
?>
<?
//Verifica a data e faz o tratamento
$data_cadastro = date("Y-m-d", strtotime($notif->data_cadastro));
?>
<?php if($data_cadastro == $data_hoje && $data_anterior != $data_cadastro) { ?>
<h4>Hoje <span><?php echo $CI->funcoes->data_abreviada($data_cadastro); ?></span></h4>
<ul class="list-notificacoes">
<?php } ?>
<?php if($data_cadastro < $data_hoje && $data_anterior != $data_cadastro) { ?>
<div class="graybox">
<header class="clearfix">
<h4><?php echo $CI->funcoes->data_abreviada($data_cadastro); ?></h4>
Toggle
</header>
<ul class="list-notificacoes">
<?php } ?>
<li class="clearfix">
<div class="box-left">
<span class="icon-lista-ate"> </span>
<?php echo stripslashes($usuario->nome); ?>
<span>fez tal tarefa</span>
<?php echo stripslashes($evento->titulo); ?>
</div>
<div class="box-right">
<span><?php echo date("H:i", strtotime($notif->data_cadastro)); ?></span>
<a class="icon-close ir" href="">Excluir</a>
</div>
</li>
<?php if($data_cadastro == $data_hoje && $data_anterior != $data_cadastro) { ?>
</ul>
<?php } ?>
<?php if($data_cadastro < $data_hoje && $data_anterior != $data_cadastro) { ?>
</ul>
</div>
<?php } ?>
<?php $data_anterior = date("Y-m-d", strtotime($notif->data_cadastro)); ?>
<?php endforeach; ?>
My problem is with the closing of the blocks and thus it breaks the code.
There's something wrong in my IF, someone help me?
I think it is a typo.
After the foreach statement "php" is missing from the opening tag:
<? instead of
<?php
//Verifica a data e faz o tratamento
$data_cadastro = date("Y-m-d", strtotime($notif->data_cadastro));
?>)
See if this fixes your problem.

Categories