【问题标题】:first iteration of sql query not being wrapped properly [closed]sql查询的第一次迭代没有正确包装[关闭]
【发布时间】:2014-03-17 09:13:17
【问题描述】:

所以,我得到了这个 php 文件,但是第一个结果没有包含在“结果”div 中,因此没有正确设置样式。所有后续结果都被正确包装。帮忙?

编辑: 根据 cmets,我试图将 html 拉出来。我承认我是个菜鸟,但这就是我想出的。似乎没有解决原来的问题:

scripts_post.php:

$connection = mysqli_connect("server", "user", "password", "dbname");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($connection, $sqlQuery);
mysqli_close($connection);
if (empty($result)) { 
    echo 'No results found'; 
    return;
}
while($row = mysqli_fetch_array($result))
    {
        echo include 'single_result.php';
    }
echo "<div class='results_end'><p>End of list</p></div>";

single_result.php:

    <div class='result'>
        <div class='result_left'>
            <div class='result_photo'><img src=<?php echo "'" . $row['PhotoLink'] . "'" ?> height='200' width='200' class='script_photo'></div>
        </div>
        <div class='results_right'>
            <div class='result_title'><?php echo $row['Title'] ?></div>
            <div class='result_summary'><?php echo $row['Summary'] ?></div>
            <div class='result_description'><?php echo $row['Description'] ?></div>
            <div class='result_preview'><a href =<?php echo "'" . $row['PreviewLink'] . "'" ?> target='_blank'>view preview</a></div>         
            <div class='result_detail'>Type: <?php echo $row['Type'] ?></div>   
            <div class='result_detail'>Biblical Characters Portrayed: <?php echo $row['BiblicalCharacters'] ?></div>
            <div class='result_detail'>Topics: <?php echo $row['Topics'] ?></div>
            <div class='result_price'>Cost: $<?php echo $row['Price'] ?></div>
            <div class='result_purchase'><a href =<?php echo "'" . $row['DownloadLink'] . "'" ?> target='_blank'><img src='http://image.payloadz.com/images/btn-addtocart-b.png' border='0'></a></div>
        </div>
    </div>

编辑#2:这就是上面所说的。也许这里有什么东西在搞砸?

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js">
    </script>
    <script language ="javascript">
        $(function() {
            $('form').submit(function(event) {
                var form = $(this);
                $.ajax({
                    type: form.attr('method'),
                    url: form.attr('action'),
                    data: form.serialize()
                }).done(function(returned_data) {
                    document.getElementById('scriptsearch_results_div').innerHTML = returned_data;
                }).fail(function() {
                    alert("The search has failed.  Please alert the webmaster.");
                });
                event.preventDefault();
            });
        });
    </script>
    ...
    <div id="formcontainer">
        <form id="scriptsearch_form" action="scripts_post.php" method="post">
            // form fields
            <p> <input type="submit" value="Search" class="submitbutton" id="mainsubmitbutton"> </p>
        </form>
    </div>
    <div id="scriptsearch_results_div">
        <p>Search for scripts using the options at left.</p>
    </div>

【问题讨论】:

  • 我不建议首先将所有这些 HTML 放入您的脚本中。这是拔掉头发的秘诀。
  • 公平。我还有哪些其他选择?
  • 使用两个文件。一个用于 HTML,一个用于脚本。使脚本中的值可用于其他 PHP 文件(包含您的 html)并替换值。
  • 我会调查的。谢谢。
  • 只是因为我很好奇。如果在 implode 中使用空字符串作为胶水会发生什么?

标签: php css sql


【解决方案1】:

发现问题。傻,真的。 scripts_post.php 中的&lt;body&gt; 标签实际上是&lt;body,所以第一个&gt; 正在关闭标签,而其间的东西正在被转储。

【讨论】:

    【解决方案2】:

    您应该转换来自数据库的文本中的特殊字符,因为它们可能会破坏您的 HTML(我猜这是第一个 div 中发生的情况)

    使用htmlspecialchars()

    所以,而不是:

    <div class='result_summary'><?php echo $row['Summary'] ?></div>
    

    应该是:

    <div class='result_summary'><?php echo htmlspecialchars($row['Summary']) ?></div>
    

    执行的翻译是:

    • &(和号)变为&amp;amp;
    • "(双引号)在未设置 ENT_NOQUOTES 时变为 &amp;quot;
    • '(单引号)只有在设置了 ENT_QUOTES 时才会变为 &amp;#039;(或 ')。
    • &lt;
    • >(大于)变为&amp;gt;

    【讨论】:

    • 这不能解释为什么第一次迭代不能正确显示,但所有后续迭代都可以
    • 毫无期望地尝试了这个。没有任何改变。
    【解决方案3】:

    您需要从 php 循环中取出“结果”div。换句话说,将php循环嵌套在'result'div中

    【讨论】:

    • 'result' div 是针对每个结果的,带有样式来进行分割等: .result { border-bottom: 1px lightgray solid;填充:20px 0px;最小高度:200px;宽度:695px;溢出:自动; } 整个结果集被包裹在自己的 div 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多