I have this code (see below) that I'm having trouble echoing the variables out. On the 5th line is an echo, within that echo is a load of html (which I've escaped the quotation marks) and a load of variables. I can't get the variables to echo out within the main echo.
Update:
Also within there is an onclick that needs to be taken into account.
<?php
if(
in_array("Branding", get_field('categories')) && $grid_title == "Branding"
){
echo "
<div class=\"grid-box\" onclick=\"location.href='<?php echo get_page_link($post->ID) ?>';\" style=\"cursor: pointer;\">
<div class=\"phase-1\">
<img class=\"grid-image\" src=\"<?php echo $fields->thumb_image; ?>\" alt=\"<?php echo $fields->company_name; ?>\" height=\"152\" width=\"210\" />
<div class=\"grid-heading\">
<h2><?php echo $fields->company_name; ?></h2>
<h3><?php echo implode(', ',get_field('categories'));?></h3>
</div>
</div>
<div class=\"phase-2\">
<div class=\"grid-info\">
<h4><?php echo $fields->project_name; ?></h4>
<p><?php echo $fields->description; ?></p>
</div>
<div class=\"grid-heading-hover\">
<h2><?php echo $fields->company_name; ?></h2>
<h3><?php echo implode(', ',get_field('categories'));?></h3>
</div>
</div>
</div>
";
}
?>
You can't use echo within an echo, you just need to concatenate your strings properly like so:
$Content = "
<div class=\"grid-box\" onclick=\"location.href='" . get_page_link($post->ID). "';\" style=\"cursor: pointer;\">
<div class=\"phase-1\">
<img class=\"grid-image\" src=\"" . $fields->thumb_image . "\" alt=\"" . $fields->company_name. "\" height=\"152\" width=\"210\" />
<div class=\"grid-heading\">
<h2> " . $fields->company_name . "</h2>
<h3>" . implode(', ',get_field('categories')) . "</h3>
</div>
</div>
<div class=\"phase-2\">
<div class=\"grid-info\">
<h4>" . $fields->project_name . "</h4>
<p>" . $fields->description . "</p>
</div>
<div class=\"grid-heading-hover\">
<h2>" . $fields->company_name . "</h2>
<h3>" . implode(', ',get_field('categories')). "</h3>
</div>
</div>
</div>";
echo $Content;
You seem to have a little misunderstanding here.
In PHP you can output HTML either by
writing simple HTML outside <?php...?>, and possibly inserting some PHP code inside
using something like echo / print to output a PHP string which includes HTML
But you cannot mix up the two.
So this is not good:
echo "... src=\"<?php echo $fields->thumb_image; ?>\" ...";
Because this is a string, and in a string you can not open a <?php section. You should do something like this instead:
echo "... src=\"{$fields->thumb_image}\" ...";
which is one of the cool ways to insert PHP variables inside a string.
Something like this
echo " ... <?php ... ?> ...";
will not work.
Instead you could "break in and out" of php, like:
...
?>
<div class="grid-box" onclick="location.href='<?php echo get_page_link($post->ID) ?>';" style="cursor: pointer;">
<div class="phase-1"> ... <?php
Or use s/printf
printf(
'<div class="grid-box" onclick="location.href='%s';" style="cursor: pointer;">',
get_page_link($post->ID)
);
Or use string concatenation:
echo "html ...", get_page_link($post->ID), "some more html...";
Or use some kind of templating code.
Just close the php tag after the if and open it before the closing semi-colon.
if(
in_array("Branding", get_field('categories')) && $grid_title == "Branding"
){
?>
<div class="grid-box" onclick="location.href='<?php echo get_page_link($post->ID) ?>';" style="cursor: pointer;">
<div class="phase-1">
<img class="grid-image" src="<?php echo $fields->thumb_image; ?>" alt="<?php echo $fields->company_name; ?>" height="152" width="210" />
<div class="grid-heading">
<h2><?php echo $fields->company_name; ?></h2>
<h3><?php echo implode(', ',get_field('categories'));?></h3>
</div>
</div>
<div class="phase-2">
<div class="grid-info">
<h4><?php echo $fields->project_name; ?></h4>
<p><?php echo $fields->description; ?></p>
</div>
<div class="grid-heading-hover">
<h2><?php echo $fields->company_name; ?></h2>
<h3><?php echo implode(', ',get_field('categories'));?></h3>
</div>
</div>
</div>
<?php
}
?>
Related
I Have a multipart/form-data form but I not require from users to send me the picture. I am changing the name of the file to random number and the put the path to MySQL. Whenever somebody doesn't put the file, in my database a new recor is beeing created without an extension(obviesly). How to stop this from happening?
Sorry for my english.
Here is a a part from the code:
case 'Sleep Aid': {
?>
<section class="summarySection">
<div class="summaryHeadingDIV">
<h1 class="summaryH1">Please check the details below</h1>
</div>
<article class="summaryPage">
<p class="boldSummaryDescr">Brand:</p><p class="paraSummary"><?php echo $brand; ?></p><br>
<p class="boldSummaryDescr">Model:</p><p class="paraSummary"><?php echo $model; ?></p><br>
<p class="boldSummaryDescr">Colour:</p><p class="paraSummary"><?php echo $colour; ?></p><br>
<p class="boldSummaryDescr">Material:</p><p class="paraSummary"><?php echo $material; ?></p><br>
<p class="boldSummaryDescr">Suitable Age:</p><p class="paraSummary"><?php echo $suitable; ?></p><br>
<p class="boldSummaryDescr">Purchase date:</p><p class="paraSummary"><?php echo $month . '/' . $year; ?></p><br>
<p class="boldSummaryDescr">Condition:</p><p class="paraSummary"><?php echo $condition; ?></p><br>
<p class="boldSummaryDescr">Dimesions:</p><p class="paraSummary"><?php echo $width . ' cm <span class="smallcaps">x</span> ' . $height . ' cm <span class="smallcaps">x</span> ' . $depth . ' cm'; ?></p><br>
<p class="boldSummaryDescr">Weight:</p><p class="paraSummary"><?php echo $weight . ' kg'; ?></p>
</article>
<div class="imageg_container">
<figure class="img_figure_show">
<?php
if(empty($picture))
{
?>
<img src="/_images/no-picture.png" alt="no picture added">
<?php
}
else
{
$random_id = rand_img_id();
add_file(db_user_connect(), $email, $random_id);
$arry = show_picture(db_user_connect(), $email, $random_id);
print "<img src=".$arry["sciezka"]." alt='product picture'>";
}
?>
</figure>
</div>
<div class="clear-left"></div>
<div class="summaryButtonContainer">
Edit details
Proced to Quote
</div>
</section>
<?php
break;
}
You need to check the $_FILES['error'] value:
if ($_FILES['name']['error'] === UPLOAD_ERR_OK) ...
The manual lists all possible values, which you could use to display different messages/do different things: https://secure.php.net/manual/en/features.file-upload.errors.php
have a phtml file riding the payment of the value of a product, the function responsible for it automatically puts on every page of the product installment conditions, but does not appear the payment terms on the homepage where I coloto one "carousel "for featured products.
Someone would have a hint of what I do?
Note 1: I use a AllPago the module to set the conditions only that I can not use the stand for the value that do not have access.
Note 2: according to the source code that assembles the installments for me (file name in magento to find looks like this: allpago_installments / productviewtable.phtml
<?php foreach ($this->getInstallments() as $installment): ?>
<?php $result = count($this->getInstallments()); ?>
<?php if($installment->getValue()==$result):?>
<div class="product-view-parcel">
<?php echo $installment->getValue() . ' x ' . $installment->getInstallment() . ' ' . $installment->getMessage(); ?>
</div>
<?php endif;?>
<?php endforeach; ?>
Note 3: a part of the home page code where I try to call the price and conditions (heeding the price appears correctly only the payment terms that do not):
<div id="ripplesslider" class="ripplesslider" style="height: 470px!important;">
<?php foreach ($_productCollection_slider as $_product): ?>
<div id="slide" style="text-align: center !important;height: 470px!important;"
class="latest-slider-item slide slider<?php echo $_product->getId() ?>">
<a href="<?php echo $_product->getProductUrl() ?>"
title="<?php echo $this->htmlEscape($_product->getName()) ?>">
<div style="height: 200px!important;">
<img onmouseover="mouseover(<?php echo $_product->getId() ?>)"
onmouseout="mouseout(<?php echo $_product->getId() ?>)"
style="width:<?php echo $t ?>px;
height:<?php echo $imageheight ?>px; "
src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image'); ?>"
alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/>
</div>
</a>
<div class="desc-item">
<div class="carousel-name-product"> <?php echo $_product->getName() ?></div>
<div class="latest-price">
<?php if ($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php endif; ?>
<?php $this->getPriceHtml($_product, true) ?>
<?php echo '<span czlass="price">' . str_replace('R$','', $this->getPriceHtml($_product, true)) . '</span>'; ?>
</div>
</div>
</div>
<?php endforeach ?>
</div>
haha staff already got, I'll post here what I did to perhaps help others.
<div>
<?php echo '<span class="price">' . str_replace('R$','', $this->getPriceHtml($_product, true)) . '</span>'; ?>
<?php // object reference?>
<?php $reference = Mage::getStoreConfig('allpago/installments/active'); ?>
<?php//returns a new object. references ?>
<?php $installmentModel = Mage::getModel('installments/'.$reference); ?>
<?php //access the parameters to get the conditions have to function "getInstallmentHighest" to return the plots?>
<?php $installmentModel->setValue($_product->getFinalPrice()); ?>
<?php $installment = $installmentModel->getInstallmentHighest(); ?>
<span style="text-align: left;color: #A8A0A8;position: absolute;margin-top: -46px !important;margin-left: -78px !important;font-size: 12px;">
<?php echo $installment->getValue().' x '. $installment->getInstallment() . ' ' . $installment->getMessage(); ?>
</span>
</div>
I am writing a custom landing page in PHP parsing an XML product feed. The product descriptions are very long, as it goes into technical information and specifications, so need to be condensed to read the first 200 characters. hopefully with a read more link once 200 characters has been reached.
The code I have so far is:
<?php
$xml = simplexml_load_file('feed.xml');
foreach ($xml->item as $item) { ?>
<div class="row">
<div class="lhs">
<h3><?php echo $item->brand.' '.$item->title ?></h3>
<p class="pri">£<?php echo $item->price ?></p>
<p><?php echo $item->description; ?></p>
</div>
<div class="rhs">
<img src="<?php echo $item->image_link ?>" alt="<?php echo $item->title ?>" height="150" />
</div>
</div>
<?php
}
?>
Please could anyone advise what else I need to add? I can follow basic patterns in PHP, I just need some guidance.
Many thanks in advance.
Notice the $short_description line.
Edited with some basic js example of "Show more"
<?php
$xml = simplexml_load_file('feed.xml');
foreach ($xml->item as $item) {
$short_description = substr($item->description, 0, 200);
?>
<div class="row">
<div class="lhs">
<h3><?php echo $item->brand . ' ' . $item->title ?></h3>
<p class="pri">£<?php echo $item->price ?></p>
<p id="shown"><?php echo $short_description; ?>... Show more</p>
<p id="hidden"><?php echo $item->description; ?></p>
</div>
<div class="rhs">
<img src="<?php echo $item->image_link ?>" alt="<?php echo $item->title ?>" height="150" />
</div>
</div>
<?php
}
?>
samcheckdb.php
$sql1="select subtitle,descript from dessert where itemId='oepd1007'";
$result1=mysqli_query($dbhandle,$sql1);
$sql2="select subtitle,descript from dessert where itemId='oepd5148'";
$result2=mysqli_query($dbhandle,$sql2);
<div id="d0"><?php if($result1) { echo $descript1 ;} ?></div>
<div id="s0"><?php if($result1) { echo $subtitle1 ;} ?></div>
<div id="d11"><?php if($result2) {echo $descript2; } ?></div>
<div id="s11"><?php if($result2) { echo $subtitle2 ;} ?></div>
customer
<html>
<li> <img src="getdesserticecreamimage.php?itemId=oepd1007" alt="image" id="img1" onclick="get_detail('oepd1007');"></li>
<li> <img src="getdesserticecreamimage.php?itemId=oepd5148" alt="image" id="img2"></li>
</html>
i need to display only 1 subtitle and descript in the page at a time, when the user clicks on the image,only the corresponding info of the particular image get displayed,not the rest..
another neater approach is to use the : block
<?php if ($result1): ?>
<div id="d0"><?php echo $descript1 ; ?></div>
<div id="s0"><?php echo $subtitle1 ; ?></div>
<?php elseif($result2): ?>
<div id="d11"><?php echo $descript2 ; ?></div>
<div id="s11"><?php echo $subtitle2 ; ?></div>
<?php endif; ?>
<?php if($result1) { ?>
<div id="0"><?php echo $descript1; ?></div>
<div id="s0"><?php echo $subtitle1; ?></div>
<?php } else { ?>
<div id="d11"><?php echo $descript2; ?></div>
<div id="s11"><?php echo $subtitle2; ?></div>
<?php } ?>
Always remember that you can echo all HTML Elements in PHP.
<?php
if($result1) {
echo '<div id="d0">' . $descript1 .'</div>';
echo '<div id="s0">' . $subtitle1 .' </div>';
}
if($result2) {
echo '<div id="d11">' . $descript2 .'</div>';
echo '<div id="s11">' . $subtitle2 .'</div>';
}
?>
try
echo (isset($result1) ? '<div id="d0">'. $descript1 .'</div><div id="s0">'.$subtitle1 .'</div>': '<div id="d11">'.$descript2 .'</div><div id="s11">'.$subtitle2 .'</div>');
I've hacked together this code with various if and elseif statement's and just wondering if it could be tidied up (my syntax knowledge is rubbish!):
Rather than show ALL the html code again (because it's the same) is there a way I can combine all the elseif and if's into one?
if(in_array("Branding", get_field('categories')) && $grid_title == "Branding"){
echo "
<div class=\"grid-box\" onclick=\"location.href='" . get_page_link($post->ID) ."';\" style=\"cursor: pointer;\">
<div class=\"phase-1\">
<img class=\"grid-image\" src=\"" . $fields->thumb_image . "\" alt=\"" . $fields->company_name ."\" height=\"152\" width=\"210\" />
<div class=\"grid-heading\">
<h2>". $fields->company_name ."</h2>
<h3>" . implode(', ',get_field('categories')) ."</h3>
</div>
</div>
<div class=\"phase-2\">
<div class=\"grid-info\">
<h4>". $fields->project_name ."</h4>
<p>". $fields->description ."</p>
</div>
<div class=\"grid-heading-hover\">
<h2>". $fields->company_name ."</h2>
<h3>". implode(', ',get_field('categories')) ."</h3>
</div>
</div>
</div>
";
}
elseif(in_array("Web", get_field('categories')) && $grid_title == "Web"){
echo "
<div class=\"grid-box\" onclick=\"location.href='" . get_page_link($post->ID) ."';\" style=\"cursor: pointer;\">
<div class=\"phase-1\">
<img class=\"grid-image\" src=\"" . $fields->thumb_image . "\" alt=\"" . $fields->company_name ."\" height=\"152\" width=\"210\" />
<div class=\"grid-heading\">
<h2>". $fields->company_name ."</h2>
<h3>" . implode(', ',get_field('categories')) ."</h3>
</div>
</div>
<div class=\"phase-2\">
<div class=\"grid-info\">
<h4>". $fields->project_name ."</h4>
<p>". $fields->description ."</p>
</div>
<div class=\"grid-heading-hover\">
<h2>". $fields->company_name ."</h2>
<h3>". implode(', ',get_field('categories')) ."</h3>
</div>
</div>
</div>
";
}
else {
echo "hello";
}
You should consider using PHP's Heredoc to delimit strings. That would help get rid of the echo and all the escape `\' characters.
Use PHP's if/else/elseif/endif short-hand syntax. It makes it read easier:
if(condition) :
//statments
elseif(condition) :
//statments
endif;
The elseif does the same as the first if. So move the condition to the first one with OR and remove elseif:
if((in_array("Branding", get_field('categories')) && $grid_title == "Branding") || (in_array("Web", get_field('categories')) && $grid_title == "Web")){
echo "
<div class=\"grid-box\" onclick=\"location.href='" . get_page_link($post->ID) ."';\" style=\"cursor: pointer;\">
<div class=\"phase-1\">
<img class=\"grid-image\" src=\"" . $fields->thumb_image . "\" alt=\"" . $fields->company_name ."\" height=\"152\" width=\"210\" />
<div class=\"grid-heading\">
<h2>". $fields->company_name ."</h2>
<h3>" . implode(', ',get_field('categories')) ."</h3>
</div>
</div>
<div class=\"phase-2\">
<div class=\"grid-info\">
<h4>". $fields->project_name ."</h4>
<p>". $fields->description ."</p>
</div>
<div class=\"grid-heading-hover\">
<h2>". $fields->company_name ."</h2>
<h3>". implode(', ',get_field('categories')) ."</h3>
</div>
</div>
</div>
";
}
else {
echo "hello";
}
If I was you, I'd keep the HTML as plain text, not a PHP string :
<?php if(condition) : ?>
// html
<?php elseif(condition) : ?>
// html
<?php endif; ?>
It makes it way easier to read IMO.