Vote once with Status: ON / OFF feature

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

v3inTe
Forum Newbie
Posts: 21
Joined: Thu Oct 06, 2011 2:39 am

Vote once with Status: ON / OFF feature

Post by v3inTe »

Good day,
im using this code posted somewhere on other forum,.

Code: Select all

<?php 
mysql_connect("localhost", "root", "password"); 
mysql_select_db("webcodez"); 
?> 
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">  
<input type="submit" name="submit" value="dai to4ka" class="bt_register" />  
</form>  
<?php  
$id = (int)$_GET['id'];  
if($_POST['submit']=='dai to4ka')  
{  
$timeout=10; //60*60*24 seconds = 1 day  
$time=time();  
$out=$time-$timeout;  
$ip=$_SERVER['REMOTE_ADDR'];  
$check_double=mysql_query("SELECT * FROM ips WHERE ip='$ip' AND time>$out")or die(mysql_error());  

if(mysql_num_rows($check_double)>0){  
echo "one time pls ";  
}  
else{  
echo "success"; 
$vote=mysql_query("INSERT INTO ips(ip,time) VALUES('$ip','$time')");  
//$query = mysql_query(" UPDATE `tz_members` SET point = point + 1 WHERE id = $id;") or die(mysql_error());  
}  

}
It works fine, but i want to add a STATUS: ON / OFF

if the status is ON then the user can vote..
otherwise, if status is OFF, the user can't vote at all + error message saying "Voting is off, you can't vote at this moment"

btw, changing of status can only be access by the admin side..
Thank you so much..
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Vote once with Status: ON / OFF feature

Post by social_experiment »

The idea is simple enough: Have an additional table with a status field (votingStatus) and allow the administrator to set said value via the administrative back-end. To use it you can just check the value before even creating the form, this would look a bit more professional imo.

Code: Select all

<?php
 // check the value from the voting status table
 $qry = "SELECT status FROM votingStatusTbl";
 $sql = mysql_query($qry);
 $ary = mysql_fetch_array($sql);
 $value = $ary['status'];

 if (strtolower($value) == 'yes') {
    // users can vote, create form, etc
 }
 else {
    // users cannot vote, don't show form, etc
 }
?>
Hth.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
v3inTe
Forum Newbie
Posts: 21
Joined: Thu Oct 06, 2011 2:39 am

Re: Vote once with Status: ON / OFF feature

Post by v3inTe »

thank you for your quick response social_experiment..
btw, i have this code.. can you check if its correct..
just disregard the table name and other names, because i changed it already..

Code: Select all

<?php 
require 'config.php';
?>

<?php
 // check the value from the voting status table
 $qry = "SELECT status FROM songreqstat";
 $sql = mysql_query($qry);
 $ary = mysql_fetch_array($sql);
 $value = $ary['status'];
