I am working on a PHP Gallery application, and need some help here. Actually I have a page where images from a specific directory are displayed directly. With each one of the images displayed there is a dynamically generated submit button that will be used to delete respective images separately.
Every image has its own submit button, that will be used to delete that image. Being new to php I need some method that can be called to delete only that image from the actual or physical directory.
There is a similarity between image and button that I have coded it such that every image and its respective button has names such as "img_1" and its button is "del_1".
<form id="albumGallery" name="albumGallery" method="POST">
<?php
$dir = htmlspecialchars($_GET["p"]) . "/";
$imgs = array();
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (!is_dir($file) && preg_match("/\.(bmp|jpe?g|gif|png)$/", $file)) {
array_push($imgs, $file);
}
}
closedir($dh);
} else {
die('cannot open ' . $dir);
}
$i=0;
echo "<div id='images'>";
foreach ($imgs as $idx=>$img) {
//$class = ($idx == count($imgs) - 1 ? ' class="last"' : '');
echo '<table style="float: left; border: 1px solid #EFEFEF; border-radius: 5px; padding: 5px; margin: 5px;"><tr><td><a href="'. $dir . $img .'" rel="example_group" ><img src="' . $dir . $img . '" alt="' . $img . '" id="'. "img_" . $i .'"/>
</a></td></tr><tr><td><input type="submit" class="ctrlDelete" value="" id="'. "del_" . $i .'"/></td></tr></table>';
$i++;
}
echo "</div>";
?></form>
So, I need to make a method so that each button deletes its respective image and the form is posted back to self.
For your issue, it is better to use anchors. You can style them as pseudo-buttons, if you want. Then just generate links like delete.php?id=23, which will execute the appropriate deletion script with $_GET argument passed.
Below is the very simple implementation:
<table>
<tr>
<td>Title</td>
<td>Image</td>
<td>Actions</td>
<tr>
<?php
foreach ($table as $row)
{
echo "<tr>";
echo "<td>".$row['title']."</td>";
echo "<td>".$row['image']."</td>";
echo "<td>";
echo "<a href='delete.php?id=".$row['id']."'>Delete</a>";
echo "<a href='edit.php?id=".$row['id']."'>Edit</a>";
echo "</td>";
echo "</tr>";
}
?>
</table>
delete.php and edit.php should contain the following code at the very end:
<?php
header("Location: http://www.example.com/");
?>
#Edward Ruchevits
Thanks for your help :D,
I did not use the header(); method but used the javascript's settimeout(); to redirect my page. Here is my code...
<script type="text/javascript">
setTimeout("window.location = '<?php echo $_SERVER['HTTP_REFERER'] ?>'", 1);
</script>
<?php
$path = htmlspecialchars($_GET["p"]);
unlink($path);
?>
I suggest adding the form tag inside your foreach loop and post each of those forms to self. Each form can simply include a hidden field with the image ID. Then each time the page loads, you can simply check the $_POST variable for the image and delete that before serving up your page.
Alternately, you might consider using checkboxes next to the images - then one form and one submit button can action multiple deletions in one - far more efficient in my opinion.
Hope this helps!
Related
I had this working earlier, but now it doesn't seem to want to pick up the data. It's possible something changed when I was trying to make it stop refreshing the page.
The form submits, but only for certain links. However the data gets sent across as normal. It's extremely strange.
This is the page the data is being sent to. Right now it just accepts the data and posts it for testing. "Text" shows but nothing else.
<?php
$ship_id = $_POST['transfer_ship'];
$char_id = $_POST['transfer_character'];
$pos_id = $_POST['transfer_position'];
$requested_by = $_POST['transfer_player'];
echo 'Success';
echo $ship_id;
echo $char_id;
echo $requested_by;
echo $pos_id;
echo $char_owner;
echo 'Text';
This is the form it's being sent from. It's part of a PHP UL series that's running. For the first result, the jQuery fires, I get the "Success" alert. For any of the other li links the data apparently gets sent (I use the F12 dev tools in Chrome, under Network, to see the header) but the page above either doesn't receive it or doesn't do anything with it.
jQuery:
<script type="text/javascript" src="../fancybox/jquery.fancybox.js?v=2.1.3"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".fancybox").fancybox();
});
</script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
// wait for the DOM to be loaded
$(document).ready(function() {
// bind 'myForm' and provide a simple callback function
$('#transfer').ajaxForm(function() {
alert('Success!');
});
});
</script>
Form and UL:
<ul>
<?php
while(list($key, $val) = each ($arrayresult))
{
echo '<a href="#inline' .$val. '" class="fancybox"><li style="padding: 2px; margin: 0px; list-style: none;">';
echo '<img src="../images/profilepics/'.$charpic.'" style="float: right; padding-left: 10px; width: 40px;" />';
echo '<h2>'.$val.' Position</h2>';
echo '<p>Click here to apply for this position.</p>';
echo '</li></a>';
echo '<div id="inline' .$val. '" style="display:none;">';
echo '<h1>Request Character Transfer</h1>';
echo '<hr>';
echo '<form id="transfer" action="TransferRequest.php" method="post">';
echo '<label for="transfer_character">Character to Transfer</label>';
echo '<select id="transfer_character" name="transfer_character">';
echo '<option value="">Select Character</option>';
$request_character_query = "SELECT * FROM character_database WHERE character_active ='1' AND user_id = $user_id ORDER BY character_firstname DESC";
$request_character_result = mysqli_query($mysqli, $request_character_query);
/*** loop over the results ***/
foreach($request_character_result as $charrow)
{
/*** create the options ***/
echo '<option value="'.$charrow['character_id'].'">'. $charrow['character_firstname'] . " " . $charrow['character_surname'] . '</option>'."\n";
}
echo '</select>';
echo '<p>Applying for the '.$val.' position on the '.$shipname.'</p>';
echo '<p>If this is correct, please submit below</p>';
echo '<input type="hidden" value="'.$val.'" name="transfer_position">';
echo '<input type="hidden" value="'.$ship_id.'" name="transfer_ship">';
echo '<input type="hidden" value="'.$user_id.'" name="transfer_player">';
echo '<input value="Submit Request" type="submit" class="styled">';
echo '</form>';
echo '</div>';
}
?>
</ul>
I figured it out! I have my htaccess file removing the .php extension, but I had .php on the redirect link for ajax. So it was hitting the .php page then redirecting WITHOUT the POST data.
I have two php pages. The first page queries a database and places some data into a form
The second page, grabs the data submited from the previous page and displays the data.
THE ISSUE:
On the second page, the information is displayed in a table. At the end of each row, there is a "information" button that allows the user to click and see more details related to the data in that particular column. For some reason, the information button for every record in the table is holding the value of the first record returned in the database query. In other words no matter what button I click on in the table, it always relates to the first record returned in the query.
I'm hoping someone can help me find a solution to linking each "information" button to each unique record. In other words If I click the info button in row one, it will display the data for the record related to row 1...etc. Here's my code.
PAGE 1
<?php
$query = mysql_query ("SELECT * from cust_data group by cabinet_number ORDER by cabinet_number ASC");
WHILE ($rows = #mysql_fetch_array($query)) {
if (($rows['account_number']=="") &&
($rows['customer_first_name']=="") &&
($rows['customer_last_name']=="") &&
($rows['company_name']=="")) {
echo '<form method="GET" action="cabinet_result_page.php">
<input type="hidden" value="'.$rows['cabinet_number'].'" name="cabinet_number">
<input type="hidden" value="'.$rows['company_name'].'" name="company_name">
<img src="images/bulletpoint_green.png">
<input type="submit" value="'.$rows['cabinet_number'].'" name="'.$rows['cabinet_number'].'" id="submit">
</form>';
} else if ($rows['cabinet_number']!=="") {
echo '<form method="GET" action="cabinet_result_page.php">
<input type="hidden" value="'.$rows['cabinet_number'].'" name="cabinet_number">
<input type="hidden" value="'.$rows['company_name'].'" name="company_name">
<img src="images/bulletpoint_red.png">
<input type="submit" value="'.$rows['cabinet_number'].'" name="'.$rows['company_name'].'" id="submit">
</form>';
}
}
}
PAGE 2:
GRABS THE DATA IN PAGE ONE AND PLACES IT IN A TABLE. WHEN THE 'INFO' BUTTON IS CLICKED, MORE INFORMATION IS DISPLAYED IN A JQUERY POPUP
db_connect();
$cabinet_number = $_GET['cabinet_number'];
$company_name = $_GET['company_name'];
$query = #mysql_query ("SELECT * FROM cust_data WHERE cabinet_number = '$cabinet_number' ");
WHILE ($rows = #mysql_fetch_array($query)) {
echo'<tr>
<td><font size="4">'; echo $rows['account_number']; echo'</font></td>
<td><font size="4">'; echo $rows['customer_first_name']; echo '</font></td>
<td><font size="4">'; echo $rows['customer_last_name']; echo '</font></td>
<td><font size="4">'; echo $rows['company_name']; echo '</font></td>
<td><font size="4">'; echo $rows['cabinet_number']; echo'</font></td>
<td><font size="4">'; echo $rows['key_tag_number']; echo'</font></td>';
if ($rows['switch_and_port1'] =="") {
echo '';
} else if ($rows['switch_and_port1'] !== "") {
echo '<td><font size="4">';
echo '<input type = "image" src= "images/view_details.png" height="16" width="16" class="my_modal_open">', '</font></td>';
}
{
echo '<td><font size="4">'; echo '<input type = "image" src= "images/view_details.png" height="16" width="16" class="my_modal_open">', '</font></td>';
}
echo '</tr>';
echo '<div class="well" style="display:none;margin:1em;" class="my_modal">';
echo '<img src="images/hdc_logo_transparent.png"><br>';
echo '<div style="height:23px; width:100%; background-color:black"> <h4><font color="#FFFFFF">',' ', 'Cabinet: ', $id, '</font></h4></div>';
echo '<p>';
echo '<br>';
echo '<img src="images/bulletpoint_orange.png">';
echo 'Power: ', $rows['power_circuit'];
echo '<br>';
echo'<img src="images/bulletpoint_orange.png">';
echo 'Sw/Po: ', $rows['switch_and_port1'];
if ($rows['switch_and_port2'] =="") {
echo '';
} else if ($rows['switch_and_port2'] !== "") {
echo '<br>';
echo '<img src="images/bulletpoint_orange.png">';
echo 'Sw/Po: ', $rows['switch_and_port2'];
}
if ($rows['switch_and_port3'] =="") {
echo '';
} else if ($rows['switch_and_port3'] !== "") {
echo '<br>';
echo '<img src="images/bulletpoint_orange.png">';
echo 'Sw/Po: ', $rows['switch_and_port3'];
}
if ($rows['switch_and_port4'] =="") {
echo '';
} else if ($rows['switch_and_port4'] !== "") {
echo '<br>';
echo '<img src="images/bulletpoint_orange.png">';
echo 'Sw/Po: ', $rows['switch_and_port4'];
}
echo '</p>';
echo'</div></form>';
}
echo'<script>
$(document).ready(function() {
$(".my_modal_open").click(function(){
$(this).closest("tr").next(".my_modal").popup({"autoopen": true});
});
});
});
</script>
</body>
</html>';
}
The issue is that you are setting the same id attribute to all the modals (which is invalid HTML markup) and expecting the click handler on the button to figure out which one to open.
UPADTE
Another issue with your markup is that you can't have <div>s as siblings of a table row, it will produce unexpected results as browsers will attempt to fix the markup and rearrange the elements, you could place the divs inside a td or use a second loop to build them.
Now I also did some digging in your popup plugin and I must say it's not very well implemented, I would recommend you look for another one. That being said I did manage to get it working, here's what you need to do:
Set an unique id for each of the modal divs, if you have some sort of id in your database data you can use that, if not then you can set up a counter variable inside the loop:
echo '<div class="well" id="my_modal'.$rows['id'].'">';
Use the same id as the class for the button that should open the modal but add "_open" as a suffix:
echo '<input type = "image" src="images/view_details.png" height="16" width="16" class="my_modal'.rows['id'].'_open">
You can now initialize the popup plugin on your divs and it will automatically assign the open action to the corresponding button:
echo'<script>
$(document).ready(function () {
$("[id^=\'my_modal\']").each(function () {
$(this).popup();
});
});
</script>';
That should fix the modal problem, here's a working demo.
Now I also have a couple observations about your code:
You should avoid the use of inline styles and as I mentioned earlier the <font> tag should be replaced for proper CSS, I would also suggest using CSS padding/margin to add space between words instead of
This type of code:
if ($rows['switch_and_port1'] =="") {
echo '';
} else if ($rows['switch_and_port1'] !== "") {
echo 'SOME CONTENT';
}
can be optimized to just:
if ($rows['switch_and_port1'] !== "") {
echo 'SOME CONTENT';
}
echoing an empty string produces no output whatsoever and there's no need for an elseif
I've used this php code within my html in order to display all pictures within a folder called uploads. The only problem is that icons images are the only type of image being displayed. What have I done wrong?
<?php
$files = glob("uploads/*.*");
$colCnt=0;
echo '<table border="1" style="width:590px;">';
for ($i=1; $i<count($files); $i++)
{
$colCnt++;
if ($colCnt==1)
echo '<tr>';
echo '<td width="25%" style="font-size:8.5px; font-family:arial">';
$num = $files[$i];
echo '<img src="'.$num.'" align="absmiddle" /> ';
print substr(substr($num,6,100),0,-4);`
echo '</td>';
if ($colCnt==4)
{
echo '</tr>';
$colCnt=0;
}
}
echo '</table>';
?>
The loop seems to be ok.
You have to focus on the result coming from
$files = glob("uploads/*.*");
Try print_r($files) to get the list and then see if it is selecting all the images properly or not.
I have a css question.
I have the following php code which displays a name.
while ($db_field = mysql_fetch_assoc($result)) {
print $db_field['ship_name'] . "<BR>";
I'm trying to add some text style to it but I'm not that good in css and I'm somehow lost.
I'm trying to do something like <t> $db_field['ship_name'].<t/> but it gives me an error.
judging by your comment, probably you want
print "<span class='t'>".$db_field['ship_name']."</span><BR>";
and for your CSS file define
.t { font-size: 50pt; color:black; text-shadow: 0 1px 2px white; }
There are 2 ways of doing this. Either you embed html in PHP or write separate HTML snippet. I will show you the both ways :
The first one is already explained above in the answer by sinclairchase.
echo "<td>" . $db_field['ship_name'] . "</td>";
The other way is :
<?php while ($db_field = mysql_fetch_assoc($result)) {
?>
<td>
<?php print $db_field['ship_name'] . "<BR>"; ?>
</td>
<?php } ?>
I dont unsderstand why you write t in question it would be td.
This will echo out the data into a span tag:
echo "<span>" . $db_field['ship_name'] . "</span>";
To add a css class:
echo "<span class=\"class_name\">" . $db_field['ship_name'] . "</span>";
Then in your css file:
span.class_name { font-size:24px; color:#666; }
I'm trying to create a simple dynamic gallery with a radio button under each image to allow the user to select an image and submit the form. I'm not concerned with processing the form yet, I'd just like to figure out how to dynamically generate the form. Currently I'm creating the gallery with this;
<?php
$images = "image_gallery/";
$big = "big/";
$cols = 2;
if ($handle = opendir($images)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != rtrim($big,"/")) {
$files[] = $file;
}
}
closedir($handle);
}
$colCtr = 0;
echo '<table width="100%" cellspacing="3"><tr>';
foreach($files as $file)
{
if($colCtr %$cols == 0)
echo '</tr><tr><td colspan="2"><hr /></td></tr><tr>';
echo '<td align="center"><img src="' . $images . $file . '" /></td>';
$colCtr++;
}
echo '</table>' . "\r\n";
?>
It seems as though I should create the radio buttons inside of the foreach loop, but I'm not sure exactly where, or how.
I appreciate any help.
in your foreach loop:
foreach($files as $file){
if($colCtr %$cols == 0)
echo '</tr><tr><td colspan="2"><hr /></td></tr><tr>';
echo '<td align="center"><img src="' . $images . $file . '" /><input type="radio" name="should be common if to choose one between mutiples" value="the value you want to send via form" /></td>';
$colCtr++;
}
echo '</table>' . "\r\n";