I have been trying to catch a variable in an array when an item is clicked on and have been struggling a great deal.
I have a list of items which each have their own name in a database. When the page is loaded these items are listed on the page and can be clicked on to open a new window which displays certain content depending on the items name in the array.
Currently I have managed to get the items names into an array when they are displayed on the page. But when I click on one item the variable used to open the unique window for that item contains every variable in the array (not just the variable name of the item I clicked on).
In my PHP code I go through the database (depending on if isLive == 1) and add each name of the items 'channel_title' into an array named $chName.
$chResult = mysql_query($chSQL);
if ($chResult) {
$chNum = mysql_num_rows($chResult);
if ($chNum>0) {
//Create the arraw
$chName = array();
while($row = mysql_fetch_array($chResult)) {
//Add channel name to array
$chName[] = ($row['channel_title']);
if ($row['is_live']=="1") {
$whatIsLive = "true";
} else {
$whatIsLive = "false";
}
echo
'<li id="'.$row['channel_id'].'" class="list-group-item col-xs-12 col-sm-6 col-md-4 col-lg-3">
<div class="item">
<div class="item-head">
<div class="badges">';
if ($row['is_live']=="1") {
echo '<span class="badge-live">Live</span>';
}
echo
'</div>
</div>
<div class="item-image">
<a href="'.SERVERPATH.'channels/'.urlencode($row['channel_title']).'"target="window" onclick="openStreamPopup(); return false;" data-islive="'.$whatIsLive.'" title="'.$row['channel_title'].'">';
$activeSSImage = 'userData/'.$row['user_id'].'/channelImages/ss_'.$row['channel_id'].'_t.jpg';
$defaultSSImage = 'images/ss_default.jpg';
if (file_exists($activeSSImage)) {
echo '<img src="'.$activeSSImage.'?rand='.rand(0, 99999999).'" alt="'.$row['channel_title'].'" width="250" height="200">';
} else {
echo '<img src="'.$defaultSSImage.'" alt="'.$row['channel_title'].'" width="250" height="200">';
}
echo '<span class="image-cover"></span>
<span class="play-icon"></span>
</a>
</div>
</div>
</div>
</li>';
}
} else {
echo '';
}
} else {
echo '';
}
In this above code is:
<a href="'.SERVERPATH.'channels/'.urlencode($row['channel_title']).'"target="window" onclick="openStreamPopup(); return false;" data-islive="'.$whatIsLive.'" title="'.$row['channel_title'].'">';
which sends the compiler to a JavaScript function which grabs the array and assigns it to a JavaScript variable - allowing a new window to open with window.open(). This is done with this code:
<script type="text/javascript">
var theChannel = <?php echo(json_encode($chName)); ?>; //Grab the Array $chName - call it theChannel
var windowObjectReference = null; // global variable
function openStreamPopup() {
if(windowObjectReference == null || windowObjectReference.closed)
/* if the pointer to the window object in memory does not exist
or if such pointer exists but the window was closed */
{
windowObjectReference = window.open("channels/"+theChannel,
"window", "resizable,scrollbars,status"); //Open link with theChannel (channel_title) as url
/* then create it. The new window will be created and
will be brought on top of any other window. */
}
else
{
windowObjectReference.focus();
/* else the window reference must exist and the window
is not closed; therefore, we can bring it back on top of any other
window with the focus() method. There would be no need to re-create
the window or to reload the referenced resource. */
};
console.log( "function complete" );
console.log( theChannel );
}
</script>
The problem I am having is that in this created variable contains every variable of the array. Therefore when I click on an item it adds the URL of each 'channel_title' and not just the 'channel_title' of the item I clicked on.
I have been trying for hours now to get my head around it - I am terrible with PHP and arrays.
The effect I want to achieve is like on http://onperiscope.com/ when you click on a stream and it opens the stream in a new window.
Thank you for your time, please let me know if I have not given enough information.