Jump to content

Review script


FlashNinja

Recommended Posts

I've develped this small script to display user reviews stored in a my databases review table. The problem with the script though is that it seems to be looping ad nauseum. The wierd thing though, is that I've used a very similair script to display another list of items on this site, and it worked correctly without any issue. Could someone take a look at this for me and diagnose the error with the script.

 

The SQL table:

 

CREATE TABLE IF NOT EXISTS `rev` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `rev_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `usr_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `text` varchar(600) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

 

The PHP script:

 

User reviews for <? echo $member;?>
<br>

<table border="0">

	<?php
	for($count = 1; $count <= $revrows; $count++)
	{
		$sqlrev = "SELECT * FROM rev WHERE rev_name = '$member' ORDER BY id DESC";
		$revresult = mysql_query($sqlrev) or die(mysql_error());
		$revrows = mysql_fetch_row($revresult);
		$revname = mysql_fetch_array($revresult);
	?>

	<tr>
	<?php
		print "Review by: " . $revname['usr_name'] . "<br>". $revname['text'];
	?>
	</tr>

	<?php
		}
	?>
</table>

Link to comment
Share on other sites

I've echoed them outside the FOR loop and within it. Outside of it they echo as they should - 1, ARRAY - but trying to echo them within the FOR loop causes an infinite loop still. It seems to be the requirements for the loop to start that is causing this infinite loop, the wierd thing is that the other FOR loops I've used have had the same requirements.  :shrug:

Link to comment
Share on other sites

Right. But were you able to tell if the requirements of loop termination have been met by the variables from within the loop? You would have to break out yourself (or put a different counter inside that loop that would break out), but you should still be able to see if the variables are filling up correctly.

Link to comment
Share on other sites

I've worked on it a bit more and the FOR loop has stopped looping for infinitly, the problem that has arose now is that it's producing the number of results that it should be but all the results are duplicates of the newest results relating to the query.

 

For example:

 

The table contains two results relating to the user Abed, the reviews are both by Jeff one reads "yes" the other reads "no" - the "no" review is the newest entry. As there are two results related to Abed two results are produced by the FOR loop, but both of them read "no" when one should read "yes" and the other should read "no". Can someone help me with this issue?

 

PHP:

 

<div = "reviews">

<?php

$sqlrev = "SELECT * FROM rev WHERE rev_name = '$member' ORDER BY id DESC";
$revresult = mysql_query($sqlrev) or die(mysql_error());
$revrows = mysql_num_rows($revresult);
$revname = mysql_fetch_array($revresult);

?>

User reviews for <? echo $member;?>
<br>
<br>

<table border="0">

	<?php
	for($count = 1; $count <= $revrows; $count++)
	{

	?>

	<tr>
	<?php
	echo "Review by: " . $revname['usr_name'];
	echo "<br>";
	echo $revname['text'] ."<br>";
	echo "<br>";
	?>
	</tr>

	<?php
	}
	?>
</table>

</div>

Link to comment
Share on other sites

mysql_fetch_array returns the next row from your query. Since you are only calling it once, you are only looking at the first row. Since mysql_fetch_array() will return false if no row is returned, you don't need to use a counter for the loop. Consider something like this:

 

<div = "reviews">

<?php

$sqlrev = "SELECT * FROM rev WHERE rev_name = '$member' ORDER BY id DESC";
$revresult = mysql_query($sqlrev) or die(mysql_error());
$revrows = mysql_num_rows($revresult); // Don't really need this any more, either
# $revname = mysql_fetch_array($revresult); MOVED THIS TO REPLACE THE FOR LOOP
?>

User reviews for <? echo $member;?>
<br>
<br>

<table border="0">

	<?php
	// PROCESS EACH ROW RETURNED BY THE QUERY
	while ($revname = mysql_fetch_array($revresult)) 
	{

	?>

	<tr>
	<?php
	echo "Review by: " . $revname['usr_name'];
	echo "<br>";
	echo $revname['text'] ."<br>";
	echo "<br>";
	?>
	</tr>

	<?php
	}
	?>
</table>

</div>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.