Jump to content

How would I display this?


mike12255

Recommended Posts

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???
}

?>

Link to comment
https://forums.phpfreaks.com/topic/260784-how-would-i-display-this/
Share on other sites

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);
}

?>

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

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 :P 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.

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

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;

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.