jquery tag cloud not working - php

Tag System I'm Using (Link)
Simply put the inline tag is never replaced by the cloud
I have my JS loading in from a folder (links confirmed)
<script type="text/javascript" src="/jquery/jqquery-1.7.2.js"></script>
<script type="text/javascript" src="/jquery/jqcloud-1.0.1.js"></script>
<link rel="stylesheet" href="/jquery/jqcloud.css" type="text/css" media="screen">
and then i use some PHP to generate the tag array
<script type="text/jscript">
var word_list = [
<?
foreach ($array as $key => $value) {
if ($value == $average) { $weight = 2;}
else if ($value > $average) { $weight = 3;}
else if ($value < $average) { $weight = 1;}
if (strlen($key) > 1 ){
echo "{text: \"".$key."\", weight:".$weight.", url: \"http://myurl.com/tags/".$key."\", title: \"".$value."\"}";
$total -= 1;
if ($total == 0) echo ",";
}
}
?>
];
$(document).ready(function() {
$("#wordcloud").jQCloud(word_list);
});
Yet all i have is an empty div in my tag section
http://jsfiddle.net/K28Mc/ Functioning example

The problem appears to be that you never close your array:
var word_list = [ // <-Note this character..
<?
foreach ($array as $key => $value) {
if ($value == $average) { $weight = 2;}
else if ($value > $average) { $weight = 3;}
else if ($value < $average) { $weight = 1;}
if (strlen($key) > 1 ){
echo "{text: \"".$key."\", weight:".$weight.", url: \"http://myurl.com/tags/".$key."\", title: \"".$value."\"}";
$total -= 1;
if ($total == 0) echo ",";
}
}
?>
}; //<------- Right here, you fail to close the array. This should be a ]. I have a feeling this is breaking everything else.
$(document).ready(function() {
$("#wordcloud").jQCloud(word_list);
});

foreach ($result['keywords'] AS $k => $keyword )
{
$font_size = rand(10, 25);
$fonts = array("Helvetica", "Arial", "Courier", "Georgia", "Serif", "Comic Sans", "Tahoma", "Roman", "Modern");
shuffle($fonts);
$randomFont = array_shift($fonts);
echo ' ' . '<span style="font-family:' . $randomFont . '; font-size:'.$font_size.'px;">' . $keyword['name'] . ' </span>';
}

Related

How can I divide the foreach result for 2 div?

Here's the thing: I have a foreach loop that adds inputs dynamically. I need it to place part of them in one div, the rest in another. The current code is the following:
$sqla=mysql_fetch_row($sql);
$x=1;
if($x<50)
{
?>
<div class="area area-1">
<?
foreach($sqla as $key=>$values){
if ($key == "0") {
continue;
}
$icheck = ($values > 0) ? "icheck" : "";
$ichecked = ($values > 0) ? "isChecked" : "";
echo "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";
if ($key == "50") {
break;
}
$x++;
if ($values > 0) {
$new_rand_arr[] = $values;
}
}
?>
</div>
<?
}else{
?>
<div class="zodiak ar-1">
<?
foreach($sqla as $key=>$values){
if ($key == "50") {
continue;
}
$icheck = ($values > 0) ? "icheck" : "";
$ichecked = ($values > 0) ? "isChecked" : "";
echo "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";
if ($key == "62") {
break;
}
$x++;
if ($values > 0) {
$new_rand_arr[] = $values;
}
}
?>
</div>
<?
}
?>
The output puts it all in the first div, but none in the "zodiak ar-1" one. The target thing is everything after the 50-th key to go into that div. Hope that managed to explain the issue...
Thank you
Right now you are doing this:
$x=1;
if($x<50)
{
// your code
} else {
// your code
}
The problem is that you do a foreach INSIDE the if statement, so $x < 50 will ALWAYS be true because just before you do $x = 1.
Now in both foreach loop you do this :
foreach($sqla as $key=>$values){
if ($key == "0") {
continue;
}
// your code
}
foreach($sqla as $key=>$values){
if ($key == "50") {
continue;
}
// your code
}
So you use a var $x that you increment each turn but you have a $key that you use too to check if value is <50 or not?
So try something like this :
$new_rand_arr = array();
$open_first_div = false;
$open_second_div = false;
$html = "";
foreach($sqla as $key=>$values){
if ($key < "50") {
// You open your first div one time
if (!$open_first_div) {
$html .= "<div class=\"area area-1\">";
$open_first_div = true;
}
$icheck = ($values > 0) ? "icheck" : "";
$ichecked = ($values > 0) ? "isChecked" : "";
html .= "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";
if ($values > 0) {
$new_rand_arr[] = $values;
}
} else {
// You close your first div and open the second div
if (!$open_second_div) {
$html .= "</div><div class=\"zodiak ar-1\">";
$open_second_div = true;
}
$icheck = ($values > 0) ? "icheck" : "";
$ichecked = ($values > 0) ? "isChecked" : "";
$html .= "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";
if ($values > 0) {
$new_rand_arr[] = $values;
}
}
}
// After the foreach your close your div
$html .= "</div>";
// You display it
echo $html;

