?
Solved

array into 2 columns using php

Posted on 2012-08-24
4
Medium Priority
?
486 Views
Last Modified: 2012-08-25
So if I have an array with the values
array('id' =  0 'title' = 'work',
         'id' = 1  'title' = 'service',
         'id' = 2  'title' = 'help',
         'id' = 3  'title' = 'No Help',
         'id' = 4  'title' = 'Im out of ideas')

and I want to display this into 2 columns my code does this but quits after the first row any help????

work                          service
help                          no help
im out of ideas

$edu = $education->fetch($statement);
		echo '<div class="outter_edu" style="border: 1px solid black;width: 500px">';
		for ($j = 0; isset($edu[$j]) && is_array($edu[$j]); $j++)
			{
			if ($j / 2)
				{
    				echo '<div class="left_col" style="float: right; border: 1px solid                               black">row 1 ' . $edu[$j]['title'] . '</div>';
					
				}
			else
  		 		{
    				echo '<div class="right_col" style="float: right; border: 1px solid black">row 2 ' . $edu[$j]['title'] . '</div>';
					
  				}
			}
		echo '</div><br>';

Open in new window

0
Comment
Question by:Easyrider43
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
grahamnonweiler earned 2000 total points
ID: 38331932
You have not specified a condition in the FOR loop!

Example below using a WHILE loop:

$edu=$education->fetch($statement); 
echo('<div class="outter_edu" style="border: 1px solid black;width: 500px">');
$j=0;
while($j<count($edu)) {
	if($j%2) { 
		$style="left_col"; 
	} else {  
		$style="right_col"; 
	}
	echo('<div class="'.$style.'" style="float: right; border: 1px solid black">Row '.$j.' '. $edu[$j]['title'] . '</div>'); 
	$j++;
} 
echo('</div><br>');

Open in new window

0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38331964
@grahamnonweiler is right, this isset($edu[$j]) && is_array($edu[$j]) only returns a boolean so there isn't an appropriate value to compare $j against.
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38331973
It looks like you array is possibly two dimensional ... and a foreach loop may be best. e.g.
$edu = array(array('id' => 0, 'title' => 'work'),
             array('id' => 1, 'title' => 'service'),
             array('id' => 2, 'title' => 'help'),
             array('id' => 3, 'title' => 'No Help'),
             array('id' => 4, 'title' => 'Im out of ideas'));
foreach ( $edu as $sub ) {
    echo $sub['id']," -- ",$sub['title'],PHP_EOL;
}

Open in new window

0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38331987
Sorry I forgot that you want two tile the output ... forget my comment.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question