hacktheplanet1337
hacktheplanet1337

Reputation: 23

PHP Trim after fetching data with MySQL

So I am new to PHP and I am having some difficulty with the trim function.

Basically I need to

Here is my source code.

<?php

include "../include/setup.inc.php";


$query="SELECT offer_id,title,email,phone,site,zip,price,return_customer,request_date,traffic_source,agent_email,released FROM jos_quote 
     where request_date >= '2011-10-04' order by request_date";
$result = mysql_query($query);          
    if (!$result) {echo "<p><b>".$query ."</b></p>";echo 'MySQL Error: ' . mysql_error(); return;}
$num_rows = mysql_num_rows($result);
    echo '<br><table border="1" align="center" width="700px">
     <tr bgcolor="#FF8000"><td colspan="11" align="center">Total: '.$num_rows.'quotes -  Date Range: '.$_POST['date1'].' -- '.$_POST['date2'].' </td></tr>
     <tr><th>Title</th><th>Email</th><th>Site</th><th>Zip</th><th>Price</th>
<th>Return Customer</th><th>Request Date</th><th>Agent</th><th>Released</th><th>Traffic Source</th>';
while ($row = mysql_fetch_assoc($result)) {
    echo '<tr>
    <td>'.$row['title'].'</td>
    <td>'.$row['email'].'</td>
    <td>'.$row['site'].'</td>
    <td>'.$row['zip'].'</td>
    <td>'.$row['price'].'</td>                               
    <td>'.$row['return_customer'].'</td>                                
    <td>'.$row['request_date'].'</td>                                
    <td>'.$row['agent_email'].'</td>                                    
    <td>'.$row['released'].'</td>                                
     <td>'.$row['traffic_source'].'</td>                                                 
     </tr>';
}
echo '</table>';
$query="select agent_email,num_quote,num_receive,set_date from quote_distro where set_date  >= '2011-10-04'";
$result = mysql_query($query);          
    if (!$result) {echo "<p><b>".$query ."</b></p>";echo 'MySQL Error: ' . mysql_error();     return;}
    while ($row = mysql_fetch_assoc($result)) {
$quote_info[$row['agent_email']][$row['set_date']] =$row;

}


}


?>

Upvotes: 1

Views: 2039

Answers (3)

stivlo
stivlo

Reputation: 85486

That's not what the trim() function does. trim() strips whitespace from the beginning and the end of a string.

I need to trim @example.com from all agent_email values.

You can use str_replace for that. For example to remove @example.com from a String.

$string = str_replace ('@example.com', '', $string);

If instead you want to remove whatever after the at sign (@), you can use explode:

list($string) = explode('@', $string);

I also need to trim the traffic_source down to 30 characters

This is again another concept. You can use php substr function for that:

$string = substr($string, 0, 30);

If you want to add ellipsis sign only when the string was truncated:

if (strlen($string) > 30) {
    $string = substr($string, 0, 30) . '&hellip;';  
}

Upvotes: 1

bfavaretto
bfavaretto

Reputation: 71918

I would do that in MySQL, not PHP. The traffic source case is as simple as this:

SELECT LEFT(traffic_source, 30) as traffic_source FROM (...)

For the @example.com, you can use REPLACE:

SELECT REPLACE(email, '@example.com', '') AS email (...)

You can also use SUBSTRING

SELECT SUBSTRING(email, 0, INSTR(url, '@')) AS email

These and more string functions are documented here.

Upvotes: 0

miki
miki

Reputation: 695

substr($row['traffic_source'], 0, 30);
str_replace('@example.com', '', $row['agent_email']);

Upvotes: 0

Related Questions