Tidy up if and elseif statement - php

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.

Related

php loop repetitive execution

what can i change in this code to make the output from This to make it look like This
The two 'This' links are screenshots of the layouts
I want all the boxes to be in-line / next to each other just like the second screenshot.
i programmed the layout in html and it worked when trying php the layout just messed up.
function getTest(){
global $con;
$get_pro = "select * from drinks";
$run_pro = mysqli_query($con, $get_pro);
while($row=mysqli_fetch_array($run_pro)){
$id = $row['id'];
$title = $row['title'];
$cat = $row['cat'];
$image = $row['image'];
$desc = $row['desc'];
$qty = $row['qty'];
$price =$row['price'];
$status= $row['status'];
echo "<!--==========================\n";
echo " Price Menu\n";
echo "============================-->\n";
echo " <!-- Page Content -->\n";
echo " <main class=\"entry-content\">\n";
echo " <section class=\"page-section\">\n";
echo " <div class=\"container\">\n";
echo " \n";
echo " <div class=\"row\">\n";
echo " <div class=\"col-md-12\">\n";
echo " <div class=\"isotope-filters\" role=\"group\">\n";
echo " <div class=\"btn-group\">\n";
// the categories go here
echo " </div>\n";
echo " </div>\n";
echo " </div>\n";
echo " </div>\n";
echo " <br><br>\n";
echo " <div class=\"row isotope-wrapper isotope-beers-wrapper\">\n";
echo " <div class=\"isotope isotope-beers gutter\">\n";
echo " <div class=\"grid-item col-lg-3 col-md-3 col-sm-6 col-ms-6 col-xs-12 $cat\">\n";
echo " <div class=\"grid-wrapper\">\n";
echo " <a href=\"javascript:void(0);\" data-remodal-target=\"bottle-$id\">\n";
echo " <figure style=\"background-image: url('admin/images/drinks/$image')\">\n";
if ($row['status'] === 'Yes'){
echo " <div class=\"offer\"></div>\n";
}
echo " <figcaption class=\"grid-content\">\n";
echo " <h5 class=\"grid-title\"><span>$title</span></h5>\n";
echo " </figcaption>\n";
echo " </figure>\n";
echo " </a>\n";
echo " </div>\n";
echo " </div>\n";
echo " </div>\n";
echo " </div>\n";
echo "\n";
echo " </div>\n";
echo " </section>\n";
echo " </main><!-- /.enry-content -->\n";
echo "\n";
echo " <!-- Off-Page Content -->\n";
echo " <!-- Mobile Navigation (Left Panel) -->\n";
echo " <!-- Preloader -->\n";
echo " <div class=\"introLoading\"></div>\n";
echo " <!-- Back to Top -->\n";
echo " <div>\n";
echo " <a class=\"back-to-top fa fa-chevron-up\" href=\"javacript:void(0);\"></a>\n";
echo " </div>\n";
echo " <!-- Modal Content -->\n";
echo " <div class=\"remodal-bg\">\n";
echo " <!-- Item One -->\n";
echo " <div class=\"remodal modal-beers\" data-remodal-id=\"bottle-$id\">\n";
echo " <button data-remodal-action=\"close\" class=\"remodal-close\"></button>\n";
echo " <div class=\"row\">\n";
echo " <div class=\"col-md-5 col-sm-12 col-xs-12\">\n";
echo " <div class=\"item-modal-image\">\n";
echo " <a class=\"image-lightbox\" href=\"admin/images/drinks/$image\"><img alt=\"\" src=\"admin/images/drinks/$image\" /></a>\n";
echo " </div>\n";
echo " </div>\n";
echo " <div class=\"col-md-7 col-sm-12 col-xs-12\">\n";
echo " <h3>$title</h3>\n";
echo " <p>$desc</p>\n";
echo " <table class=\"table\">\n";
echo " <tbody>\n";
echo " <tr>\n";
echo " <td>Price:</td>\n";
echo " <td>£$price</td>\n";
echo " </tr>\n";
echo " <tr>\n";
echo " <td>Quantity:</td>\n";
echo " <td>$qty</td>\n";
echo " </tr>\n";
echo " </tbody>\n";
echo " </table>\n";
echo " </div>\n";
echo " </div>\n";
echo " </div>\n";
echo " </div> \n";
echo "\n";
echo " <!--==========================\n";
echo " Price Menu end\n";
echo "============================-->\n";
}
}
I see you are using bootstrap, I suggest you to use this kind of structure
Take a look on this page
<div class="container">
<div class="row">
<span class="col-lg-3 col-sm-12 picture">
<img class="img-thumbnail" src="//placehold.it/200x200">
</span>
<span class="col-lg-3 col-sm-12">
<img class="img-thumbnail" src="//placehold.it/200x200">
</span>
<span class="col-lg-3 col-sm-12">
<img class="img-thumbnail" src="//placehold.it/200x200">
</span>
<span class="col-lg-3 col-sm-12">
<img class="img-thumbnail" src="//placehold.it/200x200">
</span>
</div></div>
Take all of your html out of the echos and instead insert PHP into your html. First you need to identify the areas of your html which need to loop, for example if my original html was this:
<div class="row">
<div class="col-xs-12 col-sm-4">
Box 1
</div>
<div class="col-xs-12 col-sm-4">
Box 2
</div>
<div class="col-xs-12 col-sm-4">
Box 3
</div>
</div>
But I wanted to generate the 3 boxes dynamically instead, my PHP would be something like this, which you can wrap in a function:
<?php
function outputStuff(){
$contents = array("Box 1","Box 2","Box3");
?>
<div class="row">
<?php foreach($contents as $value):?>
<div class="col-xs-12 col-sm-4">
<?php echo $value; ?>
</div>
<?php endforeach;?>
</div>
<?php
} // end the function
?>
The row has to be outside the loop otherwise it will be created for each item and clear the floats on the columns. Use these basic principles and you will achieve what you want.

