How to dump every Flickr photo url from a set? - javascript

I'm not sure if this question is possible but what I want is to take a set of photos from Flickr and dump every url into a file (text is fine). Dumping them within anchor tags would be plus. So far I've seen this and I looked through the Galleria viewers JavaScript code but no luck. I'm expecting a simple few lines of code to do this because Galleria does it but I'm not really sure where in their code Flickr is accessed to get the image urls.

I think this might be the answer actually but if someone else comes along and has another answer I'll of course accept theirs instead of just mine.
It seems that phpflickr has this example which should work:
<?php
require_once("phpFlickr/phpFlickr.php");
// Create new phpFlickr object
$f = new phpFlickr("[API Key]");
$f->enableCache(
"db",
"mysql://[username]:[password]#[server]/[database]"
);
$i = 0;
if (!empty($_POST['username'])) {
// Find the NSID of the username inputted via the form
$person = $f->people_findByUsername($_POST['username']);
// Get the friendly URL of the user's photos
$photos_url = $f->urls_getUserPhotos($person['id']);
// Get the user's first 36 public photos
$photos = $f->people_getPublicPhotos($person['id'], NULL, NULL, 36);
// Loop through the photos and output the html
foreach ((array)$photos['photos']['photo'] as $photo) {
echo "<a href=$photos_url$photo[id]>";
echo "<img border='0' alt='$photo[title]' ".
"src=" . $f->buildPhotoURL($photo, "Square") . ">";
echo "</a>";
$i++;
// If it reaches the sixth photo, insert a line break
if ($i % 6 == 0) {
echo "<br>\n";
}
}
}
?>
<h3>Enter a username to search for</h3>
<form method='post'>
<input name='username'><br>
<input type='submit' value='Display Photos'>
</form>
<p>View Source</p>

Related

Creating a new div (with its content) for each row in the database

