I'm trying to use a for loop to convert a number into star images. My program reads from an SQL database to take a rating, which that works fine, but when I try to use a for loop it's a problem due to it being inside a table. Here's my code below:

<?                      
    $conn = mysql_connect("mysql-g35a.mysqldbserver.com", "goodgu", "Goodguys123");
	$select = mysql_select_db("goodgu");
	$sql = 'SELECT * FROM `feedback` ORDER BY `Index` DESC LIMIT 0, 30 '; 
	$result = mysql_query($sql);
	$num = mysql_numrows($result);
	
	
	
	
	while($i < $num){
		
		$name=mysql_result($result,$i,"name");
		$make=mysql_result($result,$i,"make");
		$model=mysql_result($result,$i,"model");
		$year=mysql_result($result,$i,"year");
		$comment=mysql_result($result,$i,"service");
		$rating=mysql_result($result,$i,"rating");
		$date=mysql_result($result,$i,"date");
		
		print <<<HERE
		
<table width="570" border="0">
  <tr>
    <td width="208" bgcolor="#FF0000">$name</td>
    <td width="352" bgcolor="#FFFFFF"> Rating: $rating  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Date of service: $date</td>
  </tr>
  <tr>
    <td bgcolor="#FFFFCC"> 
	$year $make $model</td>
    <td valign="top" bgcolor="#FFFF99">$comment</td>
  </tr>
</table>


HERE;

$i++;
	}//end while
    ?>

Here's a snippet of what I'd like to be able to do:

print <<<HERE
		
<table width="570" border="0">
  <tr>
    <td width="208" bgcolor="#FF0000">$name</td>
    <td width="352" bgcolor="#FFFFFF"> Rating:
for($i =0; $i<$rating; $i++){
        <img src="images/star.jpg">
}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Date of service: $date</td>
  </tr>
  <tr>
    <td bgcolor="#FFFFCC"> 
	$year $make $model</td>
    <td valign="top" bgcolor="#FFFF99">$comment</td>
  </tr>
</table>


HERE;

But when I do that, it shows the for loop code in the table. I just want to be able to convert the rating into a number stars. How would I go about doing this?

Recommended Answers

All 7 Replies

Whenever you going to write a PHP script, do so within the tag <?php ... ?>. Therefore the solution to your problem is;

<table width="570" border="0">
  <tr>
    <td width="208" bgcolor="#FF0000">$name</td>
    <td width="352" bgcolor="#FFFFFF"> Rating:
<?php
for($i =0; $i<$rating; $i++){
      Print '<img src="images/star.jpg">';
}
?>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Date of service: $date</td>
  </tr>
  <tr>
    <td bgcolor="#FFFFCC"> 
	$year $make $model</td>
    <td valign="top" bgcolor="#FFFF99">$comment</td>
  </tr>
</table>

Also note the print command added to your img tag.

That didn't fix the problem. All that code is already inside a PHP tag and the table is made within a print function. That works outside of the table and print function, but that's not how I want it to work.

I would do it like this:

<?PHP                     
    $conn = mysql_connect("mysql-g35a.mysqldbserver.com", "goodgu", "Goodguys123");
	$select = mysql_select_db("goodgu");
	$sql = 'SELECT * FROM `feedback` ORDER BY `Index` DESC LIMIT 0, 30 '; 
	$result = mysql_query($sql);
	$num = mysql_numrows($result);
 
 
 
 
	while($i < $num){
 
		$name=mysql_result($result,$i,"name");
		$make=mysql_result($result,$i,"make");
		$model=mysql_result($result,$i,"model");
		$year=mysql_result($result,$i,"year");
		$comment=mysql_result($result,$i,"service");
		$rating=mysql_result($result,$i,"rating");
		$date=mysql_result($result,$i,"date");
 
echo "
<table width=\"570\" border=\"0\">
  <tr>
    <td width=\"208\" bgcolor=\"#FF0000\">$name</td>
    <td width=\"352\" bgcolor=\"#FFFFFF\"> Rating: $rating  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Date of service: $date</td>
  </tr>
  <tr>
    <td bgcolor=\"#FFFFCC\"> 
	$year $make $model</td>
    <td valign=\"top\" bgcolor=\"#FFFF99\">$comment</td>
  </tr>
</table> ";
 
$i++;
	}//end while
    ?>

But i might be wrong. I don't program all the time, just had made few websites in php, so I still have the habit to make a small mistake hear and there. :)

Or you can replace it with (If I remember it correctly):

echo "
<table width=\"570\" border=\"0\">
  <tr>
    <td width=\"208\" bgcolor=\"#FF0000\">".$name."</td>
    <td width=\"352\" bgcolor=\"#FFFFFF\"> Rating: ".$rating."  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Date of service: ".$date."</td>
  </tr>
  <tr>
    <td bgcolor=\"#FFFFCC\">".$year." ".$make." ".$model."</td>
    <td valign=\"top\" bgcolor=\"#FFFF99\">".$comment."</td>
  </tr>
</table> ";

But I use firs one. it works for me.
Sorry if I made any mistakes. :)

Unfortunately neither of those helped me. The problem wasn't the while loop, it was a for loop that's supposed to draw images. It would go after Rating: on line 5 of the second snippet on the post above. The loop should go like:

for($i=0; $i<rating; $i++){
  print"<img src='images/star.jpg'>";
}

The problem is the table is inside a print function and the PHP code doesn't work in there and when I put the php code in there, the loop code is visible on the web page when it's viewed.

1) Heredoc may not work (I am not sure) with loops. so I removed heredoc and used simple php string.
2) For FOR loop you should use another iterator, like is repalced i with j, because your while loop is already using i .

<?                      
    $conn = mysql_connect("mysql-g35a.mysqldbserver.com", "goodgu", "Goodguys123");
	$select = mysql_select_db("goodgu");
	$sql = 'SELECT * FROM `feedback` ORDER BY `Index` DESC LIMIT 0, 30 '; 
	$result = mysql_query($sql);
	$num = mysql_numrows($result);
	
	
	
	
	while($i < $num){
		
		$name=mysql_result($result,$i,"name");
		$make=mysql_result($result,$i,"make");
		$model=mysql_result($result,$i,"model");
		$year=mysql_result($result,$i,"year");
		$comment=mysql_result($result,$i,"service");
		$rating=mysql_result($result,$i,"rating");
		$date=mysql_result($result,$i,"date");
		
		echo "<table width='570' border='0'>
		  <tr>
    		<td width='208' bgcolor='#FF0000'>$name</td>
		    <td width='352' bgcolor='#FFFFFF'> Rating: ";

                for($j =0; $j<$rating; $j++){
                    echo "<img src='images/star.jpg'>";
                 }


		echo "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Date 			of service: $date</td>
		  </tr>
		  <tr>
		    <td bgcolor='#FFFFCC'> 
			$year $make $model</td>
		    <td valign='top' bgcolor='#FFFF99'>$comment</td>
		  </tr>
			</table>";


$i++;
	}//end while
    ?>

Then sorry, I have failed to fully understand the problem.

Thanks urtrivedi, your solution worked!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.