This is more of a proof of concept, but is in response to a post at Performancing where the concern is the same profile picture showing up more than one time on the front page:
What bothers me most, is how it looks when one author (me) might publish like 5 times in a row…
Let’s say you have a script which outputs articles by different authors on the front page and rolls off after that. How do you only show the author’s profile picture one time?
Solution
First, let’s make some assumptions. In the data you’ll have the author name as part of the byline and author’s ID. It’s better to use an ID than the author’s name because of name collision (there might be two authors with the name John Smith).
The database is pulling out each article and author ID. Let’s create some bogus data as an example and separate (delimit) the data by the pipe | symbol:
$article_data = array(
“1|John Smith|this is my first article”,
“2|Harry Smith|I am John’s brother”,
“1|John Smith|Their relation to me”,
“1|John Smith|I’m hung up talking about their relation to me”,
“4|John Smith|I’m a different John Smith with the same name”,
“3|Mary Smith|I’m John’s wife”,
“1|John Smith|Ok, I’m married to Mary and Harry’s my brother”
);
?>
Now, as you can see above, the author John Smith would dominate the front page, but I threw in some name collision with the John Smith and author ID 4. That’s a different John Smith so if we matched only on the name, our code that follows wouldn’t work. So we use the unique author ID field to determine if we’ve previously shown an article by author ID on the page as follows:
$article_data = array(
“1|John Smith|this is my first article”,
“2|Harry Smith|I am John’s brother”,
“1|John Smith|Their relation to me”,
“1|John Smith|I’m hung up talking about their relation to me”,
“4|John Smith|I’m a different John Smith with the same name”,
“3|Mary Smith|I’m John’s wife”,
“1|John Smith|Ok, I’m married to Mary and Harry’s my brother”
);
$picture_shown = ”; // empty array
$number_articles = count($article_data);
// start article loop
for($i=0;$i<$number_articles;$i++) {
// split the delimited data into the $parts array
$parts = explode(“|”,$article_data[$i]);
$author_ID = $parts[0]; // first item in $parts array is author ID
if(!$picture_shown[$author_id]) {
// new article by author on page, show picture
$picture_shown[$author_id] = 1; // set times shown
print “[I would show $author_ID.gif] “;
} else {
// already shown article by this author on page, do NOT show picture
$picture_shown[$author_id]++;
}
print ‘<i>’ . ucwords($parts[2]) . ” . ” by $parts[1] (ID#$author_ID)<br />”;
}
?>
Notes: The use of the ucwords($string) which makes sure to capitalize the first word of the article title. The line marked “I would show $author_ID.gif” would be replaced with the HTML img src code to insert the author’s image. This script assumes the image for the author is stored with the format: author_ID.gif. It could be .jpg, .png or whatever of course.
Happy coding to you!
