Print string with a php variable in it - php

For you guys, I imagine this will be easy.
<div class="vote_pct" style="width: $widthpx;">
I want the variable to be $width and for it to have px on the end.
If i put a space, it doesnt work.
If i put them together, it treats it as one big variable name.
Could I ask someone the correct snytax to achieve this?
Thanks

$bla = '<div class="vote_pct" style="width: '.$width.'px;">';
or
$bla = "<div class=\"vote_pct\" style=\"width: ${width}px;\">";

If you mix PHP and HTML you can do:
//PHP in HTML
<div class="vote_pct" style="width: <?php echo $width; ?>px;">
HTML in PHP
print '<div class="vote_pct" style="width: ' . $width . 'px;">';

echo '<div class="vote_pct" style="width: '.$width.'px;">';
or
$width = 5;
echo "<div class=\"vote_pct\" style=\"width: {$width}px;\">";

<?php echo sprintf('<div class="%s" style="width: %spx">','vote_pct',$width);?>

Related

Dynamically increment width of css element in a for loop

EDIT: i found out, itsin my other answer below.
I want to present a graph in php and can create the columns, but when i try to increment the percentage to correspond with the css width of the .inner class it wont increment for each element in he array. How can I increment the width for each element in the array?
$percentage = array(0.2,0.4,0.5,0.6,0.7,0.8);
for($i=0;$i<6;$i++){
echo "<div class='outter'>
<div class='inner'>"; echo $percentage[$i]; echo "
</div>
</div>";
echo "<style type='text/css'>";
echo ".outter{height:25px;width:500%;border-right:solid 1px #000;}";
echo ".inner{height:25px;width:";echo $percentage[i]; echo "%;border-right:solid 1px #000;background-color:#02abff;}";
echo "</style>";
}
i can do it in html and css but i want to do it php. Thanks.
Not sure if you have noticed, but the actual values you have there are 0.X%, and this is not exactly a valid width. You might want to multiply that value by 10 (or by 100) to get to 2% (or 20%).
$percentage = array(0.2,0.4,0.5,0.6,0.7,0.8);
foreach ($percentage as $key => $val) {
echo "<div class='outter'>
<div class='inner'>";
echo $val;
echo "</div>
</div>";
echo "<style type='text/css'>";
echo ".outter{height:25px;width:500%;border-right:solid 1px #000;}";
echo ".inner{height:25px;width:";
echo $val * 10;
echo "%;border-right:solid 1px #000;background-color:#02abff;}";
echo "</style>";
}
I also changed the for loop that you had there to foreach loop, which makes more sense when working with arrays.
I found out. I had to put the styling inside the html tag:
foreach($percentage as $percent){
echo "<div class='outter' style='height:25px;width:500%;border-right:solid 1px #000;'>
<div class='inner' style='height:25px;width:"; echo $percent; echo "%;border-right:solid 1px #000;background-color:#02abff;'>"; echo $percent; echo "
</div>
</div>";
}

How do I properly concatenate this PHP echo statement with a string and a function?

//top of the code just consists of an if statement and some code.
case 'itemimage' :
echo '<div class="product-preview-image" style="background: red; height:75px; width:75px;"><img href=' . <?php grab_item_image(); ?> . "</div>"';
break;
}
}
That was my attempt but it does not look rite on my text editor. I am also using inline-css not sure if that matters.
The only thing that i'm trying to echo out is this div with the function providing an href.
<div class="product-preview-image" style="background: red; height:75px; width:75px;"><img href="#thephpfunctionhere"</div>
It's kind of confusing me with all the single and double quotes.
Quotes Problem and you started <?php again. Use the code below
//top of the code just consists of an if statement and some code.
case 'itemimage' :
echo '<div class="product-preview-image" style="background: red; height:75px; width:75px;"><img src="' . grab_item_image() . '" /></div>"';
break;
}
}
Hope this helps you
echo '<div class="product-preview-image" style="background: red; height:75px; width:75px;"><img src="' . <?php grab_item_image(); ?> . '"></div>"';
The image needs the ''SRC'' attribute instead of href. Also, since you are echo-ing within a php-tag you don't have to open the <\?php again but directly refer to the function.
echo
'<div class="product-preview-image" style="background: red; height:75px; width:75px;">' ,
'<img src="' . grab_item_image() . '" />' ,
'</div>"';
Komma's are even faster when using a direct echo. Not suitable for assigning it to a $variable

How to apply spacing between divs using inline-table

Please excuse the excessive use of echo here, as I'm using IP.Content which allows pure PHP or pure html, but not a mix of them both. I'm attempting to get a layout in which I have the following
xxx-------xxxxxxxxxxxxxxxxxxxxxxxxxxx
Where a - resembles free space. I am trying to not use floating variables, so using a table display seems like the proper way to handle this. Here's my simplistic code below, you can see by the two widths what I'm going after, however the table display automatically expands to fill the remaining space.
echo '<div style="display: inline-table; width: 100%;">';
echo '<div id="categories" style="width: 20%; display: table-cell;">';
echo '<div class="box">';
echo 'Hello world2.';
echo '</div>';
echo '</div>';
echo '<div id="content" style="width: 70%; display: table-cell;">';
echo '<div class="box">';
echo 'Hello world.';
echo '</div>';
echo '</div>';
echo '</div>';
You can use borders to space the divs.
Use HTML-Padding.
for more referece : www.w3schools.com/css/css_boxmodel.asp

Php str_replace with &quote not working

my code is:
$retval = preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $content, $image);
$imgreal = str_replace(""", "\"", $image['src']);
if ($retval=="0") $imgthumb = ""; else $imgthumb = "<img align='left' src='".$imgreal."' width=100 />";
I need to extract an img from a string, but this gave me all time:
"http://www.racingzone.hu/pictures/news/mid/simroc-3---nyolc-uj-auto_2012-09-21-1348222495.jpg" alt="" width="490" height="214
How can I change those &quotes to normal chars "?
I tried htmlspecialchars_decode but that not worked to.
edit: the string coming from az sql table.
original string from mysql:
<p><img style="float: left; margin: 3px;" src="http://www.racingzone.hu/pictures/news/mid/simroc-3---nyolc-uj-auto_2012-09-21-1348222495.jpg" alt="" width="490" height="214" /></p>
<p>todik púőúőóüsztotodik púőúőóüsztotodik
string created by TinyMCE
coding in the table: latin2_hungarian_ci
Why does everybody always want to do these kind of things with a regex?
Simply:
$data='<p><img style="float: left; margin: 3px;" src="http://www.racingzone.hu/pictures/news/mid/simroc-3---nyolc-uj-auto_2012-09-21-1348222495.jpg" alt="" width="490" height="214" /></p>
<p>todik púőúőóüsztotodik púőúőóüsztotodik';
$a=explode('src="',$data);
if(count($a)<2)
{
echo 'no image';
die;
}
$p=strpos($a[1],'"',0);
if($p===false){
echo 'no quote found';
die;
}
$url=substr($a[1],0,$p);
echo $url;
Output:
http://www.racingzone.hu/pictures/news/mid/simroc-3---nyolc-uj-auto_2012-09-21-1348222495.jpg

html in php to call image

<?php if ($this->checkPosition('image')) : ?>
<?php
echo "<table class=\"remove-margin-t\" cellpadding=\"0\" cellspacing=\"0\" width=\"97%\" align=\"center\" border=\"0\" style=\"max-width:625px; border:1px solid;\" background=\"..\images";
?>
<?php
echo $this->renderPosition('image')
<?php
echo ".png\">";
?>
<?php endif; ?>
I am trying to figure out how to call the image properly. echo for image is called and has a specific name like 'pink','blue','green' etc. However, it depends on the position part...
This is what it is supposed to look like in html.
<table cellpadding="0" cellspacing="0" width="97%" align="center" border="0" style="max-width:625px; border:1px solid #CCC" background="http://localhost/images/[insert color name here].png" >
Here is the original php
<?php if ($this->checkPosition('color')) : ?>
<?php echo $this->renderPosition('color'); ?>
<?php endif; ?>
Any help would be appreciated. I am sure it must be a '\' or '"' issue.
Best,
Steven
To Jared:
Do you mean like this?
<?php if ($this->checkPosition('image')) : ?>
<?php
echo "<table class=\"remove-margin-t\" cellpadding=\"0\" cellspacing=\"0\" width=\"97%\" align=\"center\" border=\"0\" style=\"max-width:625px; border:1px solid;\" background=\"../images/";
echo $this->renderPosition('image')
echo ".png\">";
?>
<?php endif; ?>
You don't need to open/close PHP tags on every line of PHP code. Your code may be rewritten this way:
<?php
if ($this->checkPosition('image')) {
echo '<table class="remove-margin-t" cellpadding="0" cellspacing="0" width="97%" align="center" border="0" style="max-width:625px; border:1px solid;" background="../images"' . $this->renderPosition('image') . '.png">';
}
?>
I replaced some double quotes with single quotes to avoid using backslashes everywhere.
I concatenated your text so that only one echo is used.
And I fixed a possible mistake at the end of the first echo: I replaced the blackslash by a slash, since directory separators in URLs are slashes.
I don't know what object '$this' is, nor do I know what the method checkPosition does
Also, what output 'renderPosition('color') produces.
either way, this code
<?php if ($this->checkPosition('color')) : ?>
<?php echo $this->renderPosition('color'); ?>
<?php endif; ?>
is improper, and should be written as:
<?php
if ($this->checkPosition('color')) {
echo $this->renderPosition('color');
}
?>
With that said, the server tags "?php" and "?" represent the beginning and end of server code. So outside of those tags is standard html markup, generally.
So, you can use html markup outside of server code as so,
<?php if ($this->checkPosition('color')) { ?>
<div style="width:97%;text-align:center;max-width:625px;border:1px solid #CCC;background-image:url('<?php echo "http://localhost/images/" . $this->renderPosition('color') . ".png"; ?>');display:inline-block;position:relative;">
</div>
<?php } ?>
I turned your table into a div, and used CSSstyleAttributes, instead of depreciated html attributes.
Also, I am also assuming the output of renderPosition is a filename, without the file extension.
EDIT:
localhost refers to your own computer.
You may want to use:
echo "//" . $_SERVER['SERVER_NAME'] . "/images/" . $this->renderPosition('color') . ".png";
in place of
echo "http://localhost/images/" . $this-renderPosition('color') . ".png";

Categories