Jump to content

Need help with another way of looping arrays


Uzm

Recommended Posts

I'm creating a small project, but i can't figure out how to use array looping properly.

I'm making multiple DIVs that will show title, link, and an image. Let's say there are 20 DIVs.

In each DIV, below the image, there's text. Each DIV has different text.

I've tried in different ways, but i can't get anywhere. Google didn't help much, since it's quite "unique" system of looping. :P

Is there any solution for such task?

All right, i'll try to explain a bit more.

It's not really unique, but i haven't seen any questions related to similar problem yet, so that's why i think it's unique.

Array:

 

$div = array("Title" => "1", "Link" => "http://blablabla.com", "Image" = "image.jpg");
$divText = array("1" => "text 1", "2" => "text 2);

 

Then, the HTML:

 

<div>
<span class="title"><!-- title from $div goes here --></span><br>
<a href="<!-- link from $div goes here -->" target="_blank">link</a>
<img src="<!-- image from $div goes here -->" alt="Death Race"><br><br>
<span class="text"><!-- text from $divText goes here --></span>
</div>

 

It seems pretty simple, but i can't get it to loop. Let's say that i have 20 arrays, that need to display $div and $divText parameters.

Could you explain how those two arrays ($div and $divText) are created. Or are they hard-coded?

 

You should be able to accomplish this by a standard foreach loop:

$div[] = array("Title" => "1", "Link" => "http://blablabla.com", "Image" => "image.jpg");
$div[] = array("Title" => "2", "Link" => "http://foobar.com", "Image" => "image2.jpg");

$divText = array("1" => "text 1", "2" => "text 2");

foreach($div as $key => $arr)
{
    echo '<div>
<span class="title">'.$arr['Title'].'</span><br>
<a href="'.$arr['Link'].'" target="_blank">link</a>
<img src="'.$arr['Image'].'" alt="Death Race"><br><br>
<span class="text">'.$divText[($key+1)].'</span>
</div>';

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.