-1

Alright, so I have created a Portfolio Page for a Model containing several images, but I want to add a bunch more images using a specific HTML script. As a programmer, I try to eliminate redundancy, but I don't know anything about JavaScript or PHP.

What I want:

  • Iterate over each image file in a directory (./images)
  • Loop a specific block of HTML, plugging in the path to image.

Bonus

  • Execute code when Button is pressed, displaying more images within current window (i.e "Portfolio" button -> Extend.php).

My Site: ChrisNguyen.ml/Portfolio

Example Code (Other example in raw HTML in a above link):

<?php
$dirname = "images_all";
$images = glob($dirname."*.png");

foreach($images as $image) {
    echo '<article class="4u 12u$(xsmall) work-item">
        <a href="'.$image'" class="image fit thumb"><img src="'.$image'" alt="" /></a>
        <h3> </h3>
    </article> ';
}
?>

HTML Block that needs to be repeated:

    <article class="4u 12u$(xsmall) work-item">
        <a href="images/fulls/01.jpg" class="image fit thumb"><img src="images/fulls/01.jpg" alt="" /></a>
        <h3> </h3>
    </article>

Other Info: LAMP Server running on AWS EC2 (So I can use PHP, etc)

In case you didn't get it:

  1. How do I display multiple images with HTML using a "For loop"?
  2. How to display more images with Button.

Update: It does support PHP because of the SMTP form at the bottom of the page.

Update 2: I am implementing all your guy's suggestions (thanks!), and modifying the HTML page and other files, and testing it with the live server/site. I do see changes in the page, but not the results desired (i.e the page displays the variable name rather than the value "{$dirArray[$index]}")