Submitting and retrieving cookies in PHP

We are making an voting system for our website. You can post reports to the website which are saved as data files.The current problem is that users can upvote or downvote as many times as they want because there is no barrier to stop the votes. What we want to happen is for users to be able to either upvote or downvote once.We are trying to use cookies to achieve this (not the best system, I know, since people can just clear cookies, but this is a small student project and we just need the system down). We are able to set cookies in the upvote and downvote script. We have a vote cookie which is either set to 0, -1, or 1, depending on whether the user upvoted or not.However, we are unable to retrieve the cookies accurately. When we try to retrieve the cookie vote using $_COOKIE["vote"] it doesn't give us a value.Is there any reason why this cookie is not returning a value? Thank you in advance. All of our code is provided below if you need it.
<?php
$report = $_GET["report"];
if(!isset($_COOKIE["vote"])) {
setcookie("vote", "0", time() + 315360000, $_SERVER['REQUEST_URI']);
$_COOKIE["vote"] = "0";
}
function upvote() {
if(file_exists("DataUploads/".$GLOBALS['report'])) {
if($_COOKIE["vote"] == "1") { // Problem: $_COOKIE is not being compared to "1" properly, always returns false
$filename = "DataUploads/".$GLOBALS['report'];
$line = 3;
$lines = file($filename, FILE_IGNORE_NEW_LINES);
$lines[$line] = (string)((int)$lines[$line] - 1);
file_put_contents($filename, implode("\n", $lines));
setcookie("vote", "0", time() + 315360000, $_SERVER['REQUEST_URI']);
$_COOKIE["vote"] = "0";
} else {
$filename = "DataUploads/".$GLOBALS['report'];
$line = 3;
$lines = file($filename, FILE_IGNORE_NEW_LINES);
if($_COOKIE["vote"] == "-1") {
$lines[$line] = (string)((int)$lines[$line] + 2);
} else {
$lines[$line] = (string)((int)$lines[$line] + 1);
}
file_put_contents($filename, implode("\n", $lines));
setcookie("vote", "1", time() + 315360000, $_SERVER['REQUEST_URI']);
$_COOKIE["vote"] = "1";
}
}
}
function downvote() {
if(file_exists("DataUploads/".$GLOBALS['report'])) {
if($_COOKIE["vote"] == "-1") {
$filename = "DataUploads/".$GLOBALS['report'];
$line = 3;
$lines = file($filename, FILE_IGNORE_NEW_LINES);
$lines[$line] = (string)((int)$lines[$line] + 1);
file_put_contents($filename, implode("\n", $lines));
setcookie("vote", "0", time() + 315360000, $_SERVER['REQUEST_URI']);
$_COOKIE["vote"] = "0";
} else {
$filename = "DataUploads/".$GLOBALS['report'];
$line = 3;
$lines = file($filename, FILE_IGNORE_NEW_LINES);
if($_COOKIE["vote"] == "1") {
$lines[$line] = (string)((int)$lines[$line] - 2);
} else {
$lines[$line] = (string)((int)$lines[$line] - 1);
}
file_put_contents($filename, implode("\n", $lines));
setcookie("vote", "-1", time() + 315360000, $_SERVER['REQUEST_URI']);
$_COOKIE["vote"] = "-1";
}
}
}
if($_POST["upvote_x"]) {
upvote();
}
if($_POST["downvote_x"]) {
downvote();
}
?>
<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="stylesheet" href="styles.css">
<title>View Report</title>
<link rel="icon" href="Images/favicon.ico" type="image/x-icon">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="header"></div>
<div id="navbar">
<ul>
<li>Home</li>
<li>About</li>
<li>Submit</li>
<li>View</li>
</ul>
</div>
<div id="content">
<ul style="list-style-type: none;">
<?php
if(file_exists("DataUploads/".$GLOBALS['report'])) { // Check if the report exists
echo '<h2>Report Information:</h2>';
$data = file("DataUploads/".$GLOBALS['report']); // Gets array of lines in file
$upvote_button_url = $_COOKIE["vote"] == "1"?"Images/upvote.png":"Images/upvote_noclick.png";
$downvote_button_url = $_COOKIE["vote"] == "-1"?"Images/downvote.png":"Images/downvote_noclick.png";
echo '<h3 style="display:inline">Votes: </h3><p style="display:inline" class="wordwrap">'.$data[3].'</p>';
if($data[4] == "demo") {
echo "<br>";
echo "This is a demonstrational report, and cannot be voted on.";
echo "<br>";
} else {
echo '<form method="post">';
echo '<input type="image" src="'.$upvote_button_url.'" name="upvote" id="upvote" value="Upvote" onclick="changeUpvoteImage()" /><br/>';
echo '<input type="image" src="'.$downvote_button_url.'" name="downvote" id="downvote" value="Downvote" onclick="changeDownvoteImage()" /><br/>';
echo '</form>';
echo '<p>If a report has or has less than -40 votes, it will be deleted.</p>';
}
if(file_exists('ImageUploads/'.pathinfo($GLOBALS['report'], PATHINFO_FILENAME))) {
echo '<li><img src="ImageUploads/'.$GLOBALS['report'].'" style="max-height: 600px; max-width: 700px"></li>';
} else {
echo '<img src="Images/missing.png" width="25%"><br>';
}
echo '<li><h3 style="display:inline">Location: </h3><p style="display:inline" class="wordwrap">'.htmlspecialchars($data[0]).'</p></li>';
echo '<li><h3 style="display:inline">Description: </h3><p style="display:inline" class="wordwrap">'.htmlspecialchars($data[1]).'</p></li>';
echo '<li><b><h3 style="display:inline">Urgency: </h3>';
if($data[2] < 30) {
echo "<span style='color: #1f7725'>Low</span>";
} else if($data[2] < 50) {
echo "<span style='color: #77711e'>Medium</span>";
} else if($data[2] < 80) {
echo "<span style='color: #774e1e'>High</span>";
} else {
echo "<span style='color: #771e1e'>Immediate</span>";
}
echo "</b></li>";
function delete() {
if(file_exists("DataUploads/".$GLOBALS['report'])) {
unlink("DataUploads/".$GLOBALS['report']); //delete file
}
if(file_exists("ImageUploads/".$GLOBALS['report'])) {
unlink("ImageUploads/".$GLOBALS['report']); //delete file
}
}
if($data[3] <= -40 && $data[4] != "demo") {
delete();
}
} else {
echo '<h1>No report found with the name "'.$GLOBALS['report'].'". Check the URL!</h1>';
echo '<img src="Images/missing-report.jpg" width="50%">';
}
?>
</ul>
<br>
<br>
<br>
</div>
<script>
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
function getCookieValue(a) {
var b = document.cookie.match('(^|;)\\s*' + a + '\\s*=\\s*([^;]+)');
return b ? b.pop() : '';
}
function changeUpvoteImage() {
var img = document.getElementById("upvote");
img.src = "Images/upvote.png";
return false;
}
function changeDownvoteImage() {
var img = document.getElementById("downvote");
img.src = "Images/downvote.png";
return false;
}
if(getCookieValue(getUrlVars("report") + "vote") == 1) {
changeUpvoteImage();
} else if(getCookieValue(getUrlVars("report") + "vote") == -1) {
changeDownvoteImage();
}
</script>
<script>
if(window.history.replaceState) {
window.history.replaceState(null, null, window.location.href);
}
</script>
</body>
</html>
Use this to set the cookies:
setcookie("vote", "some_value", time() + (315360000 * 30), "/");
(This worked for me)

