...

View Full Version : Resolved pulling info out of a db to be used as an alt tag which contains ' in it?



LJackson
05-07-2009, 02:35 PM
Hi All,

i am having some validation issues with my code as for some reason when i pull out data from my db to use as an alt tag for my images it doesn't like the data with an ' in it

here is my code

$itemdata = "SELECT *
FROM category_data
WHERE page_category = 'Entertainment'
AND cat_id = '$category_list[$cat_count]'
AND subcat_id = '$subcategory_list[$sub_count]'";

$item_query=mysql_query($itemdata);
$num_rows = mysql_num_rows($item_query);

if ($num_rows == 0){
echo "No Results Please Go Back And Try Again";
}
else {
while ($row = mysql_fetch_array($item_query)){
$short_title = preg_replace("/#\d: |\s*\(.*\)|\s*\[.*\]/U", "", $row['title']);
//$title = get_string_between(, ':','[');
print "<div class='item'>

<div class='itemIMG'>
<img src='{$row['image']}'
alt='$short_title'
title='$short_title'
height='100' />
</div>

<div class='itemTitle'>$short_title</div>
<div class='itemButton'>
<img src='images/compare.gif'
alt='Compare $short_title Prices'
title='Compare $short_title Prices' /></div>

</div>";

}

the code in red is what is causing the problems, ive tried adding stuff to $short_title


$short_title = preg_replace("/#\d: |\s*\(.*\)|\s*\[.*\]/U", "", $row['title']);
$short_title = addslashes($short_title);

but this has not worked any ideas?

thanks
Luke

abduraooft
05-07-2009, 02:41 PM
$short_title=htmlentities($short_title);

LJackson
05-07-2009, 02:55 PM
hi mate still shows the same validation errors :(

sorry if this is in the wrong forum?
luke

abduraooft
05-07-2009, 03:24 PM
You need to enclose all your attribute values in double quotes. You may do it like

print '<div class="item">

<div class="itemIMG">
<img src="'.$row['image'].'"
alt="'.$short_title.'"
title="'.$short_title.'"
height="100" />
</div>
..................
';
Or

print "<div class=\"item\">

<div class=\"itemIMG\">
<img src=\"{$row['image']}\"
alt=\"{$short_title}\"
title=\"{$short_title}\"
height="100" />
</div>
..................
";
Or using heredoc (http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)

LJackson
05-07-2009, 03:51 PM
that worked mate thank you :D

Luke



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum