Jump to content

Help Work With Foreach Arrays


sac0o01

Recommended Posts

I have a script that fetches image urls from a database and then displays the images on the page. This all works fine but the problem is I cannot remove just one image from the image list. I would like to add a "delete" button below each image that would correspond to that image. Problem is I don't fully understand foreach and how it works with arrays.

 

Here is how I fetch the images:

<?php


$sqlresult = mysql_query("SELECT * FROM images WHERE user_id =".$_SESSION['userId']);

$count = 0;

while($data = mysql_fetch_array($sqlresult))
{
$image[$count] = $data['url'];


$count++;
}
?>

 

And here is how I list the images. This is where I want to add the delete button (I know the code is ugly :( ) :

<?php
foreach ($image as $image){?>
<tr>
<td width="70"><center><img src="<?php echo $image;?>" alt="rotation" /><br />
URL:<?php echo $image;?></center></td>
</tr>
<?php }?>

 

Thanks for any suggestions

Link to comment
https://forums.phpfreaks.com/topic/270272-help-work-with-foreach-arrays/
Share on other sites

Yes I have looked through the manual on foreach. That is how I came up with what I have so far. Actually the "$image as $image" part is working but I see it would be better defined by "$image as $image_arr" (or similar). What I am confused on is how to call each value in the array so that I can delete just that value (url)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.