How to write poll data to a text file? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Im trying to make a poll in php. Im trying to gather data by writing the info to a txt file. How do I get the code to write the data to a txt file?
This all the code I have in my handler, how do I make it write to my txt file. Most of the stuff at the bottom doesn't matter yet. Try to look at the code that say if ($submit == 'submit') and what follows that.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Poll</title>
</head>
<body>
<?php
//no need for sport validation is unimportant and doesnt work
if (isset($_REQUEST['Soda'])) {
$Soda = $_REQUEST['Soda'];
} else {
$Soda = NULL;
echo '<p class="error">You forgot to select your favorite soda!</p>';
}
//This is end of soda validation
if (!empty($_REQUEST['Book'])) {
$Book = $_REQUEST['Book'];
} else {
$Book = NULL;
echo '<p class="error">You forgot to write in your favorite book!</p>';
}
//End of book validation
if (isset($_REQUEST['SOTU'])) {
$SOTU = $_REQUEST['SOTU'];
} else {
$SOTU = NULL;
echo '<p class="error">You forgot to select the two biggest issues of the state of the union address!</p>';
}
//End of SOTU validation
if (isset($_REQUEST['Soda']) && !empty($_REQUEST['Book']) && isset($_REQUEST['SOTU'])) {
echo' Thank You for filling out the survey!<br> You can see the results of the pole' . " here!<br><br> Your response has been recorded.";
} else {
echo '<p class="error">Please go ' . "back" . ' and fill out the poll!<p>';
}
//End of link responses
//Define variables and make sure file works
$submit = $_REQUEST['submit'];
$filename = 'poll_data.txt';
$handle = fopen($filename, 'a');
//next is the stuff that is to be appended
if ($submit == 'Submit') {
fopen($filename, 'w');
$newdata = $Soda . PHP_EOL;
fwrite($handle, $newdata);
} else { echo 'You didn\'t click submit';}
//Now to sort the data and present it
/*explode('PHP.EOL', $filename);
$CC = 0;
$P = 0;
$MD = 0;
$SS = 0;
$BR = 0;
$DLS = 0;
$O = 0;
foreach($filename as $value) {
if ($value = 'Coca-Cola') {
$CC = $CC + 1;
}
elseif ($value = 'Pepsi') {
$P = $P + 1;
}
elseif ($value = 'MtnDew') {
$MD = $MD + 1;
}
elseif ($value ='Sprite/Sierra-Mist') {
$SS = $SS + 1;
}
elseif ('BigRed') {
$BR = $BR + 1;
}
elseif ('DontLikeSoda') {
$DLS = $DLS + 1;
}
elseif ('Other') {
$O = $O + 1;
}
}*/
?>
this should work:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Poll</title>
</head>
<body>
<?php
//no need for sport validation is unimportant and doesnt work
if (isset($_REQUEST['Soda'])) {
$Soda = $_REQUEST['Soda'];
} else {
$Soda = NULL;
echo '<p class="error">You forgot to select your favorite soda!</p>';
}
//This is end of soda validation
if (!empty($_REQUEST['Book'])) {
$Book = $_REQUEST['Book'];
} else {
$Book = NULL;
echo '<p class="error">You forgot to write in your favorite book!</p>';
}
//End of book validation
if (isset($_REQUEST['SOTU'])) {
$SOTU = $_REQUEST['SOTU'];
} else {
$SOTU = NULL;
echo '<p class="error">You forgot to select the two biggest issues of the state of the union address!</p>';
}
//End of SOTU validation
if (isset($_REQUEST['Soda']) && !empty($_REQUEST['Book']) && isset($_REQUEST['SOTU'])) {
echo' Thank You for filling out the survey!<br> You can see the results of the pole' . " here!<br><br> Your response has been recorded.";
} else {
echo '<p class="error">Please go ' . "back" . ' and fill out the poll!<p>';
}
//End of link responses
//Define variables and make sure file works
$submit = $_REQUEST['submit'];
$filename = 'poll_data.txt';
//next is the stuff that is to be appended
if ($submit == 'Submit') {
$handle = fopen($filename, 'a');
fputs($handle, $Soda.PHP_EOL);
fclose($handle);
} else { echo 'You didn\'t click submit';}
//Now to sort the data and present it
/*explode('PHP.EOL', $filename);
$CC = 0;
$P = 0;
$MD = 0;
$SS = 0;
$BR = 0;
$DLS = 0;
$O = 0;
foreach($filename as $value) {
if ($value = 'Coca-Cola') {
$CC = $CC + 1;
}
elseif ($value = 'Pepsi') {
$P = $P + 1;
}
elseif ($value = 'MtnDew') {
$MD = $MD + 1;
}
elseif ($value ='Sprite/Sierra-Mist') {
$SS = $SS + 1;
}
elseif ('BigRed') {
$BR = $BR + 1;
}
elseif ('DontLikeSoda') {
$DLS = $DLS + 1;
}
elseif ('Other') {
$O = $O + 1;
}
}*/
?>
I think if you use
fopen($filename, 'r');
that should work.
http://www.php.net/manual/en/function.fopen.php

