Limiting a results Flickr Api to avoid duplicate photos from a user? - php

Trying to output flickr images from a certain location, not using any geolocation yet, only the a string of the location, but thats a problem for another topic :).
Problem is that when I call flickr.photos.search for that place, I may get a few results from a single user. Is there any easy way in PHP to limit this, but still maintain the number of images I request?
Rather than having the output populated with a few users, I would like to have 12 images each by different users. I know this is easily possible, but its on the tip of my brain and I cant seem to write it.

Here is what I have far.
`
$users = array();
foreach($rsp->photos->photo as $photo)
{
$photo_owner = $photo["owner"];
$photo_id = $photo['id'];
$photo_title = $photo['title'];
$photo_url = "http://flickr.com/photos/" . $photo_owner . "/" . $photo_id . "/";
$flickr_getSizes = "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=$api_key&photo_id=$photo_id";
// If its not in an array, add it and output the image
if (!in_array($photo_owner, $users)){
array_push($users, $photo_owner);
$flickr_getSizes = "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=$api_key&photo_id=$photo_id";
$sizes_rsp = getXML($flickr_getSizes);
foreach($sizes_rsp->sizes->size as $size){
if ($size['label'] == $preferredSize) {
echo '<li>';
echo '<img src="' . $size['source'] .'"/>';
echo '<h3>' . $p_title . '</h3>';
echo '</li>';
}
}
}
}`
There is more above but its only getting the XML.

Related

Display specific image when a file is missing

I'm a beginner to coding and need a little help. I have a code that shows an image according to a specific value in a column of a MySQL table like this:
//Show last 10 added by ID
$sql = "(SELECT * FROM food_tbl ORDER BY food_id DESC LIMIT 10) ORDER BY food_id ASC;";
$result = $conn->query($sql);
$pathImg = "/images/";
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$pathFile = $pathImg . $row['food_name'] . ".jpg";
echo '<img src="' . $pathFile . '">';
}
}
This outputs, for example, banana.jpg ... The thing is... I would like to show a standard image when files don't actually exist, as in, if I don't have a file called banana.jpg in my "/images/" directory it would show a unknownfood.jpg file instead (which would indeed be inside that directory of course).
Now, here's the thing, I've already found some stuff over here: --- Display specific image depending on specific text in MySQL table using PHP
... and I ended up trying this:
while($row = $result->fetch_assoc()) {
$pathFile = $pathImg . $row['food_name'] . ".jpg";
if (file_exists($pathFile)) {
echo '<img src="' . $pathFile . '">';
} else {
$pathFile = $pathImg . "unknownfood.jpg";
echo '<img src="' . $pathFile . '">';
}
}
Now, what this does is this will completely ignore the files that are actually there. For example, if I try showing banana.jpg it will instead show unknownfood.jpg.
I'm pretty sure it's something really easy that I'm screwing up, or something I'm not using the way it's intended to. Thanks for the help!
When you're in PHP, using file_exists($pathFile) is the path on the server, relative to the document root (which could be /public_html/youruser), while the HTML reads from the base-folder (off the domain, /).
This means your code would have to look something like this
if (file_exists($_SERVER['DOCUMENT_ROOT'].$pathFile))
file_exists will check a filesystem path, for example /home/www/images/banana.jpg.
When you display the img tag, it will pull the image from the doc root, domain.com/images/banana.jpg.
Find the correct file system path and pass it to file_exists, it will return the correct answer.
Right now it will always return false so you will end up in the else part of the if clause.

Display each row from MySQL in a separate DIV