I have a feature, where users can post thoughts and users can add comments to each thought The way it works is that, when the comments link is clicked, all comments associated with that thought_id will be loaded.
Here is the structure of my user_comments table:
id
body_of_msg
comment_posted_by
comment_posted_to
post_id (which is the id of a thought from the `user_thoughts` table)
Consider the following data:
user_thoughts table (1 row):
id: 184
thought: "hello, this is a thought from anderson."
Assume I have two rows in the user_comments table:
id: 1
body_of_msg: Comment assigned to thought_id of 184
comment_posted_by: conor
comment_posted_to: anderson
post_id: 184
id: 2
body_of_msg: Another comment assigned to thought_id of 184
comment_posted_by: alice
comment_posted_to: anderson
post_id: 184
Problem: At the moment, when I click the comments link, only one of the comments is being shown (the latest comment is being shown, so in this case, Alice's comment).
Here is the code:
<?php
// Get the comments attached to a users post...
$get_comm = mysqli_query ($connect, "SELECT * FROM user_comments WHERE post_id='$thought_id' ORDER BY post_id DESC");
$num_of_comments = mysqli_num_rows($get_comm); // get number of comments for each post by post_id
// if there are comments for the post, get its content
if ($num_of_comments !=0 || $num_of_comments == 0){
while( $comment = mysqli_fetch_assoc ($get_comm)){
$comment_body = $comment['body_of_msg'];
$comment_posted_to = $comment['comment_posted_to'];
$comment_posted_by = $comment['comment_posted_by'];
$removed = $comment['comment_removed'];
}
echo "";
/** There are other divs and content echo'd here**/
////////////////////////////////////////////
// this is where each comment is displayed
echo "
<div id='toggleComment$thought_id' class='new_comment' style='display:none;'>
<br/><b><a href = 'profile_page/$comment_posted_by'> $comment_posted_by said</a></b>: $comment_body "; ?><?php
if ($comment_posted_by == $username){
echo "<a id='remove_comment' href = '/inc/remove_comment.php'> Delete </a>";
} echo "
</div>";
/////////////////////////////////////////////
}
?>
Where $thought_id comes from:
$count = mysqli_query ($connect, "SELECT * FROM user_thoughts");
while ($row = mysqli_fetch_assoc($get_thoughts_from_db)) {
$thought_id = $row['id'];
}
What I think:
This could just be me struggling to find a solution, but, could it be that each comment is overlapping the other? My comment feature involved comments dynamically appearing below the thought, so I have utilized Javascript, to achieve this. Just thinking the block may be getting replaced by a new comment?
What I have tried:
while( $comment = mysqli_fetch_assoc ($get_comm)){
$comment_body = $comment['body_of_msg'];
$comment_posted_to = $comment['comment_posted_to'];
$comment_posted_by = $comment['comment_posted_by'];
$removed = $comment['comment_removed'];
// this is where each comment is displayed
echo "
<div id='toggleComment$thought_id' class='new_comment' style='display:none;'>
<br/><b><a href = 'profile_page/$comment_posted_by'> $comment_posted_by said</a></b>: $comment_body "; ?><?php
if ($comment_posted_by == $username){
echo "<a id='remove_comment' href = '/inc/remove_comment.php'> Delete </a>";
} echo "
</div>";
}
}
This still only shows one comment, when there are two to be shown.
Why don't you use ajax? I had done a site web that use comments (like stackoverflow) and I used a lot ajax for that. Of course, all the creations of html elements will be done in js and what you will return from php will be only json (containing the content of comments and its info).
This will help you also to load new comments without refreshing the page (setInterval).
For the answer, I have find three things that are strange, the first one is
if ($num_of_comments !=0 || $num_of_comments == 0){
which will be always true. The second thing is the fact that the echo is outside the while bloc (this probably the cause of having one comment echoed). The last thing is display none that you put in the style of the html element. So what I suggest you is to make the echo in the while block or to make an array of comments and make an iterator. After that, try to use the inspector tool of your browser to see if the code source returned contain only one comment or more. This will help you to see if the php works or not.

How to make a slideshow banner in html?

I want to make my banner changed automatically just like a slideshow. The banner I took from a folder and it can read all banner in that folder and display it on a website automatically changes. I already make it to read all banner and display it on the website, but it kind of random image, not changed except if I reload the page. This the code:
<?php
$imglist='';
//$img_folder is the variable that holds the path to the banner images.
// see that you dont forget about the "/" at the end
$img_folder = "images/";
mt_srand( (double)microtime()*1000 );
//use the directory class
$imgs = dir($img_folder);
//read all files from the directory, checks if are images and ads them to a list (see below how to display flash banners)
while ( $file = $imgs->read() )
{
if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file))
$imglist .= "$file ";
}
closedir($imgs->handle);
//put all images into an array
$imglist = explode(" ", $imglist); $no = sizeof($imglist)-2;
//generate a random number between 0 and the number of images
$random = mt_rand(0, $no); $image = $imglist[$random];
//display image
echo '<img class="img-responsive" src="'.$img_folder.$image.'" border=0>';
?>
And how to make that PHP into HTML? if I change the extention into html, the banner div show the code not the images. Thanks.
The psuedocode for what you're doing would go something like:
load images from a directory into an array
on page load select a random image from image array
display first image
every X seconds display another image from image array
to display properly, your php file needs to be loaded/served from a web server. To do this, deploy your php file to your web server and then test it out.
an example of a banner I created a few years agao, that is similar to what you are after, is here
First, make your PHP echo out a hidden <input> element, with value containing the JSON string of image paths. Using your code, something like:
<?php
$out = " /* Beginning of your page code */ "
$imgJSON = json_encode($imglist);
$out .= '<input id="bans" type="hidden" value="' .$imgJSON. '" />';
$out .= " /* Rest of your page code */ "
echo $out;
die();
Then, you can use the example below to swap the images in the banner div. I demonstrate a very simple animation, just to show how it is done.
jsFiddle Demo
HTML:
<!-- Not req if you echoed out a hidden input via PHP:
<input type="hidden" id="bans" value='["300/49","300/51","297/50","298/51"]' />
-->
<div id="banner"></div>
javascript/jQuery:
var i = $('#bans').val();
var j = JSON.parse(i);
var max = j.length - 1;
var cnt = 0;
var timerId = null;
function swapBannersTimer() {
doSwap();
timerId = setTimeout(swapBannersTimer, 3000);
}
function doSwap(){
var str = 'http://placekitten.com/' + j[cnt];
$('#banner')
.html('<img class="far_right" src="'+str+'" />')
.promise()
.done(function(){
$('img').animate({
marginLeft: '0px'
},500);
});
cnt = (cnt < max) ? cnt+1 : 0;
}
swapBannersTimer();
**CSS:*
#banner{width:300px;height:52px;overflow:hidden;}
.far_right{margin-left:300px;}
NOTE: Because this example uses the jQuery library, you must include it on your page -- usually between the DIV tags, like this:
<head>
<!-- other stuff in head -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
If you use a CDN to load jQuery, as in the above example, it is probably already be pre-loaded from other websites.
If you want some fast lessons on jQuery, find free video tuts here:
https://www.thenewboston.com/videos.php?cat=32
or at
https://phpacademy.org/videos/jquery-basics

