Jump to content

Archived

This topic is now archived and is closed to further replies.

dannel77

Need help with trim function to remove "whitespaces"

Recommended Posts

Hi, I need some help to modify this search script so that "whitespaces" (blanks) are removed when searching,
I have tried with "trim" but can't seem to get it right, any help would be appreciated, the code looks like this:

<?php

// database connection
mysql_connect("$host","$user","$pass");

// database selection
mysql_select_db("$database");

// Nu bestämmer vi antal per sida och kollar upp totala antalet
$limit = 40; // Antal per sida
$where = "$chose LIKE '%$searchstring%' OR item LIKE '%$searchstring%' OR code_no LIKE '%$searchstring%'";

$result = @mysql_query("SELECT COUNT(*) FROM products WHERE item LIKE '%$searchstring%' OR code_no LIKE '%$searchstring%'")
or die("Error fetching number in DB<br>".mysql_error());
$numrows = @mysql_result($result, 0); // Antal i databasen

// Sedan kollar vi om startvariabeln är satt
$start = isset($_GET['start']) && intval($_GET['start']) >= 0 ? intval($_GET['start']) : 0;

// Då räknar vi ut hur många sidor det blev
$pages = intval($numrows/$limit);
if ($numrows % $limit) {
$pages++;
}

$numlink = '';

// Hämta länk till föregående sida
if ($start > 0) {

$numlink .= '<a href="?start='.($start - $limit).'&amp;searchstring='.$searchstring.'"><font color="#2B65EC" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">&laquo; </font></a> ';
} else {
$numlink .= '&laquo;  ';
}

// Hämta sidonummer
for ($i = 1; $i <= $pages; $i++) {
$newoffset = $limit*($i-1);
if ($start == $newoffset)
$numlink .= '<font style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; padding: 0px 1"><b>'.$i.'</b></font> ';
else
$numlink .= '<a href="?start='.$newoffset.'&amp;searchstring='.$searchstring.'"><font style="color: #2B65EC;"><font style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; padding: 0px 1"><u>'.$i.'</u></font></font></a> ';
}

// Hämta länk till nästa sida
if ($numrows > ($start + $limit))
$numlink .= '<a href="?start='.($start + $limit).'&amp;searchstring='.$searchstring.'"><font color="#2B65EC" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;"> &raquo;</font></a> ';
else
$numlink .= ' &raquo; ';

$numlink = '
<div style="font-size: 11px; font-family: Verdana, Arial, Helvetica, sans-serif; padding: 5px 0;">
'.$numlink.'
</div>
';

// Skriv ut sidorna
echo $numlink;

?>
<table cellpadding="2" cellspacing="1" border="1" frame="border" RULES="NONE" borderstyle="color: #D5E2EC;">
<TR><TD BGCOLOR="#D5E2EC">
<TD BGCOLOR="#D5E2EC" ALIGN="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2" color="Black"><b> Art nr. </b></FONT></TD>
<TD BGCOLOR="#D5E2EC" ALIGN="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2" color="Black"><b> Titel </b></FONT></TD>
<TD BGCOLOR="#D5E2EC" ALIGN="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2" color="Black"><b> Pris SEK </b></FONT></TD>
<TD BGCOLOR="#D5E2EC"></TD></TR>
<?php
// Nu kan vi hämta datan från databasen och skriva ut den
$result = @mysql_query("SELECT * FROM products WHERE $where ORDER BY item ASC LIMIT $start, $limit")
or die("Error fetching data<br>".mysql_error());

while ($row = @mysql_fetch_array($result)){
$color = ($coloralternator++ %2 ? "D5E2EC" : "EBF1F5");
echo "<TR BGCOLOR=\"#$color\"><TD ALIGN=\"middle\"><a href=\"item_show.php?code_no=$row[2]\" TARGET=\"main\"><IMG SRC=\"thumbs/$row[6]\" BORDER=\"0\"></a></TD>";
echo "<TD ALIGN=\"left\"><font style=\"font-size: 10px; font-family: Verdana, Arial, Helvetica, sans-serif;\"><a href=\"item_show.php?code_no=$row[2]\" TARGET=\"main\"> $row[2] </FONT></TD>";
echo "<TD ALIGN=\"left\"></a><font style=\"font-size: 10px; font-family: Verdana, Arial, Helvetica, sans-serif;\"><a href=\"item_show.php?code_no=$row[2]\" TARGET=\"main\"> $row[3] </FONT></TD>";
echo "<TD ALIGN=\"right\"></a><font style=\"font-size: 10px; font-family: Verdana, Arial, Helvetica, sans-serif;\"> $row[5] </font></TD>";
echo "<TD ALIGN=\"middle\"> <INPUT TYPE=\"hidden\" NAME=\"id$row[2]quant\" VALUE=\"1\">
<a href=\"Javascript:top.center.cart.addItem('$row[3]','$row[5]', document.itemsform.id$row[2]quant.value, '$row[2]', '$row[7]')\"><img src=\"images/buy.gif\" border=\"0\"></a>  </TD></TR>";
}
"</center>"
?>
</TABLE>

Share this post


Link to post
Share on other sites
[code]$searchstring = trim($searchstring);// remove leading and trailing blanks[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.