Jump to content

Recommended Posts

Hi, I have a problem with getting something out of my database because it has a $ in it.

 

$price = $_REQUEST['price'] ; //this gets the selection from the drop-down box that the user selected.

 

It could be "Under £10 or $15"

 

The selection is then matched with record in the database, in this case records with "Under £10 or $15" would be returned.

 

This doesn't work and I'm 100% sure its because i have $ in it.

 

I know i could use str_replace to take out the $s but i need them for when the record is returned and displayed (its a search engine). So the user would see all the other details and Under "£10 or $15", not "Under £10 or 15".

 

Thanks in advance,

 

Tom.

 

Link to comment
https://forums.phpfreaks.com/topic/149752-solved-mixed-characters-with-problem/
Share on other sites

pretty sure escaping the character would work

 

in a double quoted string characters that have meaning in php are interpreted by php, so "$15" tries to replace $15 with a variable value, but in addition to you not wanting to have a variable here, $15 is an illegal variable because they cant start with numbers, solution, use single instead of double quotes, or if you must, write the $15 as \$15, that should tell php that that is an actual dollar sign not a marker that the next bit of the string is a variable

 

EDIT: do NOT use request, use post or get, request is a major security problem, google 'dangers of request method'

I know, but the $15 is inside the string, and there are many different selections possible, like like "Under £30 or $75"

 

Right now it is stored in the database as "Under £500 or \$750". This whole string is taken from the database and stored in a variable. So i cant go in and just edit the $ bit.

 

Do I need to some how break the string apart then modify each word? mayb somthing like this "('abc' or '<' or '>' . £ . 'number' . abc . /$ . 'number')" or is there a much simpler way?

 

Currently the database is utf8 -- UTF-8 Unicode, have tried others but still get that wierd question mark thingy �

 

Thanks for your help so far.

Hi, here is the code:

<?php

if (!file_exists("dbconnect.php"))
{
die("Database settings not found, administrator intervention required.") ;
}
else
{
require("dbconnect.php") ; //Must connect to the database.
}

$word = $_POST['word'];
$type = $_POST['type'] ;
$flavor = $_POST['flavor'] ;
$tiers = $_POST['tiers'] ;
$serves = $_POST['serves'] ;
$price = $_POST['price'] ;  //THIS IS THE VARIABLE THAT HAS THE POUND AND $ SYMBOLS IN

$def = "Dont Specify" ; //Default value for drop down boxes

if(!isset($word) || $word == "Enter Search Term" || $word == "") //If the word field has nothing in or has default text then...
{
unset($word) ; //Force clear.
}
if($type == $def)
{
unset($type) ;
}
if($flavor == $def)
{
unset($flavor) ;
}
if($tiers == $def)
{
unset($tiers) ;
}
if($serves == $def)
{
unset($serves) ;
}
if($price == $def)
{
unset($price) ;
}

if(!isset($word) && !isset($type) && !isset($flavor) && !isset($tiers) && !isset($serves) && !isset($price)) //If nothing has been set...
{
echo 'some stuff' ; //Message.
die ; include("dbdisconnect.php") ;//End script and disconnect from database.
}

if(isset($word))
{	
include ('eliminator.php') ; //Include the word cleaner, deletes symbols etc.
include ('stemmer.php') ; //Include the Stemmer Algorythm, "tests" = "test" etc.

$stemmer = new PorterStemmer ; //Call class in the stemmer.php file.
$stemmed_string = $stemmer->stem(strtolower($word)); //force words to lower case.

$cleanup = new Cleaner ; //Call word cleaner class in the eliminator.php file.
$stemmed_string = $cleanup->parseString($stemmed_string) ;

$sanction = split(" ",$stemmed_string) ; //Spaces constiture a new word?

foreach ($sanction as $array => $V)
{
	$x_string .= ''.$V.' ' ;
}
$x_string = substr($x_string,0,(strlen($x_string)-1)) ;

$split_stemmed = split(" ",$x_string) ;

while(list($key,$V)=each($split_stemmed))
{
	if($V<>" " AND strlen($V) > 0){
	$wordx .=  "(tags LIKE '%$V%' OR title LIKE '%$V%' OR Description LIKE '%$V%' OR decorator LIKE '%$V%') OR" ;
	}
}
	$wordx = substr($wordx,0,(strlen($wordx)-3)) ;

}

if(isset($type))
{
if(isset($word))
{
$typex = "&& type LIKE '%$type%'" ;
}
else
$typex = "type LIKE '%$type%'" ;
}

if(isset($flavor))
{
if(isset($word) || isset($type))
{
$flavorx = "&& flavor LIKE '%$flavor%'" ;
}
else
$flavorx = "flavor LIKE '%$flavor%'" ;
}

if(isset($tiers))
{
if(isset($word) || isset($type) || isset($flavor))
{
$tiersx = "&& tiers LIKE '%$tiers%'" ;
}
else
$tiersx = "tiers LIKE '%$tiers%'" ;
}

