PHP issues displaying image gallery - php

I'm trying to make a gallery with PHP. I want to get all of the images out of a folder and then display them in rows of 3. I kind of have it working but the first 2 images mess up.
This is what I've tried:
$images = glob("$_SERVER[DOCUMENT_ROOT]/gallery/img*.{png,jpg,gif}", GLOB_BRACE);
echo '<table width="100%>';
$count="-1";
foreach($images as $image) {
if ($count%3 == 1) {
echo '<tr>';
}
$url=str_replace("/home/#####/public_html/gallery", "", $image);
echo '<td width="33%"><div class="gallery">';
echo '<img onclick="window.location='.$url.'" src="'.$url.'" alt="Image Alt" width="400" height="300">';
echo '</div></td>';
if ($count%3 == 3) {
echo '</tr>';
}
//echo $count;
$count++;
//echo "|".$count;
}
if ($count%3 != 1) {
echo ',</tr>';
}
echo '</table>';
//echo print_r($images);
This works kind of but it makes this:
(These are just stock photos, the real photos are a bit.. offensive)
I know I'm doing something wrong but I don't know what!

There were some errors in your code (see comments). Maybe try this:
$images = glob("$_SERVER[DOCUMENT_ROOT]/gallery/img/*.{png,jpg,gif}", GLOB_BRACE);
echo '<table style="width:100%">'; // error was here (missing ")
$count = 0; // error was here (counter = "-1")
foreach ($images as $image) {
// start <tr> on 0
if ($count == 0) {
echo '<tr>';
}
$url=str_replace("/home/#####/public_html/gallery/", "", $image);
echo '<td style="width:33%"><div class="gallery">'; // alternative
echo '<img onclick="window.location='.$url.'" src="'.$url.'" alt="Image Alt" width="400" height="300">';
echo '</div></td>';
// end tr at 3
if ($count == 3) {
echo '</tr>';
// reset counter
$count = -1;
}
$count++;
}
echo '</table>';

