PHP concated string is blank at the end - php

I am trying to concatenate a string in a loop and when its is over the string is blank (or whatever value it was before). I am not exactly sure what is happening but I can't seem to find a similar issue.
$toItem = "";
$recpItem = "";
if(is_array($to))
{
foreach($to as $key=>$email)
{
$recpItem = $recpItem.'<item><recip dn="'.$email.'" sa="'.$email.'" ra="'.$email.'" al="" rt="SMTP" id="" ao="3" rf="0" ei="0" uri=""/></item>';
$email = htmlentities($email);
$toItem = $toItem.'<item><Rcp DN="'.$email.'" EM="'.$email.'" RT="SMTP" AO="3"/></item>';
}
}

I would add the code to an array then implode() it:
$to = array(
'bob#gmail.com',
'sally#gmail.com'
);
if(is_array($to)){
$recpItem = array();
$toItem = array();
foreach($to as $email){
$recpItem[] = '<item><recip dn="'.$email.'" sa="'.$email.'" ra="'.$email.'" al="" rt="SMTP" id="" ao="3" rf="0" ei="0" uri=""/></item>';
$email = htmlentities($email);
$toItem[] = '<item><Rcp DN="'.$email.'" EM="'.$email.'" RT="SMTP" AO="3"/></item>';
}
$recpHtml = implode($recpItem);
$toHtml = implode($toItem);
echo $recpHtml;
echo '<hr />';
echo $toHtml;
}

Related

getting error to display all record in php json

I am trying to display all records using jason in php.
but display all filed with null value.
I'm using postman for testing purpose.
I don't know what is the problem with that code. I getting null value only.
here is my code :
<?php
header('Content-Type: application/json');
$checkFields = "";
$REQUEST = $_SERVER['REQUEST_METHOD'];
if ($REQUEST == "POST")
{
include "DB/db.php";
$userlist = mysql_query("SELECT * FROM reg_services");
if(mysql_num_rows($userlist) > 0)
{
$p = 0;
$ph = array();
while($userlistdata = mysql_fetch_row($userlist))
{
$ph[$p]["UserId"] = $userlistdata['id'];
$ph[$p]["FirstName"] = $userlistdata['fname'];
$ph[$p]["LastName"] = $userlistdata['lname'];
$ph[$p]["Email"] = $userlistdata['email'];
$ph[$p]["Mobile"] = $userlistdata['mobile'];
$ph[$p]["Password"] = $userlistdata['password'];
$p++;
}
$json = array("success" => 1, "All_User_List" => $ph);
$jsonarray = json_encode($json);
}
}
else
{
$json = array("success" => 0, "message" => "Invalid Request Type(Use POST Method)");
$jsonarray = json_encode($json);
}
echo $jsonarray;
?>
please help me if you are know what is the error in code.
just replace this code with old one
$p = 0;
$ph = array();
while($userlistdata = mysql_fetch_array($userlist))
{
$ph[$p] = array();
$ph[$p]["UserId"] = $userlistdata['id'];
$ph[$p]["FirstName"] = $userlistdata['fname'];
$ph[$p]["LastName"] = $userlistdata['lname'];
$ph[$p]["Email"] = $userlistdata['email'];
$ph[$p]["Mobile"] = $userlistdata['mobile'];
$ph[$p]["Password"] = $userlistdata['password'];
$p++;
}
You need to tell PHP about arrays
while($userlistdata = mysql_fetch_row($userlist))
{
$ph[$p] = array(); // let PHP know it is an array
$ph[$p]["UserId"] = $userlistdata['id'];
$ph[$p]["FirstName"] = $userlistdata['fname'];
$ph[$p]["LastName"] = $userlistdata['lname'];
$ph[$p]["Email"] = $userlistdata['email'];
$ph[$p]["Mobile"] = $userlistdata['mobile'];
$ph[$p]["Password"] = $userlistdata['password'];
$p++;
}
just replace this while loop condition with olde one.
while($userlistdata = mysql_fetch_array($userlist))
now it's work

MySQL echos only one elemen