Chris Nguyen
  • 150
  • 2
  • 14
  • I don't see a question here. Anyone? – u_mulder Nov 14 '16 at 20:48
  • Obviously your server does not support php execution. – u_mulder Nov 14 '16 at 20:50
  • What exactly do you need? Does your site not already loop through those images? – Luke Joshua Park Nov 14 '16 at 20:51
  • @u_mulder The questions are implied within the context. But I'll clarify that at the bottom for you ;) ...and it does support PHP, did you not look at the actual website? Or the fact it is a LAMP server? xD – Chris Nguyen Nov 14 '16 at 20:54
  • Why then I see php code in your source? – u_mulder Nov 14 '16 at 20:55
  • @LukePark I need a function (PHP or JavaScript) to display multiple images (so that I dont have to keep adding 4 lines of HTML code to display one of the dozens of images. – Chris Nguyen Nov 14 '16 at 20:55
  • @u_mulder That code doesn't work. It is an "example" code of what I want it to look like. – Chris Nguyen Nov 14 '16 at 20:55
  • @ChrisNguyen parts like `href="'.$image'"` should be `href="' . $image . '"` [String Operators](http://php.net/manual/en/language.operators.string.php) – Mihailo Nov 14 '16 at 20:56
  • So, you have a `foreach` loop. Have `echo` that echoes html. What is the problem? I don't understand – u_mulder Nov 14 '16 at 20:57
  • Sooo... The code you posted above doesn't work or..? What is the relevance of the PHP code above? – Luke Joshua Park Nov 14 '16 at 20:57
  • @u_mulder The code doesn't work. It is an example code of what I want! – Chris Nguyen Nov 14 '16 at 20:58
  • It doesn't work __HOW__? Your pc explodes? You have blank page? – u_mulder Nov 14 '16 at 20:59
  • @u_mulder Look at the site (under all the images). It just echos "' $dirArray[$index] '' ); } } ?>" – Chris Nguyen Nov 14 '16 at 21:02
  • So if in your page you see php code instead of results of it's execution - it means that your php code is __NOT EXECUTED__. How do you store it? In a `.html` file? – u_mulder Nov 14 '16 at 21:10
  • @u_mulder go to ChrisNguyen.ml/Portfolio and hit (Ctrl U). But I tried both. I left one php code inside the HTML block, and the other is a separate PHP file that is executed when the user hits the Button near the bottom. Neither of which seem to work. I think it is the string concatenation mentioned by Mihailo. All though I tried his idea, to no avail. – Chris Nguyen Nov 14 '16 at 21:33
  • @Mihailo Alright, so I tried like a dozen variations and permutations of the string concatenation listed in that link, and modified the PHP block with the HTML and the separate file, to no avail. I tried printing (echo) one of the variables, and it just displays the variable name ("{$dirArray[$index]}") . Thoughts? – Chris Nguyen Nov 14 '16 at 21:35
  • @ChrisNguyen Maybe you didn't start the LAMP server?! I don't use LAMP, so I may be wrong... – Dan Costinel Nov 14 '16 at 21:36
  • @DanCostinel I added another update above. But yeah, the server, instance, etc are all live, so my edits are immediately implemented into the site, to no avail. though. :( – Chris Nguyen Nov 14 '16 at 21:41

3 Answers3

1

if i understood your question correct (it is only an easy and short code example hope it will help you):

First Question: 1.) How do I display multiple images with HTML using a "For loop"?

in this code example, all images from the folder "images" will shown on the website.

<?php
$path = "";

$allImages= scandir($path);

foreach ($allImages as $image) { 

        $imageInf= pathinfo($path ."/".$image); 

            echo "
            <article class='4u 12u$(xsmall) work-item'>
            <a href='#' class='image fit thumb'><img src='".$imageInf['dirname']."/".$imageInf['basename']."' alt='' /></a>
            <h3> </h3>
            </article> 
            ";
}
    ?>

Second Question: 2.) How to display more images with a Button

Only with PHP: this script will show you one more img on your website (i dont know how many images you need or which specific img you need)

<?php
if(isset($_POST['NAMEOFBUTTON']))
{

                echo "
                <article class='4u 12u$(xsmall) work-item'>
                <a href='#' class='image fit thumb'><img src=' (IMAGE PATH WITH IMAGE NAME) ' alt='' /></a>
                <h3> </h3>
                </article> 
                ";
}

?>

OR

<?php
if(isset($_POST['NAMEOFBUTTON']))
{
      for ($i = 1; $i <= 10; $i++) {
      $var_name = "IMAGENAME".$i;
                    echo "
                    <article class='4u 12u$(xsmall) work-item'>
                    <a href='#' class='image fit thumb'><img src=' (IMAGE PATH WITH $var_name) ' alt='' /></a>
                    <h3> </h3>
                    </article> 
                    ";
       }
}

?>

OR

if(isset($_POST['NAMEOFBUTTON']))
{
    $path = "";

    $allImages= scandir($path);

    foreach ($allImages as $image) { 

            $imageInf= pathinfo($path ."/".$image); 

                echo "
                <article class='4u 12u$(xsmall) work-item'>
                <a href='#' class='image fit thumb'><img src='".$imageInf['dirname']."/".$imageInf['basename']."' alt='' /></a>
                <h3> </h3>
                </article> 
                ";
}
}



?>
<form method='post'>
<input type='submit' name='NAMEOFBUTTON' />
</form>

OR WITH JQuery;

HTML:

<input type='button' onclick='moreimg();' />
<div id='Show_Images'></div>

JQUERY:

<script>
function moreimg(){
   $('#Show_Images').load('IMG.php');
}
</script>

PHP: You can use in the file "IMG.php" the code from the First Question

Neptulon
  • 41
  • 6
  • I tried your first code block and got an Error. ("PHP Parse error: syntax error, unexpected ''" class="image fit thumb"> – Chris Nguyen Nov 14 '16 at 21:49
  • For the second part, I have a bunch of images, like a dozen or so. So a for loop would be ideal so that I don't have to keep manually entering the names each time. – Chris Nguyen Nov 14 '16 at 21:50
  • @ChrisNguyen i updated the code .. the error should be gone – Neptulon Nov 14 '16 at 22:00
  • @chrisNguyen if the files are in a folder you can use the code from the first question. if you want a for loop, i will edit the second question – Neptulon Nov 14 '16 at 22:02
  • Hey, so I tried your code again, added a closing brace, changed the directory path ("$path"), and still no luck. I don't think it's pointing to the right location. It gives me the default null img icon... Maybe it doesn't display the image correctly? – Chris Nguyen Nov 14 '16 at 22:10
  • @ChrisNguyen sorry... i copied the wrong variable .. i update the code sorry for the misstake – Neptulon Nov 14 '16 at 22:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128101/discussion-between-neptulon-and-chris-nguyen). – Neptulon Nov 14 '16 at 22:34
  • Alright, so I found the problem. I posted my solution below... Thanks for all the help! :) – Chris Nguyen Nov 17 '16 at 03:21
0

So the answer above is correct. However, it did not work/execute in the server currently used (Apache) because the server didn't recognize PHP within the HTML itself. That is why it only worked as a separate file. [ How do I add PHP code/file to HTML(.html) files? ]

What I needed to do:

  1. Find the directory where the "httpd.conf" was located. On my AWS Apache server, it was located at "/etc/httpd/conf"
  2. Stop your server temporaily ("sudo service httpd stop")
  3. Edit the file ("sudo nano httpd.conf")
  4. Append "AddType application/x-httpd-php .htm .html" to tell Apache/the-server how to properly execute the embedded PHP (I put it in the bottom)
  5. Also follow instructions here to ensure the httpd.conf file will accept/execute the above line/code [ Use .htaccess file on an apache localhost server ]
  6. Start your server ("sudo service httpd stop")
  7. ...and if your PHP is correct and embedded properly, your page will now execute the PHP as normal!

Check my site (ChrisNguyen.ml/Portfolio) for the finished result/code.

Community
  • 1
  • 1
Chris Nguyen
  • 150
  • 2
  • 14
0

Now remove the dollar sign from the as $image and use as in the below code. It might help you.

<?php
$dirname = "images_all";
$images = glob($dir`enter code here`name."*.png");

foreach($images as image) {
    echo '<article class="4u 12u$(xsmall) work-item">
        <a href="'.$image'" class="image fit thumb"><img src="'.$image'" alt="" /></a>
        <h3> </h3>
    </article> ';
}
?>
Ru Chern Chong
  • 3,256
  • 13
  • 30
  • 42