driz Posted April 27, 2009 Share Posted April 27, 2009 Hi, please look at the following code!Using this same code as a basis, their are three things I'd like to develop, to only show 1 row from the DB, and 2nd be able to show only the most recent row, and lastly how to randomize a row to be shown. Thanks. <?php // connect to the database $host="localhost"; $username="u0558234"; $pass="26jun87"; mysql_connect($host,$username,$pass); mysql_select_db($username); // select everything from the news table $testimonials_query="select * from testimonials"; $testimonials_result=mysql_query($testimonials_query); while($testimonials_row = mysql_fetch_array($testimonials_result)) { echo "<blockquote>"; echo "<p>".$testimonials_row['body']."</p>"; echo "<p class=\"author\">".$testimonials_row['author']."</p>"; echo "</blockquote>"; } // disconnect from the database mysql_close(); ?> Link to comment Share on other sites More sharing options...
justsomeguy Posted April 27, 2009 Share Posted April 27, 2009 To only show 1 row, remove the while loop. Replacing it with an if statement would work. To show the most recent, change the query to order rows by whichever field is chronological. To show a random row, change the query to order by a random number. e.g.:select * from testimonials order by RAND() limit 1 Link to comment Share on other sites More sharing options...
driz Posted April 27, 2009 Author Share Posted April 27, 2009 Would I just replace the word While with If? OR am i off base here?However is the a suitable method of using a numerical value as the query to select how many i want to show, so for example 3? Link to comment Share on other sites More sharing options...
Ingolme Posted April 28, 2009 Share Posted April 28, 2009 Like this: $i = 0;while($testimonials_row = mysql_fetch_array($testimonials_result) && $i < 3) {......$i++} Link to comment Share on other sites More sharing options...
driz Posted April 28, 2009 Author Share Posted April 28, 2009 This is what I have got: $testimonials_query="select * from testimonials"; $testimonials_result=mysql_query($testimonials_query); $i = 0; while($testimonials_row = mysql_fetch_array($testimonials_result) && $i < 3) { echo "<blockquote>"; echo "<p>".$testimonials_row['body']."</p>"; echo "<p class=\"author\">".$testimonials_row['author']."</p>"; echo "</blockquote>"; $i++; } Its showing 3 blockquotes meaning its working, but its no longer showing the content? Is their an error somewhere? :/ Link to comment Share on other sites More sharing options...
Ingolme Posted April 28, 2009 Share Posted April 28, 2009 Maybe the && comparator has precedence over the = operator.Try this:while( ($testimonials_row = mysql_fetch_array($testimonials_result)) && $i < 3) Link to comment Share on other sites More sharing options...
driz Posted April 29, 2009 Author Share Posted April 29, 2009 Yeah that's working fine, thanks Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.