I've a problem with a MySQL script. The script should echo every element out of the table where "gruppe" is $gruppe. But I am only getting one single output.
<?php
if ( $_SERVER["REQUEST_METHOD"] == 'POST' ) {
$gruppe = $_POST["gruppe"];
$mail = $_POST["mail"];
$betreff = $_POST["betreff"];
$nachricht = $_POST["nachricht"];
if (!empty($nachricht)) {
$sql = new rex_sql;
$sql->debugsql = 0; //Ausgabe Query
$sql->setQuery("SELECT * FROM $db_users WHERE gruppe = '$gruppe'");
for($i=0;$i<$sql->getRows();$i++)
{
$id_mail = $sql->getValue("id");
$mail_mail = $sql->getValue("email");
$ausgabe_mail = $mail_mail;
$sql->next();
}
}
}
?>
<?php echo $ausgabe_mail ?>
Your echo-statement is outside the loop. The loop fetches all email addresses, one by one, and stores them in $mail_mail and $ausgabe_mail. Each iteration of the loop overwrites the previous contents of both variables.
After the loop ends, you echo the last value of $ausgabe_email.
Retry with the echo inside the loop:
<?php
if ( $_SERVER["REQUEST_METHOD"] == 'POST' ) {
$gruppe = $_POST["gruppe"];
$mail = $_POST["mail"];
$betreff = $_POST["betreff"];
$nachricht = $_POST["nachricht"];
if (!empty($nachricht)) {
$sql = new rex_sql;
$sql->debugsql = 0; //Ausgabe Query
$sql->setQuery("SELECT * FROM $db_users WHERE gruppe = '$gruppe'");
for($i=0;$i<$sql->getRows();$i++) {
$id_mail = $sql->getValue("id");
$mail_mail = $sql->getValue("email");
$ausgabe_mail .= ',' . $mail_mail;
$sql->next();
}
echo $ausgabe_mail;
}
}
?>
EDIT: as clarified, expanded the example with string concatenation and moved the echo outside the loop again.

PHP: How to insert a <br/>-tag after the # inside an email address