Error when sending html source from javascript to PHP

I am using the following to encode the html source of a ckeditor in a web application.
var updateString = app.getValue('wysiwygHomePage');
var encodedString = encodeURIComponent(updateString);
alert(encodedString);
app.httpRequest("www.xxxx.com/techy/savealldata.php", "GET", function(data, error, httpResponse){
alert(data);
},
{
"updateType":"homePage","updateString":encodedString}, "String", {}, {});
}
Then at the PHP end I am using :
<?php
$updateType = $_GET["updateType"];
$updateString = $_GET["updateString"];
$updateString2 = urldecode($updateString);
echo 'success here '.$updateType .' '.$updateString2 ;
?>
I am adding some coloured tex and the html source for this is:
<p>
<span style="color: rgb(255, 140, 0);">123</span><br />
</p>
<p>
This works okay until I cut and paste more than 32 times.
I then just get error returned from the PHP call.
I presume there are to many chars arriving at the PHP end ???
Any ideas why this is happening ?
Mr WARBY.
UPDATED PHP Code.
<?php
include 'dbdata.php';
$updateType = $_POST["updateType"];
$updateString = $_POST["updateString"];
$updateString2 = urldecode($updateString);
//echo 'success here '.$updateType .' '.$updateString2 ;
if($updateType === 'homePage')
{
$query5 = "UPDATE pageText SET HTML= "."'".$updateString2."'"." WHERE ID = 12";
//echo $query5;
echo 'Home Page Updated 2';
mysql_query($query5);
}
if($updateType === 'instructionPage')
{
$query5 = "UPDATE pageText SET HTML= "."'".$updateString2."'"." WHERE ID = 13";
echo 'Instruction Page Updated 2';
mysql_query($query5);
}
if($updateType === 'FAQPage')
{
$query5 = "UPDATE pageText SET HTML= "."'".$updateString2."'"." WHERE ID = 14";
echo 'FAQ Page Updated';
mysql_query($query5);
}
?>
There are a lot of variables in play here. You need to change your debugging strategy. Instead of testing end to end each time try isolating each component.
In Javascript, call "app.getValue('wysiwygHomePage')", encode the string, decode the string, and put it right back in the editor. Do that in a loop until you can determine if the client-side is mangling anything.
If not, try encoding a complicated string in Javascript, sending it to a PHP script that decodes/re-encodes and echos it back. Do that in a loop several times.
If you still haven't found the problem try making a PHP script that takes a complicated string, INSERTS it, SELECTs it, UPDATEs it in a loop to see if you database encoding or escaping is affecting it.
If at any point you find the string changing when it shouldn't you've probably found your problem.

PHP - Insert Image into database

