I have a div which is not being pushed down by the content inside it. Instead the content just overlaps the div. I assume this is because there's a PHP while loop between the div tags? How do I fix this?
session_start();
if (!$_SESSION["user_name"])
{
header("Location: index.php");
}
include('header.php');
$id = $_GET['id'];
if(isset($id)) {
connect_to_db();
mysql_query("DELETE FROM content WHERE id='$id'");
$deleted = 'Content Successfully Deleted.<br>';
}
echo '<div id="content">';
echo '<h2>Delete Content</h2>';
if(isset($deleted)){
echo $deleted;
}
connect_to_db();
$query="SELECT id, date, title, image FROM content ORDER BY date DESC";
$result=mysql_query($query);
while($row = mysql_fetch_array($result)){
echo '<div id="delete" align="center">';
echo '<img src="'.$row['image'].'" style="border:1px solid black; width:100px;"><br>Delete';
echo '</div>';
}
echo '</div>';
Put overflow: hidden; CSS rule on the div which has the overlapping content in it.
try clearing the div inside the while loop, maybe something like
while($row = mysql_fetch_array($result)){
echo '<div id="delete" align="center">';
echo '<img src="'.$row['image'].'" style="border:1px solid black; width:100px;"><br>Delete';
echo '<div style="clear:both"></div></div>';
}
may help?
or declare the <a> element as a block element?
Using a PHP loop is not related to this issue, what matters is the HTML code that is served to your web browser.
Try adding a clear: both; style to the div in question:
<div id="delete" align="center" style="clear: both;">
You may also align using CSS instead of a HTML attribute:
<div id="delete" style="text-align: center; clear: both;">
UPDATE I see that your DIV elements are overleaping, not floating next to each other as I originally thought.
Check your CSS and see if you can find any position: absolute that applies to these DIV elements, and remove it. In addition, you should use class="delete" instead of id="delete", as multiple elements should not share the same id attribute.
For testing, you can try:
<div id="delete" style="text-align: center; position: block;">
Related
I've been trying to set up a website wherein if the user goes to the specific page of my website the user will be able to see the messages that corresponds to the users email and the user will be able to click the whole table to view the whole message itself, but the anchor tag wraps itself only once in the entire while loop so i get a series of tables that only has one table that's clickable what should i do to wrap every table inside the anchor tag every while loop?
I've tried setting the anchor tag on every individual table row but the same thing still happens it's only being applied in the first table
$sql = "SELECT user_email, username, departmentSent, subject, report FROM userissues";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if($row["departmentSent"] == $useremail=$_SESSION['user_email']){
echo '<a href="#">
<table border="1" style="margin-left: 162px; width: 300px; float: left; height: 50px;">
<tr><td>'.$row["user_email"].'</td></tr><br>
<tr><td>'.$row["subject"].'</td></tr><br>
<tr><td> '.$row["report"].'</td></tr><br><br></a>';
}else{
echo'<p style="margin-left: 162px; width: 300px; float: left; height: 50px;" >there are no email that correspond to this email</p>';
break;
}
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
I want the user to actually be able to click every table that's printed
You should close the </table> before the close anchor tag </a>.
remove this line after while loop ends echo </table>;
add </table> before </a>
like this: <tr><td> '.$row["report"].'</td></tr><br><br></table></a>
it's because of DOM auto adjustment, where are you making multiple tables starting but closing them once so it removes the anchor and put them on end of a table to auto adjust.
hope this makes it working.
I'm currently developing a simple web page that enables the user to: upload an image and a corresponding caption to a DB, let the user view the images and delete them.
I have already accomplished the first two with the following code:
<?php
#include_once("connection.php");
$db = new mysqli("192.168.2.2", "root", "", "proyectoti");
if ($db->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo "Información de servidor: ";
echo $db->host_info . "\n";
// Initialize message variable
$msg = "";
// If upload button is clicked ...
if (isset($_POST['upload'])) {
// Get image name
$image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); #$_FILES['image']['name'];
// Get text
$image_text = mysqli_real_escape_string($db, $_POST['image_text']);
$sql = "INSERT INTO images (image, image_text) VALUES ('{$image}', '{$image_text}')";
// execute query
mysqli_query($db, $sql);
}
$result = mysqli_query($db, "SELECT * FROM images");
?>
<!DOCTYPE html>
<html>
<head>
<title>Proyecto TI | Sube imágenes</title>
<style type="text/css">
#content{
width: 50%;
margin: 20px auto;
border: 1px solid #cbcbcb;
}
form{
width: 50%;
margin: 20px auto;
}
form div{
margin-top: 5px;
}
#img_div{
width: 80%;
padding: 5px;
margin: 15px auto;
border: 1px solid #cbcbcb;
}
#img_div:after{
content: "";
display: block;
clear: both;
}
img{
float: left;
margin: 5px;
width: 300px;
height: 140px;
}
</style>
</head>
<body>
<h1>Proyecto TI | <a> Interfaz </a></h1>
<div id="content">
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
#echo "<img src='images/".$row['image']."' >";
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'"/>';
echo "<p>".$row['image_text']."</p>";
echo "</div>";
}
?>
<form method="POST" action="index.php" enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" name="image">
</div>
<div>
<textarea
id="text"
cols="40"
rows="4"
name="image_text"
placeholder="Di algo de esta imagen ^^"></textarea>
</div>
<div>
<button type="submit" name="upload">Publicar</button>
</div>
</form>
</div>
</body>
</html>
It looks like this:
Now, the only part I'm missing is being able to delete an image (basically I only echo each image), how would you suggest for me to accomplish this, to make each item clickable and let's say, pop up a dialog or button to perform an action (delete from DB).
I really don't know much about PHP or CSS/HTML, any help would be much appreciated, Thank you!
Within this loop:
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
#echo "<img src='images/".$row['image']."' >";
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'"/>';
echo "<p>".$row['image_text']."</p>";
echo "</div>";
}
?>
Personally I would add an element to click on - like an 'x' or whatever - with a unique data attribute:
https://www.abeautifulsite.net/working-with-html5-data-attributes
You have to add the unique id of the image obviously, so you can let SQL know which row to delete... Like this:
echo "<div class='delete-image' data-id='" . $row['id'] . "'>x</div>';
Then I would link this class to an AJAX call to make an asynchronous request to the server and delete the image without reloading the page. It's not very hard.
An easier solution would be to create a new form in the loop, so you create multiple forms per image, add a hidden field with the image id in the form and make a submit button with the valeu 'delete' or simply 'x'.
The same way you created the check:
if (isset($_POST['upload'])) { ... }
You can create something like this:
if (isset($_POST['delete-image'])) { ... }
You will be carrying the image id value like a normal form input. And you can do whatever you want with it.
I would HIGHLY suggest you to look into how to work with jquery and ajax calls though.
Opening a dialogue and ask the user before he deletes an item will require that you either go another page for deletion or use javascript for this.
In both cases, you should somehow set an identifier for your image in your html-code.
I would suggest you give every image an id
'<img ... id="'.$yourImageId.'">'
or a data-attribute
'<img ... data-identifier="'.$yourImageId.'" >'
with that identifier.
First variant:
...
echo '<a href="/path/to/delete/view/page.php?image=yourImageId">'
echo '<img ... id="'.$yourImageId.'"/>'
echo '</a>'
...
and on this delete-view-page, you just have a form that triggers your delete-code
<form action="/path/to/delete/view/page.php" method="POST">
<input type="hidden" name="id" value="<?php echo $yourImageId ?>">
</form>
<!-- after this, react with $_POST['id'] --> to the id sent to the server side and delete the image in your database -->
The other way is not server side rendered.
You should give your Elements some class like "my-clickable-image".After that, you have a script on your page, that looks something like the following
<script>
/* get your images with querySelectorAll, the . stands for class and after that your name */
var clickables = document.querySelectorAll(".my-clickable-image");
clickables.foreach(function(image){
// say that for each image, when clicked the generated function is called image.addEventListener('click',generateShowDialogueFunc(image.getAttr("id")));
});
// generate a function(!) that reacts to an image being clicked
function generateShowDialogueFunc(imageIdentifier){
// return a function that adds a Pop Up to the page
// the Pop Up has approximately the code of the first options second page
// except that now, it must create and remove elements in javascript
return function createPopUp(){
removePopUp();
var popup = document.createElement("div");
popup.setAttribute("id","deletePopUp");
var deleteForm = document.createElement("form");
deleteForm.setAttr("action","/path/to/file/that/deletes/given/query.php?id="+imageIdentifier);
var deleteContents = '<p> Do you want to delete this image? </p>'
+ '<button type="submit"> yes </button>'
+ '<button onclick="removePopUp()"> no </button>'
deleteForm.innerHTML = deleteContents;
document.body.appendChild()
}
}
// remove the Pop Up that can be used to delete an image from the page
function removePopUp(){
var existingPopUp = document.getElementById("deletePopUp");
if(existingPopUp) document.body.removeChild(existingPopUp);
}
</script>
<!-- just add some styling to make the popup show on top of the page -->
<style>
#deletePopUp{
width: 50vw;
height: 50vh;
position: absolute;
z-index: 1;
padding: 1em;
}
</style>
In this case, you just call the server to delete the image, not to show the delete form.
I would suggest the second one but stack overflow is not made for opinion based answers.
Regarding simple security:
It looks like your users could give titles or texts to images.
try what happens if a user gives a title like <bold>some title</title>
and guess what would happen if the title is <script>window.location.href="google.com"</script>
(XSS * hint hint *)
Regarding code structure:
If you want to do something like web development more often, think about separating your database accessing code, and your logic code from your php page template code, this is called 3 tier architecture and standard for bigger projects but i guess this is really just a first, short prototype or test.
Echo works fine at other lines But when i try to add tag to this table, i see that tags placed out of the tag.
<table>
<th style="cursor:pointer;border-radius:5px 0px 0px 0px;">Başlık</th>
<th style="cursor:pointer;">Başlatan</th>
<th style="cursor:pointer;border-radius:0px 5px 0px 0px;">Tarih</th>
$sonuc = mysql_query("select A.subject, A.poster_name, A.poster_time, A.id_msg, A.id_topic, B.id_first_msg, B.id_member_started from smf_messages A, smf_topics B WHERE A.id_msg = B.id_first_msg ORDER BY id_topic DESC LIMIT 10");
if(mysql_num_rows($sonuc)!=0)
{
while($oku = mysql_fetch_object($sonuc))
{
echo '<tr id="iceriktablo" style="cursor:pointer;margin-top:0;margin-bottom:0;">';
echo '<a href="forum/index.php?topic='. $oku->id_topic .'"><td style="font-size:13px;font-weight:bold;">';
echo $oku->subject;
echo '</td></a>';
echo '<a href="forum/index.php?topic='. $oku->id_topic .'"><td style="font-size:13px;font-weight:bold;"><center><b>';
echo $oku->poster_name;
echo '</b></center></td></a>';
echo '<td style="font-size:13px;font-weight:bold;"><center><b>';
$zaman = $oku->poster_time;
echo gmdate("d-m-Y\ H:i:s", $zaman);
echo '</b></center></td></tr></a>';
}
}else{
echo "Hiçbir kayıt bulunamadı!";
}
mysql_close($conn);
?>
</table>
result as inspect element:
http://puu.sh/qed4c/7b04611347.png
result:
enter image description here
filename: index.php
Your HTML is invalid:
echo '<a href="forum/index.php?topic='. $oku->id_topic .'"><td style="font-size:13px;font-weight:bold;"><center><b>';
echo $oku->poster_name;
echo '</b></center></td></a>';
The A-tag isn't allowed to wrap TD (and of course some more). I guess the browser corrected that automatically.
Tag A is not a part of a table to it will be always placed out of it. You can't do somethig like this
<table>
<tr>
<a><td></td></a>
</tr>
</table>
You should place your A tag inside TD or TH tag.
show http://img28.imageshack.us/img28/3052/unledcwk.png
this is my html and the name and the picture is taken from php (mysql).I only want to make 3 of these things(name and picture) side by side not like a queue.So i want the name asdasddasd is in the right of the asdasd.
<?php
echo '<div class="box">';
$kategori=$_GET["kategori"];
$con = mysql_connect("localhost","root","root");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("marmaris", $con);
$bilgi= mysql_query("SELECT * FROM $kategori ");
while($sutun= mysql_fetch_array($bilgi))
{
$name=$sutun["name"];
$resb=$sutun["resbuyuk"];
echo '<div id="texttitle">'.$name.'</div>';
echo '<img src="upload/'.$resb.'" width="202" height="154" alt="resim" style="background-color: #000000" />';
echo '<div id="textdetailup">Detayları görmek için tıklayın.</div>';
}
echo '</div>';
?>
this is the code that makes them.How can i make the side by side showing.
this is the css of the box
.box {
margin-bottom: 10px;
margin-right: 10px;
float: left;
height: auto;
width: 207px;
}
The div you are using to display the image subtext is a block level element so it is pushing the content to the next line.
You need to put echo '<div class="box">'; inside the while loop, along with the corresponding </div>
I have the following css code:
#Layer3
{
position:absolute;
width: 89%;
height: 40%;
left: 10%;
top: 56%;
background-color: #f1ffff;
}
#Layer3 h1
{
font-size: medium;
color: #000033;
text-decoration: none;
text-align: center;
}
.tableheader {
border-width:10px; border-style:solid;
}
.tablecontent {
height: 95%;
overflow:auto;
}
However, when I use this PHP to generate the html
echo '<div id="tableheader" class="tableheader">';
echo "<h1>{$query} Auctions</h1>" . "\n";
echo "</div>";
echo '<div id="tablecontent" class="tablecontent">';
echo "<table border='0' width='100%'><tr>" . "\n";
echo "<td width='15%'>Seller ID</td>" . "\n";
echo "<td width='10%'>Start Date</td>" . "\n";
echo "<td width='75%'>Description</td>" . "\n";
echo "</tr>\n";
// printing table rows
foreach ($rows as $row)
{
$pk = $row['ARTICLE_NO'];
echo '<tr>' . "\n";
table contens generated in here
echo '</tr>' . "\n";
}
echo "</table>";
}
echo "</div>";
which generates this html:
<div id="tableheader" class="tableheader">
<h1>hardy Auctions</h1>
</div>
<div id="tablecontent" class="tablecontent">
<table border='0' width='100%'>
<tr>
<td width='15%'>Seller ID</td>
<td width='10%'>Start Date</td>
<td width='75%'>Description</td>
the rest of table stuff
</div>
The stylesheet is correctly referenced so I am unsure what is causing the error. But there is no border around tableheader at all. Both of these layers are in Layer3 which no longer displays properly on the page.
#tableheader {
border: 10px solid #000;
}
Try giving it a color.
EDIT: since its id is tableheader, try changing the style selector to be an id. You could also try using !important to see if anything is overriding your class selector.
Specificity values:
inline: 1000; id: 100, class: 10, element: 1
!important trumps all other non-important declarations.
Start by browsing the HTML DOM in the rendered page either using Firebug in Firefox or using the IE Developer Toolbar in IE.
That way, you can see what styles are actually associated with the element in the rendered page. It's a lot easier to debug the issue from there.
One possibility is that there's a syntax error somewhere in the CSS file causing the styles not to be applied correctly.
I've just had a quick look at this and it seams fine, I've also created a local copy of these and the styles work out okay as well, I get a nice thick black border around the h1 text.
So from what your explaining something is either overwriting the styles, or the styles aren't being applied to the page.