I have a Wordpress page where a user can book language classes. There are different booking options, so I built the page in a way that the booking form charges dinamically from URL. The DOM charges 16 different shortcodes but in front-end it's only displayed the one that user enters in the URL by setting some parameters. The only problem about this is that the page takes a lot of time to charge and display the form.
I'm trying to figure out a way to charge only one shortcode and only set dinamically the value that differs between all shortcodes. After some researching (as my PHP knowledge is limited), I tried this code, and it worked:
function php_insert() {
if( is_page( 645 ) ) {
$awQueryString = $_SERVER['QUERY_STRING'];
parse_str($awQueryString, $awQueryStringParams);
$language = $awQueryStringParams['lang'];
$pack = $awQueryStringParams['pack'];
if (empty($awQueryString)) {
echo 'The URL entered is invalid. Please refresh the page with the correct URL or contact the web admin.';
}
else if ($language == 'english') {
if ($pack == '5') {
echo do_shortcode('[ameliacatalog package=13]');
}
if ($pack == '10') {
echo do_shortcode('[ameliacatalog package=14]');
}
if ($pack == '25') {
echo do_shortcode('[ameliacatalog package=15]');
}
}
}
}
add_action('wp_enqueue_scripts', 'php_insert');
The problem now is that this page is protected by password, and this snippet charges the code at the top of the page and it doesn't wait to check that the password is correct.
I don't know if this is the best way to do this workaround. Also don't know how can I do to solve the password check and location problem.
I hope I have explained everything well.
Thanks
I'm pretty new to HTML, PHP and what not, but I have a basic understanding, I realize that there are obviously features for Ordering by Numeric/Alphabetical. However the feature I want I really can't find anything about, nor search the right term to find a guide for what I'm looking for
Basically I have a custom stream page for clan members, above each stream is text and an image stream status: online/offline, this feature works 100% when the person streams it will change from online to offline
However all of the streams are muddled up, a stream that is online is found at the bottom of a page or in random places
I want it to bring streams that are currently online to the top, is that even possible?
<?php
$channelName = htmlspecialchars($_GET['channel'], ENT_QUOTES);
$clientId = ''; // Register your application and get a client ID at http://www.twitch.tv/settings?section=applications
$online = 'online.png'; // Set online image here
$offline = 'offline.png'; // Set offline image here
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.strtolower($channelName).'?client_id='.$clientId), true);
if ($json_array['stream'] != NULL) {
$channelTitle = $json_array['stream']['channel']['display_name'];
$streamTitle = $json_array['stream']['channel']['status'];
$currentGame = $json_array['stream']['channel']['game'];
echo "$channelTitle is <img src='$online' alt='Online' /> playing $currentGame";
} else {
echo "$channelName is <img src='$offline' alt='Offline' />";
}
?>
Is what i'm using to pull the stream status &
<A NAME="SADMAN"><center><font size="3" color="red"><span style="color:#FFFFFF">Konvict</span> - Sadmanwhosane - <span style="color:#CEE3F6">Stream status:</span> <img src="http://teamewix.com/stream.php?streamuser=sadmanwhosane87"></font>
is all im doing to show it
Use JQuery Table Sorter:
link is here.
So i'm guessing that the page isn't refreshing? then you use ajax to get the latest data and then update the details of the page.
Link to jQuery ajax (for asynchronously getting data)
Link to jQuery html (for updating the html with the new data)
If you know php and jquery well then this will be an easy task and the links will lead you the way. But if you don't then that's another story. Goodluck!
EDIT
My above answer is async, no page refresh involved.
You have a database with users who are online and offline right? You use jQuery ajax to find out who's online and offline. Then when you have the data. You use jQuery html to change the elements of the page.
If you just want to position them depending on their status during page refresh then you use basic php and html. Use arrays. 3 arrays to be exact.
Array1 - All teams
Array2 - Online Teams
Array3 - Offline Teams
You just get all online teams from Array1 and transfer it to Array2 and all offline teams from Array1 to Array3.
Then echo the teams from Array2 first. Then Array3 last.
But again this is on page refresh. If you don't want any page refresh follow my first answer.
EDIT2
your code:
if ($json_array['stream'] != NULL) {
$channelTitle = $json_array['stream']['channel']['display_name'];
$streamTitle = $json_array['stream']['channel']['status'];
$currentGame = $json_array['stream']['channel']['game'];
echo "$channelTitle is <img src='$online' alt='Online' /> playing $currentGame";
} else {
echo "$channelName is <img src='$offline' alt='Offline' />";
}
to this code:
if ($json_array['stream'] != NULL) {
$channelTitle = $json_array['stream']['channel']['display_name'];
$streamTitle = $json_array['stream']['channel']['status'];
$currentGame = $json_array['stream']['channel']['game'];
array_push($OnlineTitleArray,$channelTitle);
array_push($OnlineCurrentGameArray,$currentGame);
} else {
array_push($OfflineArray,$channelName);
}
foreach($OnlineTitleArray as $key => $val){
echo $val.' is <img src='.$online.' alt=\'Online\' /> playing '.$OnlineCurrentGameArray[$key];
}
foreach($OfflineArray as $key => $val){
echo $channelName.' is <img src='.$offline.' alt="Offline" />';
}
So you need the arrays to put the data in using array_push then foreach to display the data. Hope this helps. And check this if you find it helpful
I have a CodeIgniter PHP application that shows two movie covers. Beside them is a "random movie" button that uses AJAX to replace the two movies with a new set of movies. You can continue to click this, over and over, and see it continue to replace the images of the movie covers. The first two covers to show are set as the defaults, and they should never show after the user has clicked the random movie button. The problem is this: When clicking the random movie button, it will some times take many clicks to finally show a new cover. That is, the same cover will be returned multiple times in a row. The two different covers being fetched are being called from slightly different URLs, so they will rarely both break at the same time. This lets me know that it is refreshing, but that the function is returning the same movie multiple times. If I access the url that is being called via AJAX directly, I never see this take place since I have used the Session class to store the last movie's and exclude it from the SQL query (i.e. WHERE id NOT IN ($default_movie, $last_movie)). Any idea why accessing the url directly would work fine, but when calling via AJAX, I'm seeing this behavior?
I know this may not have been as clear as possible, so let me know if I can clarify something that doesn't make sense. I'll add code if that helps as well. Thanks friends!
Query to get random movie:
SELECT * FROM (`movies`) WHERE `id` NOT IN (2, 10) ORDER BY RAND() LIMIT 1
Model method:
public function getRandom($count = 1, $featured = FALSE, $series = FALSE, $exclude = 0, $last = 0) {
$this->db->order_by('id', 'random');
$this->db->limit(1);
$conditions = array();
if ($exclude > 0) {
$conditions['id !='] = $exclude;
}
if ($last > 0) {
if (!empty($conditions['id !='])) {
$conditionsNotIn = "id NOT IN (" . $conditions['id !=']. ", $last)";
unset($conditions['id !=']);
$this->db->where($conditionsNotIn);
} else {
$conditions['id !='] = $last;
}
}
if ($featured) {
$conditions['featured'] = 1;
}
if ($series) {
$conditions['current_series'] = 1;
}
$movie = $this->db->get_where('movies', $conditions);
$movie = $movie->row();
if (!is_null($movie)) {
return $movie;
} else {
return FALSE;
}
}
Any idea why accessing the url directly would work fine, but when
calling via AJAX, I'm seeing this behavior?
I have an idea yes.
Browser caching.. PITA!
Try turning off caching explicitly:
$.ajaxSetup({cache: false});
Put that before your ajax request, assuming you're using jQuery.
If you're not you need to append some random variable to the url, this keep the browser from caching the requests.
I want to run/execute jquery code on facebook, twitter, google plus iframe.
I have tried to do it in many ways but it is not working.
Code is always showing error none/object not found.
Actually I want to know whether currently logged-in user has already done like/follow/+ .
If the current logged-in user has already done like/follow then I need to hide like/follow links,
If you are using the PHP Facebook SDK you can query it as follows:
$likes = $facebook->api('me/likes');
$fanpageliked = false;
$appliked = false;
foreach ($likes['data'] as $ilike) {
if ($ilike['id'] == $fanpageid) {$fanpageliked = true;}
if ($ilike['id'] == $appid) {$appliked = true; }
}
If your fanpage id is stored in $fanpageid, or app id in $appid, then $fanpageliked will be true (and/or appid).
So if you are only looking for a fanpage id, remove the appid if and add a break:
if ($ilike['id'] == $fanpageid) {
$fanpageliked = true;
break;
}
After that, you can have the following:
if (!$fanpageliked) {
echo "Like us NOW!";
// .... display like button etc
}
else {
echo "You are ours now. Bwa ha ha!";
}
You can't do that with jquery but you can use their own APIs to accomplish what you need. Facebook for example already has all that in their API.
Update, Solved:
After all this I found out that I was calling an old version of my code in the update ajax.
'boardControl.php' instead of 'boardUpdate.php' These are the kinds of mistakes that make programing fun.
I'm writing a browser gomoku game. I have the ajax statement that allows the player to play a piece.
$(document).ready(function() {
$("td").live('click',function(){
var value = $(this).attr('id');
$.get('includes/boardControl.php',{play: value, bid: bid});
});
});
value = board square location
bid = board ID
Before creating a user login for player identification, the server side php had a temporary solution. It would rotate the piece state for the squares when clicked instead of knowing what player to create them for.
After creating login stuff I set a session variable for the player's ID. I was hoping to read the session ID from the php during the ajax request and figure out what player they are from there.
session_start();
...
$playerId = $_SESSION['char'];
$Query=("SELECT p1, p2 FROM board WHERE bid=$bid");
$Result=mysql_query($Query);
$p1 = mysql_result($Result,0,"p1");
$p2 = mysql_result($Result,0,"p2");
$newPiece = 0; //*default no player
if($playerId == $p1)
$newPiece = 1;
if($playerId == $p2)
$newPiece = 2;
For some reason when I run the full web app, the pieces still cycle though, even after I deleted the code to make them cycle.
Furthermore, after logging in If i manually load the php page in the browser, it modifies the database correctly (where it only plays pieces belonging to that player) and outputs the correct results.
It seems to me that the session is not being carried over when used with Ajax. Yet Google searches tell me that, sessions do work with Ajax.
Update: I'm trying to provide more information.
Logging in works correctly. My
ID is recognized and I printed it
out next to the board to ensure that
I was retrieving it correctly.
The ajax request does update the
board. The values passed are
correct and confirmed with firebug's
console. However instead of placing
pieces only for the player they
belong to it cycles though the piece
states (0,1,2).
When manually browsing to
boardUpdate.php and putting in the
same values sent from the Ajax the
results seen in the echo'ed response
indicates that the corresponding
piece is played each time as
intended.
Same results on my laptop after
fresh load of firefox.
Manually browsing to
boardUpdate.php without logging in
before hand leave the board
unchanged (as intended when no user
is found in the session).
I've double checked the that
session_start() is on the php files
and double checked the session ID
variables.
Hope this extra information helps, i'm running out of ideas what to tell you. Should I load up the full code?
Update 2:
After checking the Ajax responce in fire-bug I realized that the 'play' request does not get a result, and the board is not updated till the next 'update'. I'm still looking into this but I'll post it here for you guys too.
boardUpdate.php
Notable places are:
Refresh Board(line6)
Place Piece(line20)
function boardUpdate($turnCount) (line63)
<?php
session_start();
require '../../omok/dbConnect.php';
//*** Refresh Board ***
if(isset($_GET['update']))
{
$bid = $_GET['bid'];
$Query=("SELECT turn FROM board WHERE bid=$bid");
$Result=mysql_query($Query);
$turnCount=mysql_result($Result,0,"turn");
if($_GET['turnCount'] < $turnCount) //** Turn increased
{
boardUpdate($turnCount);
}
}
//*** Place Piece ***
if(isset($_GET['play'])) // turn order? player detect?
{
$squareID = $_GET['play'];
$bid = $_GET['bid'];
$Query=("SELECT turn, boardstate FROM board WHERE bid=$bid");
$Result=mysql_query($Query);
$turnCount=mysql_result($Result,0,"turn");
$boardState=mysql_result($Result,0,"boardstate");
$turnCount++;
$playerId = $_SESSION['char'];
$Query=("SELECT p1, p2 FROM board WHERE bid=$bid");
$Result=mysql_query($Query);
$p1 = mysql_result($Result,0,"p1");
$p2 = mysql_result($Result,0,"p2");
$newPiece = 0; //*default no player
if($playerId == $p1)
$newPiece = 1;
if($playerId == $p2)
$newPiece = 2;
// if($newPiece != 0)
// {
$oldPiece = getBoardSpot($squareID, $bid);
$oldLetter = $boardState{floor($squareID/3)};
$slot = $squareID%3;
//***function updateCode($old, $new, $current, $slot)***
$newLetter = updateCode($oldPiece, $newPiece, $oldLetter, $slot);
$newLetter = value2Letter($newLetter);
$newBoard = substr_replace($boardState, $newLetter, floor($squareID/3), 1);
//** Update Query for boardstate & turn
$Query=("UPDATE board SET boardState = '$newBoard', turn = '$turnCount' WHERE bid = '$bid'");
mysql_query($Query);
// }
boardUpdate($turnCount);
}
function boardUpdate($turnCount)
{
$json = '{"turnCount":"'.$turnCount.'",'; //** turnCount **
$bid = $_GET['bid'];
$Query=("SELECT boardstate FROM board WHERE bid='$bid'");
$Result=mysql_query($Query);
$Board=mysql_result($Result,0,"boardstate");
$json.= '"boardState":"'.$Board.'"'; //** boardState **
$json.= '}';
echo $json;
}
function letter2Value($input)
{
if(ord($input) >= 48 && ord($input) <= 57)
return ord($input) - 48;
else
return ord($input) - 87;
}
function value2Letter($input)
{
if($input >= 10)
return chr($input += 87);
else
return chr($input += 48);
}
//*** UPDATE CODE *** updates an letter with a new peice change and returns result letter.
//***** $old : peice value before update
//***** $new : peice value after update
//***** $current : letterValue of code before update.
//***** $slot : which of the 3 sqaures the change needs to take place in.
function updateCode($old, $new, $current, $slot)
{
if($slot == 0)
{// echo $current,"+((",$new,"-",$old,")*9)";
return letter2Value($current)+(($new-$old)*9);
}
else if($slot == 1)
{// echo $current,"+((",$new,"-",$old,")*3)";
return letter2Value($current)+(($new-$old)*3);
}
else //slot == 2
{// echo $current,"+((",$new,"-",$old,")";
return letter2Value($current)+($new-$old);
}
}//updateCode()
//**** GETBOARDSPOT *** Returns the peice value at defined location on the board.
//****** 0 is first sqaure increment +1 in reading order (0-254).
function getBoardSpot($squareID, $bid)
{
$Query=("SELECT boardstate FROM board WHERE bid='$bid'");
$Result=mysql_query($Query);
$Board=mysql_result($Result,0,"boardstate");
if($squareID %3 == 2) //**3rd spot**
{
if( letter2Value($Board{floor($squareID/3)} ) % 3 == 0)
return 0;
else if( letter2Value($Board{floor($squareID/3)} ) % 3 == 1)
return 1;
else
return 2;
}
else if($squareID %3 == 0) //**1st spot**
{
if(letter2Value($Board{floor($squareID/3)} ) <= 8)
return 0;
else if(letter2Value($Board{floor($squareID/3)} ) >= 18)
return 2;
else
return 1;
}
else //**2nd spot**
{
return floor(letter2Value($Board{floor($squareID/3)}))/3%3;
}
}//end getBoardSpot()
?>
Please help, I'd be glad to provide more information if needed.
Thanks in advance =)
From the small snippet of code we have, it's difficult to tell what your problem might be. What I can say is that session_start should be one of the first things you do on each page where you're expecting to use the session. After that, I would just immediately do a var_dump of $_SESSION to see that the data is in there (put a die right after that). It is quite possible that your true problem lies somewhere else, and that the session is in fact working. Is there a problem with your login code, for example, that is causing it to wipe out the session?
You can use Firebug to look at the raw results of your AJAX calls, which should be helpful, since your script appears to work if you directly visit the page.
Cases where I've seen sessions not work as expected have generally been that session_start is being called too often or too late. The other possibility is that you have an insanely short timeout, but that sounds unlikely.
Finally, you can make sure that your PHP install is set to use cookie sessions. It's very unlikely at this point that it wouldn't be, but you could look.
One potential problem in this code is the use of $.get - it is cached by IE, so your server code doesn't run every time. Try using $.ajax with cache set to false:
$.ajax({
type: 'GET',
url: 'includes/boardControl.php',
cache: false,
data: {play: value, bid: bid}
});
Just happened to me, in my case was that i was importing a config file with the session_start and since i had deactivated errors i couldn't see that the import was never happening. Just triple check this, I know it's the basic.