0

Hi everyone,

Edited: So I figured out how to show the submit buttons. My next question is how to incorporate a security measure like html_entities in the action of the form?

<?php

require ("core/config.php");

$tablevaluegetter = mysql_query("SELECT * FROM ratingsystem WHERE typeofsite='somthingtorate' ORDER BY ratetotal DESC");

echo "<table border='1'>
<tr>
<th>Sitename</th>
<th>Siteaddress</th>
<th># of Yes </th>
<th># of No </th>
<th>Total </th>
</tr>";

while($tableratings = mysql_fetch_assoc($tablevaluegetter))
{
$whilesitename = $tableratings['sitename'];
$whilesiteaddress = $tableratings['siteaddress'];
$whilerateyes = $tableratings['rateyes'];
$whilerateno = $tableratings['rateno'];
$whileratetotal = $tableratings['ratetotal'];	

echo "<tr>";
echo "<td>" . $whilesitename . "</td>";
echo "<td>" . $whilesiteaddress . "</td>";
echo "<td><form action='' method='POST'> <input type='submit' value='$whilerateyes' /></form></td>";
echo "<td><form action='' method='POST'> <input type='submit' value='$whilerateno' /></form></td>";
echo "<td>" . $whileratetotal . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($selectdb);

?>

Thank you in advance.

Edited by d3rck: n/a

3
Contributors
16
Replies
17
Views
6 Years
Discussion Span
Last Post by d3rck
0

Instead of using buttons, I suggest you use links, it will make your work simpler. Take a look at the code below...

echo "<tr>";
echo "<td>" . $whilesitename . "</td>";
echo "<td>" . $whilesiteaddress . "</td>";
echo "<td>" . $whilerateyes . "</td>";
echo "<td>" . $whilerateno . "</td>";
echo "<td>" . $whileratetotal . "</td>";
echo "<td><a href="?id={$tableratings['id']}&rate=yes">Yes</a></td>";
echo "<td><a href="?id={$tableratings['id']}&rate=no">No</a></td>";
echo "</tr>";

I suppose that every site will have a unique id which is the primary key in your db. Change the id to whatever there is your database.

When the user clicks on either the yes or the no, you can use $_GET['rate'] and $_GET['id'] and change the rating of yes or no for that particular website, based on the id.

I hope this helps. If you have any questions please do let me know.

Edited by sudeepjd: n/a

0

Thank you so much for the reply.

I have already applied your code but there seems to be a problem whenever I click on the button.

For some reason, $_GET is not storing the "no" or "yes" value.

<?php

require ("core/config.php");


$tablevaluegetter = mysql_query("SELECT * FROM ratingsystem WHERE typeofsite='somethingtorate' ORDER BY ratetotal DESC");

echo "<table border='1'>
<tr>
<th>Sitename</th>
<th>Siteaddress</th>
<th># of Yes </th>
<th># of No </th>
<th>Total </th>
</tr>";

while($tableratings = mysql_fetch_assoc($tablevaluegetter))
{
$whilesiteid = $tableratings['id'];
$whilesitename = $tableratings['sitename'];
$whilesiteaddress = $tableratings['siteaddress'];
$whilerateyes = $tableratings['rateyes'];
$whilerateno = $tableratings['rateno'];
$whileratetotal = $tableratings['ratetotal'];	

echo "<tr>";
echo "<td>" . $whilesitename . "</td>";
echo "<td>" . $whilesiteaddress . "</td>";
echo "<td><a href='?id={$whilesiteid}&rate=yes'>$whilerateyes</a></td>";
echo "<td><a href='?id={$whilesiteid}&rate=no'>$whilerateno</a></td>";
echo "<td>" . $whileratetotal . "</td>";
echo "</tr>";
}
echo "</table>";

$ratedvaluecriteria = $_GET['rate'];
if($ratedvaluecriteria == "no") //tried echoing $ratedvaluecriteria but doesnt return a value.
{
mysql_query("UPDATE ratingsystem SET rateno='$whilerateno+1' WHERE id='$whilesiteid'");
echo "Successfully Rated No!";
}
else
echo "test failed";

mysql_close($selectdb);
?>

P.S. Also do you have a post version? I would really like to have the users be able to rate the value and stay at the same page.

Edited by d3rck: n/a

0

OK. I can't edit my previous post. (Don't know why.).

I settled for a post version using the theory of my first and only contributor:

<?php

require ("core/config.php");