Undefined variable : invites in yii view

This is my view code
<?php
Yii::import('common.extensions.chartjs.assets.js.*');
echo $eventdata['event_name']."<br>";
echo $contribution = Event::model()->contribution($eventdata['id'])."<br>";
echo $percentage = ($contribution/$eventdata['funding_goal_amount'])*100;
echo "<br>".Event::model()->dayleft($eventdata['id'])."<br>";
if($invites) {
$sms = 0;
$email = 0;
$totalinvites = count($invites);
foreach ($invites as $key => $data) {
if($data['type'] == 3) {
++$sms;
}
if($data['type'] == 1) {
++$email;
}
}
//gets the last 30 days
$d = array();
for($i = 0; $i < 28; $i++)
$d[] = date("d", strtotime('-'. $i .' days'));
$data=array(
);
$con=0;
foreach ($d as $key => $value) {
echo($value);
echo("<br>");
if($con==1)
{
die;
}
$data[]=callinvites($value);
}
echo(var_dump($data));
echo "Total invites are $totalinvites <br>";
echo "Invites by sms are $sms <br>";
echo "Invites by email are $email <br>";
}
echo "Google analytic api to come here <br>";
echo Event::model()->peopleContributed($eventdata['id'])."<br>";
if($contributors) {
foreach ($contributors as $key => $data) {
echo $data['fname']."<br>";
}
}
?>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses'],
['2013', 1000, 400],
['2014', 1170, 460],
['2015', 660, 1120],
['2016', 1030, 540]
]);
var options = {
title: 'Company Performance',
hAxis: {title: 'Year', titleTextStyle: {color: '#333'}},
vAxis: {title:'Sales',minValue: 0}
};
var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
<div id="chart_div">
</div>
<div class="visits">
<?php
$this->widget('common.extensions.HZL.google.HzlVisualizationChart', array('visualization' => 'AreaChart',
'data' => array(
array('day', 'Hours per Day'),
array('Work', 11),
array('Eat', 2),
array('Commute', 2),
array('Watch TV', 2),
array('Sleep', 7)
),
'options' => array('title' => 'INVITES')));
?>
</div>
<?php
function callinvites($date)
{
$today_date=date('d');
$month=date('m');
echo $month;
$year=date('y');
if($date>$today_date)
{
if($month=='01')
{
$month='12';
}
else
{
$month=$month-1;
}
if($month=='12')
{
$year=$year-1;
}
}
$count=0;
$count_email=0;
$count_mobile=0;
foreach ($invites as $key => $value) {
$date2=new DateTime($value["created"]);
$date1=$date2->format('d');
$year1=$date2->format('y');
$month1=$date2->format('m');
if($date1==$date && $year1==$year && $month==$month1)
{
$count=$count + 1;
}
if($date1==$date && $year1==$year && $month==$month1 && $value["type"]=='1')
{
$count_email=$count_email+1;
}
if($date1==$date && $year1==$year && $month==$month1 && $value["type"]=='3')
{
$count_mobile=$count_mobile+1;
}
}
return array($date,$count,$count_email,$count_mobile);
}
?>
I get an error when I call the function callinvites and says undefined variable : invites in yii view
Although I used this
echo(var_dump($invites));
this shows an array of size 20 having values from the database, so why is the error happening?
In your callinvites function try changing this line
foreach ($invites as $key => $value) {
to
foreach ($this->invites as $key => $value) {

PHP - Detect change on string

I have an array which i do a foreach($array as $key => $value)
in my $key i get
name[1][1]
name[1][2]
name[1][3]
name[2][1]
name[2][2]
how can I add detect when the first index changes from [1][3]->[2][1]
any help is appreciated.
What i want to achieve is this:
<h4>Header</h4>
name[1][1]
name[1][2]
name[1][3]
<h4>Header</h4>
name[2][1]
name[2][2]
<h4>Header</h4>
name[3][1]
name[3][2]
name[3][3]
name[3][4]
Don't know if it is the best option, but this is how i managed to do it.
<?php $k = 1; $flag1 = 0; $flag2 = 1;foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
<?php
$endpos = strpos($option_name,']');
$asd = substr($option_name,5,$endpos-5);
$this->firephp->log($asd);
if($asd % 2)
{
if($flag1 === 0)
{
echo ' <h4>Header '. $k .'</h4>';
$flag1 = 1;
$flag2 = 0;
$k++;
}
}
else
{
if($flag2 === 0)
{
echo ' <h4>Header '. $k. '</h4>';
$flag2 = 1;
$flag1 = 0;
$k++;
}
}
?>
You can try like
foreach($name as $parent_key => $parent_value){
echo "<h4>Header</h4><br/>";
foreach($name[$parent_key] as $key=>$value)
{
echo $name[$i][$key]."<br/>";
}
}
foreach($array as $key => $value){
$valuevalues = array();
foreach($value as $val){
if($val != "" && !isset($valuevalues[$key][$val]))
$valuevalues[$key][$val] = "different-than-previous";
if(!isset($valuevalues[$key][$val]))
$valuevalues[$key][$val] = "equal-to-first-value";
}
}

Categories