I think you have trouble with your $count initial value.
Try this:
$count="3";
foreach($images as $image) {
if ($count%3 == 0) {
echo '<tr>';
}
$count++;
...

Related

PHP: Show 3 td's of data per table row

I am wanting to use 8 images from my database and load them into a HTML Table. I would like to display 4 images per table row, but however when i run my code below, i seem to get all images in one table row. Any help would be great. Thank you in advance.
$count = $get->rowCount();
if ($count > 0)
{
echo '<table id="" class="uiGrid _51mz _1m6c" cellpadding="2" cellspacing="0">';
$i = 0;
while ($r = $get->fetch(\PDO::FETCH_OBJ))
{
$globals = new \Libraries\Helpers\Views\Globals;
if ($i == 0)
{
echo '<tr class="_51mx">';
}
echo '
<td class="_51m-">
<a href="/e/a/'.$r->data_id.'">
<div class="uiScaledImageContainer _f-u2" style="width:74px;height:74px;">
<img class="scaledImageFitWidth img" src="https://gstatic.acfee.org/akamaihd/i/'.$globals->data_image_name($r->data_id).'">
<div class="_3s6x">
<div class="_50f3"></div>
</div>
</div>
</a>
</td>
';
if ($i > 4)
{
$i = 0;
echo '</tr>';
};
$i++;
echo '
<script type="text/javascript">
$("#favs-preloader").hide();
</script>
';
}
echo '</table>';
put this code directly it will works...
echo '<table id="" class="uiGrid _51mz _1m6c" cellpadding="2" cellspacing="0">';
$i = 0;
while ($r = $get->fetch(\PDO::FETCH_OBJ))
{
$globals = new \Libraries\Helpers\Views\Globals;
$i++;
if ($i == 1)
{
echo '<tr class="_51mx">';
}
echo '
<td class="_51m-">
<a href="/e/a/'.$r->data_id.'">
<div class="uiScaledImageContainer _f-u2" style="width:74px;height:74px;">
<img class="scaledImageFitWidth img" src="https://gstatic.acfee.org/akamaihd/i/'.$globals->data_image_name($r->data_id).'">
<div class="_3s6x">
<div class="_50f3"></div>
</div>
</div>
</a>
</td>
';
if ($i >= 4)
{
echo '</tr>';
$i = 0;
};
echo '
<script type="text/javascript">
$("#favs-preloader").hide();
</script>
';
}
echo '</table>';
In your current code, you're verifying if the $i variable is == 0 in order to add a row. But you're always increasing it's value at the end, even after you set it to zero in the if($i > 4)
Change these lines
if ($i > 4)
{
$i = 0;
echo '</tr>';
};
$i++;
To this:
if ($i > 4)
{
$i = 0;
echo '</tr>';
}
else
$i++;

Handling URLs correctly with PHP

i have the following case within my plugin for wordpress: I have a details page of a database entry which can be accessed like this:
http://localhost:8888/studio/wp-admin/admin.php?page=myplugin-details&id=42
The next would be to add sorting options to this page and i solved it with this code:
The url would be:
http://localhost:8888/studio/wp-admin/admin.php?page=myplugin-details&id=42&orderby=answer&order=asc
<?php
if (!empty($_GET['orderby'])) {
$pos = strpos($_SERVER["REQUEST_URI"], 'orderby');
$url = substr($_SERVER["REQUEST_URI"], 0, $pos-1);
if ($_GET['order'] == 'desc') {
echo '<th class="sortable desc">';
echo '<a href="'.$url.'&orderby=answer&order=asc">';
} else {
echo '<th class="sortable asc">';
echo '<a href="'.$url.'&orderby=answer&order=desc">';
}
} else {
echo '<th class="sortable desc">';
echo '<a href="'.$_SERVER["REQUEST_URI"].'&orderby=answer&order=asc">';
}
?>
This works fine, but do i have to do that URL/REQUEST_URI stuff or is there a solution which is much simpler?
Thanks!
In my opinion you should not use strpos to extract the URL. Things could get funky if you made changes to your code later on. You could use str_replace instead :
if ($_GET['order'] == 'desc') {
echo '<th class="sortable desc">';
echo '<a href="'.str_replace("order=desc", "order=asc", $_SERVER["REQUEST_URI"]).'">';
} else {
echo '<th class="sortable asc">';
echo '<a href="'.str_replace("order=asc", "order=desc",$_SERVER["REQUEST_URI"]).'">';
}

How to put mysql data into popup window?

I have a web page with images and when user clicks on any of the image, it has to derive data of that particular image from MYSQL database. What I am doing is using a simple JavaScript popup and putting the data from database. However I am just getting the first item from database on all images.
This is the code:
$files = glob("admin/images/paintings/*.*");
echo '<div id="painting"><table border="0" style="width:590px;">';
$colCnt=0;
$i = 0;
while($row = mysql_fetch_array($result))
{
if ($colCnt%4==0)
echo '<tr>';
echo '<td width="25%" style="font-size:8.5px; font-family:arial">';
echo($i);
$num = $files[$i];
echo '<img id="indPainting" src="'.$num.'" align="absmiddle" /> <br> <div id="paintingName">';
print $row['name'];
echo '<div id="openModal" class="modalWindow">
<div>
<p>This is a sample modal window that can be created using CSS3 and HTML5.'.$row['name'].'</p>
Ok
</div>
</div>';
echo '</td>';
$colCnt++;
if ($colCnt==4)
{
echo '</tr>';
$colCnt=0;
}
$i++;
}
mysql_close($con);
include 'footer.php';
?>
$row['name'] is just giving out the first name as it is in a while loop. I am not being able to get other names for other images. How can this be done. Any help would be appreciated.
Does one iteration in your while fetch single image data? And what I can understand according to your code is that you are displaying 4 image in a row.
Can you please format your code a bit..its looking too ugly.
I need to know which statement is calling your modal window.
<?php
$files = glob("admin/images/paintings/*.*");
echo '<div id="painting"><table border="0" style="width:590px;">';
$colCnt=0;
$i = 0;
echo '<tr>';
while($row = mysql_fetch_array($result))
{
$num = $files[$i];
echo '<td width="25%" style="font-size:8.5px; font-family:arial">';
echo '<img id="indPainting" src="'.$num.'" align="absmiddle" /> <br>
<div id="paintingName">';
print $row['name'];
echo '<div id="openModal" class="modalWindow"><div><p>This is a sample modal window that can be created using CSS3 and HTML5.'.$row['name'].'</p>Ok</div>
</div></td>';
$colCnt++;
if ($colCnt % 4 == 0)
{
echo '</tr>';
$colCnt=0;
}
$i++;
}
mysql_close($con);
include 'footer.php';
?>
Try this.
Also see how beautiful the code looks if its properly formatted..
try this
<?php
$files = glob("admin/images/paintings/*.*");
echo '<div id="painting"><table border="0" style="width:590px;">';
$colCnt=4;
while($row = mysql_fetch_array($result))
{
for ($i = 0; $i < $colCnt; $i++) {
echo '<tr>';
echo '<td width="25%" style="font-size:8.5px; font-family:arial">';
echo($i);
$num = $files[$i];
echo '<img id="indPainting" src="'.$num.'" align="absmiddle" /> <br> <div id="paintingName">';
print $row['name'];
echo '<div id="openModal" class="modalWindow">
<div>
<p>This is a sample modal window that can be created using CSS3 and HTML5.'.$row['name'].'</p>
Ok
</div>
</div>';
echo '</td>';
}
if ($colCnt==4)
{
echo '</tr>';
$colCnt=0;
}
}
mysql_close($con);
include 'footer.php';
?>

Displaying all Images within a Folder using php

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.

why is this ajax call not reading my internal php?

Too many edits to keep track of, but I have simplified the issue. I have this code in my index.php:
<div class="calendar_top">
<?php
include(SITE_ROOT . "/includes/sub_top_divs.php"); ?>
</div>
<table class="tablebodycontainer"><tr><td>
<?php
include(SITE_ROOT . "/includes/view-monthly-calendar-ajax.php");
?>
</td></tr></table>
<?php include(SITE_ROOT . "/includes/copyright.php"); ?>
</div>
The subtop above contains the navigation links:
includes/subtop-divs.php:
<?php
//include(SITE_ROOT . "/includes/set-variables.php");//dateFormat($date)
echo "<table class='navtabs' cellpadding='0' cellspacing='0'>";
echo "<tr><td class='right'>";
echo buildMenuNavigation($currentPageIndex);
echo '</td></tr></table>';
?>
Here is that buildMenuNavigation function:
function buildMenuNavigation($currentIndex=0) {
$navtabs = array(
'0'=>array('Monthly'=>'index.php'),
'1'=>array('Daily'=>'agenda.php'),
'2'=>array('Admin'=>'admin/view-timelines.php'),
'3'=>array('Help'=>'help.php'),
);
$sep = '<li> | </li>';$builtNav=array();
foreach($navtabs as $index=>$tablinks) {
foreach($tablinks as $key=>$value) {
$class='';
if($index==$currentIndex) {
$class=' class="selected"';
}
//pr($value);
$builtNav[] = '<li><a href="' . SITE_URL . '/' . $value.'"' . $class .'> '.$key.' </a></li>';
}
}
return '<ul>' . implode($sep,$builtNav) . '</ul>';
}
The only data that actually changes when switching pages is the contents in the above:
<?php
include(SITE_ROOT . "/includes/view-monthly-calendar-ajax.php");
?>
So this seems like a perfect candidate for ajax.
Here is the contents of /includes/view-monthly-calendar-ajax.php:
<?php
$counter = 0;
?>
<table class="tabbody">
<tr>
<td class='head unselectable'>Sun</td>
<td class='head unselectable'>Mon</td>
<td class='head unselectable'>Tue</td>
<td class='head unselectable'>Wed</td>
<td class='head unselectable'>Thu</td>
<td class='head unselectable'>Fri</td>
<td class='head unselectable'>Sat</td>
</tr>
<tr>
<?php
//echo $year."-".$month."-";
$flag = 0;
$daysInrow = 0;
for($i = 1; $i < $numDays+1; $i++, $counter++)
{
$daysInrow++;
$zero = "";
if($i < 10)
{
$zero = "0";
}
$t_date = $year."-".$month."-".$zero.$i;
//$t_date = "$year-$month-$i";
$timeStamp = strtotime($t_date);
$eventID = 0;
$eventName = " ";
$bgColor = "";
$funcBG = "setbgcolorMonth($i);";
if($i == 1)
{
// Workout when the first day of the month is
$firstDay = date("w", $timeStamp);
for($j = 0; $j < $firstDay; $j++, $counter++)
echo "<td class='tddaybox'></td>";
}
if($counter % 7 == 0)
{
$daysInrow = 1;
echo "</tr><tr>";
}
if(date("w", $timeStamp) == 0)
if($i == date("d") && $month == date("m") && $year == date("Y"))
$class = "class='today'";
else
$class = "class='weekend'";
else
if($i == date("d") && $month == date("m") && $year == date("Y"))
$class = "class='today'";
else
$class = "class='normal'";
$numric_time = getNumericTime($_SESSION['userData']['timezone']);//Get the numeric timezone
$query = "SELECT * FROM events WHERE date(convert_tz(StartDate,'+00:00','". $numric_time."'))='".$t_date."' AND UserID='" . $_SESSION['userData']['UserID'] ."' ORDER BY PTLType ASC";
//br();
$result = mysql_query($query);
if(mysql_num_rows($result))//cursor:hand;cursor:pointer;width:112px;
{
$funcBG = "";
echo "<td valign='top' class='a_cursor_width tddaybox' id='day$i' onclick='setbgcolorMonth($i)'>";
echo "<div class='td_overlow'>";
echo '<table style="width:100%;border:0;">';
echo '<tr class="rowColor"><td '.$class.'><div class="div_left">'.$i.'</div><div class="div_left_80" onclick="'.$funcBG.' get_event_popup(window.event, this, \''.SITE_URL.'/event-popup.php\', \'eventID='.$eventID.'\', \'date='.$date.'\', \'day='.$i.'\', \'type=M\');return false"> </div></td></tr>';
while($row = mysql_fetch_assoc($result))
{
$eventID = $row['EventID'];
$parentEventID = $row['ParentEventID'];
$eventName = stripslashes($row['EventName']);
$PTLType = $row['PTLType'];
$textclass = "title4";
$onclick_call = 'get_event_popup(window.event, this, \''.SITE_URL.'/event-popup.php\', \'eventID='.$eventID.'\', \'date='.$date.'\', \'day='.$i.'\', \'type=M\')';
if($PTLType != 0) {
$onclick_call = 'get_event_popup(window.event, this, \''.SITE_URL.'/timeline-popup.php\', \'eventID='.$eventID.'\', \'parentEventID='.$parentEventID.'\', \'type=M\')';
$bgColor = "";
$textclass = "redtext";
if($PTLType == 3) {
$textclass = "bluetext";
$display = "none";
} else {
$display = "block";
}
$event_name_wrapped = '<div>'.$eventName.'</div>';
} else {
$textclass = "mainEvent";
$display = "block";
$event_name_wrapped = '<div>'.$eventName.'</div>';
}
echo '<tr><td onclick="'.$onclick_call.';return false;" class="'.$textclass.' a_cursor" title="'.$eventName.'" style="display:'.$display.';">'.$event_name_wrapped.'</td></tr>';
}
echo '<tr><td onclick="get_event_popup(window.event, this, \''.SITE_URL.'/event-popup.php\', \'eventID=0\', \'date='.$date.'\', \'day='.$i.'\', \'type=M\');return false;" class="a_cursor"> </td></tr>';
echo "</table>";
echo "</div>";
echo "</td>";
}
else
{
echo '<td id="day'.$i.'" height="80" valign="top" onclick="'.$funcBG.' get_event_popup(window.event, this, \''.SITE_URL.'/event-popup.php\', \'eventID='.$eventID.'\', \'date='.$date.'\', \'day='.$i.'\', \'type=M\');return false;" class="tddaybox a_cursor">';
echo '<table style="width:100%;">';
echo "<tr class='rowColor'><td $class>$i</td></tr>";
echo "<tr><td> </td></tr>";
echo "</table>";
echo "</td>";
}
}
for($l=0;$l<7-$daysInrow;$l++)
{
echo "<td class='tddaybox'> </td>";
}
?>
</tr>
</table>
<?php
/*
if(isset($divRed))
{
$divRed = implode(",",$divRed);
echo "<div id='divRed1' style='display:none'>$divRed</div>";
}
else
{
echo "<div id='divRed1' style='display:none'>0</div>";
}
if(isset($divBlue))
{
$divBlue= implode(",",$divBlue);
echo "<div id='divBlue1' style='display:none'>$divBlue</div>";
}
else
{
echo "<div id='divBlue1' style='display:none'>0</div>";
}
if(isset($divMainEvent))
{
$divMainEvent= implode(",",$divMainEvent);
echo "<div id='divMainEvent1' style='display:none'>$divMainEvent</div>";
}
else
{
echo "<div id='divMainEvent1' style='display:none'>0</div>";
}
*/
?>
<div id='gig1' style='display:none'>0</div>
<div id='todo1' style='display:none'>0</div>
<div id='completed1' style='display:none'>1</div>
<input type="hidden" id='lastselectedday' value='' />
So the output for all of the above is correct now, but when I try and do this:
<form>
<input type="button" value="Display Message" onclick="getData('/Dev2/includes/view-monthly-calendar-ajax.php', 'targetDiv')">
</form>
<table class="tablebodycontainer">
<tr>
<td id="MainCalendarContainer">
<div class="ajaxswap" id="targetDiv">
</div>
The output of the ajax call doesn't execute the php.
First off, you shouldn't pull <script> elements via Ajax, they are not going to be interpreted and if they are, they are going to break everything on the second request.
Do some debugging by calling /Dev2/includes/view-monthly-calendar-ajax.php in your browser first. Check out the source code of what you are getting. PHP runs before the document is delivered, so there is no way to pull the HTML, but not the contents generated by PHP. They are one entity.
You either have an error in your PHP code that leads to the wrong output, or your PHP code is not being interpreted at all, which is easy to spot because the original PHP source code will be in the document's source code (which must never, ever happen).
The PHP code is processed by the server; the browser will never see it unless the web server glitches.
PHP is preprocessed before the data is sent to the client. The server parses and executes the php code and only sends what is output by the code. If you want to get the full file you would need to have it not processed by php, for instance by renaming the file extension to something else.
You should implement another php page such as...
getData('getPHP.php?page=/Dev2/includes/view-monthly-calendar-ajax.php', 'targetdic');
Then in that page you need to implement a method to load the php file as a string and return it. at the moment the php is being interpreted as you would expect.

Categories