0

I've had excellent support here, so I figured I'd try again, as I have no clue where to even begin looking for this answer.

I have a simple MySQL database, named "testimonials", which contains 3 tables, "id", "name", "content"

What I want to do, is display testimonials within a fixed size block. Just simply displaying the content is no problem at all, however where I'm stuck is the (somewhat) unique way I'm trying to make it work. I would like to display a random item on each page load, and then to check the character length of the "content" within the testimonial, and if it's equal to or greater than XX length, then just display the one testimonial, otherwise if it's less than XX in length to display a second testimonial (assuming it combined with the first doesn't break the container box).

The box in question is 362px in width and 353px in height using 14px font with Verdana. An example of how the testimonial will appear on the page is like this:

"This is the testimonial content, some nice message from a client."
-- Billy Bob, Owner, Crazy Joe's Tavern

The "name" table in the database holds everything in bold (minus the -- of course), in case someone felt the need to ask.

As I typed that, I felt as if I was asking for a miracle, however I'll still post the question, hoping someone might just know the answer. As always, thanks for any help I may get, if this is just simply asking too much, I'm not totally against the idea of only displaying one testimonial at a time making a ground rule saying they have to contain a minimum of XX characters.

Thanks!

Quick Update: I didn't expect to get answers so quickly, I'm not at my desk at the moment, so as soon as I sit back down I'll go through and see which answer fits best. However, do you guys get together and try to make your answer more complex than the previous answer? lol, thanks though, for anyone who's offering help, you guys rock!

Final edit: I decided against this whole idea, as it just way over complicated everything. For the time being, I'm just going to display all testimonials, and make them scroll, while I work on a jQuery snippet to make it prettier. Thanks everyone for your help though! Should I decide again to do this, I'll be trying my chosen answer.

4

3 に答える 3

0

このようなものが必要になります。

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

明らかに、2 番目の証言が収まるほど短いかどうかを判断するために考え出さなければならない処理がいくつかあります。しかし、それで始められるはずです。

編集:ランダム化のために、私は自分のサイトでこれを使用します:

$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]."'"));
  }
}
于 2013-10-28T21:48:39.450 に答える
0

必要なのはループだけです。擬似コード:

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

見栄えを良くするために、表示ボックスが固定の高さになる場合は、jQuery を使用して、2 番目のコメントを表示するかどうかを切り替えることができます。

于 2013-10-28T21:46:02.997 に答える
0

配列に証言があると仮定します。

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

maxLengthTestimonialsContentandmaxLenthAllTestimonnials変数を持つことができます:

$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'];
    }

}
于 2013-10-28T21:52:38.043 に答える