如何将CSS应用于动态生成的PHP数据库结果

时间:2011-10-31 22:21:46

标签: php mysql css

我正在尝试弄清楚如何将CSS或PHP的大块文本拆分成段落或换行符。我的PHP脚本查询MYSQL数据库并根据用户选择的数字返回随机数量的条目(句子)。结果在一个大段落中得到了回应。

反正我是否可以将这一段拆分成较小的段落?我不想在每个结果后换行。

我可以使用带有<p>标记或<br>标记的HTML中的固定文字执行此操作但是当我不知道文本块会有多长时间时,如何使用动态生成的结果执行此操作?我是PHP的新手。任何建议表示赞赏。

以下是代码:

<?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="db"; // Database name 
$tbl_name="sentence"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sn=$_POST['numberofsentences'];


$query="SELECT line FROM `sentence` ORDER BY rand() LIMIT $sn";
$result = mysql_query($query);

while ( $row = mysql_fetch_array($result) ) {
// do something with $row. The following echos the results and adds a space after each sentence.
echo $row['line'], "&nbsp";
}

// Close the database connection
mysql_close();

 ?>

4 个答案:

答案 0 :(得分:1)

不幸的是,你唯一能做的就是使用HTML(以及可选的CSS)来做到这一点。

此外,您的示例代码也存在一些问题。 '&amp; nbsp'最后应该有一个;。我假设您的意思是mysql_fetch_assoc()而不是mysql_fetch_array(),因为您使用字符串索引而不是数字来引用$row数组?

$results = 0;
$max_results = 26;

while ($row = mysql_fetch_assoc($result))
{
  $results++;
  echo $row['line'] . "&nbsp;";

  if ($results % max_results == 0)
    echo "<br /><br />";
}

每行最多可打印25个以空格分隔的结果。这非常粗糙,但可以在浏览器中使用。理想情况下,您应该添加支持HTML标记,例如html,body等...

此外,作为旁注:我知道您仍在学习,但请请记住,无论何时在SQL字符串中接受任何类型的外部输入,您必须首先必须清理它们。如果$_POST['numberofsentences']等于0;DROP DATABASE db;

,该怎么办?

如果您希望POST变量为整数,请使用mysql_real_escape_string()后跟intval()


编辑:我不想删除我在帖子中关于点对逗号的错误,因为它在下面的评论中被引用,但是如果我离开它似乎可能会引起一些混乱它在这里。您可以echo string1 . string2echo string1, string2。这些都提供相同的输出,但在引擎盖下工作方式完全不同。要查看原始帖子,请查看历史记录。

答案 1 :(得分:0)

试试这个:

<?php
$host     = "localhost"; // Host name 
$username = "";          // Mysql username 
$password = "";          // Mysql password 
$db_name  = "db";        // Database name 
$tbl_name = "sentence";  // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sn           = $_POST['numberofsentences'];
$s_count_orig = $s_count = 1; // Sentence counter
$p_count      = 1;            // Paragraph counter
$per_para     = 4;            // 4 sentences per paragraph
$paras        = array();      // Array to hold the paragraphs
$para         = array();      // Array to hold the sentences

$query  = "SELECT line FROM `sentence` ORDER BY rand() LIMIT $sn";
$result = mysql_query($query);

while ( $row = mysql_fetch_array($result) ) {
    // do something with $row. The following echos the results and adds a space after each sentence.

    // If there is room in the paragraph array for a sentence...
    if($s_count <= $per_para) {

        // Add the sentence to the array
        $para[$p_count] = $row['line'];

        // Next sentence
        $s_count++;
    }
    else {
        // We have a full paragraph, add it to our master array
        $paras[] = $para[$p_count];

        // Next paragraph
        $p_count++;

        // Reset sentence counter
        $s_count = $s_count_orig;
    }
}

// Iterate our master array of paragraphs
foreach($paras as $p){

    // Echo each paragraph, and each sentence in each paragraph separated by two spaces
    echo '<p>' . implode('&nbsp;&nbsp;',$p) . '</p>';
}

// Close the database connection
mysql_close();

 ?>

答案 2 :(得分:0)

未测试

define('NB_SENTENCE_PER_PARAGRAPH', 5);

// after the query...

$paragraphs = array();
$i = 0;

// prepare data

while ($row = mysql_fetch_array($result))
{
  $index = floor(%i / NB_SENTENCE_PER_PARAGRAPH);

  if (!isset($paragraphs[$index]))
    $paragraphs[$index] = array();

  $paragraphs[$index][] = $row['line'];
  $i++;
}

// display

foreach ($paragraphs as $sentences)
{
  echo '<p>'.implode('&nbsp;', $sentences).'</p>';
}

请注意,将数据检索与显示分开通常是一个好主意(提供更大的灵活性)。

答案 3 :(得分:0)

一种快速简便的方法是为每个段落设置一个静态的句子数量,如4或5,并在循环中使用它,如下所示:

$count = 0; 
while ($row = mysql_fetch_array($result)) { 
    // do something with $row. The following echos the results and adds a space after each sentence. 
    echo $row['line'], "&nbsp"; 
    if ($count >= 4) { 
        echo '<br />&nbsp;&nbsp;&nbsp;&nbsp;'; 
        $count = 0; 
    } else { 
        $count++; 
    } 
}