Jump to content

whats wrong with this loop?


unistake

Recommended Posts

hi all,

I have possibly up to 9 images that I want to find and show on a webpage.

 

I am trying to put a $file in a loop and get all the .jpg images that are found in to an array and show them all.

 

I think from the script you can see what I am attempting to do!

 

Any pointers would be grateful for

 

<?php
while($i=0 $i < 9 $i++) {
$file = "aircraft/".$_GET['reg'].$i.".jpg";
}
foreach($file) {
	echo "<li class=\"active\"><img src=\"./aircraft/".$reg.$i.".jpg\" alt=\"Flowing Rock\" style=\"width: auto; height: 70px; margin-left: -28.5px; display: block;\" class=\"thumb\" ><br />";
}
?>

Link to comment
Share on other sites

First off you are making $file a variable when I think you want to make it an array.  You would need something like:

 

while($i=0 $i < 9 $i++)
    $file[] = "aircraft/".$_GET['reg'].$i.".jpg";

 

Then you have a foreach loop.  Why not do this inside the while loop?  Like this maybe:

 

while($i=0 $i < 9 $i++)
    echo '<li class="active"><img src="/aircraft/'.$_GET['reg'].$i.'.jpg" alt="Flowing Rock" style="width: auto; height: 70px; margin-left: -28.5px; display: block;" class="thumb" ></li>';

Link to comment
Share on other sites

Like this? Something is still wrong?

 

<?php
while($i=0 $i < 9 $i++) {
	$file[] = "aircraft/".$_GET['reg'].$i.".jpg";
	echo "<li class=\"active\"><img src=\"./aircraft/$file.jpg\" alt=\"Flowing Rock\" style=\"width: auto; height: 70px; margin-left: -28.5px; display: block;\" class=\"thumb\" ><br />";
}
?>

Link to comment
Share on other sites

Each time you set $file you were overwriting the previous $file, an array would be best. However a foreach() is definitely the preferred way to loop through it, not a while(). Although I'm wondering if you need two loops? Do you loop through the second time later in the code, but just posted them together on here? If not, then why not just echo out from the first loop..?

 

while($i=0 $i < 9 $i++) {
    echo '<li class="active"><img src="./aircraft/' . $_GET['reg'] . $i . 'jpg" alt="Flowing Rock" style="width: auto; height: 70px; margin-left: -28.5px; display: block;" class="thumb" /></li>';
}

 

Also if you're asking for tips, using single quotes to echo out HTML is much easier to read and write. Plus would it not be better to place the styles in a CSS class?

 

Edit:

 

Missed ninedoors' comment about using 2 loops before.

Link to comment
Share on other sites

I have put everything inside the one loop so there is no reason to collect them in the $file variable.

 

You are also gettign the wrong image reference.  Your code will give you <img src="./aircraft/aircraft/'$_GET['reg'].$i.'.jpg.jpg" />

 

Plus you don't close your li tag, you have a br tag instead.

Link to comment
Share on other sites

Oh, I copied the loop from a previous post. It's missing the semi-colons between the expressions:

 

<?php 
while($i=0; $i < 9; $i++) {
    echo '<li class="active"><img src="./aircraft/' . $_GET['reg'] . $i . '.jpg" alt="test"><br />';
}
?>

 

As PFMaBiSmAd said though, this assumes the values are there. Although I'm not sure if glob is what you're after? You seem to be passing the images via a GET parameter..

Link to comment
Share on other sites

The value $_GET['reg'] is taken from www.websiteexample.com/index.php?reg=abcd

 

The images are stored in a directory off where the scripts are called 'aircraft'

 

In the script I am trying to show all the images (if there are any) that are stored called abcd1.jpg, abcd2.jpg etc up to abcd9.jpg

 

If anyone has a better way to do it please tell me! I thought a foreach would be best.???

Link to comment
Share on other sites

Then PFMaBiSmAd was right. Use glob to loop through the files:

 

foreach (glob('./aircraft/' . $_GET['reg'] . '*.jpg') as $file)
{
    echo '<li class="active"><img src="' . $file . '" alt="test" /></li>';
}

 

Although I'd remove any slashes within the 'reg' parameter, to prevent the user viewing other directories (although they're limited to .jpg files only) on your file system.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.