Variable inside a php echo function - php

I have a php function which displays a rating bar with the arguments. I have a variable called itemID inside my php page which holds the unique item number. I need to send this value to my function and also echo command must stay. Is there a way to achieve this?
Here is the code, which does not work. When I try it on the server, it does not show the id of item, it prints the variable name as it is.
<?php echo rating_bar('$as',5) ?>
What I get at html file:
<div id="unit_long$as">
instead of the item id in place of $as.

Single Quotes do not support variable replace,
$as = "test";
echo '$as'; //$as in your end result
echo "$as"; // test in your end result
echo $as; // test in your end result
//For proper use
echo " ".$as." "; // test in your end result
Update for newer PHP versions you should now use Template Syntax
echo "{$as}"

If I get what you are saying, this is what you are asking.
<?php echo rating_bar($itemID,5); ?>
With the limited code you are providing, thats what looks like you are asking.

Related

Show only column value; not column name in Yii

I'm trying to modify a script written in php, js with Yii. I simply need the value that I'm outputting to show up as a number without the column header. In this case, the number would be 20.
I'm using formulas.php to get the value that I need:
public function getMarkup()
{
$mid=$this->getMerchantID();
$DbExt=new DbExt;
$stmt="SELECT markup FROM
{{merchant}}
WHERE
merchant_id='$mid'
LIMIT 0,1
";
if ( $res=$DbExt->rst($stmt)){
return $res[0];
}
return false;
}
Then I'm calling the value on another page as:
<?php
$anArray2=Yii::app()->functions->getMarkup();
echo json_encode($anArray2);
?>
This returns:
{"markup":"20"}
I tried changing it to:
<?php
echo Yii::app()->functions->getMarkup();
?>
And that returns:
Array
I'm trying to get it to return with just 20 (the value of the column) without the column header.
<?php
echo Yii::app()->functions->getMarkup()["markup"];
?>
Is that what you're looking for?

Call a value customer page

I have design a webpage to and make all necessary code in admin section. How do I call this value to show on the customer page in the number format?
I am trying to show the amount in double eval. I mean $750,100.15.
When a customer logs in the value will be shown on their page. I used the following code it work.
<?php echo $_SESSION['hlmgt_user']['amount']; ?>
This prints out $750100.15, but I need it to print $750,100.15. I am using the above code to call the value on customer page.
How to fix the below code to get it working?
<?php echo $_SESSION['hlmgt_user']number_format($row['balance'], 2); ?>
try this maybe helpful
<?php
$number = "750100.15";
setlocale(LC_MONETARY, 'en_US');
echo money_format('%(#10n', $number) . "\n";
?>
OUTPUT
$ 750,100.15
I think you should look about the "number_format" or "money_format" function in PHP here:
https://php.net/number_format
https://php.net/money_format

PHP variable Syntax using _GET

I have a redirectURL that currently redirects to a default page and works great
$WA_redirectURL = "mypage.php";
I want to insert a _GET value that is available but am getting a syntax error. Here is what I am trying. I thought the periods would allow me to get the value into the URL?
$WA_redirectURL = "mypage.php?EmpNumber=". echo $_GET['EmpNumber'] .";
You don't use echo when concatenating strings:
$WA_redirectURL = "mypage.php?EmpNumber=". echo $_GET['EmpNumber'] .";
should be:
$WA_redirectURL = "mypage.php?EmpNumber=". $_GET['EmpNumber'];
(The last quote is also an error).
You use echo to send text to the user.
If you want to join strings together use the period to join things.
For example:
echo "hello"; // this will print hello to the screen
$testing = "hello "."world!";
echo $testing; // this will print hello world! to the screen.

HREF to call a PHP function and pass a variable?

