array into 2 columns using php

Posted on 2012-08-24
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>';
    				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

Question by:Easyrider43
    LVL 16

    Accepted Solution

    You have not specified a condition in the FOR loop!

    Example below using a WHILE loop:

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

    Open in new window

    LVL 82

    Expert Comment

    by:Dave Baldwin
    @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.
    LVL 25

    Expert Comment

    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

    LVL 25

    Expert Comment

    Sorry I forgot that you want two tile the output ... forget my comment.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now