Jump to content

[SOLVED] Help please


jck

Recommended Posts

im trying to create a script for my jokes site that lets u see what others have voted for any joke the strucure of the table is simple itemid,rating and username this is the sample code i wrote for a joke of item id 54 but when i run it i get blank page please help me

<?
require_once("conn.php");
require_once("templates/MainHeader.php");

$q1 = "select * from dd_rating where ItemID = '54' ";
$r1 = mysql_query($q1) or die(mysql_error());
$numvote=mysql_numrows($r1)
echo "<b><center>See what Rating others gave for this joke </center></b><br><br>";

$i=0;
while($i<$numvote){

$name=mysql_result($r1,$i,"username");
$rate=mysql_result($r1,$i,"Rating");

echo"$name." had given a rating of".$rate." to this joke<br>";

$i++
}
require_once("templates/MainFooter.php");
?>

 

Please tell me whats wrong with it

Link to comment
Share on other sites

how about:

<?php
require_once("conn.php");
require_once("templates/MainHeader.php");

$q1 = "select * from dd_rating where ItemID = '54' ";
$r1 = mysql_query($q1) or die(mysql_error());
$numvote = mysql_num_rows($r1); //<~Semi colon missing
echo "<b><center>See what Rating others gave for this joke </center></b><br><br>";

$i=0;
while($i<$numvote){
$name=mysql_result($r1,$i,"username");
$rate=mysql_result($r1,$i,"Rating");

echo $name." had given a rating of ".$rate. " to this joke<br>";

$i++
}
require_once("templates/MainFooter.php");

?>

Link to comment
Share on other sites

Try this.

 

<?php
require_once("conn.php");
require_once("templates/MainHeader.php");

$request = mysql_query("
SELECT rt.username, rt.Rating
FROM dd_rating AS rt
WHERE ItemID = 54") or die(mysql_error());

echo '<b><center>See what Rating others gave for this joke </center></b><br><br>';

while ($row = mysql_fetch_assoc($request))
{
$name = $row['username'];
$rate = $row['Rating'];

echo '
' . $name . ' had given a rating of ' . $rate . ' to this joke.<br />';
}
mysql_free_result($request);

require_once("templates/MainFooter.php");
?>

Link to comment
Share on other sites

Try

<?php
require_once("conn.php");
require_once("templates/MainHeader.php");

if (isset($_GET['ItemID']) && is_numeric($_GET['ItemID']))
{
$request = mysql_query("
	SELECT rt.username, rt.Rating
	FROM dd_rating AS rt
	WHERE ItemID = $_GET[itemID]") or die(mysql_error());

echo '<b><center>See what Rating others gave for this joke </center></b><br><br>';

if (mysql_num_rows($request) > 0)
{
	while ($row = mysql_fetch_assoc($request))
	{
		$name = $row['username'];
		$rate = $row['Rating'];

		echo '
		' . $name . ' had given a rating of ' . $rate . ' to this joke.<br />';
	}
}
else
	echo 'No ratings for this joke.';
}
else
echo 'The ItemID is not valid.';

require_once("templates/MainFooter.php");
?>

Link to comment
Share on other sites

im very new to this could u please explain me what to what was that

rt.username thing and obviously ur code will be more efficient than mine can u explain how??

 

and im very lucky that a devoloper of smf helped me :)

Link to comment
Share on other sites

Ok lets break it down.

 

This part checks first to see if "ItemID" is set in the URL.  If it's set then it checks to see if it's numeric.  The point of that is so that you can have some sanity checks and make sure that a number was entered.

if (isset($_GET['ItemID']) && is_numeric($_GET['ItemID']))

 

In dd_rating AS rt the rt part is the alias for that table.  You usually use table alias when you are joining more than one tables.  You don't want to keep typing xxx.yyy for each column that you want.

SELECT m.id_member, m.name, m.age, rt.ratings
FROM members AS m
LEFT JOIN ratings AS tr ON (tr.id_member = m.id_member)

Imagine having to write members.x each time and so on.

 

Also it is good to select the columns that you will be using instead of doing a SELECT *.  Doing that will select all the columns in that table.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.