How do i multiply variables under echo quotations - php

I'm using echo to print a database info using html tags. So the entire statement is under quotation marks " ".
The user is buying some things so i have their price (for example in $price variable, really doesn't matter) and their number (in $number variable) and i'm trying to print out the total price of the bought item. It just prints out $price*$number expectedly, but i'm not sure what to try to get it to work the way i want it to.
I'm sure its rather simple i just can't think of a solution right now and its really bugging me :D

You should use string concatenation:
echo "text text " . ($number * $price) . " text text"

Related

Add a hyperlink to each entry of a table from mysqli

I'm trying to add a direct link to the genomic region of each gene on a table generated with mysqli data, but can't figure out the way. The idea is that every gene name has a hyperlink to it's region on a genome browser.
The problem comes when I have to generate the link dynamically for each gene depending on the gene selected by the user.
I've tried this:
echo '<td><a href="http://genome.ucsc.edu/cgi-bin/hgTracks?"'.urlencode($genome.$row['name2'])'>'$row['name2']'</a></td>';
$genome is the par of the url specific for each species and assembly, and $row['name2'] is the name of each gene.
I complete my previous comment with some advice - maybe this is the answer to your question.
1. How to use echo
You should separate each part of the echo function by a separator.
The common separator is the coma ,. Of course, you can also concatenate with a dot .
echo 'a', 'b', 'c', $var, 'con'ca' . 'tenated';
Tips: use the coma only for echo instruction. It's faster :)
2. Issues on your code
If I take your generated output, you should have something like this - with **cho* corrections:
<td><a href="http://genome.ucsc.edu/cgi-bin/hgTracks?"%20gen%20The+name>The name</a><td>
As you can see, the link is http://genome.ucsc.edu/cgi-bin/hgTracks?. The content after the " is ignored.
Solution: Move to the dynamic part of the link, at the correct place :)
You had your quote double quote in wrong location if there are additional problems will need more info
// Yours
echo '<td><a href="http://genome.ucsc.edu/cgi-bin/hgTracks?"'.urlencode($genome.$row['name2'])'>'$row['name2']'</a></td>'
// Fixed Quote
echo '<td>'$row['name2']'</td>';

How do i fix this dynamic query parameter in PHP?

I'm trying to get a URL to have a dynamic parameter based on the value of a variable.
Here's what Im doing
echo "The file name is ".$value.''.$value.''."<br>";
However, when I click on the corresponding link I get the following URL in the browser
http://localhost/HelloWorld/filespecificpage.php?filename=<?php echo $value ?> rather than the actual value in $value. Any help on how I can fix this would be appreciated.
PHP Noob here, appreciate the patience.
You're already in PHP script mode, you don't need <?php. You just need to concatenate the variable, like you did earlier in the line. You should also use urlencode() when substituting a variable into a URL parameter.
echo "The file name is ".$value.''.$value.''."<br>";
If you break the PHP String/Echo dont use the doublequotes, it makes your live much harder as you need ist.
For your question
echo 'The file name is ' . $value . '<a href="filespecificpage.php?filename=' . urlencode($value) . '>' . $value . '</a><br>';
An expample why single and not doublequotes. If you write an Hyperlink in HTML you use
Link description
and all is fine.
If you do it in an PHP echo with doublequotes you must escape all quotes.
echo "Link description";
Perfect look and way
echo '<a href="website" target="_blank" ' . $anyPHPvar . '>Link description</a>';
and you can use " for clear html and ' for PHP ;)
So, use singlequotes makes your life easyer and its a little bit faster and welcome to PHP ;)

Would this be correct in PHP?

I have created a form, a text box and a button. When I enter text into the text-box and click the button the text I entered is supposed to show up beneath the button which it does. My question would be how do you get the text when entered and outputted to also show up the amount of characters that the text consists of? I've looked on PHP.net that you can use strlen since that counts the amount of characters but for some reason my code wont work with this. I'm not looking for the answer I'm just looking for helpful advice of the next step I should take because I'm confused on what I'm doing wrong. Thanks for the help everyone.
$count = strlen('');
echo "The word has " . $count . " characters.<br />";
You count an empty string here: $count = strlen('');
Instead, you need the user input from your form. Show the code of the form.
Something like
$count = strlen($_POST['input_name']);
should work than.
Besides, updating live, while typing, is only possible with client side Javascript.
lets say:
$text = $_POST["q"];
$count = strlen($text);
echo "The word has " . $count . " characters.<br />";

PHP code inside echo

I have following code which displays the price in Magento.
<?php echo $this->getPriceHtml($_product); ?>
I have to put this code inside echo instead and I can't get it to work.
I am trying
echo "$this->getPriceHtml ($_product)";
It just displays () on the page.
I tried other combinations and I can't come up with anything else.
What am I doing wrong?
Using single quotes will prevent $vars to be interpreted:
echo '$this->getPriceHtml ($_product)';
Or escape the $ sign:
echo "\$this->getPriceHtml (\$_product)";
http://php.net/manual/en/language.types.string.php
Or if by echo-ing you mean that you want to get something like
The price is 123.00
then do:
echo "The price is {$this->getPriceHtml($_product)}";
or even :
echo sprintf("The price is %s", $this->getPriceHtml($_product));
why not you are using this ?
$_product->getFinalPrice() or
if you want in formatted order then why you are not using this
number_format($pr->getPrice(), 2)
and if you want price with currency format then you can use this also
Mage::helper('core')->currency($pr->getPrice());
Hope it will help you.
:)

How to echo randomly a portion of a string?

i have already succesfully translated some quotes via my translation function __(); and now I want to echo only one of those quotes at random. All quotes are separated in this string with a special character like a |
Sofar I only have this. What code could should go below this tackle my random echo?
$quotes =
__("IF YOU MAKE EVERYTHING BOLD, NOTHING IS BOLD") . "|" .
__("Quality of design is an indicator of credibility") . "|" .
__("People ignore design, that ignores people");
(An important restriction: it is essential that the quotes be exactly closed with __(" and "); sothat they can be checked and translated.) __($variable) doest not work with current clean up scripts that I have bought so these won't work.
You're already calling __() on each of your quotes individually, why not save all the extra translating and do something like:
$quotes = array('quote1', 'quote2', 'quote3');
$index = array_rand($quotes);
echo __($quotes[$index]);
Edit: To satisfy your other requirement, that the call to __() must immediately surround each string, you could do this:
$quotes = array(__('quote1'), __('quote2'), __('quote3'));
$index = array_rand($quotes);
echo $quotes[$index];
The big downside here is that you're now looking up a translation for every string in that array, even though only one is printed, but that's the same situation you had in the "one big string" solution.
Why don't you keep them in an array and translate only what is actually outputted?
$quotes = array(
"IF YOU MAKE EVERYTHING BOLD, NOTHING IS BOLD",
"Quality of design is an indicator of credibility",
"People ignore design, that ignores people",
);
$randomQuote = $quotes[ rand(0, count($quotes)-1)];
echo __($randomQuote);
Why the biscuits are they all in one string, and not an array? Your problem would be immediately solved if this was the case. As stands, split in | and index randomly into the array created to pick a random quote.

Categories