Jump to content

Table Help


php_newbie2010

Recommended Posts

Hey everyone,

 

Hopefully someone can help me here I have just started to teach myself PHP with MySQL and I have come across the first problem that I cannot seem to solve. I have a db that has reviews and comments on it for movies. When I try to echo the review using a heredoc syntax it echos each comment twice.  I am new to this  so I am not quite sure what to post but I will post the part of the code that I believe is causing the problem (it may be earlier in the code, but I set up the or die(mysql_error($db)); for any problems that may occur early on, and I didn't receive any syntax errors.

 

 

 

while ($row = mysql_fetch_assoc($result)) {

$date = $row['review_date'];

$name = $row['review_name'];

$comment = $row['review_comment'];

$rating = generate_rating($row['review_rating']);

 

echo <<<ENDHTML

<tr>

<td style="vertical-align:top; text-align: center;">$date</td>

<td style="vertical-align:top;">$name</td>

<td style="vertical-align:top;">$comment</td>

<td style="vertical-align:top;">$rating</td>

</tr>

ENDHTML;

}

 

 

 

I think that the problem is somewhere in there, but I don't know where. Again my problem is that each review is posted TWICE instead of posting each review once..... its like the program loops the code and parses it again. :confused:  I hope that I have provided you with what is necessary to help me, if not, let me know what you need and I will post it. I am hosting on a localhost Apache server so I cannot show you the website unfortunately....I can take screenshots of the page to show you exactly what happens.... let me know and I hope someone can find the problem

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/189126-table-help/
Share on other sites

Buddski thanks for you help. First, Im not quite sure what you mean when you say "the code inside your code tags",  as my name implies Im a newbie trying to teach myself.

 

I attached the whole code for this page, and I think what you are asking for is the very first bit of code there.

 

Also, I wrote a php program to put the data in, I may have run it twice. I read somewhere about a program I can download for MySQL that gives me GUI to recall the data and see the tables.  Do you have some advice for such a program?

 

Thanks again

 

 

<?php

 

//function to generate ratings

 

function generate_rating($rating) {

$movie_rating = '';

for ($i = 0; $i < $rating; $i++) {

$movie_rating .= '<img src="star.png" alt="*"/>';

}

return $movie_rating;

}

 

// take in the id of a director and reutnr his/her full name

function get_director($director_id) {

global $db;

 

$query = 'SELECT

people_fullname

FROM

people

WHERE

people_id = ' . $director_id;

$result = mysql_query($query, $db) or die(mysql_error($db));

 

$row = mysql_fetch_assoc($result);

extract($row);

 

return $people_fullname;

}

 

//take in the id of a lead actor and return his/her full name

function get_leadactor($leadactor_id) {

 

global $db;

 

$query = 'SELECT

people_fullname

FROM

people

WHERE

people_id = ' . $leadactor_id;

$result = mysql_query($query, $db) or die(mysql_error($db));

 

$row = mysql_fetch_assoc($result);

extract($row);

 

return $people_fullname;

}

 

//take in the id of a movie type and reutnr the meaningfull textual description

function get_movietype($type_id) {

 

global $db;

 

$query = 'SELECT

movietype_label

FROM

movietype

WHERE

movietype_id = ' . $type_id;

$result = mysql_query($query, $db) or die(mysql_error($db));

 

$row = mysql_fetch_assoc($result);

extract($row);

 

return $movietype_label;

}

 

//function to calculate if a movie made a profit, loss or just broke even

function calculate_difference($takings, $cost) {

 

$difference = $takings - $cost;

 

if ($difference < 0) {

$color = 'red';

$difference = '$' . abs($difference) . ' million';

} elseif ($difference > 0) {

$color = 'green';

$difference = '$' . abs($difference) . ' million';

} else {

$color = 'blue';

$difference = 'broke even';

}

 

return '<span style="color:' . $color . ';">' . $difference . '</span>';

}

//connect to mysql

 

$db = mysql_connect('localhost', 'root', 'admin') or die('Unable to connect, Check your connection parameters.');

mysql_select_db('moviesite', $db) or die(mysql_error($db));

 

//retrieve information

$query = 'SELECT

movie_name, movie_year, movie_director, movie_leadactor, movie_type, movie_running_time, movie_cost, movie_takings

FROM

movie

WHERE

movie_id = ' . $_GET['movie_id'];

$result = mysql_query($query, $db) or die(mysql_error($db));

 

$row = mysql_fetch_assoc($result);

$movie_name = $row['movie_name'];

$movie_director = get_director($row['movie_director']);

$movie_leadactor = get_leadactor($row['movie_leadactor']);

$movie_year = $row['movie_year'];

$movie_running_time = $row['movie_running_time'] . ' mins';

$movie_takings = $row['movie_takings'] . ' million';

$movie_cost = $row['movie_cost'] . ' million';

$movie_health = calculate_difference($row['movie_takings'], $row['movie_cost']);

 

 

//display the information

echo <<<ENDHTML

<html>

<head>

<title>Details and Reviews for: $movie_name</title>

</head>

<body>

<div style="text-align: center;">

<h2>$movie_name</h2>

<h3><em>Details</em></h3>

<table cellpadding="2" cellspacing="2" style="width: 70%; margin-left: auto; margin-right: auto;">

<tr>

<td><strong>Title</strong></strong></td>

<td>$movie_name</td>

<td><strong>Release Year</strong></strong></td>

<td>$movie_year</td>

</tr><tr>

<td><strong>Movie Director</strong></td>

<td>$movie_director</td>

<td><strong>Cost</strong></td>

<td>$$movie_cost</td>

</tr><tr>

<td><strong>Lead Actor</strong</td>

<td>$movie_leadactor</td>

<td><strong>Takings</strong></td>

<td>$$movie_takings</td>

</tr><tr>

<td><strong>Running Time</strong></td>

<td>$movie_running_time</td>

<td><strong>Health</strong></td>

<td>$movie_health</td>

</tr>

</table>

ENDHTML;

// retrieve reviews for this movie

$query = 'SELECT

review_movie_id, review_date, review_name, review_comment, review_rating

FROM

reviews

WHERE

review_movie_id = ' . $_GET['movie_id'] . '

ORDER BY

review_date DESC';

 

$result = mysql_query($query, $db) or die(mysql_error($db));

 

//display the reviews

echo <<<ENDHTML

<h3><em>Reviews</em></h3>

<table cellpadding="2" cellspacing"2" style="width: 90%; margin-left: auto; margin-right: auto;">

<tr>

  <th style="width: 7em;">Date</th>

  <th style="width: 10em;">Reviewer</th>

  <th>Comments</th>

  <th style="width: 5em;">Rating</th>

</tr>

ENDHTML;

 

while ($row = mysql_fetch_assoc($result)) {

$date = $row['review_date'];

$name = $row['review_name'];

$comment = $row['review_comment'];

$rating = generate_rating($row['review_rating']);

 

echo <<<ENDHTML

<tr>

<td style="vertical-align:top; text-align: center;">$date</td>

<td style="vertical-align:top;">$name</td>

<td style="vertical-align:top;">$comment</td>

<td style="vertical-align:top;">$rating</td>

</tr>

ENDHTML;

}

 

echo <<<ENDHTML

</div>

</body>

</html>

ENDHTML;

?>

Link to comment
https://forums.phpfreaks.com/topic/189126-table-help/#findComment-998477
Share on other sites

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.