$tablevaluegetter = mysql_query("SELECT * FROM ratingsystem WHERE typeofsite='somethingtorate' ORDER BY ratetotal DESC");

echo "<table border='1'>
<tr>
<th>Sitename</th>
<th>Siteaddress</th>
<th># of Yes </th>
<th># of No </th>
<th>Total </th>
</tr>";

while($tableratings = mysql_fetch_assoc($tablevaluegetter))
{
$whilesiteid = $tableratings['id'];
$whilesitename = $tableratings['sitename'];
$whilesiteaddress = $tableratings['siteaddress'];
$whilerateyes = $tableratings['rateyes'];
$whilerateno = $tableratings['rateno'];
$whileratetotal = $whilerateyes - $whilerateno;

echo "<tr>";
echo "<td>" . $whilesitename . "</td>";
echo "<td>" . $whilesiteaddress . "</td>";
echo "<td><form action='' method='POST'><input type='submit' name='addedyes' value='$whilerateyes' /><input type='hidden' value='$whilesiteid' name='addedyesid' /></form></td>";
echo "<td><form action='' method='POST'><input type='submit' name='addedno' value='$whilerateno' /><input type='hidden' value='$whilesiteid' name='addednoid' /></form></td>";
echo "<td>" . $whileratetotal . "</td>";
echo "</tr>";
}
echo "</table>";

$addedyes = $_POST['addedyes']+1;
$addedno = $_POST['addedno']+1;
$addedyesid = $_POST['addedyesid'];
$addednoid = $_POST['addednoid'];

if(isset($addedyesid))
{
mysql_query("UPDATE ratingsystem SET rateyes = '$addedyes' WHERE id = '$addedyesid'");
echo "Successfully Rated YES!";
}
elseif(isset($addednoid))
{
mysql_query("UPDATE ratingsystem SET rateno = '$addedno' WHERE id = '$addednoid'");
echo "Successfully Rated No!";
}
else
echo "You haven't rated yet!";

mysql_close($selectdb);
?>

This is a very simple code to my liking. The only things to add are auto refresh so that the page would show the updated ratings, per member vote limit, and some designs. :)

Thanks for the help. If anyone could recommend how to make the code more effective, please do post here.

For the mean time, the above would work (and I would like to hope - would help a noob like me.)

0

hey there,
why dont you put this part like this:

echo "<td>" . $whilesiteaddress . "</td>";

?> ///break the php code and start html

<td><a href="?id = <?php echo $tableratings['id'];?>&rate=yes">Yes</a></td>
<td><a href="?id = <?php echo $tableratings['id'];?>&rate=no">No</a></td>

<?php
echo "<td>" . $whileratetotal . "</td>";

then

$_GET['rate']

works
hope this helps

0

Yes, it does help.

Every single contrib counts.

Thanks for posting. :)

P.S. I used

echo "<META HTTP-EQUIV='REFRESH' CONTENT='0.1'>";

after the if statements to solve the refresh issue.

Edited by d3rck: n/a

1

Instead of using a refresh at the bottom of the page, move your PHP code to the top of the page. This way it gets processed before the page gets displayed and the processed numbers would show up and you do not need to refresh the page again.

<?php

require ("core/config.php");

$addedyes = $_POST['addedyes']+1;
$addedno = $_POST['addedno']+1;
$addedyesid = $_POST['addedyesid'];
$addednoid = $_POST['addednoid'];

if(isset($addedyesid)) {
	mysql_query("UPDATE ratingsystem SET rateyes = '$addedyes' WHERE id = '$addedyesid'");
	echo "Successfully Rated YES!";
}
elseif(isset($addednoid)){
	mysql_query("UPDATE ratingsystem SET rateno = '$addedno' WHERE id = '$addednoid'");
	echo "Successfully Rated No!";
}
else{
	echo "You haven't rated yet!";
}

$tablevaluegetter = mysql_query("SELECT * FROM ratingsystem WHERE typeofsite='somethingtorate' ORDER BY ratetotal DESC");

echo "<table border='1'>
<tr>
<th>Sitename</th>
<th>Siteaddress</th>
<th># of Yes </th>
<th># of No </th>
<th>Total </th>
</tr>";

while($tableratings = mysql_fetch_assoc($tablevaluegetter)){
	$whilesiteid = $tableratings['id'];
	$whilesitename = $tableratings['sitename'];
	$whilesiteaddress = $tableratings['siteaddress'];
	$whilerateyes = $tableratings['rateyes'];
	$whilerateno = $tableratings['rateno'];
	$whileratetotal = $whilerateyes - $whilerateno;
	
	echo "<tr>";
	echo "<td>" . $whilesitename . "</td>";
	echo "<td>" . $whilesiteaddress . "</td>";
	echo "<td><form action='' method='POST'><input type='submit' name='addedyes' value='$whilerateyes' /><input type='hidden' value='$whilesiteid' name='addedyesid' /></form></td>";
	echo "<td><form action='' method='POST'><input type='submit' name='addedno' value='$whilerateno' /><input type='hidden' value='$whilesiteid' name='addednoid' /></form></td>";
	echo "<td>" . $whileratetotal . "</td>";
	echo "</tr>";
}
echo "</table>";

mysql_close($selectdb);
?>

Also to your statement

P.S. Also do you have a post version? I would really like to have the users be able to rate the value and stay at the same page.

Both the POST and GET methods work the same way by transferring data through the url and requires the page to be refreshed (except via AJAX). The difference between the two is that while the POST method does no show the url in the address bar, the GET method does. This makes POST a little more secure. The code I gave you earlier with ?id=<id>&rate=yes/no would take the user back to the same page while also including the data in the url.

Edited by sudeepjd: Added more info

Votes + Comments
Thank you very much. Your comments and advise help create a more efficient script.
0

wow. that's a very helpful advise. It takes out additional server load and makes my script more efficient.

btw, one thing I forgot to add is the sorting process.

Initially I was intending to create a hidden input where I will add values to "ratetotal" column then sort it using a mysql ORDER command, however, I found out that everytime I try to store those values it gets converted to decimals.

$tablevaluegetter = mysql_query("SELECT * FROM ratingsystem WHERE typeofsite='somethingtorate' ORDER BY ratetotal DESC");

Question: Do you know of a script where I can make the values whole numbers so that when it gets entered to the db it is stored as a whole number?

If not, do you have a code to sort the $whileratetotal without having to store it in my db?

I think not storing a ratetotal would be better though as storing a rateyes, rateno, and ratetotal would be redundant when I can simply store a rateyes and rateno.

0

You could pull all the data to array's and then do an array_multisort instead of directly displaying the data on the page.

Pull all the data (site names, #yes, #no, calculate ratetotal) into an array using the already existing while loop.

Use array_multisort to sort the data in the arrays according to the ratetotal DESC.

Use a foreach loop to display the array's in tabular format on the page.

Try and write the code for it. Let me know if you get stuck somewhere.

0

Thank you for the quick response.

I am now trying to implement your advise, however, I am a bit stuck on how to show the values in the array.

I have so far:

while($tableratings = mysql_fetch_assoc($tablevaluegetter))
{
$whilesiteid = $tableratings['id'];
$whilesitename = $tableratings['sitename'];
$whilesiteaddress = $tableratings['siteaddress'];
$whilerateyes = $tableratings['rateyes'];
$whilerateno = $tableratings['rateno'];
$whileratetotal = $whilerateyes - $whilerateno;

}

$whileratetotalsorted = array_multisort($whileratetotal, SORT_ASC);
foreach($whileratetotalsorted as $total)
{
echo $total;
}

I think I am getting the foreach wrong and am not too confident on how to use this yet. As far as I the code goes $total doesnt seem to display anything.

Any ideas on how to implement?

0

Since, multisort is way above my head as of this time and I need a quick "band aid", I settled for a less preferred option.

That is to store the values of the ratetotal in my database.

Below is a fully working script (I am still posting the script even though it is already solved to help others in the future):

<?php
     
require ("core/config.php");

echo "<br />";

$addedyes = $_POST['addedyes']+1;
$addedno = $_POST['addedno']+1;
$addedyesid = $_POST['addedyesid'];
$addednoid = $_POST['addednoid'];
$addedratetotalyes = $_POST['addedratetotal']+1;
$addedratetotalno = $_POST['addedratetotal']-1;
     
if(isset($addedyesid))
{
mysql_query("UPDATE ratingsystem SET rateyes = '$addedyes',ratetotal = '$addedratetotalyes'  WHERE id = '$addedyesid'");
echo "Successfully Rated YES!";
}
elseif(isset($addednoid))
{
mysql_query("UPDATE ratingsystem SET rateno = '$addedno',ratetotal = '$addedratetotalno' WHERE id = '$addednoid'");
echo "Successfully Rated No!";
}
else
echo "You haven't rated yet!";

echo "<br /><br />";
     
$tablevaluegetter = mysql_query("SELECT * FROM ratingsystem WHERE typeofsite='somethingtorate' ORDER BY ratetotal DESC");

echo "<table border='1'>
<tr>
<th>Sitename</th>
<th>Siteaddress</th>
<th># of Yes </th>
<th># of No </th>
<th>Total </th>
</tr>";
     
while($tableratings = mysql_fetch_assoc($tablevaluegetter))
{
$whilesiteid = $tableratings['id'];
$whilesitename = $tableratings['sitename'];
$whilesiteaddress = $tableratings['siteaddress'];
$whilerateyes = $tableratings['rateyes'];
$whilerateno = $tableratings['rateno'];
$whileratetotal = $whilerateyes - $whilerateno;


     
echo "<tr>";
echo "<td>" . $whilesitename . "</td>";
echo "<td>" . $whilesiteaddress . "</td>";
echo "<td><form action='' method='POST'><input type='submit' name='addedyes' value='$whilerateyes' /><input type='hidden' value='$whilesiteid' name='addedyesid' /><input type='hidden' name='addedratetotal' value='$whileratetotal' /></form></td>";
echo "<td><form action='' method='POST'><input type='submit' name='addedno' value='$whilerateno' /><input type='hidden' value='$whilesiteid' name='addednoid' /><input type='hidden' name='addedratetotal' value='$whileratetotal' /></form></td>";
echo "<td>" . $whileratetotal . "</td>";
echo "</tr>";
}
echo "</table>";
       
mysql_close($selectdb);

?>
0

Since, multisort is way above my head as of this time and I need a quick "band aid", I settled for a less preferred option.

That is to store the values of the ratetotal in my database.

Below is a fully working script (I am still posting the script even though it is already solved to help others in the future):

<?php
     
require ("core/config.php");

echo "<br />";

$addedyes = $_POST['addedyes']+1;
$addedno = $_POST['addedno']+1;
$addedyesid = $_POST['addedyesid'];
$addednoid = $_POST['addednoid'];
$addedratetotalyes = $_POST['addedratetotal']+1;
$addedratetotalno = $_POST['addedratetotal']-1;
     
if(isset($addedyesid))
{
mysql_query("UPDATE ratingsystem SET rateyes = '$addedyes',ratetotal = '$addedratetotalyes'  WHERE id = '$addedyesid'");
echo "Successfully Rated YES!";
}
elseif(isset($addednoid))
{
mysql_query("UPDATE ratingsystem SET rateno = '$addedno',ratetotal = '$addedratetotalno' WHERE id = '$addednoid'");
echo "Successfully Rated No!";
}
else
echo "You haven't rated yet!";

echo "<br /><br />";
     
$tablevaluegetter = mysql_query("SELECT * FROM ratingsystem WHERE typeofsite='somethingtorate' ORDER BY ratetotal DESC");

echo "<table border='1'>
<tr>
<th>Sitename</th>
<th>Siteaddress</th>
<th># of Yes </th>
<th># of No </th>
<th>Total </th>
</tr>";
     
while($tableratings = mysql_fetch_assoc($tablevaluegetter))
{
$whilesiteid = $tableratings['id'];
$whilesitename = $tableratings['sitename'];
$whilesiteaddress = $tableratings['siteaddress'];
$whilerateyes = $tableratings['rateyes'];
$whilerateno = $tableratings['rateno'];
$whileratetotal = $whilerateyes - $whilerateno;


     
echo "<tr>";
echo "<td>" . $whilesitename . "</td>";
echo "<td>" . $whilesiteaddress . "</td>";
echo "<td><form action='' method='POST'><input type='submit' name='addedyes' value='$whilerateyes' /><input type='hidden' value='$whilesiteid' name='addedyesid' /><input type='hidden' name='addedratetotal' value='$whileratetotal' /></form></td>";
echo "<td><form action='' method='POST'><input type='submit' name='addedno' value='$whilerateno' /><input type='hidden' value='$whilesiteid' name='addednoid' /><input type='hidden' name='addedratetotal' value='$whileratetotal' /></form></td>";
echo "<td>" . $whileratetotal . "</td>";
echo "</tr>";
}
echo "</table>";
       
mysql_close($selectdb);

?>

P.S. Thank you so much for all those who have helped. :)

0

I would love to... If I could just find the solved button... :)

0

LOL... It's on the top of the page, in the forum path.

PHP Discussion Thread View First Unread >Unsolved (Mark Solved) >Views: 52

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.