I have a posts table which contains a field for the posts. Let's say I have 10 posts that i want to show in 10 divs. How should I proceed in doing that? I've managed to get the full contents using a while loop, but that only shows the full contents in one place, and I want to have individual divs (so i can use different background colors) for each individual post.
Help me out please, I hope it makes sense. Just think at the way facebook displays posts for example. Each post has it's own box. I want something similar.
Snippet of the code I have to get the posts is available here:
<?php
require_once("includes/database.php"); // Get the database connection
$get_post = "SELECT full_post FROM posts";
$show_post = mysqli_query($connection, $get_post);
if (!$show_post) {
echo "Could not load post. " . "(" . mysqli_error($connection) . ")";
}
while ($post = mysqli_fetch_assoc($show_post)) {
echo $post["full_post"] . "<br />";
}
mysqli_free_result($show_post);
?>
The easiest method of doing this is creating the divs from the while function that's showing your posts and adding CSS classes to them.
Example:
while ($post = mysqli_fetch_assoc($show_post)) {
echo '<div class="blue">';
echo $post["full_post"] . "<br />";
echo '</div>';
}
I imagine you are looping through the individual posts using the while-loop. You can start a new div every round.
while($post = mysqli_fetch_assoc($result)) {
print '<div>';
print $post->content;
print '</div>';
}

php replace database result with an image

This is the first time I have ever posted a question. I am a newbie, and I hope I get all the formatting correct in this message. I love stack overflow. It has helped me get through many challenging situations. But this one has me stymied.
I would like to replace a MySQL database result with an image. I have a plant database that I'm querying to show lists of plants. My results table consists of the plant type, the botanical name, the common name, and whether it is a native plant.
The result for the native plant comes in from the database as the letter 'y' (for yes). I would like to replace that 'y' with an image of an oak leaf. The code I'm currently using displays the oak leaf on all plants, not just the ones that are native plants.
What to do? I feel like I need to insert an if statement in the while loop, but it never works when I try it. I've tried tons of different solutions for a few days now, and would like to ask for help with this.
Here's the latest code I've got that displays the image for every plant, not just the natives:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$native_image = $row['native'];
$image = $native_image;
$image = "images/oak_icon.png";
echo '<tr><td>' .
$row['plant_type'] .
'</td><td>' .
$row['botanical_name'] .
$row['common_name'] .
'</td><td>' .
$image .
'</td></table>';
}
I hope I understood your question.
Some of rows in MySQL have $row['native'], that are equals 'y'. If it is so, then you are on the right way - you have to put an if statement in your loop.
Try it
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
if ($row['native'] == "y") {
//we show our image
$image = "images/oak_icon.png";
} else {
//we don't have to show our image
$image = "";
}
echo '<tr><td>' .
$row['plant_type'] .
'</td><td>' .
$row['botanical_name'] .
$row['common_name'] .
'</td><td>' .
$image .
'</td></table>';
}
And where is your IMG tag? =)
For the future: In my Projects, if I want to save the information about image, that belongs to the row in MySQL, I just save the name of the picture, where it has to be, and if not, i save the name of empty 1x1 PNG image (usual "blank.png"). If you put blank.png in an IMG tag, that has fixed height and width, so IMG takes just the place on your page.
I hope I helped you, and this is my first Answer! =)
P.s. sorry for my english...
Explosion Pills is correct. You will need to check if the $row['native'] is 'y' then set the image html to whatever is required. Hope this helps.
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$image='';
if($row['native']=='y'){
$image = '<img src="images/oak_icon.png" />';
}
echo '<tr><td>' .
$row['plant_type'] .
'</td><td>' .
$row['botanical_name'] .
$row['common_name'] .
'</td><td>' .
$image .
'</td></table>';
}

php search module

I need your help in creating a search result page for my site. Its a simple php cms site simply fetching content from mysql.
Using a simple query to fetch search
$search = $_GET["search"];
$search = preg_replace('#[^0-9]#i','',$_GET['search']);
$searchresult = mysql_query ("SELECT * FROM pages WHERE pgcontent LIKE '%$search%'");
while ($row = mysql_fetch_array($searchresult))
{
echo '<h3>' . $row["PageTitle"] . '</h1>';
echo '<p>' . $row["PageContent"] . '</p><br /><br />';
}
What i want to do is not to display the whole page content, just the line where any word matches with the search term ... or just the first few lines of that page where the search term was found.
Friends can you help me in doing this please? it will be a big favor ... thank you
You could run post processing on $row["PageContent"]
Something like
while ($row = mysql_fetch_array($searchresult))
{
echo '<h3>' . $row["PageTitle"] . '</h3>';
$position = strpos($row["PageContent"], $search);
$snippet = substr($row["PageContent"], $position - 100, $position + 100);
echo '<p>' . $snippet . '</p>';
echo '<br /><br />';
}
Where you begin your snippet 100 characters from the beginning of the matched term and end it 100 characters after the beginning of the term. Obviously you tune this to whatever you want but this is the basic idea.
If I missed the point let me know and I may update.

