...

View Full Version : parse error and html table filled from mysql db



bosstones
01-21-2010, 04:28 AM
hey folks, I am getting a "Parse error: syntax error, unexpected T_SL" on line 97 of this code. It is a php file where I am trying to get a table (in html) to grow and fill according to the information in my DB. This is for a beer-rating site, the table will show details about each beer and the name can also be linkedAm I doing this firght with the for-loop? Any suggestions and fixes would be greatly appreciated.



<?php
// Set connection details.
$db_hostname = // Host
$db_username = // Username
$db_pass = // Password
$db_name = // Database Name


// This initializes the connection to the database.
$con = mysql_connect($db_hostname, $db_username, $db_pass) or die('Could not connect to mysql server.');

// Connect to the specific database.
mysql_select_db($db_name, $con) or die('Could not select database.');

// Select variable, with LIMIT 1, just in case.
$sql = "SELECT * FROM beers WHERE `id`='$id'";

// Query variable, with error handling.
$result = mysql_query($sql) or die('The error was: ' . mysql_error() . '<br />The query was: ' . $sql);

// Begin the while loop, also executing the query variable.
while($row = mysql_fetch_assoc($result)) {

// Set Variables...
$beername = $row['beername'];
$date = $row['date'];
$style = $row['style'];
$brewer = $row['brewer'];
$abv = $row['abv'];
$brewer_url = $row['brewer_url'];
$cost = $row['cost'];
$avg_score = $row['avg_score'];
$id = $row['id'];
}
//echo HTML Header and navigation
echo <<<HTML
<html>
<head>
<title>Beer Club NJ</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body background="homebg.jpg" bgproperties="fixed" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<center>
<table bgcolor="#ffffff">
<tr>
<td>
<script src="sorttable.js">
<a href="index.html"><img src="Banner.jpg" border="0"></a>
<center>
<table width="720">
<tr>
<th align="left"> <a href="beers.html"><IMG style="filter: alpha(Opacity=100, FinishOpacity=0, Style=2)" src="beers.gif" width="150" height="37.5" border="0"></a></th>
<th align="left"> <a href="learn.html"><img src="learn.gif" width="150" height="37.5" border="0"></th>
<th align="left"> <a href="http://beerclubnj.proboards.com/index.cgi?board=general"><img src="talk.gif" width="150" height="37.5" border="0"></a></th>
</tr>
<tr>
<th align="right"> <a href="idiots.html"><img src="idiots.gif" width="150" height="37.5" border="0"></a></th>
<th align="right"> <a href="articles.html"><img src="articles.gif" width="150" height="37.5" border="0"></a></th>
<th align="right"> <a href="contacts.html"><img src="contacts.gif" width="150" height="37.5" border="0"></a></th>
</tr>
</table>
<img src="beerstext.gif" border="0">
<br>
Click on a catagory to re-order the list. Scores are an average score between <br>all raters so make sure to look at the beer's review for more information. <br>The type of beer is also a broad span which is narrowed down on each individual beer's review. <br>So check them out, find out what you like, and then hit the liquor store!
</center>
<center>
<table bgcolor="#ffffff" class="sortable" cellpadding="5">
<tr>
<th>
Beer Name
</th>
<th>
Brewer
</th>
<th>
Style
</th>
<th>
Average Score
</th>
<th>
Date Reviewed
</th>
</tr>
HTML;

//create a table displaying info in table including link to each beer profile
//loop so that there are only as many rows in table as in db

if (mysql_count_rows($query) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
for ($i = 0; $i < count($row); $i++)
{

echo <<<TABLE
<tr><td><a href="beers.php?id=$id">$beername</a></td>
<td>$brewer</td>
<td>$style</td>
<td>$avg_score</td>
<td>$date</td>
</tr>
TABLE;

}
}

}

//echo HTML footer and mailing list form

echo <<<FOOT
</table>
</center>
</td>
</tr>
<tr>
<td>
<center>
//mailing list form will go here
Materials Copywrite of BeerClubNJ and Bread 'n' Buttah Design LLC. 2009
<br>
<a href="sponsors.html">Sponsors and Special Thanks</a>
</center>
</td>
</tr>
</table>
</center>
</body>
</html>
FOOT;

?>

Fou-Lu
01-21-2010, 04:42 AM
Yeah, this one is a tough one to see. Took me a few minutes to find this:


echo <<<TABLE // <-- REMOVE ALL WHITE SPACE HERE
<tr><td><a href="beers.php?id=$id">$beername</a></td>
<td>$brewer</td>
<td>$style</td>
<td>$avg_score</td>
<td>$date</td>
</tr>
TABLE;



Heredoc syntax requires the heredoc operator (>>>) followed by the identifier (TABLE) followed immediately by the linefeed.

bosstones
01-21-2010, 05:17 AM
ACK! Thanks Fou Lu! Had no clue that white space could matter! You're a lifesaver! How about the loop for the table? does that seem right?? thanks again!

_Aerospace_Eng_
01-21-2010, 08:35 AM
You don't need the for loop. The while loop will take care of it. As it is now your table code will print out the size of the number of rows of your result times the number of rows of your result. That isn't what you want.

Fou-Lu
01-21-2010, 01:59 PM
The loops are incorrect. You're first run:


// Begin the while loop, also executing the query variable.
while($row = mysql_fetch_assoc($result)) {

// Set Variables...
$beername = $row['beername'];
$date = $row['date'];
$style = $row['style'];
$brewer = $row['brewer'];
$abv = $row['abv'];
$brewer_url = $row['brewer_url'];
$cost = $row['cost'];
$avg_score = $row['avg_score'];
$id = $row['id'];
}


will populate you're variables with only the last result, and the second one:


if (mysql_count_rows($query) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
for ($i = 0; $i < count($row); $i++)
{

echo <<<TABLE
<tr><td><a href="beers.php?id=$id">$beername</a></td>
<td>$brewer</td>
<td>$style</td>
<td>$avg_score</td>
<td>$date</td>
</tr>
TABLE;

}
}

}

will fail for two reasons. The first is that $query isn't defined anywhere, and the second is that mysql_count_rows doesn't exist.
So, from the looks of it, remove this:


// Begin the while loop, also executing the query variable.
while($row = mysql_fetch_assoc($result)) {

// Set Variables...
$beername = $row['beername'];
$date = $row['date'];
$style = $row['style'];
$brewer = $row['brewer'];
$abv = $row['abv'];
$brewer_url = $row['brewer_url'];
$cost = $row['cost'];
$avg_score = $row['avg_score'];
$id = $row['id'];
}


And change this:


if (mysql_count_rows($query) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
for ($i = 0; $i < count($row); $i++)
{

echo <<<TABLE
<tr><td><a href="beers.php?id=$id">$beername</a></td>
<td>$brewer</td>
<td>$style</td>
<td>$avg_score</td>
<td>$date</td>
</tr>
TABLE;

}
}

}

to this:


while ($row = mysql_fetch_assoc($result))
{
$beername = $row['beername'];
$date = $row['date'];
$style = $row['style'];
$brewer = $row['brewer'];
$abv = $row['abv'];
$brewer_url = $row['brewer_url'];
$cost = $row['cost'];
$avg_score = $row['avg_score'];
$id = $row['id'];

echo <<<TABLE
<tr><td><a href="beers.php?id=$id">$beername</a></td>
<td>$brewer</td>
<td>$style</td>
<td>$avg_score</td>
<td>$date</td>
</tr>
TABLE;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum