PHP/MySQL - 如何根据数据库中的长度(字符)调整查询


PHP/MySQL - How to adjust query based on the length (characters) within database

我在这里得到了很好的支持,所以我想我会再试一次,因为我不知道从哪里开始寻找这个答案。

我有一个简单的MySQL数据库,名为"推荐",其中包含3个表,"id","名称","内容"

我想做的是在固定大小的块中显示推荐。只是简单地显示内容根本没有问题,但是我陷入困境的是我试图使其工作的(某种)独特方式。我想在每次页面加载时显示一个随机项目,然后检查推荐中"内容"的字符长度,如果它等于或大于 XX 长度,则只显示一个推荐,否则如果长度小于 XX 以显示第二个推荐(假设它与第一个结合不会破坏容器框)。

有问题的框宽度为 362px,高度为 353px,使用 Verdana 的 14px 字体。推荐书在页面上的显示方式示例如下所示:

"这是推荐内容,来自客户的一些好信息。
——比利·鲍勃(Billy Bob),Crazy Joe's Tavern的老板

数据库中的"name"表以粗体显示所有内容(当然减去 - ),以防有人觉得有必要询问。

当我输入它时,我觉得我好像在要求一个奇迹,但我仍然会发布这个问题,希望有人可能知道答案。与往常一样,感谢我可能得到的任何帮助,如果这只是要求太多,我并不完全反对一次只显示一个推荐信的想法,制定一个基本规则,说它们必须包含至少 XX 个字符。

谢谢!

快速更新:我没想到这么快就得到了答案,我现在不在办公桌前,所以只要我坐下来,我就会仔细看看哪个答案最适合。但是,你们会聚在一起,试图让你的答案比之前的答案更复杂吗?哈哈,谢谢,对于任何提供帮助的人,你们摇滚!

最终编辑:我决定反对这整个想法,因为它只是把一切都复杂化了。目前,我只是显示所有推荐,并使它们滚动,同时我处理jQuery片段以使其更漂亮。谢谢大家的帮助!如果我再次决定这样做,我将尝试我选择的答案。

你只需要一个循环。伪代码:

$length = 0;
$target = 200; // or whatever
while( $length < $target ) {
    $comment = getOneComment();
    displayComment($comment);
    $length += strlen( $comment['content'] ); // assuming getOneComment() returns an associative array
}

为了使它漂亮,如果显示框将是一个固定的高度,你可以使用一些jQuery来切换是否显示第二个注释。

假设您在数组中有推荐:

$testimonials = array(
     't1' => array(
         'content' => 'testimonials 1 content..',
         'author' => 'the author'
     ),
     't2' => array(
         'content' => 'testimonials 2 content..',
         'author' => 'the author 2'
     ),
);

你可以有一个maxLengthTestimonialsContentmaxLenthAllTestimonnials变量:

$maxLengthTestimonialsContent = 120;
$maxLenthAllTestimonnials = 240;

现在,通过一个简单的循环,您可以构建数组推荐,您将用于显示:

$testimonialsToShow = array();
$i = 1; $totalLength = 0
foreach($testimonials as $t) {
    if( $i > 1 && strlen( $t['content']) < $maxLengthTestimonialsContent
        &&  $totalLength < $maxLenthAllTestimonnials  )
        break; // basically here you test that testimonials less first
               // and with less length than maxLengthTestimonial, and also
               // total length less than maxLengthAll to be stored 
               //in $testimonialsToShow
    else {
        $testimonialsToShow[] = $t;
        $totalLength = $t['content'];
    }
}

这样的东西是你需要的。

<?php
$str = $res["testimonial"];
if (strlen($str) > 50) {
    // Logic to retrieve and display second testimonial
}
?>

显然,您必须提出更多的处理来确定第二个推荐是否足够短以适合。但这应该让你开始。

编辑:对于随机化,我在自己的网站上使用它:

$referrals = mysql_query("SELECT id FROM ts_testimonials");
$referralView = array();
$i = 0;
while ($newReferral = mysql_fetch_array($referrals)) {
  $referralView[$i] = $newReferral['id'];
  $i++;
}
if (sizeof($referralView) >= 1){
  $referralTop = rand(0,sizeof($referralView)-1);
  $newReferralTop = mysql_fetch_array(mysql_query("SELECT * FROM ts_testimonials WHERE id = '".$referralView[$referralTop]."'"));
  if (sizeof($referralView) >=2){
    $referralBottom = rand(0,sizeof($referralView)-1);
    while ($referralBottom == $referralTop) {
      $referralBottom = rand(0,sizeof($referralView)-1);
    }
    $newReferralBottom = mysql_fetch_array(mysql_query("SELECT * FROM ts_testimonials WHERE id = '".$referralView[$referralBottom]."'"));
  }
}