mike12255 Posted April 12, 2012 Share Posted April 12, 2012 I want to grab a list of all the school programs I have in a database (math english biology chemistry ect ect) and dispaly them in a order so that they go alphabetically downward for 12 rows and then go across so it would look like soo: Academic Advisory Class Schedules Academic Assistance Counselling Center Academic Calendars Course Descriptions and Catalogue Academics Office Department Directory Administration Departments & Programs Adult Learners Fellowships Alumnimni Chapters Finals Schedules Alumni Events Athletics Campus Life At a Glance Campus Recreation Campus Safety & Security I know to get here: I am just unsure what to do in the while array to make it look how I want <?php $sql = "SELECT name,id FROM table_name ORDER BY name ASC"; $res =mysql_query($sql) or die (mysql_error()); while ($row = mysql_fetch_array($res)){ //now what??? } ?> Quote Link to comment Share on other sites More sharing options...
dhmm Posted April 12, 2012 Share Posted April 12, 2012 Hello !!! Sorry about my English... Try this... I'm not tested the code... <?php //changes line in html and html code fuction endl() { return"\n<br/>"; } //show the id and name functin show($id,$name) { echo "ID : $id - NAME : $name".endl(); } $sql = "SELECT name,id FROM table_name ORDER BY name ASC"; $res =mysql_query($sql) or die (mysql_error()); while ($row = mysql_fetch_array($res)){ $name = $row['name']; $id=$row['id']; show($id,$name); } ?> Quote Link to comment Share on other sites More sharing options...
mike12255 Posted April 12, 2012 Author Share Posted April 12, 2012 That would make it go to a new line after each item, but after 12 items have been displayed how can I make it go to the top line and start going down again Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted April 12, 2012 Share Posted April 12, 2012 you would need to do an array count, set up a counter, split off the array in groups of 12, updating the counter, and then display by itterating through each group simultaneously (using the same counter value from a second counter to reffernce the number of columns set by the first counter) to generate the row. Also you will need a check to make sure you stop trying to access each group of 12 when it is empty or you will get undefined offeset errors. I'm in a bit of a rush, so that may not make as much sense as I would like it, but hope it gets you on the right track Quote Link to comment Share on other sites More sharing options...
mike12255 Posted April 12, 2012 Author Share Posted April 12, 2012 I was only able to take a quick look as i am too in a rush but just wanted to say thanks for pointing me in the right direction. And your right it doesnt make much sense right now but when I can sit down tonight take my time to read it and look at the php manual i think i should be able to come up with something, so thanks alot man. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 12, 2012 Share Posted April 12, 2012 Sample code that would do this - <?php // retrieve your data into an array (faked here for demo) $data[] = 'Academic Advisory'; $data[] = 'Academic Assistance'; $data[] = 'Academic Calendars'; $data[] = 'Academics Office'; $data[] = 'Administration'; $data[] = 'Adult Learners'; $data[] = 'Alumni Chapters'; $data[] = 'Alumni Events'; $data[] = 'Athletics'; $data[] = 'Campus Life At a Glance'; $data[] = 'Campus Recreation'; $data[] = 'Campus Safety & Security'; $data[] = 'Class Schedules'; $data[] = 'Counseling Center'; $data[] = 'Course Descriptions and Catalog'; $data[] = 'Department Directory'; $data[] = 'Departments & Programs'; $data[] = 'Fellowships'; $data[] = 'Finals Schedules'; $num_rows = 12; // number of rows down $num_cols = ceil(count($data)/$num_rows); // calculate number of columns echo "<table>"; for($i=0; $i<$num_rows;$i++){ echo "<tr>"; for($j=0; $j<$num_cols;$j++){ $element = $i + $j * $num_rows; if(isset($data[$element])){ echo "<td>$data[$element]</td>"; // cell with data } else { echo "<td> </td>"; // empty cell } } echo "</tr>\n"; } echo "</table>"; Quote Link to comment Share on other sites More sharing options...
MMDE Posted April 12, 2012 Share Posted April 12, 2012 Yeah, I would just do this: // $data is an array with strings $rows = 12; $columns = ceil(count($data)/$rows); $output = '<table>'; for($i=0; $i<$rows; $i++){ $output .= ' <tr>'; for($j=0; $j<$columns; j++){ $output .= ' <td>'.$data[i+($j*$rows)].'</td>'; } $output .= ' </tr>'; } $output .= ' </table>'; echo $output; Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 12, 2012 Share Posted April 12, 2012 Do you need to use a table? This is more suited for un-ordered lists or even just divs. Quote Link to comment Share on other sites More sharing options...
mike12255 Posted April 13, 2012 Author Share Posted April 13, 2012 no i prefer divs actually, if you see a better way Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 13, 2012 Share Posted April 13, 2012 Not tested. <style> .floating{ float: left; width: 200px; } </style> <?php $num_per = 12; $count = 0; echo '<div class="floating">'; $sql = "SELECT name,id FROM table_name ORDER BY name ASC"; $res =mysql_query($sql) or die (mysql_error()); while ($row = mysql_fetch_array($res)){ $count++; if($count%$num_per==0){ echo '</div><div class="floating">'; } echo '<p>'.$row['name'].'</p>'; } echo '</div>'; ?> Quote Link to comment Share on other sites More sharing options...
mike12255 Posted April 13, 2012 Author Share Posted April 13, 2012 Thanks guys, the only thing I had to change about the div code, was put the count++ after it echo'ed out the name Quote Link to comment 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.