Hello my fellow coding buddies.
I need some help, i've got this problem.
I have this database, and i want to retrive this data into my code via JS.
My database info:
create table datadatabase (
id int(10) not null AUTO_INCREMENT PRIMARY KEY,
name varchar(25) not null default '',
text longtext(25) not null default '',
image blob not null );
And i want the data into these fields.
<img src="<?php echo $img ?>"/>
<h3> <?php echo $text.','.$name ?> </h3>
But the problem is, the text & image is a slider and i want retrive new data everytime there is a function click on the "Next" button & the ID should be random so the first slides could be id 99 and the next slide could be 158 and so on.
$('.Next').click(function(e) {
e.preventDefault();
Slide.appendSlide('<img src"<?php echo $img ?>"/><h3> </h3>')
})
How should i approach this? AND Thanks you so much. :)
First of all, it is not a good practice to store images in databases, but if you want to do this you need to create a script that output an specific image from the db (i.e. by ID) and then output the raw data with the specific content-type. (I also recommend to insert the type of the image in one of the database fields).
url: /image.php?id=5
image.php
<?php
$id = isset($_GET['id'])?intval($_GET['id']):0;
$query = mysql_query("SELECT img FROM datadatabase WHERE id = '$id'");
$row = mysql_fetch_array($query);
if($type=="pjpeg")
$type="jpeg"; // the type could change depending on the database image type
header("Content-type:$type");
echo $row['image'];
?>
for the other fields , just read the database as usual. The result:
<img src="http://..../image.php?id=<?php echo $id ?>"/>
<h3> <?php echo $text.','.$name ?> </h3>
$('.Next').click(function(e) {
e.preventDefault();
Slide.appendSlide('<img src"http://..../image.php?id=<?php echo $id?>"/><h3> </h3>')
});
It is recommended as well implementing cache and image resizing before upload to avoid server payload.
There are others approaches like base64. I prefer by myself using the filesystem with image metadata instead of using only the database for image storage to make it scalable.
Insert.php :
$bin_string = file_get_contents($_FILES["file"]["name"]);
$mysqli->query("INSERT INTO upload(image) VALUES ('" . $bin_string . "')");
Retrieve.php?id=1 :
$result = $mysqli->query("SELECT * FROM upload id=1");
$output_bin_string = $result["image"];
header("Content-Type: image/png");
header("Content-Length: " . strlen($output_bin_string ));
echo $output_bin_string;
Access Via Js :
<img src="Retrieve.php?id=1" />

JavaScript setTimeout function not executing as expected

I'm driving myself crazy here trying to figure out why this script is no longer executing as expected (it was working great yesterday, but I made a few minor changes and didn't back it up...now I can't figure out what the problem is).
<form method="POST" name="inventory_check" id="inventory_check">
<input type="text" name="stock_part" id="part_input">
<input type="hidden" name="site" value="2">
<input type="submit" value="Check Stock" id="submit">
</form>
<?php
if(isset($_POST['stock_part'])) {
$part = strtoupper($_GET['stock_part']);
$site = $_POST['site'];
$filename = 'system/inventory/'.$part.'.txt';
if(file_exists($filename)) {
$handle = fopen($filename, 'r');
$content = fread($handle, filesize($filename));
$content = trim($content, '&l0O(10U');
$content = trim($content, 'E');
fclose($handle);
date_default_timezone_set('UTC');
$mod_date = (filemtime($filename));
$current_date = time();
$subtract_date = ($current_date - $mod_date);
$subtract_date = ($subtract_date/3600);
if ($subtract_date <= 12) {
$stock = number_format($content);
echo '<script>document.inventory_check.style.display="visible";</script>';
echo '<div id="stock_results">There are '.$stock.' '.$part.' in stock.</div>';
}
else {
echo 'File too old.';
}
}
else {
echo '<iframe src="http://example.com/inventory_check.php?part='.$part.'&site='.$site.'" height="50" width="150"></iframe>';
echo '<script>document.inventory_check.style.display="none";</script>';
echo '<div align="center"><img src="http://www.aifittingsproto.com/images/load.gif"><br /><br />Searching</div>';
echo '<script>setTimeOut("recheck()", 2000);</script>';
}
}
?>
<script>
function recheck() {
document.inventory_check.stock_part.value="<?php echo $part ?>";
document.inventory_check.site.value="<?php echo $site ?>";
document.inventory_check.submit();
}
</script>
Basically this checks stock of a certain item on our internal servers (different domain than web server). What I'm trying to accomplish is the user keys in the part number...first it checks if the ftp-ed file exists on our server..if so, it checks the time it checks if its fresher than 12hrs, and then displays it if so. If not, it opens an iframe and sends the variables to our internal server for processing, which all works as expected. My issue is, I need to be able to have it recheck if the file exists after variables are passed through the iframe. I attempted to set this up by resending the variables and submitting the form every 2 seconds. Looking at the source, all the variables are populating as expected, etc. but it is not looping through and resubmitting the form. Any suggestions why this is failing, or a better approach? Thanks in advance.
You should try replacing setTimeout("window.reload()", 2000) with setTimeout("window.reload", 2000). My understanding is that you need to pass functions without the brackets for timeouts.

Categories