if(isset($serves))
{
if(isset($word) || isset($type) || isset($flavor) || isset($tiers))
{
$servesx = "&& serves LIKE '%$serves%'" ;
}
else
$servesx = "serves LIKE '%$serves%'" ;
}

if(isset($price))
{
if(isset($word) || isset($type) || isset($flavor) || isset($tiers) || isset($serves))
{
$pricex = "&& price LIKE '%$price%'" ;
}
else
$pricex = "price LIKE '%$price%'" ;
}

$query = "SELECT DISTINCT * FROM image_bank WHERE $wordx $typex $flavorx $tiersx $servesx $pricex" ;

$result = mysql_query($query) ;

$rowamnt = mysql_num_rows($result) ; //Count how many results there are.

if ($rowamnt > 0) //If there are more than 0 results...
{
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{

$dbtitle = $row['title'] ;
$dbdescr = $row['description'] ;
$dbtags = $row['tags'] ;
$dbdec = $row['decorator'] ;
$dbtiers = $row['tiers'] ;
$dbtype = $row['type'] ;
$dbflavor = $row['flavor'] ;
$dbserves = $row['serves'] ;
$dbprice = $row['price'] ;
$imgurl = $row['url'];
$thumburl = $row['thumb_url'] ;
$rating = $row['rating'] ;

echo "$dbtags" . "<br />" ;
echo "$dbtitle" . "<br />" ;
echo "Decorated by : $dbdec" . "<br />" ;
echo "Description : $dbdescr" . "<br />" ;
echo "$dbtiers tier cake" . "<br />" ;
echo "Type : $dbtype cake" . "<br />" ;
echo "Flavor : $dbflavor" . "<br />" ;
echo "Serves : $dbserves" . "<br />" ;
echo "Costs : $dbprice" . "<br />" ;
echo 'Information Page : <a href="' . "$imgurl" . '">' . "$imgurl" . "</a>" . "<br />" ;
echo '<img src="' . "$thumburl" . '" height="100" width="50" />' ;
echo "$rating" ;

} 
}
else
{
echo 'stuff' ;
}

include("dbdisconnect.php") ;

?>

 

Edit by thorpe: We have code tags for a reason.

yeah, entities should work too.

 

are you haveing problems with the pound symbol too?

 

you said 'weird question mark thingy', thats a black diamond with a ? in it right, if so, that means you have a character set that doesnt support those symbols

Thanks for all your help, in the end I did this:

 

in the database, records use the html symbol codes - "Under £500 or &#x24;750" (this also fixed the diamond ? issue)

 

then in the php:

 

<?PHP

 

if (!file_exists("dbconnect.php"))

{

die("Database settings not found, administrator intervention required.") ;

}

else

{

require("dbconnect.php") ; //Must connect to the database.

}

 

$word = $_POST['word'];

$type = $_POST['type'] ;

$flavor = $_POST['flavor'] ;

$tiers = $_POST['tiers'] ;

$serves = $_POST['serves'] ;

$price = $_POST['price'] ;  //THIS IS THE VARIABLE THAT HAS THE POUND AND $ SYMBOLS IN

 

 

$symbol[0] = '£' ;

$symbol[1] = '$' ;

 

$replace[0] = '£' ;

$replace[1] = '&#x24;' ;

 

$price = str_replace($symbol,$replace,$price) ;

 

$def = "Dont Specify" ; //Default value for drop down boxes

 

if(!isset($word) || $word == "Enter Search Term" || $word == "") //If the word field has nothing in or has default text then...

{

unset($word) ; //Force clear.

}

if($type == $def)

{

unset($type) ;

}

if($flavor == $def)

{

unset($flavor) ;

}

if($tiers == $def)

{

unset($tiers) ;

}

if($serves == $def)

{

unset($serves) ;

}

if($price == $def)

{

unset($price) ;

}

 

if(!isset($word) && !isset($type) && !isset($flavor) && !isset($tiers) && !isset($serves) && !isset($price)) //If nothing has been set...

{

echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Cake Photos Decorators Suppliers</title>

<meta name="keywords" content="" />

<meta name="description" content="" />

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

 

<link href="../cakes-css.css" rel="stylesheet" type="text/css" />

</head>

 

<body>

<div id="page">' ;

include "../includes/headnav.html" ;

echo '<div id="head"></div>

 

<div id="search">' ;

include "../includes/header-search.html" ;

echo '</div>

<div id="sidebar">' ;

include "../includes/sidenav.html" ;

echo'</div>

<div id="main">

<h1>Cake Photos</h1>

<p>You did not specify anything to search for.</p>

<p>Either enter a word into the word field, or make a selection from at least one drop down box.</p>

</div>

<div id="rightzone">

Latest cake submissions here, if no cake submissions display random cakes from the database.

</div>

 

<div id="footer">' ;

include "../includes/footer.html" ;

 

echo '</div>

</div>

 

</body>

</html>' ; //Message.

die ; include("dbdisconnect.php") ;//End script and disconnect from database.

}

 

if(isset($word))