PHP - How to remove comma or image if database field is empty

I am trying to figure out how to remove the comma from, say the
['address_2'] field if this is left empty and also not show the icons for say the ['tel'] phone number if this is not inputted, It seems hard working within the echo.
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)) {
echo "<div class='result-container'>
<div class='result-wrap'>
<div class='leftbox'>
<div class='titlebox'>
<h2>" . $row['company_name'] . "</h2>
<p><strong>" . $row['company_services'] . "</strong></p>
</div>
<div class='box'><strong> Address: </strong>" . $row['address_1'] . ",&nbsp" . $row['address_2'] . ",&nbsp" . $row['town'] . ",&nbsp" . $row['county'] . ",&nbsp" . $row['postcode'] . "</div>
<div class='box'><strong> Telephone: </strong>" . $row['tel'] ."<strong> Mobile: </strong>". $row['mob'] . "</div>
<div class='iconbox'>
<a href='" . $row['tel'] . "'><img src='img/icons/phone.png' class='hvr-push'></a>
<a href='" . $row['mob'] . "'><img src='img/icons/mobile.png' class='hvr-push'></a>
<a href='mailto:" . $row['email'] . "'><img src='img/icons/email.png' class='hvr-push'></a>
<a href='" . $row['web_url'] . "'><img src='img/icons/web.png' class='hvr-push'></a>
</div>
<div class='buttonwrap'><div class='buttonbox'><p>View Profile</p></div></div>
</div>
<div class='rightbox'>
<div class='service-pic'>
<img src='img/image1.png'>
</div>
<div class='mapbox'>
" . $row['gmap_url'] . "
</div>
</div>
</div>
<br/>";
}
}
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)) {
?>
<div class='result-container'>
<div class='result-wrap'>
<div class='leftbox'>
<div class='titlebox'>
<h2><?=$row['company_name']?></h2>
<p><strong><?=$row['company_services']?></strong></p>
</div>
<div class='box'><strong> Address: </strong><?=$row['address_1']?>,
<?php
if(!empty($row['address_2'])){
?>
&nbsp <?=$row['address_2']?>,
<?php
}
?>
&nbsp <?=$row['town']?>,&nbsp <?=$row['county']?>,&nbsp <?=$row['post code']?></div>
<div class='box'><strong> Telephone: </strong><?=$row['tel']?><strong> Mobile: </strong><?=$row['mob']?></div><div class='iconbox'>
<a href='
<?=$row['tel']?>'><img src='img/icons/phone.png' class='hvr-push'></a>
<a href='
<?=$row['mob']?>'><img src='img/icons/mobile.png' class='hvr-push'></a>
<a href='mailto:"
<?=$row['email']?>'><img src='img/icons/email.png' class='hvr-push'></a>
<a href='"
<?=$row['web_url']?>'><img src='img/icons/web.png' class='hvr-push'></a>
</div>
<div class='buttonwrap'><div class='buttonbox'><p>View Profile</p></div></div>
</div>
<div class='rightbox'>
<div class='service-pic'>
<img src='img/image1.png'>
</div>
<div class='mapbox'>
<?=$row['map_url']?>
</div>
</div>
You can use Mysql, see it here: http://php.net/manual/es/book.mysqli.php
And a final tip: 'Do not enter HTML into a echo'

PHP get array value and save it to be displayed in another page

I have a page displaying data from an array. When the user click on one of the picture displayed, I would need to save the value $row["Rif"] as I'd need to display the item details in another page.
I was looking around but it seems that Jquery, Ajax are the only available solutions but I don't know these.
Is there any way to implement it using just PHP?
Thank you!
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<div class='col-md-3 col-sm-6 col-xs-12'>
<div class='property-container'>
<div class='property-image'>
<img src='img/img02.jpg' alt='test theme'>
<div class='property-price'>
" . $row["DescCom"] . "<br>
<span>" . "€ ". $row["Pre"] . " </span>
</div>
<div class='property-status'>
<span>" . $row["Rif"] . "</span>
</div>
</div>
<div class='property-features'>
<span><i class='fa fa-home'></i> " . $row["Mq"] . " m<sup>2</sup></span>
<span><i class='fa fa-hdd-o'></i> " . $row["Cam"] . " Cam</span>
<span><i class='fa fa-male'></i> Piano " . $row["Pia"] . " </span>
</div>
<div class='property-content'>
<h3>" . $row["TIP"] . "<small>" . $row["Fra"] . "</small></h3>
<button type='submit' name='submit' class='btn btn-default btn-warning btn-xs pull-right btn-dettagli'>Details</button>
</div>
</div>
</div>";
}
} else {
echo '0 results';
}
$conn->close();
?>
Assuming the data is stored in $row["Rif"]
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<div class='col-md-3 col-sm-6 col-xs-12'>
<div class='property-container'>
<div class='property-image'>
// if this is the image you can hyper-link it to next page that will carry the id as well.
<img src='img/img02.jpg' alt='test theme'>
<div class='property-price'>
" . $row["DescCom"] . "<br>
<span>" . "€ ". $row["Pre"] . " </span>
</div>
<div class='property-status'>
<span>" . $row["Rif"] . "</span>
</div>
</div>
<div class='property-features'>
<span><i class='fa fa-home'></i> " . $row["Mq"] . " m<sup>2</sup></span>
<span><i class='fa fa-hdd-o'></i> " . $row["Cam"] . " Cam</span>
<span><i class='fa fa-male'></i> Piano " . $row["Pia"] . " </span>
</div>
<div class='property-content'>
<h3>" . $row["TIP"] . "<small>" . $row["Fra"] . "</small></h3>
<button type='submit' name='submit' class='btn btn-default btn-warning btn-xs pull-right btn-dettagli'>Details</button>
</div>
</div>
</div>";
}
} else {
echo '0 results';
}
$conn->close();
?>
if this is the image you can hyper-link it to next page that will carry the id as well.
<img src='img/img02.jpg' alt='test theme'>

Div not looping correctly

I'm trying to get my code to display multiple divs. But seems to only display one div correctly.
<?php
// database connection
//$result = mysqli_query($con,"SELECT * FROM test WHERE field5='Cohen'");
echo " <div id='1'>";
while($row = mysqli_fetch_array($result))
{
echo "<div class='tutorName'>" . $row['field1'] . "</div>";
echo "<div class='tutorPrice'>" . $row['field2'] . "</div>";
echo "<div class='tutorInstitution'>" . $row['field3'] . "</div>";
echo "<div class='tutorLocale'>" . $row['field4'] . "</div>";
echo "<div class='tutorPhone'>" . $row['field5'] . "</div>";
}
echo "</div>";
mysqli_close($con);
?>
I want the following to loop and over using the fields above (field1, feild2, etc):
<div id='' name='' class="column threecol">
<div class="course-preview premium-course">
<div class="course-image">
<img src="img.png" />
<div class="course-price">
<div class="corner-wrap">
<div class="corner"></div>
<div class="corner-background"></div>
</div>
<div class="price-text"><span class="amount">PerHour</span></div>
</div>
</div>
<div class="course-meta">
<header class="course-header">
<h5 class="nomargin">TutorName </h5>
<div class='gender'>Gender: </div>
<div class='price-range'>Price Range: </div>
<div class='institute'> Institute: </div>
</header>
</div>
</div>
</div>
Is your $result line really commented out in the real code? Or did you just add that comment for the example? If it's commented-out in the code you're testing, then that's probably the issue.
you want this? , 1 div wrap all content like table tr
$i=1;
while($row = mysqli_fetch_array($result))
{
echo " <div id='{$i}'>";
echo "<div class='tutorName'>" . $row['field1'] . "</div>";
echo "<div class='tutorPrice'>" . $row['field2'] . "</div>";
echo "<div class='tutorInstitution'>" . $row['field3'] . "</div>";
echo "<div class='tutorLocale'>" . $row['field4'] . "</div>";
echo "<div class='tutorPhone'>" . $row['field5'] . "</div>";
echo "</div>";
$i++;
}

Syntax help with echo html and variables

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
}
?>

Categories