Friday, 15 April 2011

php - Multiple mt_rand in same script -



php - Multiple mt_rand in same script -

i have random comment script wordpress i've been using widget. has worked fine showing 1 comment need show three. tried adding new variables , output copying existing code possibility of generating same comment multiple times lot more thought be. suggestions appreciated.

here's original code showing 1 comment:

<div style="width:276px;margin:0 15px"> <h2>testimonials</h2> <div style="background:rgba(65, 115, 148, 0.3);border-radius:0 0 6px 6px;height:265px;border:1px solid #ddd;box-shadow:0 0 3px #fff;padding:5px;overflow-y:auto"> <?php $post_id = 276; // set 'testimonials' id here $comments = get_comments("post_id=$post_id&status=approve"); if ($comments) { $ndx = mt_rand(2,sizeof($comments)) - 1; $comment = $comments[$ndx]; ?> <p style="margin-bottom:20px"> <span style="float:left;margin-right:3px"><?php echo get_avatar($comment->comment_author_email, 48, $default, $alt ); ?></span> <?php if (empty($comment->comment_author)){echo "$comment->comment_author";} else{echo "<a href='$comment->comment_author_url'>$comment->comment_author</a>";} ?> - <?php echo "$comment->comment_date"; ?><br /> <?php echo "$comment->comment_content"; ?></p> <span style="float:right;margin-right:3px;"><?php echo "$comment1->extra_sitename"; ?></span> <?php } ?> </div> </div>

perhaps approach. lets build array of available comments ids.

$commids = array(4,34,56,76,100); // comment id database

then can utilize array_rand() 3 random ids of array.

$choosenids = array_rand($commids, 3);

then can info db based on selected ids. close for?

edit: 2nd solution can 3 random records straight database.

select field table field2='$value' order rand() limit 3;

php wordpress

No comments:

Post a Comment