if (strtolower($value) == 'yes') {

echo "<form action='<?=$_SERVER['PHP_SELF'];?>' method='post'>";
echo "<input type='submit' name='submit' 

value='Submit'></form>";

$id = (int)$_GET['id'];  
if($_POST['submit']=='Submit')  
{  
$timeout=10; //60*60*24 seconds = 1 day  
$time=time();  
$out=$time-$timeout;  
$ip=$_SERVER['REMOTE_ADDR'];  
$check_double=mysql_query("SELECT * FROM ips WHERE ip='$ip' AND 

time>$out")or die(mysql_error());  

if(mysql_num_rows($check_double)>0){  
echo "Sorry, But you've already made a request earlier. Try 

again after 30 minutes. Thank you!";  
}  
else{  
echo "Thank you for requesting a song. <br> Please wait the DJ 

to approve/decline your request. <br> Thank 

you!"; 
$reqonce=mysql_query("INSERT INTO ips(ip,time) 

VALUES('$ip','$time')");  
//$query = mysql_query(" UPDATE `tz_members` SET point = point + 

1 WHERE id = $id;") or die(mysql_error());  
}  

}
}
else {
echo "Sorry, Song Request will not be entertained at this 

moment.<br>a Current DJ is playing/finishing all 

remaining song request(s).<br>or DJ is not accepting song 

request anymore.";
}
?>
i got this error..

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/***private***/public_html/test.php on line 13

line 13 state:

Code: Select all

echo "<form action='<?=$_SERVER['PHP_SELF'];?>' method='post'>";
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Vote once with Status: ON / OFF feature

Post by flying_circus »

Correct, line 13 should be like:

Code: Select all

echo "<form action='{$_SERVER['PHP_SELF']}' method='post'>";
Actually, you should really strive to use double quotes for attributes:

Code: Select all

echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">";
It also looks like line 14 is un-terminated
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Vote once with Status: ON / OFF feature

Post by social_experiment »

You can leave $_SERVER['PHP_SELF'] away completely and simply leave the action attribute blank, that will also call the page on itself.
Another thing to note is that it is easiest to use single quotes when you want to create double quotes.

Code: Select all

echo "<form action='<?=$_SERVER['PHP_SELF'];?>' method='post'>";
// or use
echo '<form action="" method="post" >';
This is useful where you create html code but shoulnd't be used excessively because sometimes single quotes adversely affects output i.e when you print out newlines or carriage lines.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
v3inTe
Forum Newbie
Posts: 21
Joined: Thu Oct 06, 2011 2:39 am

Re: Vote once with Status: ON / OFF feature

Post by v3inTe »

Thank you so much social_experiment! & flying_circus! :) it works like a charm..
this code basically works like a song request.. i just dont put the fields for the artist, title, requested by..
anyways, how to display all records descending using a <table><tr> etc,., i know it uses loop, but i dont know how to code it in a proper way..
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Vote once with Status: ON / OFF feature

Post by social_experiment »

v3inTe wrote:i know it uses loop, but i dont know how to code it in a proper way..

Code: Select all

<?php
 // select the data from the database
 $qry = "SELECT * FROM table ORDER BY desiredField DESC";
 $sql = mysql_query($qry);

  echo '<table>';
  // echo table headers, etc
  while ($ary = mysql_fetch_array($sql)) {
    echo '<tr>';
    echo '<td>' . $ary['field1'] . '</td>';
    // echo other rows
    echo '</tr>';
  }
?>
You need to sanitize data that is printer to the browser using htmlentities() with the ENT_QUOTES flag.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
v3inTe
Forum Newbie
Posts: 21
Joined: Thu Oct 06, 2011 2:39 am

Re: Vote once with Status: ON / OFF feature

Post by v3inTe »

wow.. cool, how to put a looping number?? it depends on how many records my table has.
i.e.

1. testing 1
2. testing 2
3. testing 3

and soo on..
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Vote once with Status: ON / OFF feature

Post by social_experiment »

Code: Select all

<?php
 // 
 $qry = "SELECT * FROM table";
 $sql = mysql_query($qry);
 $rows = mysql_num_rows($sql);

 for ($i=1; $i<=$rows; $i++) {
     while ($ary = mysql_fetch_array($sql)) {
       echo $i . '. ' . $ary['field'];
     }
 }
?>
This is something i did that does what you have in mind. Hth.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
v3inTe
Forum Newbie
Posts: 21
Joined: Thu Oct 06, 2011 2:39 am

Re: Vote once with Status: ON / OFF feature

Post by v3inTe »

I got it.. :D

Code: Select all

<?php 
require 'config.php';
?>

<?php
 // select the data from the database
 $qry = "SELECT * FROM songreqlist ORDER BY srlid DESC";
 $sql = mysql_query($qry);
 $counter = 1;
  echo '<table>';
  // echo table headers, etc
  while ($ary = mysql_fetch_array($sql)) {
    echo '<tr>';
    echo '<td bgcolor="red">';
    echo $counter;
    echo '</td>';
    echo '<td bgcolor="red">';
    echo $ary['song_title'];
    echo '</td>';
    echo '<td bgcolor="red">-</td>';
    echo '<td bgcolor="red">';
    echo $ary['song_artist'];
    echo '</td>';
    // echo other rows
    echo '</tr>';
    echo '<tr>';
    echo '<td></td>';
    echo '<td>Requested by:';
    echo $ary['song_reqby'];
    echo '</td>';
    echo '</tr>';
    $counter = $counter + 1;
  }
    echo '</table>';
?>
i just tried to experiment, and it work..
but my code is kinda messed up because of the redundancy, forgive me. :)
i hope you can help me to improve and suggest for a good and clean code that will do the same.. ^^
v3inTe
Forum Newbie
Posts: 21
Joined: Thu Oct 06, 2011 2:39 am

Re: Vote once with Status: ON / OFF feature

Post by v3inTe »

:) thanks in advance.,
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Vote once with Status: ON / OFF feature

Post by social_experiment »

At first glance it looks like it will work, the only improvement i could suggest at the moment is to escape / sanitize the data that you display to the browser.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
v3inTe
Forum Newbie
Posts: 21
Joined: Thu Oct 06, 2011 2:39 am

Re: Vote once with Status: ON / OFF feature

Post by v3inTe »

Hello,
my code is working..
but i want to put a message or text in front of the 1st two data..
OLD:
======================
1. Song 1 - Artist
-------------------------
2. Song 2 - Artist 2
-------------------------
3. Song 3 - Artist 3
-------------------------
NEW:
======================
1. Song 1 - Artist
NOW PLAYING
-------------------------
2. Song 2 - Artist 2
UP NEXT
-------------------------
3. Song 3 - Artist 3
-------------------------
4. Song 4 - Artist 4
-------------------------
Thank you so much! :)
v3inTe
Forum Newbie
Posts: 21
Joined: Thu Oct 06, 2011 2:39 am

Re: Vote once with Status: ON / OFF feature

Post by v3inTe »

anyone? :D
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Vote once with Status: ON / OFF feature

Post by social_experiment »

Do you only want to add 'Song' infront of the specific track?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply