Mark Lyons Mark Lyons - 11 months ago 69
MySQL Question

Using a PHP foreach - how to create individual divs per each row?

I have a query that selects all the information from a database table and puts it into an array. I then use a PHP foreach statement to display all that in a uniform manner. It's the left table here to get a sense of what I'm talking about.

What I want to do is to make one of the divs (it normally just appears repeatedly under the same name) to have a unique name for each sumbission row. For example, instead of the "response" divs all just being called response, they are "response1", "response2", and so on. Is there any way to do this? (code below)

Any help would be greatly appreciated.

Here's where I call the info from the query:

<?php foreach($images as $image) { ?>

<table id="front_pgs">

<tr>
<td id="front_text">
<div id="imagetitle">
<?php echo $image['name'];?>
</div>
<div id="submission_info">
submitted by <?php echo $image['submitter'];?>
</div>
<div id="ratingcontainer">
<form id="ratingform">
<input name="vote" type="button" onclick="getVote('<?php echo $image['filename'];?>')" value='Like' id="likebutton"/>
<input name="dislike" type="button" value='Disike' id="dislikebutton"/>
</form>


<div id="rate_count">
<div id="response">

<?php echo $image['rating'];?>

</div>
</div>
</div>
</td>
<td id="front_pg_img" valign="center" align="center">
<a onClick="switchImageUrl('<?php echo $image['filename']; ?>', '<?php echo $image['width']; ?>', '<?php echo $image['height']; ?>')"><img src="<?php echo $image['filename'];?>" id="front_pg_thumbnail"/></a>
</td>
</tr>
</table>

<?php } ?>

Answer Source
<?php $i = 1; foreach($images as $image) { ?>

<table id="front_pgs">

<tr>
<td id="front_text">
<div id="imagetitle">
<?php echo $image['name'];?>
</div>
<div id="submission_info">  
submitted by <?php echo $image['submitter'];?>
</div>
<div id="ratingcontainer">    
<form id="ratingform">   
<input name="vote" type="button" onclick="getVote('<?php echo $image['filename'];?>')" value='Like' id="likebutton"/> 
<input name="dislike" type="button" value='Disike' id="dislikebutton"/> 
</form>


<div id="rate_count">
<div id="response<?php echo $i; ?>">

<?php echo $image['rating'];?>

</div>
</div>
</div>
</td>
<td id="front_pg_img" valign="center" align="center">
<a onClick="switchImageUrl('<?php echo $image['filename']; ?>', '<?php echo $image['width']; ?>', '<?php echo $image['height']; ?>')"><img src="<?php echo $image['filename'];?>" id="front_pg_thumbnail"/></a>
</td>
</tr>
</table>

<?php $i ++; } ?>

Notice the $i = 1 before the foreach as well as the $i ++ before the closing }. Also, echo $i in the response div id.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download