Jump to content

[SOLVED] What's wrong with my code, should be simple.


mr_badger

Recommended Posts

I'am doing this tutorial from one of my books and I have copied it all word for word but when it comes to testing I'am gettin alot of errors, I don't know if it's old code syntax or what but I need help.

 

<?php
require("config.php");
require("functions.php");

    if(pf_check_number($_GET['id']) == TRUE) {
       $validid = $_GET['id'];
}
else {
header("Location: " . $config_basedir);

}

    require("header.php");

    $sql = "SELECT * FROM stories WHERE id = " . $validid . ";";
    $result = mysql_query($sql);
    $row = mysql_fetch_assoc($result);
    
    echo "<h1>" . $row['subject'] . "</h1>";
    echo date("D jS F Y g.iA" , strtotime($row['dateposted'])) . "<br />";
    echo nl2br($row['body']);
    
    $avgsql = "SELECT COUNT(id) AS number, AVG(rating)AS avg FROM ratings WHERE story_id = " .$validid . ";";
    $avgresult = mysql_query($avgsql);
    $avgrow = mysql_fetch_assoc($avgresult);
    
    echo "<p>";
    echo "<strong>Rating</strong> ";
    
        if($avgrow['number'] == 0) {
echo "No ratings";
}
else {
	$a = (round($avgrow['avg'] * 2) / 2) . "<br />";

	$a *= 10;

	if($a%5 == 0 && $a%10 != ) {
		$range = ($a / 10) - 0.5;
	}
	else {
		$range = $a / 10;
	}

	for($i=1;$i<=$range;$i++) {
		echo "<img src='" . $config_basedir . "news/full.gif'>";
	}

	if($a%5 == 0 && $a%10 ! = ) {
		echo "<img src='" . $config_basedir . "news/half.gif'>";
	}

	$a = $a / 10;
	$remain = 10 - $a;

	for($range=1;$range<=$remain;$range++) {
		echo "<img src='" . $config_basedir . "news/off.gif'>";
	}
}
       echo "<br />";
       
       echo "<strong>Rate this story</strong>: ";
       
       if($_SESSION['SESS_USERNAME']) {
	for($i=1;$i<=10;$i++) {
		echo "<a href='ratestory.php?id=" . $validid . "&rating=" . $i . "'>" . $i . "</a> ";
	}
}
else {
	echo "To vote, please <a href='userlogin.php'>login</a>.";
}

echo "</p>";
require("footer.php");



?>

It never siezes to amaze me how books do not teach proper coding strategies.

 

<?php
require("config.php");
require("functions.php");

if(pf_check_number($_GET['id'])) {
       $validid = $_GET['id'];
}
else {
header("Location: " . $config_basedir);

}

require("header.php");

$sql = "SELECT * FROM stories WHERE id = " . $validid . ";";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
   
echo "<h1>" . $row['subject'] . "</h1>";
echo date("D jS F Y g.iA" , strtotime($row['dateposted'])) . "<br />";
echo nl2br($row['body']);
   
$avgsql = "SELECT COUNT(id) AS number, AVG(rating)AS avg FROM ratings WHERE story_id = " .$validid . ";";
$avgresult = mysql_query($avgsql);
$avgrow = mysql_fetch_assoc($avgresult);
   
echo "<p>";
echo "<strong>Rating</strong> ";
   
if($avgrow['number'] == 0) {
echo "No ratings";
}
else {
$a = (round($avgrow['avg'] * 2) / 2) . "<br />";
$a *= 10;

if(($a%5) == 0 && ($a%10) != ) { // Does not equal WHAT?????
	$range = ($a / 10) - 0.5;
}
else {
	$range = $a / 10;
}

for($i=0;$i<$range;$i++) {
	echo "<img src='" . $config_basedir . "news/full.gif'>";
}

if(($a%5) == 0 && ($a%10) ! = ) {  // Does not equal WHAT????
	echo "<img src='" . $config_basedir . "news/half.gif'>";
}

$a = $a / 10;
$remain = 10 - $a;

for($range=0;$range<$remain;$range++) {
	echo "<img src='" . $config_basedir . "news/off.gif'>";
}
}
      echo "<br />";
      
      echo "<strong>Rate this story</strong>: ";
      
      if($_SESSION['SESS_USERNAME']) { 
	for($i=1;$i<=10;$i++) {
		echo "<a href='ratestory.php?id=" . $validid . "&rating=" . $i . "'>" . $i . "</a> ";
	}
}
else {
	echo "To vote, please <a href='userlogin.php'>login</a>.";
}

echo "</p>";
require("footer.php");

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.