I'm having problems with some email addresses that are too long. So I'd like to set a break after the # character.
But unfortunately, the # symbol seems to be a special character. That's why it doesn't work with:
str_replace ('#' , '#<br/>' , $email);
Is there any other way to get it done?
I'll make an attempt at hopefully answer this question, seeing that you are most likely not showing us your full code.
If by any chance that you're using another function in conjunction with this, say for example strlen(), then that could account for it, and how you're using it.
You may also be using another function, which is unknown at the time of your posting. Therefore you will need to elaborate on that, should my answer not provide you with a solution.
Far as I'm concerned, your "posted" code checked out, so I can't see how that could be failing.
Using the following and in conjunction with your posted code, proved to be successful.
<?php
// email with 33 characters
$email = "emailwith33characters#example.com";
// if more or equal to 33 characters
if(strlen($email) >= 33){
echo "The email <b>$email</b> contains 33 or more characters. ";
echo "Now putting them into 2 lines...";
echo "<br>";
// echo str_replace('#', '#<br/>', $email);
echo "First line: " . str_replace('#', '#<br/>Second line: ', $email);
}
else{
echo "Email is within character limit.";
}
Using echo strlen($email); will show you the email's string length.
Reference(s):
http://php.net/strlen
http://php.net/str_replace
Thank you for your help. Unfortunately "str_replace" does not work with the "#" character in my code. If I test it with the "." character it sometimes works. Ever time I reload the page in the browser the "str_replace" result changes.
Here's some more of the code. It's from a Wordpress plugin to show contact details of staff members. It loops through all staff posts.
$i = 0;
if( $staff->have_posts() ) {
if ($headline =='yes') {
$output .= '<h1 class="staff-group-mainheader">'.$maingroupname.'</h1>';
}
$output .= '<div class="staff-member-listing '.$group.'">';
while( $staff->have_posts() ) : $staff->the_post();
unset($staff_member_classes);
$terms = get_the_terms( $post->id, 'staff-member-group' ); // get an array of all the terms as objects.
$term_slug = array(); // save the slugs in an array
$term_name = array(); // save the slugs in an array
foreach( $terms as $term ) {
$term_slug[] = $term->slug; // this grabs the hyphenated slug
$term_name[] = $term->name; // this grabs the actual name
$staff_member_classes .= ' '.$term->slug;
}
if ($i == ($staff->found_posts)-1) {
$staff_member_classes .= " last";
}
if ($i % 2) {
$oddorevenpost = 'even';
} else {
$oddorevenpost = 'odd';
}
if (($oddorevenpost == $oddoreven) || ($oddoreven == '')) {
$output .= '<div class="staff-member '.$oddorevenpost.' '.$staff_member_classes.'">';
global $post;
$custom = get_post_custom();
$name = get_the_title();
$name_slug = basename(get_permalink());
$title = $custom["_staff_member_title"][0];
$function = $custom["_staff_member_fb"][0];
$email = antispambot($custom["_staff_member_email"][0]);
if (!empty($custom["_staff_member_phone"][0])) {
$phone = '<li><i class="icons lycon-phone"></i><span class="staff-phone">'.$custom["_staff_member_phone"][0].'</span></li>';
}
if (!empty($custom["_staff_member_mobile"][0])) {
$mobile = '<li><i class="icons icon-mobile-6"></i><span class="staff-mobile">'.$custom["_staff_member_mobile"][0].'</span></li>';
}
if (!empty($custom["_staff_member_fax"][0])) {
$fax = '<li><i class="icons lycon-fax"></i><span class="staff-fax">'.$custom["_staff_member_fax"][0].'</span></li>';
}
$company = $custom["_staff_member_company"][0];
$street = $custom["_staff_member_street"][0];
$city = $custom["_staff_member_city"][0];
$country = $custom["_staff_member_country"][0];
$bio = $custom["_staff_member_bio"][0];
if(has_post_thumbnail()){
$postidthumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID));
$photo_url = wp_get_attachment_medium_url( $postidthumb );
$photo = '<div class="staff-member-container" style="background: url('.$photo_url.') no-repeat right bottom;">';
}else{
$photo_url = '';
$photo = '';
}
if (function_exists('wpautop')){
$bio_format = '<div class="staff-member-bio">'.wpautop($bio).'</div>';
}
$emailbreak = str_replace ('#' , '#<br/>' , $email);
$email_mailto = '<li><i class="icons icon-mail-7"></i><span class="staff-email"><a class="staff-member-email" href="mailto:'.$email.'" title="Email '.$name.'">'.$emailbreak.'</a></span></li>';
$email_nolink = antispambot( $email );
$accepted_single_tags = $default_tags;
$replace_single_values = array($name, $name_slug, $photo_url, $title, $function, $email_nolink, $phone, $mobile, $fax, $company, $street, $city, $country, $bio, $fb_url, $tw_url);
$accepted_formatted_tags = $default_formatted_tags;
if ( $title =='') {
$formattedname = '<h3 class="staff-member-name">'.$name.'</h3>';
} else {
$formattedname = '<h3 class="staff-member-name">'.$title.' '.$name.'</h3>';
}
$replace_formatted_values = array($formattedname, '<h4 class="staff-member-position">'.$title.'</h4>', $photo, $email_mailto, $bio_format );
$loop_markup = str_replace($accepted_single_tags, $replace_single_values, $loop_markup);
$loop_markup = str_replace($accepted_formatted_tags, $replace_formatted_values, $loop_markup);
$output .= $loop_markup;
$loop_markup = $loop_markup_reset;
$output .= '</div> <!-- Close staff-member -->';
}
$i += 1;
endwhile;
$output .= "</div> <!-- Close staff-member-listing -->";
}
}
wp_reset_query();
$output = $style_output.$output;

How to email $_SESSION variables

