Jump to content

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

}

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.