I'm working with an older web app, and aside from rewriting everything to modern standards I'm trying to make the modifications as simple as possible.
As you can see below I have a dynamically created list of a directory. The code works but I can't figure out the best way that I can split the list into 3 columns. I'm limited to having this thing work in IE9 (if it works on any other browser that's just a plus). The nested tables are a mess, I know, but that's how this thing was built, I'm just trying to modify it to us the dynamic directory listing (with a couple checkboxes).
Is there some PHP that would do this? Or is there some CSS techniques that can work? I'm just a bit lost on what will work for this blast from the past.
<table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#808080">
<tr>
<td>
<table width="100%" border=0 cellpadding=0 cellspacing=0>
<tr align="center" bgcolor="#000000">
<td valign=bottom class="style6 Normal"><strong>Watertown Network Shared Directory Access<br>
Applies to "P" drive permissions - <br>
By Default all users have at <em>least</em> "read-only" access to all folders/files on the "S" drive. </strong></td>
</tr>
<tr>
<td align="left"><table width="100%" border="0">
<?php
//$path = '\\\\wttfs001\\private';
$path = '\\\\wttfs001\\shared';
$directories = scandir($path);
echo '<ul>';
foreach ($directories as $directory){
if ($directory === '.' or $directory === '..') {
continue;
}
if(is_dir($path . '/' . $directory)){
echo '<input type="checkbox" name="read[]" value="' . $directory . '" />R';
echo '<input type="checkbox" name="write[]" value="' . $directory . '" />W';
echo ' ' . $directory . '<br>';
}
}
echo '</ul>';?>
</table></td>
</tr>
</table>
It's going to depend on how you want them to flow.
What I'm not seeing in your loop are any <li> tags. I would amend your loop as follows:
foreach ($directories as $directory){
if ($directory === '.' or $directory === '..') { continue; }
if (is_dir($path . '/' . $directory)){
echo '<li><input type="checkbox" name="read[]" value="' . $directory . '" />R</li>';
echo '<li><input type="checkbox" name="write[]" value="' . $directory . '" />W</li>';
echo '<li>' . $directory . '</li>';
}
}
Now, you have markup to play around with. From there, you can apply styling to adjust your rows or columns, given a mark up structure that resembles:
HTML
<ul class="dir-1">
<li><input type="checkbox" name="read[]">R</li>
<li><input type="checkbox" name="write[]">W</li>
<li>dir-1</li>
</ul>
<ul class="dir-2">
<li><input type="checkbox" name="read[]">R</li>
<li><input type="checkbox" name="write[]">W</li>
<li>dir-2</li>
</ul>
CSS
ul[class^="dir-"],
ul[class*=" dir-"] {
padding: 0;
margin: 0;
clear: both;
}
ul[class^="dir-"] li,
ul[class*=" dir-"] li {
display: inline-block;
width: 32%;
float: left;
margin-right: 1%;
}
http://codepen.io/anon/pen/aOgOdg
The above was helpful, pointed me in a direction on CSS....found an answer so simple I can't believe I struggled so much with it.
<?php
//$path = '\\\\wttfs001\\private';
$path = '\\\\wttfs001\\shared';
$directories = scandir($path);
echo '<ul class="fred">';
foreach ($directories as $directory){
if ($directory === '.' or $directory === '..') {
continue;
}
if(is_dir($path . '/' . $directory)){
echo '<li class="wilma"><input type="checkbox" name="chkRead[]" value="' . $directory . '" />R</li>';
echo '<li class="wilma"><input type="checkbox" name="chkWrite[]" value="' . $directory . '" />W</li>';
echo '<li class="directory">' . $directory . '</li>';
}
}
echo '</ul>';
?>
I put in the li tag as mentioned and gave them a 2 different classes. 1 for the checkboxes and one for the directory listing.
For CSS I used:
li.wilma{
display: inline-block;
padding-left: .2em;
padding-bottom: .25em;
padding-top: .25em;
}
li.directory{
display: inline-block;
width: 15%;
padding-right: 1em;
padding-left: 2em;
padding-bottom: .25em;
padding-top: .25em;
}
As is, it works so far for what I was looking for, to format a dynamically created directory list with checkboxes. Honestly I have no idea if all the padding is necessary, CSS is such mystery to me yet, but I have a nice looking list now.
Related
In my code, I have a center tag and have styled the center tag so it looks the way I want it to look. It currently looks like this:
Whenever I go on my admin account (one that has a different user id than the normal users) it looks way different than if a regular user would see it. It looks like this:
I would like to know if there is any way around this so it looks the same as if a regular would see it on an admin account.
I've tried putting a div tag around it and using the same styling features, but that didn't work. I've tried putting the center tag around different parts of the code, but since it's an "if" statement, it will look different.
Here is the code:
<style>
center{
border-left: .17em dashed;
border-top: .17em solid;
border-right: .17em dashed;
border-bottom: .17em solid;
padding-left:25px;
padding-bottom: 20px;
width: 1000px;
background-color: #E1A0A0;
border-color: black;
margin: auto;
text-align: left;
}
</style>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<center><b><h2>' . $row['title'] . '</h2></b>' . $row['post'] . '<br/><br/> <b>Posted On: </b>' . $row['postdate'] . '<br/>View Post Comments | ';
if($_SESSION['user_id'] == 3) {
echo '<a href="update.php?id=' . $row['blogid'] . '" >Update Blog Post</a> | <a href="' . basename(__FILE__) . '?id=' . $row['blogid'] . '" >Delete Blog Post</a></center>';
}
}
?>
I know that I can't just put another center tag around the
"echo '<center><b><h2>' . $row['title'] . '</h2></b>' . $row['post'] . '<br/><br/> <b>Posted On: </b>' . $row['postdate'] . '<br/>View Post Comments |';"
part of the code and
"echo '<a href="update.php?id=' . $row['blogid'] . '" >Update Blog Post</a> | <a href="' . basename(__FILE__) . '?id=' . $row['blogid'] . '" >Delete Blog Post</a></center>';"
part of the code because it will look like this:
Additionally, if I put the center tag around everything, it will look like this:
I just want to make it look the same no matter who views it.
after the close of the if(){} you need to echo '</center>' and remove it from inside the if(){}. You have setup a loop where sometimes center is not closed
echo '<center><b><h2>' . $row['title'] . '</h2></b>' . $row['post'] . '<br/><br/> <b>Posted On: </b>' . $row['postdate'] . '<br/>View Post Comments | ';
if($_SESSION['user_id'] == 3) {
echo '<a href="update.php?id=' . $row['blogid'] . '" >Update Blog Post</a> | <a href="' . basename(__FILE__) . '?id=' . $row['blogid'] . '" >Delete Blog Post</a>';
}
echo '</center>';
Not sure if I quite understand what is being asked for, but if all you want to do is make everything look like the last image you posted, remove this in the css:
padding-left:25px;
Please forgive me if I'm asking a stupid question! But I really tried hard and still failing
I was trying to create an error message that would appear in a loop using PHP
The original code looks like this:
// go through lines that had errors
if (property_exists($result, 'return') && is_object($result->return) && property_exists($result->return, 'failed') && $result->return->failed > 0) {
foreach ($result->return->failedRows as $failedRow) {
foreach ($failedRow->errors as $rowErrors)
$message .= "\nline: " . ($failedRow->line) . ", column: " . $rowErrors->column . ", value: '" . $rowErrors->value . "'" . ", message: '" . $rowErrors->details[0]->translated . "'";
}
}
if ($message != "")
throw new Exception('Error details: ' . $message . ' [' . $method . ']');
The error message looks like this right now!
what I'm trying to achieve is to have the error message looking like this:
Again sorry if this seems very simple for any of you but my PHP skills are rather limited and I'm trying to learn all these tricks that may seem very simple to create!
ok here's what I've tried but it's wrong of course!
// go through lines that had errors
if (property_exists($result, 'return') && is_object($result->return) && property_exists($result->return, 'failed') && $result->return->failed > 0) {
foreach ($result->return->failedRows as $failedRow) {
foreach ($failedRow->errors as $rowErrors)
$message .= "<th>Line</th>" .
"<td>" . ($failedRow->line) . "</td>" .
"<th>Column</th>" .
"<td>".$rowErrors->column. "</td>".
"<th>Value</th>" .
"<td>".$rowErrors->value . "</td>" .
"<th>Error message</th>" .
"<td>".$rowErrors->details[0]->translated."</td>";
}
}
if (property_exists($result, 'errors') && is_object($result->errors) && count($result->errors) > 0) {
foreach ($result->errors as $error)
$message .= "\nerror: " . $error;
}
if ($message != "")
echo "<div class='container'><strong>Error details:</strong></br>
<table style='width:100%;'>
<tr>
$message
</tr>
</table></div>;
and here's the result of my misurable try :!
You can try the following and then add your styling classes to it. That should create your table!
if (property_exists($result, 'return') && is_object($result->return) && property_exists($result->return, 'failed') && $result->return->failed > 0) {
foreach ($result->return->failedRows as $failedRow) {
foreach ($failedRow->errors as $rowErrors)
$message .= "<tr>" .
"<td>" . ($failedRow->line) . "</td>" .
"<td>".$rowErrors->column. "</td>".
"<td>".$rowErrors->value . "</td>" .
"<td>".$rowErrors->details[0]->translated."</td>" .
"</tr>";
}
}
if ($message != "")
echo "<div class='container'><strong>Error details:</strong></br>
<table id='t01' style='width:100%;'>
<tr>
<th>Line</th>
<th>Column</th>
<th>Value</th>
<th>Error message</th>
$message
</tr>
</table></div>";
You can also add the following CSS in the front end:
<style>
table {
width:100%;
}
table, th, td {
border: 1px solid #fff;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
table#t01 tr:nth-child(even) {
background-color: #eee;
}
table#t01 tr:nth-child(odd) {
background-color:#fff;
}
table#t01 th {
background-color: #ED7D31;
color: white;
}
</style>
You didn't post anything you tried, so the general way to do it is:
Inside the if-condition (if there are errors), but before the foreach loop, echo the beginning of the table, including the header row, like
echo "<table>
<tr>
<th>Header for Row 1<th>
<th>Header for Row 2<th>
<th>Header for Row 3<th>
</tr>";
Then inside the foreach loop, echo a <tr> tag first, then before every column content variable a <td>, after every column content variable a closing </td> and eventually a cloasing <tr>
After the foreach loop (but still inside the if condition), echo the closing </table> tag.
In my code I use this MySQL query:
$qry="SELECT ServiceIcon FROM Services INNER JOIN UserServices ON UserServices.ServiceID=Services.ServiceID WHERE UserServices.UserID=$_SESSION[SESS_MEMBER_ID]";
$result=mysql_query($qry);
header("Content-type: image/png");
echo mysql_result($result,0);
I want these images to be displayed in a grid of 3 icons wide. Currently they're displayed on top of each other however. So I should echo an like this:
echo '<ul>';
foreach ($images as $image) {
echo '<li><img src="' . $image['src'] . '" id="' . $image['id'] . '" /></li>';
}
echo '</ul>';
What I don't understand is how I should incorporate into my code. Or maybe my code is not even the right way to do it. Please help me out.
EDIT: So this is the complete file in question:
<?php
session_start();
include "connection.php";
// just so we know it is broken
error_reporting(E_ALL);
// some basic sanity checks
$qry="SELECT ServiceIcon FROM Services INNER JOIN UserServices ON UserServices.ServiceID=Services.ServiceID WHERE UserServices.UserID=$_SESSION[SESS_MEMBER_ID]";
$result=mysql_query($qry);
header("Content-type: image/png");
?>
<html>
<head>
<style>
ul.horizontal-display {
margin: 0;
padding: 0;
}
ul.horizontal-display li {
list-style: none;
display: inline-block;
}
</style>
</head>
<body>
<?php
echo '<ul class="horizontal-display">';
foreach ($images as $image) {
echo '<li><img src="' . $result . '" id="' . $image['id'] . '" /></li>';
}
echo '</ul>';?>
</body>
</html>
Hello you need to add CSS. Something like this:
echo '<ul class="horizontal-display">';
foreach ($images as $image) {
echo '<li><img src="' . $image['src'] . '" id="' . $image['id'] . '" /></li>';
}
echo '</ul>';
And in the CSS rules
ul.horizontal-display {
margin: 0;
padding: 0;
}
ul.horizontal-display li {
list-style: none;
display: inline-block;
}
I'm having trouble styling the information that I'm pulling from the database. If anyone can help I'd really appreciate it. I tried defining $style within the while loop, and then assigning it the $questions, but nothing happens on the webpage. I'm new with coding in general, and while I have some knowledge of css, I don't know how you use it within php script.
style for the background I was trying to put behind each question*
#frm1
{
background: #D9D9D9;
margin:auto;
top:150px; left:200px; width:880px; height:60px;
position:absolute;
font-family: "Comic Sans MS", cursive, sans-serif;
font-size: 9px;
font-style: italic;
line-height: 24px;
font-weight: bold;
text-decoration: none;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
padding:10px;
border: 1px solid #999;
border: inset 1px solid #333;
-webkit-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.5);
-moz-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.5);
box-shadow: 0px 0px 40px rgba(0, 0, 0, 0.7);
}
PHP code retrieving info from database*
if (mysql_num_rows($result) >= 0)
{
$toggle = false;
while ($rows = mysql_fetch_array($result, MYSQL_ASSOC) and $i<10 )
{
$i++;
$toggle = !$toggle;
if($toggle)
$style = "id:frm1;";
else
$style = "background: white;";
questions .= "<a style='$style'> </a>";
questions .= "Titlee: " ."<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['title'] . "</a> <br> ";
questions .= "Details: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['detail'] . "</a> <br> ";
questions .= "Category: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['categories'] . "</a> <br> <br> <br> ";
}
echo questions;
}
while I have some knowledge of css, I don't know how you use it within
php script.
Okay.
Your PHP script is a PHP script on the server, and results in a regular HTML page for the user. [See the bottom of the answer, I'll try to give you a quick overview]
You can use CSS exactly as you would with a plain HTML page, and it will work just fine despite being backed by PHP.
This means do not use style="$style". Style attributes are Bad.
As it looks like you want to construct your CSS conditionally, my suggestion is either:
Change a class using PHP, and have an external stylesheet which acts on that class
Put the styles you're conditionally changing inside <style> tags in your header, and change those with PHP.
This answer will use the first option
(Edited to take into account new information)
In your PHP code, before your links:
if($toggle) {
$questions.='<div id="frm1">';
}
else {
$questions.='<div id="frm2">';
}
In your PHP code, after your links:
$questions .= "</div>";
And finally, in either your external stylesheet, or your in-head <style> tags:
#frm1 {
...
}
#frm2 {
...
}
Quick overview of server-side languages
So, web programming. This is generally done in two ways. client side (read: javascript) and server side (in your case, read: php, but there's a lot more to this).
With a client side language like javascript, the code actually gets sent to the web browser. The web browser then modifies the contents of the page according to what the script says for it to do. This means your users can see the code, even turn it off in their web browser or execute other javascript in its place.
With a server side language, there's a different workflow.
The user asks for your webpage (identified by its URL)
The web server (read: your webhosting) receives this request, and looks up what the webpage is
Finding that the webpage is a php page, the server executes the php code
The php code gives the server an html page (which you have built, as you can see, your php script outputs HTML)
The server sends the resulting html code to the user
Note that the web browser, which is the component doing all of the processing of HTML and CSS, never sees the php. By the time your php script reaches your users, it's just an html page.
Because the web browser only sees an HTML page, there is no functional difference between using CSS on your php script, and using CSS on a regular HTML page.
This will work:
if (mysql_num_rows($result) >= 0) {
$toggle = false;
while ($rows = mysql_fetch_array($result, MYSQL_ASSOC) and $i<10 ) {
$i++;
$toggle = !$toggle;
if($toggle)
$style = "background: #D9D9D9;"; else
$style = "background: green;";
questions .= "<a href='#' style='display:block;$style'> </a>";
questions .= "Titlee: " ."<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['title'] . "</a> <br> ";
questions .= "Details: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['detail'] . "</a> <br> ";
questions .= "Category: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['categories'] . "</a> <br> <br> <br> ";
}
echo questions;
}
The problem was your a-tag doesn't have a href attribute and since it's displayed inline (default-behaviour) the background CSS property won't work.
Instead of style, build classes and define them in css.
if ($toggle)
$questionClass="redBackground";
else
$questionClass="greenBackground";
$questions.="<a class='$questionClass'>";
Also, definitely look into mysqli or pdo. mysql_ functions are deprecated and not nearly as cool!
You can do -
if (mysql_num_rows($result) >= 0)
{
$toggle = false;
while ($rows = mysql_fetch_array($result, MYSQL_ASSOC) and $i<10 )
{
$i++;
$toggle = !$toggle;
if($toggle)
$style = "bg";
else
$style = "bg_green";
echo("<a class='".$style."'> </a>");
echo("Titlee: " ."<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['title'] . "</a> <br> ");
echo("Details: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['detail'] . "</a> <br> ");
echo("Category: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['categories'] . "</a> <br> <br> <br> ");
}
In this file add -
<style type="text/css">
.bg {
background: #D9D9D9;
}
.bg_green {
background: green;
}
</style>
As ben said use class.
first create a class
<style>
.gray{background: #D9D9D9;}
.green{background: green;}
</style>
Then try this
if (mysql_num_rows($result) >= 0)
{
$toggle = false;
while ($rows = mysql_fetch_array($result, MYSQL_ASSOC) and $i<10 )
{
$i++;
$toggle = !$toggle;
$style = ($toggle)?"green":"gray";
$questions .= "<a class='".$style."'> Put some thing here </a>";
$questions .= "Titlee: " ."<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['title'] . "</a> <br> ";
$questions .= "Details: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['detail'] . "</a> <br> ";
$questions .= "Category: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['categories'] . "</a> <br> <br> <br> ";
}
echo $questions;
}
Please try it, I have not tested but it should work, according to your need.
You can alternate on your counter $i with $i % 2 to switch between two CSS classes. This will give you 0, 1, 0, 1, 0, 1, ... and so select the first and second CSS class name in turn.
PHP:
$css_class = array('frm1', 'second');
while ($rows = mysql_fetch_array($result, MYSQL_ASSOC) and $i<10 )
{
$i++;
questions .= "<a class='$css_classes[$i % 2]'> </a>";
questions .= "Titlee: " ."<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['title'] . "</a> <br> ";
questions .= "Details: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['detail'] . "</a> <br> ";
questions .= "Category: " . "<a href='show_question2.php?question_id=" . $rows["question_id"] . "'>". $rows['categories'] . "</a> <br> <br> <br> ";
}
and in your CSS file you define the two classes
.frm1 {
background: #D9D9D9;
margin:auto;
top:150px; left:200px; width:880px; height:60px;
position:absolute;
...
}
.second {
background: white;
}
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!