I'm completely new to creating dynamic pages - so for the moment, I am using txt files to manage my simple data! I'll explain my set up first. I have one php page - it gets data from two .txt files:
brief.txt: written content (title and supporting text)
photos.txt: image file names.
I am using php pagination, and this changes the content in the php page by referring to the numbers I have used in the .txt file, here is the code I am using:
<?php
$data=file("brief.txt");
$pages=0;
foreach($data as $temp){
$x=explode("|",$temp);
if($x[0]>0){
$pages=$pages+1;
}
}
if($_GET['p']){
$page=$_GET['p'];
}
if($_GET['i']){
$index=$_GET['i'];
}
if($index == "p"){
$page=$page-1;
}
if($index == "n"){
$page=$page+1;
}
if($page < 1){
$page=1;
}
if($page > $pages){
$page=$pages;
}
$line=$data[$page-1];
$fields=explode("|",$line);
?>
and this is an example of my text file
brief.txt:
1|TITLE 1|Content goes here
2|TITLE 2|Content goes here
photos.txt:
1|imageone1.jpg
2|imagetwo1.jpg
Hopefully that all makes sense! Now, my problem is, I have a few video files. At the moment, the images load in a jquery slideshow. I would like to somehow have the script change, for example, when it makes page 3 with a video file it doesn't create the slideshow, but instead creates a video player and can load the url of my video that is placed in the txt file. The code I am using at the moment for the jquery slideshow is this:
<?php
echo"
<div id='portfolioslider'><div class='slider'>";
$photos=file("photos.txt");
foreach($photos as $image){
$item=explode("|",$image);
if($item[0]==$fields[0]){
$photo=trim($item[1]);
echo"<div><img src='images/work/$photo' alt='' /></div>\n";
}
}
echo"</div></div>"?>
I would like the above code, to change similar to this when you go to a page with a video:
<iframe src="http://player.vimeo.com/video/23024550?title=0&byline=0&portrait=0" width="1024" height="576" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>
And if you go back or forward to a page which doesn't have a video, I would like it to change back to the original jquery slideshow...
Please can someone help me out with the necessary code I would need to make this work! I am very much a beginner coder :(
Edit: Assuming I understand your logic's structure, and it's possible I missed something:
Perhaps, using a videos.txt to store videos, as opposed to putting both inside of photos.txt? Also, look into using a MySQL database for this.
And if you absolutely must use this manner of coding, I'd create a function that accepts a file name, and depending on the extension determines if it is a video or an image, thus dictating what content to load, and how.
Or, you can add an extra flag to your photos.txt file:
1|blah|1
2|blahblah|0
Where the third '1' means video, and a '0' means photo? It's your metadata, so you can dictate its structure.
Basically what you need to do is just check to see if the current file is a picture or a video. The easiest way would be to look at the extension. Your videos are also stored in photos.txt, right? try something like this:
<?php
$photos=file("photos.txt");
// since you already know the page number, you can just skip to that index instead of looping through the entire file
$item = explode("|",$photos[$page-1]);
$photo = trim($item[1]);
//lets see if it's a photo or a video
if(substr($photo,-3) == 'jpg') {
//echo out code for images here
} else {
//echo out code for video here
}
?>
Related
I have a few php files, lets call them planet1.php, planet2.php, planet3.php and satellite.php.
Each "planet" has a link to this unique "satellite.php" file this is because the information there is all the same no matter the "planet" where its being redirected. Now as I say all infomartion is the same but an image -which is inside a DIV as a background-image-.
Here's my question
How can I click the link in planet1.php and open satellite.php with visible or click on planet2.php and open satellite.php with ?
I have three differents "planets" and I need to display only one acoording where it's being redirected.
Thanks!
This is how I solved it (with the help of Lev Buchel)
Inside -> satellite.php
if (<? echo $_GET["planet"] ?> == 1){
document.getElementById("planet").classList.add("planet1");
}else if (<? echo $_GET["planet"] ?> == 2) {
document.getElementById("planet").classList.add("planet2");
}else{
document.getElementById("planet").classList.add("planet3");
}
Inside every planetX.php file
Styles...
<style>
.planet1{background-image: url('img1.JPG');}
.planet2{background-image: url('img2.JPG');}
.planet3{background-image: url('img3.JPG');}
</style>
Actual div with the image...
If i understood you correctly, you want the same "satellite" page to display a different image according to which "planet" file was clicked to reach it.
What you can do is to use a "GET" method, for example in your planet1.php file the href will look like this :
href="satellite.php/?planet=1"
Same goes for planet2 (planet=2) and planet3 (planet=3).
In your satellite.php page you can check which planet the user came from using
if ($_GET["planet"] == 1)
document.getElementById("yourDiv").classList.add("planet1");
Now you can set a different background for class planet1/2/3.
I have PHP code that generates HTML code which makes a grid of images. The images are taken from links that are generated for each new image, I also add 133x100 at the end of the image link to resize it on the page. My problem is that a seemingly random selection of images won't display, and I just get a broken image symbol. For example:
This is a link to an image that is generated on my page and is displayed.
This is a link to an image that will not be displayed.
I am only allowed to post 2 links, but removing the %20.%20/133x100 from the end of the last link will show what the picture should be.
Here is the part of the code for the image source:
function display_images(){
//This cycles through each image and displays it as HTML
while($row = $item->fetch()){
Echo "`<img src= '$link[Image_Link] . /133x100' />`"
}
}
It is then called here in a class which puts the images in a grid:
<ul class="rig columns-4">
<?php
display_images();
?>
</ul>
Seemingly about every 2/20 images won't work, and seeing all the links are in the same format, I don't understand why they won't work, and it just seems random.
EDIT: I have noticed that the links that work have 62fx62f at the end of them before the added %20.%20/133x100. If I add it to the raw link in the right place, it makes the image work. But using that generated link, the image still won't load on the page. So using a link with a working image will not work on the page. (This is the same with the raw link without %20.%20/133x100, that links to an image but also won't work on the website)
When visiting the links, the urls look like this:
http://www.example.com/image/randomcharacters%20.%20/133x100
The links work without the %20.%20 at the right dimensions, like so:
http://www.example.com/image/randomcharacters/133x100
This leads me to believe that it may work if you try using the following for the image source instead:
<img src= '$link[Image_Link]/133x100' />
The full code would look like this, for the while function:
while($row = $item->fetch()){
echo "<img src= '" . $link['Image_Link'] . "/133x100' />";
}
I am not aware of steamcommunity much but from the looks of it, i think you should try this.
Instead of putting
. /133x100
Use
/133fx100f
So your URLs would be
while($row = $item->fetch()){
echo "<img src= '" . $link['Image_Link'] . "/133fx100f' />";
}
Just did some trial and error and found out. No explanations for this though!!
I wrote a short PHP script to scrape the images from the homepage of http://www.imgur.com. Here is the code:
$imgur = file_get_html('http://www.imgur.com');
foreach($imgur->find('div[class="post"]') as $images)
echo "<span id=\"images\">$images</span>";
The problem is that after scraping the divs with class=post, the pictures link to the gallery for the image, but the "a href=" looks like /gallery/48ZPT instead of http://www.imgur.com/gallery/48ZPT.
So when I click the link where the images are on my website it takes me to mysite.com/gallery/48ZPT but that directory doesn't exist so I get an error.
Is there anyway to add "http://www.imgur.com" to each variable href as it is pulled from the site?
I know this might be a little confusing, but I would appreciate any help.
$imgur = file_get_html('http://www.imgur.com');
foreach($imgur->find('div[class="post"]') as $images)
{
echo "<span id=\"images\">"."http://www.imgur.com"."$images</span>";
}
I am creating an image gallery in fancybox but am trying to automate image uploading.
Fancybox galleries are a collection of <a> tags, but the first <a> tag listed also has an <img> tag to provide a thumbnail to open like so:
<img src="image1thumb.jpg">
Currently I would have to manually add <a> tags for every new image I upload which would be tedious. I would rather add the links to a mySQL database which will then be picked up by a piece of PHP.
In plain English the code would read as follows :
For the last uploaded file, show <a href.......><img src=.......></a> and then for all other images show <a href........></a> only.
I already have the code for the last uploaded file which displays as $lastupload, but am struggling with a foreach loop. So far I have:
foreach ($lastupload) {
echo '<a class="fancybox" rel="'.$r["rel"].'" href="'.$r["imagelink"].'" title="'.$r["comment"].'"><img src="thumb.jpg"></a>';
}
else {
echo '<a class="fancybox" rel="'.$r["rel"].'" href="'.$r["imagelink"].'" title="'.$r["comment"].'"></a>
}
Now, I know that this code is basically useless. I know that I need more in the initial foreach brackets, and that else shouldn't be a part of this loop (it should be foreach/ifelse), but I don't know how to proceed further. All of the examples I've looked introduce counts which I'm not sure fits into what I'm trying to do. Plus, no example I've looked at so far shows how to generate a list of links from a mySQL database.
Any help you can give will go a long way.
Thanks
Have you looked at how the foreach() loop actually works? You don't need a count for this particular loop. Or you could loop until the end of your results (see below).
http://php.net/manual/en/control-structures.foreach.php
As for extracting links from your database, look at mysqli_fetch_assoc(). There are some examples of while() loops to run through your query.
http://php.net/manual/en/mysqli-result.fetch-assoc.php
Look at the above examples in the PHP.net manual, this is a really good source of information.
I'm not sure I fully understand your question. I don't know what $r is in your example, but from what I can tell you want to do something like this:
$i = 0;
foreach ($r as $row) {
if ($i == 0) {
echo '<a class="fancybox" rel="'.$row["rel"].'" href="'.$row["imagelink"].'" title="'.$row["comment"].'"><img src="'.$lastupload.'"></a>';
}
else {
echo '<a class="fancybox" rel="'.$row["rel"].'" href="'.$row["imagelink"].'" title="'.$row["comment"].'"></a>
}
$i++;
}
The answer was not to look for the last uploaded file, just treat the first result of the query differently.
Here is the full code for what I got:
<?php
$foo_query->setFetchMode(PDO::FETCH_ASSOC);
$is_first=false;
while($r=$foo_query->fetch()) {
if($is_first) {
echo '<a...blah blah blah><img src="thumb.jpg"></a>';
} else {
echo '<a...more blah></a>';
}
}
?>
I have some thumbnail images with its larger version.I placed the thumbnail images in a page.Now for link I just gave a link
<img src="thumbnail1.jpg>
but for this I have to make different pages for showing larger one.I want to give a link to show them in a single page.means whenever I will click the thumbnail it will open the larger one in a page with the same url but with its name like
imagegallery.php?news=images/largerimage1/13.jpg
imagegallery.php?news=images/largerimage1/14.jpg
so how to do that?
Pretty basic stuff, I suggest you get to read some PHP tutorials on the internet to get some knowledge on one thing and another.
The ?news= part in your URL is a parameter that can be read by PHP. This type is known as $_GET. To get this part you would need $_GET['news'] so if we'd use your first link and place this inside a script: echo $_GET['news']; the page would say images/largerimages1/13.jpg.
In order to get the image loaded on your website we need some simple steps, I'm changing the news parameter into image, that suits better for your script since it ain't news items:
<?php
// Define the path (used to see if an image exists)
$path = 'your/absolute/path/to/public_html/'; # or wwwroot or www folder
// First check if the parameter is not empty
if($_GET['image'] != "") {
// Then check if the file is valid
if(file_exists($path . $_GET['image'])) {
// If an image exists then display image
echo '<img src="'. $_GET['image'] . '" />;
}
}
?>
Below this script you can put all your thumbnails the way you want. Ofcourse, also for these thumbnails there are some automated options. But I strongly suggest you get a good look at the script above and some beginner PHP tutorials so you completely understand the example given. This still isn't the best method, but it's kicking you in the right direction.
if your imagegallery.php is in root of your domain, you can just add slash as a first char to links like this:
<img src="thumbnail1.jpg>
else you will have to write some php function which it returns BaseUrl of your web. Then it should looks like this:
<img src="thumbnail1.jpg>
maybe you can something like this,
Techincally, there is no thumbnail image, just a stretch version of the regular image
I don't understand which part you don't know how to do:
- the link part?
it should look like
<img src="thumbnail1.jpg>
- or the PHP part (the file called imagegallery.php)?