Is it possible to create an HREF link that calls a PHP function and passes a variable along with it?
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='search($id)' >$name</a>";
}
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
You can do this easily without using a framework. By default, anything that comes after a ? in a URL is a GET variable.
So for example, www.google.com/search.html?term=blah
Would go to www.google.com/search.html, and would pass the GET variable "term" with the value "blah".
Multiple variables can be separated with a &
So for example, www.google.com/search.html?term=blah&term2=cool
The GET method is independent of PHP, and is part of the HTTP specification.
PHP handles GET requests easily by automatically creating the superglobal variable $_GET[], where each array index is a GET variable name and the value of the array index is the value of the variable.
Here is some demo code to show how this works:
<?php
//check if the get variable exists
if (isset($_GET['search']))
{
search($_GET['search']);
}
function Search($res)
{
//real search code goes here
echo $res;
}
?>
Search
which will print out 15 because it is the value of search and my search dummy function just prints out any result it gets
The HTML output needs to look like
anchor text
Your function will need to output this information within that format.
No, you cannot do it directly. You can only link to a URL.
In this case, you can pass the function name and parameter in the query string and then handle it in PHP as shown below:
print "<a href='yourphpscript.php?fn=search&id=$id' >$name</a>";
And, in the PHP code :
if ($_GET['fn'] == "search")
if (!empty($_GET['id']))
search($id);
Make sure that you sanitize the GET parameters.
No, at least not directly.
You can link to a URL
You can include data in the query string of that URL (<a href="myProgram.php?foo=bar">)
That URL can be handled by a PHP program
That PHP program can call a function as the only thing it does
You can pass data from $_GET['foo'] to that function
Yes, you can do it. Example:
From your view:
<p>Edit
Where 1 is a parameter you want to send. It can be a data taken from an object too.
From your controller:
function test($id){
#code...
}
Simply do this
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='?search=" . $id . "' > " . $name . "</a>";
}
}
if (isset($_REQUEST['search'])) {
search($_REQUEST['search']);
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
Also make sure that your $json_output is accessible with is the sample() function. You can do it either way
<?php
function sample(){
global $json_output;
// rest of the code
}
?>
or
<?php
function sample($json_output){
// rest of the code
}
?>
Set query string in your link's href with the value and access it with $_GET or $_REQUEST
<?php
if ( isset($_REQUEST['search']) ) {
search( $_REQUEST['search'] );
}
function Search($res) {
// search here
}
echo "<a href='?search='" . $id . "'>" . $name . "</a>";
?>
Yes, this is possible, but you need an MVC type structure, and .htaccess URL rewriting turned on as well.
Here's some reading material to get you started in understanding what MVC is all about.
http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
And if you want to choose a sweet framework, instead of reinventing the MVC wheel, I highly suggest, LARAVEL 4

show hide div based on mysql table row value

hi guys im trying to show and hide div according to mysql value but i couldnt do it can you help me what im doing wrong
here is my code thanks a lot for your ideas
var Value = <?php echo json_encode($valuek) ?>;
if (Value==1){
$('#show_hide').show();
}
else{
$('#show_hide').hide();
}
<?php
$valuek = $session->userinfo['vcc'];
?>
<div id="show_hide">
some code
</div>
<?php echo json_encode($valuek) ?>
will return a json string, instead try just using "echo"
<?php echo $valuek ?>
If all you are going for is a boolean value then there is simply no need for JSON.
Echo the value directly into the JavaScript. Remember to ensure you are passing a valid boolean value.
PHP code -
<?php
$showDiv = ($dbValue == 1? 'true' : 'false');
?>
JavaScript + PHP injection -
<script>
var value = '<?php echo $showDiv; ?>';
<script>
Don't forget to wrap the PHP injected value with quotes.
$valuek = $session->userinfo['vcc'];
I'm not sure if you have the code in this order in your php file, or just showed pieces of code in this order, but Should go BEFORE your js code. It has no value when js code is run.
To see what $valuek is, just echo it on top of the screen
<?php echo "<h1>$valuek</h1>" ?>.
Or just look at the source - at your js function, to see what is printed after 'var Value ='
That's the main thing really - to make sure that you getting what you expect from session.
And as been said, you don't need jason_encode, but you do need a semi-colon after echo command.
Also, I hope your jquery code is within $(document).ready function, not as is.

Categories