{

include ('eliminator.php') ; //Include the word cleaner, deletes symbols etc.

include ('stemmer.php') ; //Include the Stemmer Algorythm, "tests" = "test" etc.

 

$stemmer = new PorterStemmer ; //Call class in the stemmer.php file.

$stemmed_string = $stemmer->stem(strtolower($word)); //force words to lower case.

 

$cleanup = new Cleaner ; //Call word cleaner class in the eliminator.php file.

$stemmed_string = $cleanup->parseString($stemmed_string) ;

 

$sanction = split(" ",$stemmed_string) ; //Spaces constiture a new word?

 

foreach ($sanction as $array => $V)

{

$x_string .= ''.$V.' ' ;

}

$x_string = substr($x_string,0,(strlen($x_string)-1)) ;

 

$split_stemmed = split(" ",$x_string) ;

 

while(list($key,$V)=each($split_stemmed))

{

if($V<>" " AND strlen($V) > 0){

$wordx .=  "(tags LIKE '%$V%' OR title LIKE '%$V%' OR Description LIKE '%$V%' OR decorator LIKE '%$V%') OR" ;

}

}

$wordx = substr($wordx,0,(strlen($wordx)-3)) ;

 

}

 

if(isset($type))

{

if(isset($word))

{

$typex = "&& type LIKE '%$type%'" ;

}

else

$typex = "type LIKE '%$type%'" ;

}

 

if(isset($flavor))

{

if(isset($word) || isset($type))

{

$flavorx = "&& flavor LIKE '%$flavor%'" ;

}

else

$flavorx = "flavor LIKE '%$flavor%'" ;

}

 

if(isset($tiers))

{

if(isset($word) || isset($type) || isset($flavor))

{

$tiersx = "&& tiers LIKE '%$tiers%'" ;

}

else

$tiersx = "tiers LIKE '%$tiers%'" ;

}

 

if(isset($serves))

{

if(isset($word) || isset($type) || isset($flavor) || isset($tiers))

{

$servesx = "&& serves LIKE '%$serves%'" ;

}

else

$servesx = "serves LIKE '%$serves%'" ;

}

 

if(isset($price))

{

if(isset($word) || isset($type) || isset($flavor) || isset($tiers) || isset($serves))

{

$pricex = "&& price LIKE '%$price%'" ;

}

else

$pricex = "price LIKE '%$price%'" ;

}

 

$query = "SELECT DISTINCT * FROM image_bank WHERE $wordx $typex $flavorx $tiersx $servesx $pricex" ;

 

$result = mysql_query($query) ;

 

$rowamnt = mysql_num_rows($result) ; //Count how many results there are.

 

if ($rowamnt > 0) //If there are more than 0 results...

{

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{

 

$dbtitle = $row['title'] ;

$dbdescr = $row['description'] ;

$dbtags = $row['tags'] ;

$dbdec = $row['decorator'] ;

$dbtiers = $row['tiers'] ;

$dbtype = $row['type'] ;

$dbflavor = $row['flavor'] ;

$dbserves = $row['serves'] ;

$dbprice = $row['price'] ;

$imgurl = $row['url'];

$thumburl = $row['thumb_url'] ;

$rating = $row['rating'] ;

 

echo "$dbtags" . "<br />" ;

echo "$dbtitle" . "<br />" ;

echo "Decorated by : $dbdec" . "<br />" ;

echo "Description : $dbdescr" . "<br />" ;

echo "$dbtiers tier cake" . "<br />" ;

echo "Type : $dbtype cake" . "<br />" ;

echo "Flavor : $dbflavor" . "<br />" ;

echo "Serves : $dbserves" . "<br />" ;

echo "Costs : $dbprice" . "<br />" ;

echo 'Information Page : <a href="' . "$imgurl" . '">' . "$imgurl" . "</a>" . "<br />" ;

echo '<img src="' . "$thumburl" . '" height="100" width="50" />' ;

echo "$rating" ;

 

}

}

else

{

echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Cake Photos Decorators Suppliers</title>

<meta name="keywords" content="" />

<meta name="description" content="" />

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

 

<link href="../cakes-css.css" rel="stylesheet" type="text/css" />

</head>

 

<body>

<div id="page">' ;

include "../includes/headnav.html" ;

echo '<div id="head"></div>

 

<div id="search">' ;

include "../includes/header-search.html" ;

echo '</div>

<div id="sidebar">' ;

include "../includes/sidenav.html" ;

echo'</div>

<div id="main">

<h1>Cake Photos</h1>

<p>No cakes were found that match what you searched for.</p>

<p>Make sure your have entered a keyword OR have selected an item from atleast one drop down menu.</p>

</div>

<div id="rightzone">

Latest cake submissions here, if no cake submissions display random cakes from the database.

</div>

 

<div id="footer">' ;

include "../includes/footer.html" ;

 

echo '</div>

</div>

 

</body>

</html>' ;

}

 

include("dbdisconnect.php") ;

 

?>

 

This was my very first try at a search engine for my new site I'm making, I've learned a lot with your help so thank you all :)

 

If you have any tips on how I could make this code more efficent please let me know :D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.