/    Sign up×
Community /Pin to ProfileBookmark

Need Help With Add Comment Part Of Site

hey everyone,
I’m making a new part for my site, so people can add comments to picture other people have uploaded. The way i’ve done this is by making a tread that comes off each image. what i’m having a problem with is if there are no comments, I want it to say “No comments added” and not to display the table that the comment would of been displayed in.
here’s my script.

[code=php]<table border=”0″ cellspacing=”0″ cellpadding=”2″ width=”504″>
<!–DWLayoutTable–>
<tr>
<td height=”20″ colspan=”2″ bgcolor=”#330099″ id=”comtop” class=”coms”><?php $id = $_SESSION[‘id’];

mysql_connect(“host”,”user”,”password”)
or die(“Could not connect”);
mysql_select_db(“unicyclep1”)
or die(“Could not select database”);

$sql2 = “SELECT COUNT(*) as `count` FROM image_com WHERE image_id = ‘$id’ ORDER BY `date`”;
$result = mysql_query($sql2) or die(“<br><br><span class=”subHeader”>No Images Uploaded</span>”);
$row = mysql_fetch_array($result);
$numrows = $row[‘count’];

$p = (isset($_GET[“comment”]) and is_numeric($_GET[“comment”]) and ($_GET[“comment”] > 0) and ($_GET[“comment”] <= $numrows))
? $_GET[“comment”]
: $numrows ;

$sql1 = “SELECT * FROM image_com WHERE image_id = ‘$id’ ORDER BY id LIMIT 1 OFFSET “.($p = 0);
$result = mysql_query($sql1) or die(“<br><br><span class=”subHeader”>No Images Uploaded</span>”);
$r = mysql_fetch_assoc($result); ?><?php echo $r[‘date’];
?> </td>
</tr>
<tr>
<td width=”128″ height=”89″ align=”center” valign=”top” id=”style5″>
<p class=”com”><?php echo $r[‘user_name’];
?></p><form action=”viewprofile.php” method=”post”>
<input type=”hidden” name=”username” value=”<?php echo $r[‘user_name’] ?>”>
<input type=”submit” value=”View Profile”></form></td>
<td width=”368″ valign=”top” id=”style10″><p class=”com”><?php echo $r[‘comment’]; ?></p></td>
</tr>
</table>
<br>
<table border=”0″ cellspacing=”0″ cellpadding=”2″ width=”504″>
<!–DWLayoutTable–>
<tr>
<td height=”20″ colspan=”2″ bgcolor=”#330099″ class=”coms” id=”comtop”><?php $id = $_SESSION[‘id’];

mysql_connect(“host”,”user”,”password”)
or die(“Could not connect”);
mysql_select_db(“unicyclep1”)
or die(“Could not select database”);

$sql2 = “SELECT COUNT(*) as `count` FROM image_com WHERE image_id = ‘$id’ ORDER BY `date`”;
$result = mysql_query($sql2) or die(“<br><br><span class=”subHeader”>No Images Uploaded</span>”);
$row = mysql_fetch_array($result);
$numrows = $row[‘count’];

$p = (isset($_GET[“comment”]) and is_numeric($_GET[“comment”]) and ($_GET[“comment”] > 0) and ($_GET[“comment”] <= $numrows))
? $_GET[“comment”]
: $numrows ;

$sql1 = “SELECT * FROM image_com WHERE image_id = ‘$id’ ORDER BY id LIMIT 1 OFFSET “.($p = 1);
$result = mysql_query($sql1) or die(“<br><br><span class=”subHeader”>No Images Uploaded</span>”);
$r = mysql_fetch_assoc($result); ?><?php echo $r[‘date’];
?></td>
</tr>
<tr>
<td width=”128″ height=”89″ align=”center” valign=”top” id=”style5″><p class=”com”><?php echo $r[‘user_name’];
?></p><form action=”viewprofile.php” method=”post”>
<input type=”hidden” name=”username” value=”<?php echo $r[‘user_name’] ?>”>
<input type=”submit” value=”View Profile”></form></td>
<td width=”368″ valign=”top” id=”style10″><p class=”com”><?php echo $r[‘comment’]; ?></p></td>
</tr>
</table><br>[/code]

and so on.
I was thinking something like

[code=php]<?php if($p => 1)
{
echo
}; ?>[/code]

but i’m not to sure how to put it all together. ?
any ideas?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@tgrk35Oct 02.2006 — I would use something like [code=php]if (!$result){ [don't display your table] } else { [display your table with your data] } [/code]
Copy linkTweet thisAlerts:
@PJStewauthorOct 02.2006 — I would use something like [code=php]if (!$result){ [don't display your table] } else { [display your table with your data] } [/code][/QUOTE]

I've tryed this, but i must be doing something wrong, my script now looks like this...
[code=php]<?php $id = $_SESSION['id'];

mysql_connect("host","user","password")
or die("Could not connect");
mysql_select_db("unicyclep1")
or die("Could not select database");

$sql2 = "SELECT COUNT(*) as count FROM image_com WHERE image_id = '$id' ORDER BY date";
$result = mysql_query($sql2) or die("<br><br><span class="subHeader">No Images Uploaded</span>");
$row = mysql_fetch_array($result);
$numrows = $row['count'];

$p = (isset($_GET["comment"]) and is_numeric($_GET["comment"]) and ($_GET["comment"] > 0) and ($_GET["comment"] <= $numrows))
? $_GET["comment"]
: $numrows ;

$sql1 = "SELECT * FROM image_com WHERE image_id = '$id' ORDER BY id LIMIT 1 OFFSET ".($p = 0);
$result = mysql_query($sql1) or die("<br><br><span class="subHeader">No Images Uploaded</span>");
$r = mysql_fetch_assoc($result); ?><?php echo $r['date'];
?>
<td width="580" height="600"valign="top"><br /><?PHP
if (!$result)
{ echo "No comments added"; }
else { echo "<table border="0" cellspacing="0" cellpadding="2" width="504">
<!--DWLayoutTable-->
<tr>
## line 90 ## <td height="20" colspan="2" bgcolor="#330099" id="comtop" class="coms"><?php echo $r['date'] ?>
</td>
</tr>
<tr>
<td width="128" height="89" align="center" valign="top" id="style5">
<p class="com"><?php echo $r['user_name'];
?></p><form action="viewprofile.php" method="post">
<input type="hidden" name="username" value="<?php echo $r['user_name'] ?>">
<input type="submit" value="View Profile"></form></td>
<td width="368" valign="top" id="style10"><p class="com"><?php echo $r['comment']; ?></p></td>
</tr>
</table>"; } ?>[/code]

This is giving me this error.
[CODE]
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/fhlinux184/u/unicyclepics.co.uk/user/htdocs/view.coms2.php on line 90[/CODE]

I have marked on there where line 90 is, it is also where the first php echo starts, and I thnik that's what's causing the error.

? any ideas?
Copy linkTweet thisAlerts:
@bokehOct 02.2006 — [I][code=php]echo "<table border="0" cellspacing="0" cellpadding="2" width="504">
<!--DWLayoutTable-->
<tr>
<td height="20" colspan="2" bgcolor="#330099" id="comtop" class="coms"><?php echo $r['date'] ?>
</td>
</tr>
<tr>
<td width="128" height="89" align="center" valign="top" id="style5">
<p class="com"><?php echo $r['user_name'];
?></p><form action="viewprofile.php" method="post">
<input type="hidden" name="username" value="<?php echo $r['user_name'] ?>">
<input type="submit" value="View Profile"></form></td>
<td width="368" valign="top" id="style10"><p class="com"><?php echo $r['comment']; ?></p></td>
</tr>
</table>";[/code]
[/I]You can't have opening and closing php tags in the middle of a double quoted string literal. Also array elements should be encapsulated like this: [code=php]{$r['user_name']}[/code]
×

Success!

Help @PJStew spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...