This is the email I just sent to Tumblr for help on this issue. If any of you know anything about this, I would GREATLY GREATLY appreciate it!
For awhile now I've been trying to display my blog on my website, but I can not seem to find anyway to do it. I mean I've tried everything. From the JS, and incrementing it through PHP.
<?php
$location = isset( $_REQUEST['nav'] ) ? $_REQUEST['nav'] : '';
$page = isset( $_POST['page'] ) ? $_POST['page'] : '0';
if (isset($_POST['next'])) {
$page++; }
else if (isset($_POST['previous'])) {
$page--;}
if ($page === 0) {
echo "<script src='http://myblog.tumblr.com/tagged/" . $_REQUEST["tag"] . "/js'></script>"; }
if ($page === 1) {
$page++;
echo "<script src='http://myblog.tumblr.com/tagged/" . $_REQUEST["tag"] . "/page/" . $page . "/js'></script>"; }
else {
echo "<script src='http://myblog.tumblr.com/tagged/" . $_REQUEST["tag"] . "/page/" . $page . "/js'></script>"; }
?>
<form method="POST">
<div class="btn-group">
<input type="submit" name="next" class="btn btn-large" value="Click For Next Page" />
</div>
<input type="hidden" name="navigation" value="location" />
<input type="hidden" name="page" value="<?php echo $page ?>" />
<input type="hidden" name="navigation" value="<?php echo $location; ?>" />
</form>
But you only really do that if you have a "tag", and I want to display all of my posts. So I even tried your JSON.
<script type='text/javascript' src='http://myblog.tumblr.com/api/read/json'></script>
<?php
$offset = 9;
$page = 1;
$placeholder = 1;
if (isset($_GET['post']) && is_numeric($_GET['post'])) {
$page = $_GET['post'];}
$start_number = ($page - 1) * $offset;
$end_number = $start_number + 9;
$num = $start_number;
while ($num <= $end_number) {
echo "<img border='0' style='margin-bottom:15px;' id='ji-tumblr-photo-myblog-" . $placeholder . "' src='' alt='' />\n";
echo "<script type='text/javascript'> document.getElementById('ji-tumblr-photo-myblog-" . $placeholder . "').setAttribute('src', tumblr_api_read.posts[" . $num . "]['photo-url-500']);</script><br />\n";
$num++;
$placeholder++;}
echo sprintf('More', $page + 1);
?>
But with the JSON you can only display the last few posts, and can't access all of them! So you can see my struggle, and all I want is a widget for my website. I post all of my photography, videos, life on Tumblr. I love Tumblr! PLEASE PLEASE PLEASE, help me in anyway possible.
Cheers,
Jade Allen Cook
You could run your Tumblr under your own (sub) domain.
You could copy all your posts to your own database. You could use your Tumblr’s sitemap to get all post URLs: http://MYBLOG.tumblr.com/sitemap.xml. Then you could scrape/download your posts.
You could run a tumblelog on your own site and duplicate all your posts to Tumblr via their API.
The last way should be preferred. That way it’s your content. Tumblr might will be gone sometime and with it all your content/URLs. But if you use your own site/server as base, you will have no problem if Tumblr goes evil. You can also easily move your content to the next hot centralized service that will appear.
Related
I am new to wordpress. I am trying to call function myprefix_edit_user_cb() to get the edit form after user clicks on edit.
function getdata()
{
$blogusers = get_users();
foreach ( $blogusers as $user ) {
echo '<span>' . esc_html( $user->user_email ) . '</span>';
$editUrl = ??
echo "<a href='".$editUrl. "'>Edit User</a>";
echo '<br>';
}
}
with function:
function myprefix_edit_user_cb(){
$user = intval($_REQUEST['user']);
echo '
<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
<label>Username</label>
<input type="text" value="' .$user->user_login . '"
<input type="submit">
';
}
According to me you need to put some request flag with your edit url.
Try the below code.
function getdata(){
$blogusers = get_users();
foreach ( $blogusers as $user ) {
echo '<span>' . esc_html( $user->user_email ) . '</span>';
$deleteUrl = add_query_arg(array('action'=>'myprefix_delete_user', 'user_id'=>$user->ID));
$editUrl = add_query_arg(array('action'=>'myprefix_edit_user', 'user'=>$user));
echo "<a href='".$deleteUrl. "'>Delete User</a>";
echo "<a href='".$editUrl. "&edit=1'>Edit User</a>";
echo '<br>';
}
}
with action and callback function with flag :
add_action('init','myprefix_edit_user_cb');
function myprefix_edit_user_cb(){
$user = intval($_REQUEST['user']);
if($user == '')
return;
if($_REQUEST['edit'] == 1 )
{
echo '
<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
<label>Username</label>
<input type="text" value="' .$user->user_login . '"
<input type="submit">
';
}
}
What you are asking all depends on where you would like to allow the user to be edited. Here is my preferred option (assuming you are doing everything on the front side of the website):
Create a page with a page template.
By default most themes come with some basic templates for how a page will look. Seeing as you may wish to add an edit form to a page, creating a custom page template would be a straight forward move. A good tutorial for creating these can be found here. Once created you would add some code like this to the template:
<?php if (isset($_GET['user_id'])): ?>
<?php $user = get_user_by('id', intval($_GET['user_id'])); ?>
<form action="#" method="post">
<label>Username</label>
<input type="text" value="<?= esc_attr($selected_user->user_login); ?>" />
<input type="submit" />
...
</form>
<?php else: ?>
<p>Error, please specify a user id!</p>
<?php endif; ?>
Which would do a basic test to make sure user_id had been passed to the page, then load the form accordingly (to improve on this I would also check to see if get_user_by returns an object before showing an edit form just in-case the user_id is invalid). In the provided example a URL (with permalinks set to page-name) would look like this:
https://example.com/edit-page/?user_id=55
There are ways of making the URL cleaner, however for now I am just trying to make sure your question is answered with a correct working example.
Koda
I have a drop down menu
<?php
for ($i=1; $i<=52; $i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
once a user has selected a number I need it to return that number of empty forms- with its number contained inside it.
can anyone help?
You will need two PHP files : in one you get the number, in the second file you generate the fields :
number_of_fields.php
<?php
// SESSION IS NECESSARY TO PASS DATA BETWEEN PAGES.
session_start();
?>
<html>
<head>
<title>By José Manuel Abarca Rodríguez</title>
</head>
<body>
<!-- FORM TO SEND THE CHOSEN NUMBER TO THE PHP SCRIPT. -->
<form method="post" action="empty_fields.php" >
<select name="my_select">
<?php
for ($i=1; $i<=52; $i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
</select>
<input type="submit" value="Generate empty fields"/>
</form>
<?php
// IF IT'S COMING BACK FROM PHP SCRIPT, DISPLAY THE DATA.
if ( IsSet( $_SESSION["empty_fields"] ) )
{ echo "Empty fields generated:" .
"<br/>" .
$_SESSION["empty_fields"];
unset( $_SESSION["empty_fields"] );
}
?>
</body>
</html>
empty_fields.php
<?php
session_start();
$fields = (int) $_POST["my_select"]; // NUMBER SENT FROM FIRST PAGE.
$my_str = "";
for ( $i = 0; $i < $fields; $i++ )
$my_str .= "<input type='text' value='" . ($i+1) . "' /><br/>";
$_SESSION["empty_fields"] = $my_str; // SAVE DATA HERE FOR FIRST PAGE.
header( "Location: number_of_fields.php" ); // BACK TO FIRST PAGE.
?>
Save previous codes in two files with the given names in your www folder. Open your browser and run localhost/number_of_fields.php
I have modified the code to make it work with onmouseover. Both files changed :
number_of_fields.php
<?php
session_start();
// FIND OUT IF PAGE IS COMING BACK FROM EMPTY_FIELDS.PHP.
if ( IsSet( $_SESSION["empty_fields"] ) )
$fields = $_SESSION["empty_fields"];
else $fields = "";
?>
<html>
<head>
<title>By José Manuel Abarca Rodríguez</title>
<script type="text/javascript">
// SENDS FORM PROGRAMMATICALLY.
function send_form () {
document.getElementById( "frm" ).submit();
}
</script>
</head>
<body>
<form method="post" action="empty_fields.php" id="frm">
<select name="my_select">
<?php
// DISPLAY SELECT OPTIONS.
for ( $i=1; $i<=52; $i++ )
{ if ( $i == $_SESSION["selected_number"] )
$sel = "selected";
else $sel = "";
echo "<option value='" . $i . "' " . $sel . ">" . $i . "</option>";
}
?>
</select>
<input type="submit" value="Generate empty fields" onmouseover="send_form();"/>
</form>
<?php
// DISPLAY INPUT FIELDS (IF IT IS COMING FROM EMPTY_FIELDS.PHP.
if ( strlen( $fields ) > 0 )
{ echo "Empty fields generated:" .
"<br/>" .
$_SESSION["empty_fields"];
unset( $_SESSION["empty_fields"] );
}
?>
</body>
</html>
empty_fields.php
<?php
session_start();
$fields = (int) $_POST["my_select"];
$my_str = "";
for ( $i = 0; $i < $fields; $i++ )
$my_str .= "<input type='text' value='" . ($i+1) . "' /><br/>";
$_SESSION["empty_fields"] = $my_str;
$_SESSION["selected_number"] = $fields;
header( "Location: number_of_fields.php" );
?>
Probably, you want the code to fire after the page has already loaded and after the user has clicked the drop-down box.
Probably, you want the number of forms to load onto the same page without refreshing the page?
In this case, you cannot use PHP -- it has already finished rendering the page. You need to use javascript (or, better yet, jQuery) in order to interact with the user.
If I understand correctly, the best solution is to use AJAX. Not to worry, it is actually super simple, especially when using jQuery.
Here are some simple examples for getting the basics of AJAX. Do not just review them -- copy the code, and try them yourself. Both jQuery and AJAX are much simpler than you may think.
A simple AJAX example
More complicated example
Populate dropdown 2 based on selection in dropdown 1
Note that you must load the jQuery library, usually in the <head> 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
http://phpacademy.org
I'm using PHP's Rand() function to generate two random numbers that I can compare against the user's, and echo out a success message if the user's answer equals (1st random number + 2nd random number.) However, I'm running into problems.
I suspected that the form was re-generating the numbers every time the form POSTED and the input was collected, so I tried using sessions instead to keep those numbers persistent. It's a mess to say the least.
I found this existing post: Problems with guess a number game , but the solution didn't remedy my problem.
<?php
if(empty($_POST))
{
$_SESSION['$number1'] = Rand(0, 100);
$_SESSION['$number2'] = Rand(0, 100);
}
if($_POST["submit"])
{
$input = $_POST['input'];
if($input == ($_SESSION['$number1'] + $_SESSION['$number2']))
{
echo "Correct! ";
}
else
{
echo "Incorrect! ";
}
}
echo "<hr><br> What is... <b>" . $_SESSION['$number1'] . " + " . $_SESSION['$number2'] . "</b>";
?>
<form action="" method="post">
<input type="text" name="input">
<input type="submit" name="submit">
</form>
<?php
echo "<b>DEBUG ANSWER: </b> " . ($_SESSION['$number1'] + $_SESSION['$number2']);
?>
Any help would be appreciated!
I changed a few things, personally, I wouldn't use the session, rather user hidden inputs. (if you're worried about security.. you shouldn't be.. numbers game, not banking site)
<?php
//Create a function to generate the random numbers (So we can re-use it)
function generateNumbers()
{
$one = Rand(0, 100);
$two = Rand(0, 100);
//Now return the random numbers
return array('number1' => $one, 'number2' => $two);
}
//Check if the submit button has been clicked
if($_POST["submit"])
{
$input = $_POST['input'];
if($input == $_POST['number1'] + $_POST['number2'])
{
echo "Correct! ";
}
else
{
echo "Incorrect! ";
}
}
//Now we create the numbers
$numbers = generateNumbers();
echo "<hr><br> What is... <b>" . $numbers['number1'] . " + " . $numbers['number2'] . "</b>";
?>
<form action="" method="post">
<input type="text" name="input">
<input type="submit" name="submit">
<!-- Personally I would rather use hidden inputs, than use the session -->
<input type="hidden" name="number1" value="<?php echo $numbers['number1'] ?>" />
<input type="hidden" name="number2" value="<?php echo $numbers['number2'] ?>" />
</form>
<?php
echo "<b>DEBUG ANSWER: </b> " . ($numbers['number1'] + $numbers['number2']);
?>
I am trying to save form data to an array so I can display it in a table later. The problem I am having is that when I click the submit button and it reloads using php_self it seems to initialize the variables everytime. Here is an example of what I am trying to do.
<?php
// if first time initialize variables
if (!isset($i)) {
echo "in initialize section<br />";
$i = 0;
$itemno[] = "";
$desc[] = "";
}
if (isset($_POST['submitbtn'])) {
$itemno[$i] = $_POST['item'];
$desc[$i] = $_POST['desc'];
echo "Item# = " . $itemno[$i] . "<br />";
echo "Desc. = " . $desc[$i] . "<br />";
$i += 1;
echo "i = $i";
var_dump($itemno);
var_dump($desc);
}
?>
<form id="submititem" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="submit" >
<input name="item" placeholder="Enter item #" size="18" />
<input name="desc" placeholder="Enter Description" size="18" />
<input name="submitbtn" type="submit" value=">">
</form>
Thanks
Ralph
The issue you're having is that PHP starts "fresh" with every page load. You'll need to store the data somewhere if you want to preserve your array over several submissions. Session or a database are two of the commonest ways of doing this.
Here's how you might do it with session:
<?php
session_start();
$items = isset($_SESSION['items']) ? $_SESSION['items'] : array();
$descriptions = isset($_SESSION['descriptions']) ? $_SESSION['descriptions'] : array();
// your logic here...
$_SESSION['items'] = $items;
$_SESSION['descriptions'] = $descriptions;
Note that if your array is likely to get very big or if you have very many users, you'll probably want to use a database to store the item/description information.
You have to deal with session
$_SESSION['itemno'][$_SESSION['i']] = $_POST['item'];
I'm converting my code from extremely long GET statements (is that the correct word?) into separate files for each page. The code I'm about to show worked fine before I moved it to it's own file.
The page's full code is:
<?
require_once('./inc/glob_head.php');
$database->openConnection();
$listOfGamesQuery = $database->queryDB("SELECT * FROM mainSite_games");
if (isset($_GET) && $_GET['action'] == 'deleteGame')
{
$gameID = $_GET['gameID'];
$database->queryDB("DELETE FROM mainSite_games WHERE id='$gameID'");
redir('viewGames.php');
}
elseif (isset($_GET) && $_GET['action'] == 'editGame')
{
$gameID = $_GET['gameID'];
$gameNameQry = $database->queryDB("SELECT gameName FROM mainSite_games WHERE id='$gameID'");
while ($gameNameDta = $database->fetchArray($gameNameQry))
{
$gameName = $gameNameDta['gameName'];
}
$gameDescQry = $database->queryDB("SELECT gameDesc FROM mainSite_games WHERE id='$gameID'");
while ($gameDescDta = $database->fetchArray($gameDescQry))
{
$gameDesc = $gameDescDta['gameDesc'];
}
?>
<form name="editGame" id="editGame" action="viewGames.php?action=processEdit&gameID=<? echo $gameID; ?>" method="POST">
<input type="text" name="gameName" value="<? echo stripslashes($gameName); ?>" /><br />
<textarea name="gameDesc" class="span12" rows="10"><? echo stripslashes($gameDesc); ?></textarea><br />
<input type="submit" name="submitEditGame" class="btn btn-primary" />
</form>
<?
}
elseif (isset($_GET) && $_GET['action'] == 'processEdit')
{
$gameID = $_GET['gameID'];
$gameName = $database->escapeString($_POST['gameName']);
$gameDesc = $database->escapeString($_POST['gameDesc']);
$database->queryDB("UPDATE mainSite_games SET gameName='$gameName' WHERE id='$gameID'");
$database->queryDB("UPDATE mainSite_games SET gameDesc='$gameDesc' WHERE id='$gameID'");
redir('viewGames.php');
} else {
echo '<div class="contCont">';
echo '<table>';
echo '<thead>';
echo '<tr>';
echo '<th>Game Name</th>';
echo '<th>Delete</th>';
echo '<th>Edit</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while ($listOfGames = $database->fetchAssoc($listOfGamesQuery)) {
echo '<tr>';
print '<td>' . stripslashes($listOfGames['gameName']) . '</td>';
print '<td>Delete</td>';
print '<td>Edit</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</body>';
echo '</html>';
}
$database->closeConnection();
?>
glob_head just provides the database class, the database connection, the functions file requirement, and styling/page structure that is constant around the site. Having stated this, the $database calls are not mistakes, and are actually defined elsewhere.
Now the problem is, in the above code, the editGame elseif block pulls information from the database successfully, therefore I assume that it must be getting the information correctly. Now, when a user clicks submit, it'll take them to the next block, processEdit, and that for some reason makes the fields blank and sets the blank values in the database. I have no idea what's going on. Maybe this needs a fresh set of eyes? Thanks in advance.
For your reference, the 'redir' calls are a custom function that uses javascript redirection instead of relying on headers. I find it cleaner, and possibly easier to use than changing the structure of the code.
You are submitting METHOD="POST" but looking at the $_GET superglobal variable. The $_GET variable is empty, because there is no GET submission being made.
Change all instances of $_GET to $_REQUEST (or $_POST), or change METHOD="POST" to "METHOD="GET".