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
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)

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.