I am very new in php, though was wable to make opendir() work (just show list of files in a table) now I have a problem of creating a pagination. Sorry I have been working with tutorials out there but can't make them work.I quick help would be very much appreciated.
here's my code:
$dir = 'files/memoranda';
$exclude = array('.','..','.htaccess');
$y = (isset($_GET['y']))? strtolower($_GET['y']) : '';
$res = opendir($dir);
while(false!== ($file = readdir($res))) {
if(strpos(strtolower($file),$y)!== false &&!in_array($file,$exclude)) {
echo'
<div class="Row">
<div class="Cell">
<h4>'.$file.'</h4>
</div>
<div id="Cell" style="text-align: center;vertical-align:middle;border-top:solid 1px #cecece;">
<h4><img style="text-align:center;padding-top:6px;" src="icon_download.png" width="30px" height="30px" ></h4>
</div>
</div>
';
}
}
Related
I have an employee database which includes images as well as their work location (specialty). I have created a page where I fill out a form and upload the image to a directory and the path to the database. I then load the main page where I pull in all the images from the database (into the "photos" DIV. Everything works fine.
What I would like to do is reload the images in the DIV based on a MySQL query from a button. For example, instead of showing all employees, I only want to see those who have a specific job function i.e. Management. I currently have this accomplished by redirecting to a new page, where I run a specific query and that works fine as well. However, I'd like to learn how this is done without creating a new page for each query. I've spent many days looking at AJAX and PHP tutorials, which I how I was able to accomplish what I have, but I can't find a method to do what I want. This is the relevant part of my code:
Main.php
<div class="container-fluid">
<div class="row">
<div class ="col-lg-12" style="width: 18%; height: 100%; border:3px solid red;">
MANAGEMENT
HIGH
</div>
<?php
include ('db_connect.php');
//include_once ('functions.php');
$result = $db->query("SELECT * from monctonfir order by initials ASC");
if($result->num_rows > 0){
while ($row = $result->fetch_assoc()){
$imageURL = 'image/'.$row["file"];
$initial = $row["initials"];
$name = $row["name"];
?>
<div id="photos" class = "col-lg-1 no-gutters" style="margin-top:1rem;">
<div class="card text-center" style="width: 5rem;">
<a href = "#">
<img class="img card-img-top" src = "<?php echo $imageURL ?>">
</a>
<div class = "card-body">
<h5 class = "card-title round-button" style="text-align: center;"><?php echo $initial ?></h5>
</div>
</div>
</div>
<?php }
} ?>
</div>
Can someone point me in the right direction?
Thanks!
You don't need jQuery for what you are doing. You can use query/GET parameters to build your sql so you don't have to create a different page. Like:
<div class="container-fluid">
<div class="row">
<div class ="col-lg-12" style="width: 18%; height: 100%; border:3px solid red;">
MANAGEMENT
HIGH
ALL
</div>
<?php
include ('db_connect.php');
//include_once ('functions.php');
$sql = "SELECT * from monctonfir WHERE 1 ";
if(isset($_GET['job'])) $sql .= " AND job = '".$_GET['job']."' ";
$sql .= " order by initials ASC";
$result = $db->query($sql);
if($result->num_rows > 0){
while ($row = $result->fetch_assoc()){
$imageURL = 'image/'.$row["file"];
$initial = $row["initials"];
$name = $row["name"];
?>
<div id="photos" class = "col-lg-1 no-gutters" style="margin-top:1rem;">
<div class="card text-center" style="width: 5rem;">
<a href = "#">
<img class="img card-img-top" src = "<?php echo $imageURL ?>">
</a>
<div class = "card-body">
<h5 class = "card-title round-button" style="text-align: center;"><?php echo $initial ?></h5>
</div>
</div>
</div>
<?php }
} ?>
</div>
The simplest way is to use the load function of Jquery
Jquery
For example:
$( "#divID" ).load( "loadEmploye.php", { parameters1: 25, parameters2:3 });
I have written a page that displays images in a list style with a jquery slider added to it. However, what i need help with is for the jquery slider to recognizes it as a list of images so that the slider can work. Any help would be much appreciated.
Below is my code:
<div id="slideshow">
<ul class="slides">
<?php
$target = "admin/photogallery/";
$getImages = mysql_query("SELECT * FROM photogallery");
if(!$getImages) die("Cannot execute query. " . mysql_error());
$target = "admin/photogallery/";
while($row = mysql_fetch_array($getImages)){
if (file_exists($target)) {
print '"<li><img src="http://localhost/crystalvirgins/admin/photogallery/'.$row_rsphotogallery['photo'].'" border="0" width="702" alt="'.$row_rsphotogallery['description'].'" /></li>"
<div class="clear_3"></div>';
$i++;
}
}?>
</ul>
<span class="arrow previous"></span>
<span class="arrow next"></span>
</div>
just try this code
<ul>
<?php
$directory = "data/uploads/homegallery/";
if (glob($directory . "*") != false)
{
$filecount = count(glob($directory . "*"));
}
else
{
}
$files_index = glob("data/uploads/"."home"."gallery/*.*");
for ($i=0; $i<$filecount; $i++)
{
$num2 = $files_index[$i];
?>
<li><img src="<?php echo $num2;?>" width="650" height="276" alt="" /> <?</li>
}?>
</ul>
Please include the jQuery Plugin and code being used. Also what does the generated HTML source look like.
I want to put a condition on images come from directory that if the images count is 1 then show this div. But the condition is not running properly after applying the condition on it. Please help me to resolve this problem: My code is given below:
<?php
$id = 115;
$path_image = "pageimage/".$id."/";
if(file_exists($path_image)){
$dir = dir($path_image);
//List files in images directory
while (($file = $dir->read()) !== false){
if($file>=1){
?>
<div id="middleimg" style="background:url(<?php echo $path_image.$file; ?>); background-repeat:no-repeat;">
<div id="nav">
<div id="navinner">
<ul>
<?php
foreach($pageMenu as $pmenu){
$url = generateURL($pmenu->custom_url, $pmenu->page_friendlyname, $pmenu->page_title, $pmenu->page_id);?>
<li><?php echo $pmenu->page_name; ?> </li>
<?php } ?>
<!--<li>HOME</li>
<li>ABOUT</li>
<li>MENU</li>
<li>LOCATION</li>
<li>DELIVERY</li>
<li>SPECIAL OFFER</li>
<li>CONTACT</li>-->
</ul>
</div>
</div>
<div id="banner-containerhome">
<div id="promoBanner">
<!--<div id="pizza">-->
<div id="wowslider-container1">
<div class="ws_images">
<span><img src="data1/images/banner.jpg" alt="" title="" id="wows0"/></span>
<span><img src="data1/images/banner.jpg" alt="" title="" id="wows0"/></span>
</div>
<div class="ws_bullets"><div>
<img src="data1/tooltips/banner.jpg" alt=""/>1
<img src="data1/tooltips/chrysanthemum.jpg" alt=""/>2
</div></div>
<div class="ws_shadow"></div>
</div>
<script type="text/javascript" src="engine1/script.js"></script>
<!-- </div>-->
</div>
<div class="rightcontents">
<?php
echo $page_content[1]->page_content;
?>
</div>
</div>
</div>
<?php }}} ?>
There are a few things...
1.- read() reads the directory as an iterator, returning the file name, have you tried to echo $file; ?
if($file >= 1) will never be true, as $file is always a String, never an integer, you are comparing there: if("myfilename.ext" >= 1).
To count the total files you should do a while through the full directory and use a counter, increasing it for each file.
Or you can use scandir("myPath") function, that get all the files in the path specified as an array. for example:
$dir = scandir("myDir");
$total = count($dir); // this gives you the lenght of the array, therefore the files.
While counting, remember that the parent, and root directory are counted as "files" as well, and with some methods also the thumbs.db(windows) or DS_store(osx) are hidden files that store information about how you sort the folder, folder icons and stuff, will be counted as well, so remember to add an If statement to skip them.
I would like to display folder contents on top of the image "folderContents05.jpg" inside of it's DIV tag. The PHP script should output the folder contents of the directory it resides in. The script works fine by itself, but I can't seem to get anything to display when I use it in html, or call the script.
Here is the HTML-
<head>
<title>index</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="style.css" rel="stylesheet" type="text/css">
<!-- End Save for Web Styles -->
</head>
<body style="background-color:#353535; margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px;">
<!-- Save for Web Slices (index.psd) -->
<div id="wrapper">
<div id="Table_01">
<div id="folderContents01">
<img src="images/folderContents01.jpg" width="359" height="120" alt="">
</div>
<div id="folderContents02">
<img src="images/folderContents02.jpg" width="591" height="150" alt="">
</div>
<div id="folderContents03">
<img src="images/folderContents03.jpg" width="359" height="30" alt="">
</div>
<div id="folderContents04">
<img src="images/folderContents04.jpg" width="126" height="460" alt="">
</div>
<div id="folderContents05">
<img src="images/folderContents05.jpg" width="698" height="396" alt="">
</div>
<div id="folderContents06">
<img src="images/folderContents06.jpg" width="126" height="460" alt="">
</div>
<div id="folderContents07">
<img src="images/folderContents07.jpg" width="698" height="64" alt="">
</div>
<div id="folderContents08">
<img src="images/folderContents08.jpg" width="950" height="90" alt="">
</div>
</div>
</div>
<!-- End Save for Web Slices -->
</body>
</html>
and the php script
<?php
$count = 0;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {$count++;
print("".$file."<br />\n");
}
}
closedir($handle);
}
?>
Use style= "z-index: 0" on your <img> tags and change the PHP to this:
<?php
$count = 0;
if ($handle = opendir('.')) {
echo '<div style="z-index: 1">';
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {$count++;
print("".$file."<br />\n");
}
}
echo '</div>';
closedir($handle);
}
?>
Using the z-index establishes an order of 'on-top-ness' for elements
Is this what you want?
<div id="folderContents05" style="position: relative">
<img src="images/folderContents05.jpg" width="698" height="396" alt="">
<div style="position: absolute; left: 0; top: 0; z-index: 100">
<?php
$count = 0;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$count++;
print("".$file."<br />\n");
}
}
closedir($handle);
}
?>
</div>
</div>
You have to make the "folder content" images as a background to your div and generate the HTML markup by php itself something like the code below
if ($file != "." && $file != "..") {$count++;
print(<div id="folderContents +i">
</div>)
and put a counter so you can use it to generate ( folderContents1 , folderContents2, etc).
I have some php and css code that I'm trying to break down into functions. I need some advice on how to approach this.
<?php
$info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
while ($info_row = mysqli_fetch_array($info))
{
$info_id = $info_row['info_id'];
$info_title = $info_row['info_title'];
?>
<div style="width: 100%;">
<div style="float: left;">
<?php echo $info_id; ?>
</div>
<div style="float: left;">
<?php echo $info_title; ?>
</div>
<div style="clear: both;"></div>
</div>
<?php } ?>
This appears as such:
1 One
2 Two
3 Three
4 Four
Below is what I have so far with separating everything into functions:
function get_info()
{
$dbc = get_dbc();
$info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
while ($info_row = mysqli_fetch_array($info))
{
$result[] = $info_row;
}
mysqli_free_result($info);
return $result;
}
function display_info_id()
{
$result=forum();
foreach ($result as $info_row)
{
$info_id = $info_row['info_id'];
echo $info_id;
}
}
function display_info_title()
{
$result=forum();
foreach ($result as $info_row)
{
$info_title = $info_row['info_title'];
echo $info_title;
}
}
<?php get_info(); ?>
<div style="width: 100%;">
<div style="float: left;">
<?php display_info_id(); ?>
</div>
<div style="float: left;">
<?php display_info_title(); ?>
</div>
<div style="clear: both;"></div>
</div>
This displays as:
1234 OneTwoThreeFour
I understand why it displays as such, but I don't understand how to get it to show the way I want it to. Any advice would be helpful.
Edit--
Is this at all possible to do with the 3 functions I have? I do not want info_id and info_title in the same function.
When I look at software, such as WordPress, it seems I may have to surround my HTML in some kind of loop, and change the way the functions get info?
You are calling a separate loop for each of your variables. It is kind of unnecessary to split this into multiple functions, since the loop requires that the two variables be displayed together.
How would I handle it? I would store the SQL result into a complete array, then loop over that array to output your HTML. That could be split into two functions, if you wish. Maybe useful if you need to use the results more than once on the page.
I'm using the HEREDOC string syntax to combine the PHP variables, (wrapped in {}) with the HTML block. This saves you having to jump in and out of PHP via <?php ?>
// This function is fine.
function get_info()
{
$dbc = get_dbc();
$info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
while ($info_row = mysqli_fetch_array($info))
{
$result[] = $info_row;
}
mysqli_free_result($info);
return $result;
}
function display_result($result)
{
// Loop over your results and display each
foreach ($result as $r)
{
// Now use the HEREDOC syntax to form your HTML block instead
// of intermingling PHP function calls
echo <<<HTML
<div style="width: 100%;">
<div style="float: left;">
{$r['info_id']}
</div>
<div style="float: left;">
{$r['info_title']}
</div>
<div style="clear: both;"></div>
</div>
HTML;
// There must be no whitespace before or after the HTML; on the previous line!
}
}
Now to retrieve your data and display it:
<?php
$results = get_info();
display_result($results);
?>
You need to refactor your display logic into one loop, instead of two separate loops.
For example:
<?php get_info(); ?>
<div style="width: 100%;">
<div style="float: left;">
<?php display_info_id(); ?>
</div>
<div style="float: left;">
<?php
$result=forum();
foreach ($result as $info_row)
{
$info_id = $info_row['info_id'];
$info_title = $info_row['info_title'];
echo $info_id . " . $info_title . "<br/>\n";
}
?>
</div>
<div style="clear: both;"></div>
</div>