Uzm Posted August 30, 2008 Share Posted August 30, 2008 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. Is there any solution for such task? Quote Link to comment https://forums.phpfreaks.com/topic/122026-need-help-with-another-way-of-looping-arrays/ Share on other sites More sharing options...
Barand Posted August 30, 2008 Share Posted August 30, 2008 Give us clue about what's unique about your system of looping. Do you have an array to start with? Quote Link to comment https://forums.phpfreaks.com/topic/122026-need-help-with-another-way-of-looping-arrays/#findComment-629906 Share on other sites More sharing options...
Uzm Posted August 31, 2008 Author Share Posted August 31, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/122026-need-help-with-another-way-of-looping-arrays/#findComment-630363 Share on other sites More sharing options...
wildteen88 Posted August 31, 2008 Share Posted August 31, 2008 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/122026-need-help-with-another-way-of-looping-arrays/#findComment-630370 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.