PHP MySql Query performance with variable declaration

Alright - this may be a dumb question, but my desire for perfection is fueling me, so, if it is stupid and pointless, just let me know.
I have a mysql query where I fetch rows from a database table, and I need the value in some shape or form for all the columns I collect. Look at the following sample code (this could use an array to gather output instead - the fundamental question should be the same):
$query="SELECT A,B,C,D FROM table1 WHERE 'X'='Y'";
$result=mysql_query($query,$resource);
while($row=mysql_fetch_assoc($result)){
$var1=$row['A']; $var2=$row['B'];
echo '<li class="' . $row['C'] . '" id="' . $row['D'] . '">';
}
My question is as follows:
I only need $row['A'] and $row['B'] once since they are the same value for every row in this case, but I do need them once. The other values will be different for every row, and I need them as well, as in the example.
Is there a problem with continuously setting the variable throughout the loop? Or is it better to use if(!isset...? or is there some other way to do this? Or is the performance hit so minimal as to make this question irrelevant?
Thanks
why not just set the variables outside the loop.
$result=mysql_query($query,$resource);
$result_new = mysql_fetch_assoc($result);
$var1 = $result_new['A']; //considering $result_new['A'] always exists
$var2=$result_new['B']; //considering $result_new['B'] always exists
while($row=$result_new){
echo '<li class="' . $row['C'] . '" id="' . $row['D'] . '">';
}
Really don't bother with that. Unless you're looping over millions of records.
If you need more speed, don't echo in the while loop:
$result=mysql_query($query,$resource);
$string ='';
if ($result) {
$row=mysql_fetch_assoc($result);
$string .= '<li class="' . $row['C'] . '" id="' . $row['D'] . '">';
$var1=$row['A'];
$var2=$row['B'];
while($row=mysql_fetch_assoc($result)){
$var1=$row['A']; $var2=$row['B'];
$string .= '<li class="' . $row['C'] . '" id="' . $row['D'] . '">';
}
}
echo $string;
Two more things. Don't use mysql_*: Use of this extension is discouraged!
And (like I thought someone else said) if a/b is always the same, you should rethink you're database structure.
Edit:
To give a real answer to your question:
I did a small test.
Setting the value on each loop (1,000 times) takes 0.00027s. Checking inside the loop if the value was already set takes 0.00030s. So just setting it each time is even faster than checking.
the performance hit in this scenario is irrelevant, in fact i would speculate if if(isset..) guards would take a tiny bit longer - only scenario I can think of, where this question would matter would be a situation where you assign (larger) objects or arrays to $var1 and somehow force php to make a copy, not a reference
if it bothers you for aesthetic reasons (I could relate ;)), you could use a do - while loop:
//assuming you always get at least one result
$result=mysql_query($query,$resource);
$row=mysql_fetch_assoc($result);
$var1=$row['A']; $var2=$row['B'];
do {
// the loop stuff - stringify or echo the list
} while ($row=mysql_fetch_assoc($result)) ;
you can do it with a do-while expression
$query="SELECT A,B,C,D FROM table1 WHERE 'X'='Y'";
$result=mysql_query($query,$resource);
if($row=mysql_fetch_assoc($result)) {
$var1=$row['A']; $var2=$row['B'];
do {
echo '<li class="' . $row['C'] . '" id="' . $row['D'] . '">';
} while($row=mysql_fetch_assoc($result));
}

Categories