...

View Full Version : Display ALT tag on image from DB



John37309
12-09-2008, 07:11 PM
I have this piece of code, and it does work OK. It searches for a word i have stored in the MySql database (srfr_url01_word), if it finds the word, it then adds an image to the news item. It gets the image from a URL stored in the database ($srfr_url01_img)


<?php $match = false;$string_to_match = $params->get('srfr_url01_word');$word = $feed->get_title();
{if(strchr($string_to_match, rtrim($word)) !== false){$match = true;}}
if($match == true){echo '<img src="' . $srfr_url01_img . '" alt="BBC News">';}?>

But it add's the ALT tag from the php code, i need to get the ALT tag from the database where i have it stored. I cannot seem to get the alt tag that i have stored in the database as $srfr_url01_tags

1. Any idea how to remove this - alt="BBC News" from the php code and get it from the database instead?

2. If this peice of code looks redicioulous, its because of my limited ability with PHP. Please do suggest corrections to the code!

Extra info; This is what i have in the database for the alt tag; $srfr_url01_tags = alt="BBC News"

Thank you,
John.

Fumigator
12-09-2008, 08:04 PM
Well John, since you're using a class to handle the database data retrieval (which is a fine idea), we'd need to see that class to be able to know how it's used.

John37309
12-09-2008, 10:56 PM
Well John, since you're using a class to handle the database data retrieval (which is a fine idea), we'd need to see that class to be able to know how it's used.
Thanks for the reply Fumigator,
I hope i supply you with the correct info here

The block of code posted has the class;

<li class="srfr-row<?php echo ($key%2); ?>">

This is the css sheet for this code;

div#srfr_container {} /* Don't forget the module class suffix, if you've entered one in the module parameters! */

/* generic list styling */
ul.srfr {margin:4px 2px;padding:0;list-style:none;background:none;}
ul.srfr li {margin:4px 0;padding:2px 4px;border-bottom:1px dotted #ccc;list-style:none;clear:both;}
ul.srfr li.srfr-row0 {background:none;} /* List item row styling - odd */
ul.srfr li.srfr-row1 {background:none;} /* List item row styling - even */

/* generic link styling (fill as you see fit or use specific values below) */
ul.srfr li a {}
ul.srfr li a:hover {}

/* feed item title */
a.srfr-feed-title {display:block;color:#555;}
a.srfr-feed-title:hover {display:block;color:#006699;}

/* feed item timestamp */
span.srfr-feed-timestamp {color:#909090;margin-right:4px;font-size:11px;}

/* feed name */
a.srfr-feed-name {}
a.srfr-feed-name:hover {}

/* feed item intro content */
p.srfr-feed-intro {margin:0;padding:2px 0;clear:both;}
p.srfr-feed-intro img {display:block;float:left;margin:0 4px 4px 0;}
p.srfr-feed-intro a {}
p.srfr-feed-intro a:hover {}

/* feed item full content */
p.srfr-feed-full {margin:0;padding:2px 0;clear:both;}
p.srfr-feed-full img {display:block;float:left;margin:0 4px 4px 0;}
p.srfr-feed-full a {}
p.srfr-feed-full a:hover {}

I hope thats enough info to solve this problem.

Thank you,
John.

Fumigator
12-10-2008, 07:24 PM
That's a CSS class, not a PHP class. Annoying isn't it! ;)

The class I'm after is PHP code, probably included into your script using a require() or include() statement.

John37309
12-10-2008, 11:30 PM
That's a CSS class, not a PHP class. Annoying isn't it! ;)

The class I'm after is PHP code, probably included into your script using a require() or include() statement.
Thanks Fumigator,
The the majority of the code for this mod is posted in this message; http://www.codingforums.com/showpost.php?p=756756&postcount=3

John.

Fumigator
12-10-2008, 11:41 PM
Have you tried $params->get('srfr_url01_tags');?

John37309
12-11-2008, 12:24 AM
Have you tried $params->get('srfr_url01_tags');?

Fumigator,
help me here, i'm really very poor at this coding thing. How would i add your bit to my code?

This is the statement that gets the news item and displays it;

<?php $match = false;$string_to_match = $params->get('srfr_url01_word');$word = $feed->get_title();
{if(strchr($string_to_match, rtrim($word)) !== false){$match = true;}}
if($match == true){echo '<img src="images/pro30h/'.$srfr_url01_img.'">';}?>

So i tried adding your bit to the code where the ALT tag would normally be for an <img src=""> statement. This is what i tried and it did not work;


<?php $match = false;$string_to_match = $params->get('srfr_url01_word');$word = $feed->get_title();
{if(strchr($string_to_match, rtrim($word)) !== false){$match = true;}}
if($match == true){echo '<img src="images/pro30h/'.$srfr_url01_img.'" $params->get('srfr_url01_tags')>';}?>

It came back with a fatal error.

Remember i'm trying to add an ALT tag into this part of the code;
<img src="images/pro30h/'.$srfr_url01_img.'">

But i need to get the alt tag from the database where i have this stored; alt="BBC news tag test"

{EDIT} I keep getting this error; Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'

{EDIT again} essentially its this echo statement that i'm trying to get the alt tag into the statement;

{echo '<img src="images/pro30h/'.$srfr_url01_img.'">';}

This works, but its not taking the alt tags and stuff from the DB;

{echo '<img src="images/pro30h/'.$srfr_url01_img.'" alt="BBC news tag test">';}

Fumigator this is what i tried adding your peice to the echo statement;

{echo '<img src="images/pro30h/'.$srfr_url01_img.'" $params->get('srfr_url01_tags')>';}

Where is the error in the way i added your bit to the echo statement? Do i need to add some semi-colons, or dots or dashes or something, maybe some kind of brackets?

John.

John37309
12-11-2008, 01:13 AM
Have you tried $params->get('srfr_url01_tags');?
Fumigator,
You solved it!!!:thumbsup:

It just took me some time to understand what you suggested to me! But i have it working perfect!

This is what i done;

I have these statements and i don't really know what they do but i copied one of them and added the bit of code you suggested like this;


$srfr_url01_tags = $params->get('srfr_url01_tags','');

They happen early on in my page of code!

So then further down the page, i edited my echo statement like this;

{echo '<img src="images/pro30h/'.$srfr_url01_img.'" alt="'.$srfr_url01_tags.'">';}

I needed the combimnation of the two peices of code to get this to work!

This works like a dream. So first my code must be getting the params for the ALT tags i added and storing them. Then its able to get the tags when i call them further down the code!

A big sincere thank you Fumigator,

John.

Fumigator
12-11-2008, 03:17 AM
In the spirit of teaching, I'm going to assume you're actually interested in learning how this works (as opposed to interested only in someone fixing up your code for you). So first of all, let's format your code properly. Don't be afraid to take up some space; the internet doesn't care about your spaces and carraige returns.



<?php
$match = false;
$string_to_match = $params->get('srfr_url01_word');
$word = $feed->get_title();

if(strchr($string_to_match, rtrim($word)) !== false) {
$match = true;
}

if($match == true) {
echo '<img src="images/pro30h/' . $srfr_url01_img . '" $params->get('srfr_url01_tags')>';
}
?>


Whew, now we can see what's going on. Your echo statement is using the single-quote, so that means we have to break out of the string to glue variables to the string. There are several other ways to do this but this way is fine so let's stick with it.

To glue pieces of a string together, you use the period (.). Each piece of your echo statement must be glued to together with the period.

Now that you know both rules in use here, you can form a proper echo statement. You should also include the title parameter to be cross-browser compatable:



echo '<img src="images/pro30h/' . $srfr_url01_img . '" alt="' . $params->get('srfr_url01_tags') . '" title="' . $params->get('srfr_url01_tags') . '">';

John37309
12-11-2008, 03:19 PM
I read this last night, but i was tired so i had to go to bed.

In the spirit of teaching, I'm going to assume you're actually interested in learning how this works (as opposed to interested only in someone fixing up your code for you). So first of all, let's format your code properly. Don't be afraid to take up some space; the internet doesn't care about your spaces and carraige returns.
I'm always interested in learning, i only wish i had someone to explain things the way you have here Fumigator. I only compressed the code like that because i am multiplying this code by 70. I am duplicating this code for 70 different feeds. This number will get much larger in time. Compressing the code made it easier to duplicate. I don't really know if this is a bad thing.




Whew, now we can see what's going on. Your echo statement is using the single-quote, so that means we have to break out of the string to glue variables to the string. There are several other ways to do this but this way is fine so let's stick with it.

To glue pieces of a string together, you use the period (.). Each piece of your echo statement must be glued to together with the period.
I didn't know that :)



Now that you know both rules in use here, you can form a proper echo statement. You should also include the title parameter to be cross-browser compatable:



echo '<img src="images/pro30h/' . $srfr_url01_img . '" alt="' . $params->get('srfr_url01_tags') . '" title="' . $params->get('srfr_url01_tags') . '">';

I didn't know about the title thing either, i have added this to my code.

I am just learning about this code stuff. So what i do is take other peoples code and chop it up and re-assemble it for myself. That might make me sound very clever indeed but if i can find some code where someone else has written it out with all the colons, dots and dashes, i cant do anything.

So Fumigator thank you again for taking the time here, you are an educator and not everyone is able to be an educator, its a special quality!. I am also an educator, but i educate people about other things, not code...LOL.

Thanks for helping me,
John.

Fumigator
12-11-2008, 04:24 PM
Hey glad to help-- I may have sounded a little snarky but I truly do enjoy teaching, but there are plenty who come here just wanting someone to fix up their code.

So one last thing to mention-- if you find yourself duplicating the same code even once, you have actually identified a piece of code you can make into a function or class and then just call the function or instantiate the class.

For example:



<?php
createImgTag($params, $feed, "srfr_url01_word", "images/pro30h/$srfr_url01_img", "srfr_url01_tags");

function createImgTag ($params, $feed, $keyWord, $imgFile, $tags) {

$match = false;
$string_to_match = $params->get($keyWord);
$word = $feed->get_title();

if(strchr($string_to_match, rtrim($word)) !== false) {
$match = true;
}

if($match == true) {
echo '<img src="' . $imgFile . '" alt="' . $params->get($tags') . '" title="' . $params->get($tags) . '">';
}
}
?>


You may need to tweak that a little, but that's the general idea.

John37309
12-11-2008, 08:40 PM
I think i'm starting to get a better picture of how this stuff works. I have learned more in this message than i have messing around for the last 3 months :)

No, i have another problem that is part of this news mod. I will need to fix the problem before i can make this news mod public on my site. Might be best to post it in a new message as its a different topic.

Well maybe you can solve this as you are already fimilar with what i'm doing.

The problem happens with the error handeling when this mod gets news from a website where the server is gone down or the server is very slow. Then this script can take 30 seconds or maybe a minute to get the page of news. Nobody is going to wait that long for the script to finish. I currently have a time set to time out after 2 second of trying to get an individual feed, but its not working.

I think its this peice of code;

$feed->set_timeout($srfr_timeout);

But that is the only line of code that seems to be there for error handeling, and its just not working. I have most of this code posted here (http://www.codingforums.com/showpost.php?p=756756&postcount=3).

Any idea's?

John.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum