对于那些见过我之前问题的人来说,问题就在于此。我正在将一些ColdFusion转换为PHP。这是ColdFusion很好用的。
<cfset ColCount = 0>
<cfloop query="getArt">
<cfset ColCount = ColCount + 1>
<cfif ColCount is 1>
<div id="clear"></div>
</cfif>
<div id="thumb" onClick="window.location='gallery_detail.cfm?AID=#AID#';" title="Enlarge">
<div id="thumb_container">
<a href="gallery_detail.cfm?AID=#AID#" ><img src="/#ThumbFilePath#" alt="Enlarge" border="0"></a>
<div id="name_spacer"></div>
<div id="thumbdesc">
#Title#
</div>
</div>
</div>
<cfif ColCount LT 4></cfif>
<cfif ColCount is 4>
<cfset ColCount = 0>
</cfif>
</cfloop>
这是我想要做的那些与ColdFusion做同样事情的PHP。
<?php
$dbname = 'pdartist2';
$table = 'artwork';
// query
$result = mysql_query("SELECT AID, ThumbFilePath, Title, DisplayOrder FROM artwork where SCID = '$SCID' ") or die(mysql_error());
while($row = mysql_fetch_row($result))
{
$colCount = 0;
foreach($row as $cell) {
$colCount++;
if ColCount is 1
echo "<div id="clear"></div>";
echo "<div id="thumb" onClick="window.location='gallery_detail.php?AID=$AID';" title="Enlarge">";
echo "<div id="thumb_container">";
<a href="gallery_detail.php?AID=$AID" ><img src="/$ThumbFilePath" alt="Enlarge" border="0"></a>
echo "<div id="name_spacer"></div>";
echo "<div id="thumbdesc">";
$Title
echo "</div>";
echo "</div>";
echo "</div>";
}
}
mysql_free_result($result);
?>
我知道这有些粗糙,但你明白了。显然,ColdFusion和PHP处理HTML和SQL的方式完全不同。除非我有,否则我不会做出改变,唉,我做了。
答案 0 :(得分:1)
第11行:
if ColCount is 1
应为if( $colCount > 0 )
第12行和以下几个:
echo "<div id=\"clear\"></div>";
- “字符需要在此转义。或者,以这种方式生成字符串:echo '<div id="clear"></div>';
第15行和第18行缺少输出法。
您应该考虑在逻辑文件中放置较少的演示文稿。
编辑:让我详细说明一下。在这种情况下,除了语法错误之外,您还可以通过将如此多的演示数据直接放入逻辑文件来绕过良好实践。您可以做的是将您的逻辑放入gallery.php或其他东西,然后在php模板文件中,使用该逻辑。例如(这不适合你,完全一样):
<?php
// Here is our logic
$dbname = 'pdartist2';
$table = 'artwork';
// query
$result = mysql_query("SELECT AID, ThumbFilePath, Title, DisplayOrder FROM artwork where SCID = '$SCID' ") or die(mysql_error());
$row = mysql_fetch_row($result);
$colCount = 0;
?>
<!-- Here is the view -->
<?php foreach($row as $cell): ?>
<div id="clear"></div>
<div id="thumb" onClick="window.location="gallery_detail.php?AID=<?php echo $AID ?>"" title="Enlarge">
<div id="thumb_container">
<a href="gallery_detail.php?AID=<?php echo $AID ?>" ><img src="/$ThumbFilePath" alt="Enlarge" border="0"></a>
<div id="name_spacer"></div>
<div id="thumbdesc"><?php echo $Title ?></div>
</div>
</div>
<?php endforeach ?>
答案 1 :(得分:0)
<?php
$dbname = 'pdartist2';
$table = 'artwork';
// query
$result = mysql_query("SELECT AID, ThumbFilePath, Title, DisplayOrder FROM artwork where SCID = '$SCID' ") or die(mysql_error());
$colCount = 0;
while($cell = mysql_fetch_assoc($result)) {
$colCount++;
if($colCount > 0) {
echo '<div id="clear"></div>';
}
echo '<div id="thumb" onClick="window.location=\'gallery_detail.cfm?AID='.$cell['AID'].'\';" title="Enlarge">
<div id="thumb_container">
<a href="gallery_detail.cfm?AID='.$cell['AID'].'" ><img src="/'.$cell['ThumbFilePath'].'" alt="Enlarge" border="0"></a>
<div id="name_spacer"></div>
<div id="thumbdesc">
'.$cell['Title'].'
</div>
</div>
</div>';
if($colCount == 4) $colCount = 0;
}
?>
你去吧
如果您发现下一个PHP问题,请考虑学习PHP。