HakonHoy
07-22-2006, 04:14 PM
I got this code:
$sql = "SELECT * FROM cmt_table WHERE cmtnews_id = '$news_id' AND cmt_checked = '1' ORDER BY cmt_date DESC";
$results3 = mysql_query($sql)
or die(mysql_error());
echo "</table><table width=100% height=1 cellspacing=0 cellpadding=2 vspace=0 hspace=0><tr><td valign=top width=100% bgcolor=#EEEEEE>";
while ($row3 = mysql_fetch_array($results3)) {
extract($row3);
echo "<font face=\"Verdana,Arial\" size=\"1\" color=\"#000000\"><b>$cmt_poster, $cmt_country</b><br>$cmt_txt</font><br><br>";
}
I want the possibility, that if there are no records in the database (using the sql), it will show a link called "Be the first to comment this news" , and if there are records in the database, there will be a link called "Comment this news" and the records in the db. Is that possible? What kind of code do I use for that?
bigfan
07-22-2006, 07:28 PM
mysql_num_rows() (http://php.net/manual/en/function.mysql-num-rows.php)
Saeven
07-23-2006, 10:23 PM
Learn to use PHP template syntax for stuff like this.
<?php
$res = myqsql_query("SELECT * FROM cmt_table WHERE cmtnews_id = '$news_id' AND cmt_checked = '1' ORDER BY cmt_date DESC" ) or die(mysql_error());
$count = 0;
?>
<table width="100%" height="1" cellspacing="0" cellpadding="2" vspace="0" hspace="0">
<tr>
<td valign=top width=100% bgcolor=#EEEEEE>
<?
while( $row = mysql_fetch_assoc($res) ):
$count++;
?>
<font face="Verdana,Arial" size="1" color="#000000">
<b><?= $row['cmt_poster'] ?>, <?= $row['cmt_country']?></b><br><?=$row['cmt_txt']?></font><br><br>
<? endwhile; ?>
<? if( !$count ): ?>
Be the first to rate this review!
<? endif; ?>
</td>
</tr>
</table>
Don't use variable exports, just export to assoc array so that you can use your column names directly. Do you export everything to global like that? Smells like an XSS attack in the making.
mysql_num_rows does unnecessary work with the query pointer, just use a counter - basic, and more efficient.
Regards.
Alex
Burhan
07-24-2006, 01:21 AM
Ermm, there is no such thing as a 'php template syntax'. Your original code is good, just needs a bit of polish:
// -- if cmtnews_id is a number, no need to put quotes around the value
$sql = "SELECT * FROM cmt_table WHERE cmtnews_id = ".mysql_real_escape_string($news_id)." AND cmt_checked = 1 ORDER BY cmt_date DESC";
$results3 = mysql_query($sql)
or die(mysql_error());
if (mysql_num_rows($result3) == 0)
{
echo 'Be the first one to post!';
} else {
echo "</table><table width=100% height=1 cellspacing=0 cellpadding=2 vspace=0 hspace=0><tr><td valign=top width=100% bgcolor=#EEEEEE>";
// -- no need to use extract(), just mysql_fetch_assoc
while ($row3 = mysql_fetch_assoc($results3)) {
//extract($row3);
echo "<font face=\"Verdana,Arial\" size=\"1\" color=\"#000000\"><b>".$row['cmt_poster'].",". $row['cmt_country']."</b><br>".$row['cmt_txt']."</font><br><br>";
}
}
Saeven
07-24-2006, 01:26 AM
Ermm, there is no such thing as a 'php template syntax'.I wouldn't recommend accepting advice that starts with lines such as these either.
It's true, your original code 'works'. However, it is critical to keep your code as clean and succint as possible. The code I'd suggested achieves the same in less lines, and is self sufficient - there are no export methods required and is entirely encapsulated in the block presented. Further, fyrestrtr's code above still makes use of mysql_num_rows which I'd warned against. Perhaps a knowledge of MySQL pointers is something that isn't common, nonetheless, mysql_num_rows forces a set scan which you should avoid. It's comparatively cheap to just increment a little counter however primitive it may seem. Compare yourself the time that a counter takes, vs the time that this function takes, vs an actual count using COUNT(*) vs again the time taken with SQL_CALC_FOUND_ROWS (RDBMS tag) - the results are self evident.
Take my advice, wrap your HTML in yes, php's combined alternative syntax (aka template syntax if you've been a part of the PHP community long enough) instead of wrapping everything in ugly backslashed blocks of echoed HTML. When your rewrite comes along, or when you decide to edit it in a WYSIWYG editor, you'll see why it's just that much better. For good measure, it's documented here, and 99% overlooked: http://www.php.net/manual/en/control-structures.alternative-syntax.php
One could argue that these suggestions, on a small scale, aren't critical - perhaps true - but bad habits die hard; better learn healthy code practices now, before you get 3200 posts deep into mediocre coding advice ;)
Regards.
A