As the title reads I am using the echo function to create an h3 string which will insert a php value $lowprice and $highprice. The goal is to have the text read
Here are all the houses whos prices are between $lowprice and $highprice. The code breaks into individual lines like this
Here are all the houses whose prices are between $
100000
and $
500000
:
This is the code I have written, how do I get it all onto one line.
<?php
echo '<caption>';
echo '<h3> Here are all the houses whose prices are between $ </h3>'.$lowprice.'<h3> and $</h3>'.$highprice.'<h3> : </h3>';
echo '</caption>';
?>
<h3> is a block element, meaning it will take up a whole line. I think you want to replace your inner <h3>'s with <span> tags which are inline elements:
Like this:
<?php
echo '<caption>';
echo '<h3> Here are all the houses whose prices are between $ <span>'.$lowprice.'</span>
and $<span>'.$highprice.'</span></h3>';
echo '</caption>';
?>
Or you can simply remove all the inner tags all together, like this:
<?php
echo '<caption>';
echo '<h3> Here are all the houses whose prices are between $'.$lowprice.' and $'.$highprice.'</h3>';
echo '</caption>';
?>
The line breaks appear because you've made several h3 elements. You're closing and reopening h3 tags at every insertion, which is not necessary. The html output of your code is the following:
<h3>Here are all the houses whose prices are between $</h3>
<h3>100000 and $</h3>
<h3>500000 : </h3>
Which automatically adds breaks, since that is the behaviour of h3 elements.
What you need is this:
echo '<h3> Here are all the houses whose prices are between $'.$lowprice.' and $'.$highprice.':</h3>';
Better yet, don't use echo to define your html; html and php are interchangeable within the same file. A cleaner, more readable and more easily maintainable solution would be to form your script like this:
<caption>
<h3>Here are all the houses whose prices are between $<?= $lowprice ?> and $<?= $highprice ?>:</h3>
</caption>
Generally, you switch between php and html like this:
<?php
...do some php
?>
<somehtml></somehtml>
<?php do some more php ?>
<morehtml>...
Note that <?= is a shorthand for <?php echo.
Related
So I have a html table that is automatically generated after passing a query to my database. I want to create a hyperlink within my html table to a page that will pull more detailed information from a Second Table.
I was thinking of using the Tablecell creator that pulls from the First Table, and modifying so that it would encompass the table's contents with hyperlink tags. I was thinking it would look like this.
foreach(new TableRow(new AutoArrayMaker($stmt->fetchAll()) as $rowend => $row){
echo <a href = "the reusable HTML Page">;
echo $row;
echo </a>;
}
Is my idea sound from a coding standpoint?
Firstly, echo's need to be in quotation marks " So that code wouldn't fire.
There are a few ways you can output HTML. The first is using echo's:
echo "Google";
Notice how I put a back-slash before hand? This is what is known as an escape. This puts the character after into a letter depending on what it escapes to. See php docs: http://php.net/manual/en/regexp.reference.escape.php (as my description of it was poor)
The other option would be to run out of php then join back on so to speak:
<?PHP
foreach(new TableRow(new AutoArrayMaker($stmt->fetchAll()) as $rowend => $row){
?>
<a href="abc">
<?PHP echo $row; ?>
</a>
<?PHP
}
However, this is not advised.
Edit:
Also, you can make your own table very simply:
<table>
<?PHP
foreach($stmt as $row){
?>
<tr>
<td>
<a href="abc"><?PHP echo $row[id]; ?>
</td>
</tr>
<?PHP
}
?>
</table>
See https://www.w3schools.com/html/html_tables.asp for more info.
I have a list of items that a user can select from when listing their house. I then display these on the frontend using the following:
<? if($group[property_amenities] != "") { ?>
<hr>
<h2 class="vmargin">Amenities</h2>
<?php echo $group[property_amenities]; ?>
<? } ?>
The issues is that I believe I don't have access to change the HTML and the items are listed as follows:
Amenities
Open Plan,Carpeted Floors,
How can I modify the above code to display the items in separate rows, as well as remove the "," or replace the "," with something like " - "
Use str_replace to replace all the commas with break tags, or dashes, or whatever you want.
<?php echo str_replace(',', '<br>', $group[property_amenities];) ?>
I have a product called customized gift box with different sizes like 5,9,12 etc.
When the users select items to the gift box and add to cart, I am fetching those chocolate names too with the Quantity of the chocolate.
In the code below, I have written if the product name includes customized then fetch the related chocolate using session in a for loop.
And the For loop is working properly but, sometimes it doesn't.
How can this be resolved?
<?php
if(stristr($this->getProductUrl(),"customized")){
?>
<div>
<?php
echo "<br/>";
$itid = $_item->getId();
echo $itid;
$strrep = str_replace(' ','_',$this->htmlEscape($this->getProductName()));
for($k=1;$k<=sizeof($_SESSION[$strrep."item".$itid]);$k++){
if($_SESSION[$strrep."item".$itid][$k]!=""){
echo " <font size='1px'>".$_SESSION[$strrep."qty".$itid][$k]." x ".$_SESSION[$strrep."item".$itid][$k]."</font><br/>";
}
}
?>
</div>
Your for loop starts with 1 and finishes with the sizeof's element. Array indices are zero based in PHP so you should do
for ($k=0;$k<count($_SESSION[$strrep."item".$itid]);$k++){
...
}
instead. I also replaced sizeof() by count() but that is a matter of preference ...
I am having trouble creating a solution that will target the end row of a MySQL query. Currently I have a foreach function that works through the query and displays each one as a div with the information inside:
<?php $residents = Resident::find_all();
foreach($residents as $resident): ?>
<div class="submenu">
<p class="menuitem submenuheader"><?php echo $resident->name; ?></p>
<img src="images/<?php echo $resident->image_path(); ?>" width="250" class="image" />
<p><?php echo $resident->info; ?></p>
</div>
.submenu currently has a bottom border. I need to remove this on the last row returned. I have looked at DESC LIMIT 1, however this requires another MySQL query and could make things very messy...
Addd this to your CSS:
.submenu:last-child { border-bottom: 0; }
Note: this is not supported by IE < 9.
You could switch to putting the border on the top of the element, and use the :first-child pseudo selector in CSS to remove it.
http://reference.sitepoint.com/css/pseudoclass-firstchild
The :last-child selector would be nice, but it's not supported in IE before version 9, so it's not a good idea to use it if you want compatibility.
If you separate your HTML and PHP a little this is easily achieved:
<?php
function echoBlock($resident,$pClass="menuitem submenuheader") {
echo "<div class=\"submenu\">\n<p class=\"$pClass\">\n";
echo $resident->name;
echo "</p>\n<img src=\"images/";
echo $resident->image_path();
echo "\" width=\"250\" class=\"image\" />\n<p>";
echo $resident->info;
echo "</p>\n</div>\n\n";
}
$residents = Resident::find_all();
$last=count($residents)-1;//2 element array last pos is 1
for ($i=0;$i<$last;$i++) {
echoBlock($residents[$i]);
}
echoBlock($residents[$last],"menuitem");
?>
echoBlock (which could easily be a method on a class) requires the calling code to know about the classes it uses, which isn't really separating intent but it does prevent the need for an if branch on every loop. That being said it would be less efficient but perhaps more usable to set it up as:
function echoBlock($resident,$isLast=false) {
$pClass="menuitem".($isLast?"":" submenuheader");
//...
Which then doesn't need the caller to know anything about what echoBlock does.
You could try and pop the array using array_pop(), to get the last value out of the array and then inputing it using the special class after the foreach loop.
What about
Instead of echo'ing each line one by one, create one big string in PHP, search for the last entry of "submenu" and change the class.
I have an array of user ids and I need to go through the a user: id field within a view, adding a bit of specific HTML for matching and non-matching results.
So I'm guessing something like this needs to go in views-view-field--uid.tpl.php:
<?php if (//Field content matches an array value): ?>
<span class="friend"><?php print $output; ?></span>
<?php endif; ?>
<?php if (//Field content doesn't match an array value): ?>
<span class="not-friend"><?php print $output; ?></span>
<?php endif; ?>
Can someone help me fill in the gaps, please? :)
Assuming $ouput is going to be only an integer representing the uid (and not HTML markup), you could do something like this:
<span class="<?php if(!in_array($output, $your_array)): ?>not-<?php endif; ?>friend">
<?php print $output; ?>
</span>
see php in_array()
However, $output might be HTML. If that's the case you should use $row instead of $output.
To see what $row contains I love doing this in template files:
<!-- <?php echo print_r($row,true); ?> -->
(then view source in browser)
Also, I'd recommend not doing this at all in your template file because it ties logic to the theme... Check out Views Customfield -- it will let you do PHP in a custom field... If you put it under the UID, and exclude UID from display, you can access the UID and execute the same code I've got above in the custom field, using the $data object instead of $row or $output.