Truncating magento text - php

I am printing the customers email address in the header like so:
<?php echo $email=$this->__('Hello, %s', Mage::getSingleton('customer/session')->getCustomer()->getEmail()); ?>
However I want to truncate this to a certain number due to users with long email addresses.
I have tried to use the truncate helper which magento has but I am getting no joy, wondering if anyone can help.
<?php echo $email=$this->__('Hello, %s', Mage::getSingleton('customer/session')->getCustomer()->getEmail()->truncate('text', 12)); ?>
Thanks

Ok if that ok then you are doing in wrong way.For truncate using Magento you should try like this :
<?php echo Mage::helper('core/string')->truncate('abacdkdslsdfkjdfss#yahoo.com', 12); ?>
AS for you email concerned you can try like this : User proper email getter function .you can assign that to any variable instead of echo
<?php echo Mage::helper('core/string')->truncate(Mage::getSingleton('customer/session')->getCustomer()->getEmail(), 12); ?>
OUTPUT : abacdkdslsdf
First parameter will be your string and second will be length of that text.Hope this will help you

Related

PHP if/then statement

Need help with some php to modify a WP plugin which is paid memberships pro. Terrible at php here.
What am trying to do is create one line of code that would say if the membership level equals XXX then print this link. SO the variable I would need are somewhere in this line I imagine:
<li><strong><?php _e('Membership Level', 'paid-memberships-pro' );?>:
</strong> <?php echo $current_user->membership_level->name?></li>
The above is just a snippet of code already found in the page I want to create this if/then link statement.
so something like:
<?php if($Membership Level == $Conflicts of Interest #14124(that's the name
of one level) then print this link.
AM I making sense?
Edit:
Thanks to some help below, this seems to work:
<?php if($membership_level == 'Conflicts of Interest #14124') {
echo "<a href=\"conflicts-of-interest-in-modern-legal-practice-and-internal-
investigations-14124/\">Testing</a>";
}
?>
But the 'Conflicts of Interest #14124' doesn't match even though it is the correct name.
Then general If else statement in the html page
<?php if($membership_level == 'string to be compared') {
echo 'the link that you want to print if it is a string. you can replace this string
with a variable if the value is in variable'
} else {
'any other text if require else you can remove the else block'
}
?>
Hope that helps.

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

Using PHP code in the value of a variable

I am trying to send an email in PHP where the content of the email has some conditional checks and some database query lookups.
What I would like to acheive is having my email code as a variable (similar to below) so that I can sent mail() the content to the relevant people.
$emailContent = "<p>My email content</p>";
However the value of this variable would have some code like this:
<table>
<?php
$get_course_units = "SELECT * FROM course_units where course_units.course_code = {$courseCodeExtract}";
$course_units_results = $conn->query($get_course_units);
if ($course_units_results->num_rows > 0) {
while ($courseUnits = $course_units_results->fetch_assoc()) {
?>
<tr>
<td><?php echo $courseUnits["unit_code"]; ?> – <?php echo $courseUnits["unit_name"]; ?> </td>
</tr>
<?php
} //end loop for course units
} //end if for course units
?>
</table>
How should I continue?
Split up your script into an html template file and your php logic.
Use shortcodes in your templates where you want to have custom information and then use str_replace to replace that content with the actual values.
$template_string = file_get_contents('myfile.html');
$shortcodes = array("{{FNAME}}","{{LNAME}}","{{OTHER_STUFF}}");
for(/* all the people you want to mail */){
$custom_info = get_custom_info(/* person */); //eg returns assoc array
$result = $template_string;
foreach($shortcodes as $code){
$result = str_replace($code, $custom_info[$code], $result);
}
//do what you want with result and mail it
}
In the example above, get_custom_info would be returning an associative array with the same values as the shortcodes array, just for convenience.
Now anywhere I put {{FNAME}} in my html, it will be replaced with the value I get back from the custom info function.
You can easily extend this to scrape the template and look for {{ and }} (or whatever shortcode syntax you want) anddetermine what variables you will need from your custom info, shaping the query to only give you what you actually need.
Not sure if this is the best way, but it seems to work pretty well. (also best way is subjective, so might want to ask questions a little differently)

MySQL value embedded within HTML and PHP

I am trying to open a page and send $username (which I got from MySQL) through a URL parameter. The value is not sent to AddPage.php as it is embedded in the PHP/HTML. I think there is something wrong with the syntax but i could not figure it out.
The following is the code of the hyperlink:
<?php
echo"<h2 > Please try to <a href='AddPage.php?id=" . $username . "'>Add</a> again</h2>";
?>
Can someone look at it and tell me where is the problem?
1 - Are you sure $username is populated by the mysql query? Try a var_dump($username) just before your link to see what and better, if the variable is populated.
2 - Is the variable send in the URl?
3 - A typo: there is no space between echo and "
4 - In AddPage.php did you use $_GET['username'] to get the username?
On the side: why do you call you variable user- name when in fact it's an id ?
At first get the value from url parameter like:
<?php $username = $_GET['username']; ?>
<?php
echo '<h2 > Please try to Add again</h2>';
?>
OR
<h2 > Please try to Add again</h2>

Variable inside a php echo function

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.

Categories