Handling URLs correctly with PHP - 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"]).'">';
}

Related

How can I run a single PHP function that gives diffirent results depending on the button value?

First off I do apologize if this has been asked before or something similar. I'm working on this little project for at home and to help me learn a bit on PHP.
What I am trying to achieve here is to have buttons/links that are dynamically generated by reading a directory from one function. This I have accomplished which works as intended for that aspect. Once those are generated and displayed to the user with the webpage. I want to use only one function for all buttons that depending on the button value/name it is passed to the function displays the corresponding directory in HTML.
How can I pass a value from the dynamic buttons to a variable in a different function?
Code I have so far:
This first function works as intended(it echo's out the specific directory):
function btnSeries(){
$season = "./SomeDirectory/";
$files = glob("./Some/Directory_images/*.*");
for ($i=0, $f=1; $i<count($files) && $f<count($files); $i++, $f++)
{
$num = $files[$i];
print '<div class="col-md-4">';
print '<div class="card">';
print '<img class="card-img-top" src="'.$num.'"alt="Season '.$f.'">';
print '<div class="card-body">';
//print '<br>';
print '<form method="post" action="episodes.php">';
print '<input type="submit" class="btn btn-primary" value="Season'.$f.'" name="button" />';
print '</form>';
//print 'Season '.$f.'';
print '</div>';
print '</div>';
print '</div>';
}
}
The single function that receives the value for Variable from the onclick event
if(array_key_exists('button', $_POST)) {
seasonEpisodes();
}
function seasonEpisodes(){
$buttonValue = "value" //Value passed from button (ie: button name which represent the folder of series)
$season = "./SomeDirectory/";
$files = glob("./Series/.$buttonValue/*.*");
include 'layout/header.php';
print '<td>';
print '<p align="center">';
print '<img border="0" src='.$someVar.' width="370" height="529">';
print '</p>';
print '</td>';
for ($i=0, $f=1; $i<count($files) && $f<count($files); $i++, $f++)
{
$num = $files[$i];
print '<p align="center"><font color="#FFFFFF" size="6">';
print '<a href=".$season.$file.">';
print '<font color="#FFFFFF">.$file.</font></a>'
}
print '</td>'
include 'layout/footer.php';
}
Thought I would Provide an updated Function with the variable being able to be passed on to the second function thank to RGriffiths. Adding this worked like a charm.
function btnSeries(){
$season = "./SomeDirectory/";
$files = glob("./Some/Directory_images/*.*");
for ($i=0, $f=1; $i<count($files) && $f<count($files); $i++, $f++)
{
$num = $files[$i];
print '<div class="col-md-4">';
print '<div class="card">';
print '<img class="card-img-top" src="'.$num.'"alt="Season '.$f.'">';
print '<div class="card-body">';
//print '<br>';
print '<form method="post" action="episodes.php">';
print '<input type="hidden" value="Season'.$f.'" name="seasonClicked" />';
print '<input type="submit" class="btn btn-primary" value="Season'.$f.'"/>';
print '</form>';
//print 'Season '.$f.'';
print '</div>';
print '</div>';
print '</div>';
}
}
if(array_key_exists('button', $_POST)) {
seasonEpisodes();
$buttonValue = $_POST["seasonClicked"]; //Value passed from button
$season = "./SomeDirectory/";
$files = glob("./Series/$buttonValue/*.*");
include 'layout/header.php';
print '<td>';
print '<p align="center">';
print '<img border="0" src='.$someVar.' width="370" height="529">';
print '</p>';
print '</td>';
for ($i=0, $f=1; $i<count($files) && $f<count($files); $i++, $f++)
{
$num = $files[$i];
print '<p align="center"><font color="#FFFFFF" size="6">';
print '<a href=".$season.$file.">';
print '<font color="#FFFFFF">.$file.</font></a>'
}
print '</td>'
include 'layout/footer.php';
}
Hide the input of the season value for each form:
print '<form method="post" action="episodes.php">';
print '<input type="hidden" value="Season'.$f.'" name="seasonClicked" />';
print '<input type="submit" class="btn btn-primary" value="Season'.$f.'"/>';
print '</form>';

PHP issues displaying image gallery

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++;
...

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';
?>

how to show items in a row wise (i.e 5 items per rows)

i have a to show tags onto my view page from controller. I would like to show 5 tags per row ,currently with my below code its showing 1 tag per row. please help me how to do this?
$query = $this->tagsmodel->fetch_all_tags($postnumbers, $offset);
if (is_array($query))
{
foreach ($query as $row)
{
$content = substr(strip_tags($row->tags_name), 0, 180);
echo '<div class="parent" id='.$row->tags_id.'>';
echo '<a class="tagsbtn" href="" >'.$content.'</a>';
echo '</div>';
}
}
Currently i am getting output like this
$count=0;
$query = $this->tagsmodel->fetch_all_tags($postnumbers, $offset);
if (is_array($query))
{
foreach ($query as $row)
{
$count++;
$content = substr(strip_tags($row->tags_name), 0, 180);
echo '<div class="parent" style='float:left;' id='.$row->tags_id.'>';
echo '<a class="tagsbtn" href="" >'.$content.'</a>';
echo '</div>';
if($count%5==0) {
echo "<div style='clear:both'></div>";
}
}
}
so now it will display 5 divs per row
You can do with using css
.main {
width : 400px;
}
.main .parent{
float : left;
margin-right : 10px;
}
Adjust the width as per you will get 5 tags in row.
echo "<div class='main'>";
foreach ($query as $row)
{
$content = substr(strip_tags($row->tags_name), 0, 180);
echo '<div class="parent" id='.$row->tags_id.'>';
echo '<a class="tagsbtn" href="" >'.$content.'</a>';
echo '</div>';
}
echo '</div>';
Try to set floating for your div style="float: left;":
foreach ($query as $row)
{
$content = substr(strip_tags($row->tags_name), 0, 180);
echo '<div class="parent" id='.$row->tags_id.' style="float: left;">';
echo '<a class="tagsbtn" href="" >'.$content.'</a>';
echo '</div>';
}
You can use Kasyx solution but you won't be able to control the number of tags displayed on each row. By default, it will fill your div and break to a new line each time the width of displayed tags will be to large.
Use a counter in the loop to control the exact number of tags displayed. You may need to set a fixed width for your div and/or play with the overflow.
Your DOM should looks like
<div id="main">
<div class="rowdiv">
<div class="parent"><a ... >foo</a></div>
<div class="parent"><a ... >bar</a></div>
...
</div>
and the css properties :
.parent { float : left, ...} .rowdiv{ clear : both, ...}
The only thing you have to do is to use modulo in your loop and create a new "rowdiv" each 5 iteration.
PHP
echo '<ul>';
foreach ($query as $row)
{
$content = substr(strip_tags($row->tags_name), 0, 180);
echo '<li id="'.$row->tags_id.'">';
echo '<a class="tagsbtn" href="#">'.$content.'</a>';
echo '</li>';
}
echo '</ul>';
CSS
ul {style-list-type:none}
ul li {display:block;float:left
ul li:nth-child(5n+1) {clear:left}
Update
This will allow your tag to have dynamic width
New Working Example
jsFiddle

How to place bullet point for each element in a list

echo '<td width="11%" class="imagetd">';
if (empty($arrImageFile[$key])) {
echo ' ';
} else {
echo '<ul class="qandaul"><li>';
echo htmlspecialchars(is_array($arrImageFile[$key]) ? implode("\n", $arrImageFile[$key]) : $arrImageFile[$key]);
echo '</li></ul>';
}
echo '</td>';
In code above, I have a lit of image names, now each image name is seperated by line break, but the problem I am having is that it does not display a bullet point for each image file name inserted in the line break. It is only displaying one bullet point for the first image name and that is it. In example above how do I insert a bullet point for each image name?
UPDATE
Does CSS take effect below:
ul, ul li { margin: 0; padding: 0; }
ul { margin-left: 1.3em; }
Below refers to Juan's answer which I want to make sure is correct as it did not work:
echo '<td width="11%" class="imagetd">';
if (is_array($arrImageFile[$key]) {
foreach($arrImageFile[$key] as $img) {
if (empty($arrImageFile[$key])) {
echo ' ';
} else {
echo '<ul class="qandaul"><li>';
echo '<li>' . htmlspecialchars($img) . '</li>'
}
}
echo '</li></ul>';
}
echo '</td>';
You are putting all your image files inside the <li>. Create a separate <li> for each image
echo '<ul class="qandaul>';
if (empty($arrImageFile[$key])) {
echo '<li> </li>';
} else {
if (is_array($arrImageFile[$key]) {
foreach($arrImageFile[$key] as $img) {
echo '<li>' . htmlspecialchars($img) . '</li>';
}
} else {
echo '<li>' . htmlspecialchars($arrImageFile[$key]) . '</li>';
}
}
echo '</ul>';
or you can use the array implode trick others have suggested. I tend to think that is less readable.
echo implode("</li>\n<li>", array_map(function($item){
return htmlspecialchars($item);
}, $arrImageFile[$key]));
Instead of implode("\n", $arrImageFile[$key]),
use implode("</li>\n<li>", $arrImageFile[$key]),
the point is you need to create one li for one image.
echo '<td width="11%" class="imagetd">';
if (empty($arrImageFile[$key])) {
echo ' ';
} else {
echo '<ul class="qandaul"><li>';
echo htmlspecialchars(is_array($arrImageFile[$key]) ? implode("</li>\n<li>", $arrImageFile[$key]) : $arrImageFile[$key]);
echo '</li></ul>';
}
echo '</td>';
It's because everything is wrapped in one <li> tag. echo '<ul class="qandaul"><li>';
Put each in it's own <li>
A pointed list markup in html is:
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
So you'll have to implode with "</li><li>" or use a for loop (or foreach).
The bullet points are dependent on your CSS style, please check what the CSS style says for ul and the class qandaul.

Categories