I'm have a function which shows the user which products he has added to his cart. In this function I simply echo all the $_SESSION data. The problem is that I want to email all the $_SESSION data. I can't store the function in a variable and simple use $message = $function because in the email I get nothing is being displayed. So my question is: what's the easiest way to email $_SESSION data?
I use this code to display all sessions in a php file:
function cart_summary() {
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$id2 = mysql_real_escape_string((int)$id);
$get_colour_size_category = "SELECT * FROM products WHERE id='$id2'";
$result_colour_size_category = mysql_query($get_colour_size_category);
while ($get_colour_size_category_row = mysql_fetch_assoc($result_colour_size_category)) {
$get_webshop_category = $get_colour_size_category_row['webshop_category'];
$get_webshop_category2 = mysql_real_escape_string((int)$get_webshop_category);
$get_name_and_price = "SELECT * FROM webshop_categories WHERE id='$get_webshop_category2'";
$result_name_and_price = mysql_query($get_name_and_price);
$row_name_and_price = mysql_fetch_assoc($result_name_and_price);
$sub = $row_name_and_price['price'] *$value;
echo '<tr><td>'. $row_name_and_price['ev_name'] .' '. $get_colour_size_category_row['colour'].' '. $get_colour_size_category_row['size'].'</td><td class="value"> '. $value .' </td><td> €'.number_format($row_name_and_price['price'], 2).'</td><td> €'.number_format($sub, 2).'</td><td></td></tr>';
}
?><?
}
$total += $sub;
}
}
I want to email the output of this
The easiest way would be to store the output of your $_SESSION array using print_r() (by using the optional parameter to return the information, rather than print it):
$session_data = print_r($_SESSION, TRUE);
Now it is a string containing all the session data and you can use it however you wish.
Managed to put the $_SESSION data in an email by doing the following:
function cart_summary() {
$message = "";
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$id2 = mysql_real_escape_string((int)$id);
$get_colour_size_category = "SELECT * FROM products WHERE id='$id2'";
$result_colour_size_category = mysql_query($get_colour_size_category);
while ($get_colour_size_category_row = mysql_fetch_assoc($result_colour_size_category)) {
$get_webshop_category = $get_colour_size_category_row['webshop_category'];
$get_webshop_category2 = mysql_real_escape_string((int)$get_webshop_category);
$get_name_and_price = "SELECT * FROM webshop_categories WHERE id='$get_webshop_category2'";
$result_name_and_price = mysql_query($get_name_and_price);
$row_name_and_price = mysql_fetch_assoc($result_name_and_price);
$sub = $row_name_and_price['price'] *$value;
$message .= '<tr><td>'. $row_name_and_price['ev_name'] .' '. $get_colour_size_category_row['colour'].' '. $get_colour_size_category_row['size'].'</td><td class="value"> '. $value .' </td><td> €'.number_format($row_name_and_price['price'], 2).'</td><td> €'.number_format($sub, 2).'</td><td></td></tr>';
}
?><?
}
$total += $sub;
}
return $message
}
After I did this I was able to email the output of the function with
$message .= cart_email() . "\n";

Converting telephone numbers into "tel:" hyperlinks with regular expression replacement

I have the following being extracted from a database:
Switchboard:265-7404-6,Marketing Hotline: 265-7403, B'ce: 333-6848
I want to convert it to something like this in PHP:
In the event of extension as: 265-7404-6 it only parses: 265-7404
function printdir()
{
$mysqli = new mysqli(constant("host"),constant("username"),constant("password"), constant("database"));
$storedProc = "SELECT * FROM `directory`;";
$result = $mysqli->query($storedProc);
if($mysqli->affected_rows>0)
{
while( $row = $result->fetch_assoc()) {
echo $row['telephone']; /*Formatting here*/
}
}
}
$telnumber = explode('-', $row['telephone'], 3));
echo $telnumber[0].'-'.$telnumber[1];
The extension and anything after the second value will be in $telnumber[2]
<?php
function format_number($phone){
$phone = preg_replace("#[^\d{10}\s]#",'',$phone);
if(strlen($phone)==7)
{
$area = substr($phone,0,3);
$prefix = substr($phone,3,4);
$format = $area."-".$prefix;
$phone = "$format";
return($phone);
}
if(strlen($phone) != 10) return(False);
$area = substr($phone,0,3);
$prefix = substr($phone,3,3);
$number = substr($phone,6,4);
$format = $area."-".$prefix."-".$number;
$phone = "$format";
return($phone);
}
function telephoneText($string)
{
$format_one.= '\d{10}'; //5085551234
$format_two_and_three .= '\d{3}(\.|\-)\d{3}\2\d{4}'; //508.555.1234 or 508-555-1234
$format_four .= '\(\d{3}\)\-\d{3}\-\d{4}'; //(508)-555-1234
$format_five .='\d{3}\-\d{4}'; //(508)555-1234
$format_five_seven .='\d{7}';
$string = preg_replace("~({$format_one}|{$format_two_and_three}|{$format_four}|{$format_five}|{$format_five_seven})~e", 'format_number("$1")', $string);
return $string;
}
?>
Thats is the what i was trying to achieve. Thanks guys..

Categories